main.yml 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. - name: Ensure restic binary is in place
  2. copy:
  3. src: restic_linux_amd64
  4. dest: /usr/local/bin/restic
  5. mode: 0777
  6. - name: Ensure restic password file exists
  7. copy:
  8. dest: /root/.restic.password
  9. content: "{{ restic_repo_password }}"
  10. mode: 0600
  11. - name: Check repository
  12. register: restic_repo_check
  13. shell: /usr/local/bin/restic -p /root/.restic.password -r {{ restic_repo }} check
  14. environment: "{{ restic_repo_environment }}"
  15. ignore_errors: yes
  16. - name: Init restic repository
  17. when: restic_repo_check.rc != 0
  18. shell: /usr/local/bin/restic -p /root/.restic.password -r {{ restic_repo }} init
  19. environment: "{{ restic_repo_environment }}"
  20. - name: Ensure restic cron jobs exist
  21. cron:
  22. name: Restic backups for {{ item.dir }}
  23. job: /usr/local/bin/restic -p /root/.restic.password -r {{ restic_repo }} backup {{ item.dir }}
  24. hour: 2
  25. minute: 35
  26. cron_file: restic
  27. user: root
  28. with_items: "{{ restic_stateful_dirs }}"
  29. # TODO find a more elegant way to set environment variables here
  30. - name: Set restic environment variables
  31. cron:
  32. cron_file: restic
  33. name: "{{ item.name }}"
  34. value: "{{ item.value }}"
  35. env: yes
  36. user: root
  37. with_items:
  38. - name: "AWS_ACCESS_KEY_ID"
  39. value: "{{ restic_repo_environment.AWS_ACCESS_KEY_ID }}"
  40. - name: "AWS_SECRET_ACCESS_KEY"
  41. value: "{{ restic_repo_environment.AWS_SECRET_ACCESS_KEY }}"
  42. - name: Create initial backup of statefule dirs
  43. when: restic_backup_now
  44. shell: /usr/local/bin/restic -p /root/.restic.password -r {{ restic_repo }} backup {{ item.dir }}
  45. environment: "{{ restic_repo_environment }}"
  46. with_items: "{{ restic_stateful_dirs }}"