main.yml 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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']}}
  16. {%- if item.source_host is defined %}
  17. "{{ selector['shell_pattern'] | replace(item.source_path, '$mount_path') }}"
  18. {%- else %}
  19. "{{selector['shell_pattern']}}"
  20. {%- endif %}
  21. {%- endif %}
  22. {% endfor %}
  23. {%- if item.source_host is defined %}
  24. --allow-source-mismatch "$mount_path"
  25. {%- else %}
  26. "{{item.source_path}}"
  27. {%- endif %}
  28. "{{item.target_url}}";
  29. {%- if item.source_host is defined %}
  30. fusermount -u "$mount_path" && rm -r "$mount_path";
  31. {%- endif %}
  32. with_items: '{{duplicity_backups}}'
  33. register: duplicity_backup_commands
  34. - name: setup cron jobs
  35. blockinfile:
  36. dest: '/etc/crontab'
  37. marker: '# ANSIBLE DUPLICITY BACKUPS {mark}'
  38. block: |-
  39. {% for backup in duplicity_backup_commands.results %}
  40. {% for backup_job in backup.item.backup_cron_jobs %}
  41. {{backup_job.minute}} {{backup_job.hour}} * * * {{backup.item.local_user}} {{backup.ansible_facts.backup_command}}
  42. {% endfor %}
  43. {% endfor %}
  44. become: yes