main.yml 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. # The target directory shall not already exist on the host
  43. - name: Restore previous backup of stateful data
  44. when: not restic_backup_now and restic_restore
  45. shell: /usr/local/bin/restic -p /root/.restic.password -r {{ restic_repo }} restore --path {{ item }} -t {{ item }} latest
  46. environment: "{{ restic_repo_environment }}"
  47. with_items: "{{ restic_stateful_dirs }}"
  48. - name: Create initial backup of statefule dirs
  49. when: restic_backup_now and not restic_restore
  50. shell: /usr/local/bin/restic -p /root/.restic.password -r {{ restic_repo }} backup {{ item.dir }}
  51. environment: "{{ restic_repo_environment }}"
  52. with_items: "{{ restic_stateful_dirs }}"