main.yml 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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-print-statistics --verbosity warning
  15. {%- if target.encrypt_key is defined %}
  16. --encrypt-key "{{target.encrypt_key}}"
  17. {%- else %}
  18. --no-encryption
  19. {%- endif %}
  20. {%- for selector in source.selectors|default([]) %}
  21. {% if selector['option'] in ['exclude', 'include'] %}
  22. --{{selector['option']}}
  23. {%- if source.source_host is defined %}
  24. "{{ selector['shell_pattern'] | replace(source.source_path, '$mount_path') }}"
  25. {%- else %}
  26. "{{selector['shell_pattern']}}"
  27. {%- endif %}
  28. {%- endif %}
  29. {% endfor %}
  30. {%- if source.source_host is defined %}
  31. --allow-source-mismatch "$mount_path"
  32. {%- else %}
  33. "{{source.source_path}}"
  34. {%- endif %}
  35. "{{target.url}}";
  36. {%- if source.source_host is defined %}
  37. fusermount -u "$mount_path" && rm -r "$mount_path";
  38. {%- endif %}
  39. with_subelements:
  40. - '{{duplicity_backups}}'
  41. - targets
  42. register: duplicity_backup_commands
  43. - name: setup cron jobs
  44. blockinfile:
  45. dest: '/etc/crontab'
  46. marker: '# ANSIBLE DUPLICITY BACKUPS {mark}'
  47. block: |-
  48. {% for backup in duplicity_backup_commands.results %}
  49. {% set source = backup.item.0 %}
  50. {% set target = backup.item.1 %}
  51. {% for backup_job in target.backup_cron_jobs %}
  52. {{backup_job.minute}} {{backup_job.hour}} * * * {{source.local_user}} {{backup.ansible_facts.backup_command}}
  53. {% endfor %}
  54. {% endfor %}
  55. become: yes