main.yml 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. - name: install duplicity
  2. apt:
  3. name: duplicity
  4. state: present
  5. become: yes
  6. - name: generate commands
  7. set_fact:
  8. backup_command: |-
  9. {% set source = item.0 %}
  10. {% set target = item.1 %}
  11. {% if source.source_host is defined %}
  12. mount_path="$(mktemp --tmpdir --directory ansible-duplicity-sshfs-XXXXXXXX)"; sshfs "{{source.source_host}}:{{source.source_path}}" $mount_path;
  13. {%- endif %}
  14. duplicity --no-encryption --no-print-statistics --verbosity warning
  15. {%- for selector in source.selectors|default([]) %}
  16. {% if selector['option'] in ['exclude', 'include'] %}
  17. --{{selector['option']}}
  18. {%- if source.source_host is defined %}
  19. "{{ selector['shell_pattern'] | replace(source.source_path, '$mount_path') }}"
  20. {%- else %}
  21. "{{selector['shell_pattern']}}"
  22. {%- endif %}
  23. {%- endif %}
  24. {% endfor %}
  25. {%- if source.source_host is defined %}
  26. --allow-source-mismatch "$mount_path"
  27. {%- else %}
  28. "{{source.source_path}}"
  29. {%- endif %}
  30. "{{target.url}}";
  31. {%- if source.source_host is defined %}
  32. fusermount -u "$mount_path" && rm -r "$mount_path";
  33. {%- endif %}
  34. with_subelements:
  35. - '{{duplicity_backups}}'
  36. - targets
  37. register: duplicity_backup_commands
  38. - name: setup cron jobs
  39. blockinfile:
  40. dest: '/etc/crontab'
  41. marker: '# ANSIBLE DUPLICITY BACKUPS {mark}'
  42. block: |-
  43. {% for backup in duplicity_backup_commands.results %}
  44. {% set source = backup.item.0 %}
  45. {% set target = backup.item.1 %}
  46. {% for backup_job in target.backup_cron_jobs %}
  47. {{backup_job.minute}} {{backup_job.hour}} * * * {{source.local_user}} {{backup.ansible_facts.backup_command}}
  48. {% endfor %}
  49. {% endfor %}
  50. become: yes