main.yml 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. {% if item.source_host is defined %}
  10. mount_path="$(mktemp --tmpdir --directory ansible-duplicity-sshfs-XXXXXXXX)"; sshfs "{{item.source_host}}:{{item.source_path}}" $mount_path;
  11. {%- endif %}
  12. duplicity --no-encryption --no-print-statistics --verbosity warning
  13. {%- for selector in item.selectors|default([]) %}
  14. {% if selector['option'] in ['exclude', 'include'] %}
  15. --{{selector['option']}} "{{selector['shell_pattern']}}"
  16. {%- endif %}
  17. {% endfor %}
  18. {%- if item.source_host is defined %}
  19. --name "{{item.source_host}}:{{item.source_path}}"
  20. {%- endif %}
  21. {%- if item.source_host is defined %}
  22. --allow-source-mismatch "$mount_path"
  23. {%- else %}
  24. "{{item.source_path}}"
  25. {%- endif %}
  26. "{{item.target_url}}";
  27. {%- if item.source_host is defined %}
  28. fusermount -u "$mount_path" && rm -r "$mount_path";
  29. {%- endif %}
  30. with_items: '{{duplicity_backups}}'
  31. register: duplicity_backup_commands
  32. - name: setup cron jobs
  33. blockinfile:
  34. dest: '/etc/crontab'
  35. marker: '# ANSIBLE DUPLICITY BACKUPS {mark}'
  36. block: |-
  37. {% for backup in duplicity_backup_commands.results %}
  38. {% for backup_job in backup.item.backup_cron_jobs %}
  39. {{backup_job.minute}} {{backup_job.hour}} * * * {{backup.item.local_user}} {{backup.ansible_facts.backup_command}}
  40. {% endfor %}
  41. {% endfor %}
  42. become: yes