main.yml 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ---
  2. - name: Check if restic is installed
  3. stat:
  4. path: '{{ restic_path }}'
  5. register: restic_binary
  6. - include_tasks: install.yml
  7. when: not restic_binary.stat.exists or restic_install
  8. # TODO: check if exists?
  9. - name: Overwrite SSH config for backup server
  10. become: yes
  11. template:
  12. src: ssh_config.j2
  13. dest: '{{ restic_user_home }}/.ssh/config'
  14. owner: root
  15. group: root
  16. mode: '0600'
  17. when: restic_ssh_enabled
  18. - name: Add SSH private key
  19. become: yes
  20. template:
  21. src: ssh_private_key.j2
  22. dest: '{{ restic_ssh_private_key_path }}'
  23. mode: '0600'
  24. when: restic_ssh_private_key is defined and restic_ssh_enabled
  25. - name: Add backup server host fingerprint
  26. become: yes
  27. known_hosts:
  28. name: '[{{ restic_ssh_hostname }}]:{{ restic_ssh_port }}'
  29. key: '{{ restic_ssh_host_fingerprint }}'
  30. path: '{{ restic_user_home }}/.ssh/known_hosts'
  31. state: present
  32. - name: Add restic_env in home folder
  33. become: yes
  34. template:
  35. src: restic_env.j2
  36. dest: '{{ restic_user_home }}/.restic_env'
  37. owner: root
  38. group: root
  39. mode: '0600'
  40. - name: Add systemd service for restic
  41. become: yes
  42. template:
  43. src: restic-backup.service.j2
  44. dest: /etc/systemd/system/restic-backup.service
  45. mode: '0644'
  46. vars:
  47. restic_folders_combined: '{{ restic_default_folders + restic_folders }}'
  48. notify: systemd reload
  49. - name: Add systemd timer for restic
  50. become: yes
  51. template:
  52. src: restic-backup.timer.j2
  53. dest: /etc/systemd/system/restic-backup.timer
  54. mode: '0644'
  55. notify: systemd reload
  56. - name: Enable and start restic timer
  57. become: yes
  58. systemd:
  59. name: restic-backup.timer
  60. enabled: true
  61. state: started
  62. - name: Initialize restic repo if needed
  63. become: yes
  64. command: "{{restic_path}} init"
  65. environment:
  66. RESTIC_REPOSITORY: "sftp:{{ restic_ssh_host }}:{{ restic_repository_name }}"
  67. RESTIC_PASSWORD: "{{restic_password}}"
  68. no_log: true
  69. register: restic_init
  70. changed_when: "'created restic repository' in restic_init.stdout"
  71. failed_when:
  72. - restic_init.rc != 0
  73. - not 'config file already exists' in restic_init.stderr
  74. - not 'config already initialized' in restic_init.stderr
  75. - not 'config already exists' in restic_init.stderr