Playbooks to a new Lilik
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.2 KiB

  1. ---
  2. - name: 'install borg'
  3. apt:
  4. pkg: 'borgbackup'
  5. state: 'latest'
  6. update_cache: true
  7. cache_valid_time: 3600
  8. - name: 'create .ssh folder'
  9. file:
  10. path: '/root/.ssh'
  11. mode: '0700'
  12. state: 'directory'
  13. - name: 'create backup keypair'
  14. openssh_keypair:
  15. path: '/root/.ssh/id_ed25519_BORG'
  16. force: '{{ borgrepo_force_new_key }}'
  17. type: 'ed25519'
  18. comment: 'backup@{{ host_fqdn }}'
  19. register: ssh_keypair
  20. - name: 'create host repos namespace'
  21. file:
  22. path: '/home/backup/repos/{{ host_fqdn }}'
  23. owner: 'backup'
  24. group: 'backup'
  25. mode: '0700'
  26. state: 'directory'
  27. delegate_to: '{{ borgrepo_backup_host }}'
  28. - name: 'authorize host key'
  29. lineinfile:
  30. path: '/home/backup/.ssh/authorized_keys'
  31. owner: 'backup'
  32. group: 'backup'
  33. mode: '0600'
  34. create: yes
  35. line: >-
  36. command="cd {{ repodir }}; borg serve --append-only --restrict-to-path {{ repodir }}",restrict
  37. {{ ssh_keypair.public_key }}
  38. regexp: '{{ ssh_keypair.comment }}$'
  39. state: 'present'
  40. vars:
  41. repodir: '/home/backup/repos/{{ host_fqdn }}'
  42. delegate_to: '{{ borgrepo_backup_host }}'
  43. - name: 'upload host ssh ca'
  44. copy:
  45. content: |
  46. {% for ca in ssh_server_ca %}
  47. @cert-authority *.dmz.{{ domain }} {{ ca }}
  48. {% endfor %}
  49. dest: '/root/.ssh/known_hosts'
  50. mode: '0600'
  51. ##ToDo setup encryption
  52. - name: 'initialize repo'
  53. shell:
  54. cmd: >
  55. borg init -e none backup@{{ borgrepo_backup_host }}.dmz.{{ domain }}:{{ item.key }}
  56. register: borgrepo_init_cmd
  57. failed_when:
  58. - borgrepo_init_cmd.rc != 0
  59. - borgrepo_init_cmd.stderr !='A repository already exists at backup@backup.dmz.lilik.it:'+item.key+'.'
  60. changed_when: borgrepo_init_cmd.rc == 0
  61. environment:
  62. BORG_RSH: 'ssh -i /root/.ssh/id_ed25519_BORG'
  63. loop: '{{ borgrepo_repos|dict2items }}'
  64. - name: 'create backup directory'
  65. file:
  66. path: '/etc/backup'
  67. state: 'directory'
  68. owner: 'root'
  69. group: 'root'
  70. mode: '0700'
  71. - name: 'create log backup directory'
  72. file:
  73. path: '/var/log/backup-status'
  74. state: 'directory'
  75. owner: 'root'
  76. group: 'root'
  77. mode: '0755'
  78. - name: 'create repo log directory'
  79. file:
  80. path: '/var/log/backup-status/{{ item.key }}'
  81. state: 'directory'
  82. owner: 'root'
  83. group: 'root'
  84. mode: '0755'
  85. loop: '{{ borgrepo_repos|dict2items }}'
  86. - name: 'create backup scripts'
  87. template:
  88. src: 'backupscript.sh.j2'
  89. dest: '/etc/backup/{{ item.key }}.sh'
  90. owner: 'root'
  91. group: 'root'
  92. mode: '0700'
  93. loop: '{{ borgrepo_repos|dict2items }}'
  94. - name: 'create systemd service'
  95. template:
  96. src: 'backupservice.service'
  97. dest: '/etc/systemd/system/borg-backup@.service'
  98. notify: reload systemd
  99. - name: 'create systemd timers'
  100. copy:
  101. content: |
  102. [Unit]
  103. Description=BorgBackup %I repo timer.
  104. [Timer]
  105. WakeSystem=false
  106. OnCalendar=*-*-* 02:00:00
  107. RandomizedDelaySec=20min
  108. [Install]
  109. WantedBy=timers.target
  110. dest: '/etc/systemd/system/borg-backup@.timer'
  111. notify: reload systemd
  112. - name: 'enable systemd timers'
  113. systemd:
  114. name: 'borg-backup@{{ item.key }}.timer'
  115. daemon_reload: true
  116. enabled: true
  117. state: 'restarted'
  118. loop: '{{ borgrepo_repos|dict2items }}'
  119. - name: 'MONITORING | create entry'
  120. set_fact:
  121. borg_monitoring_repos: >
  122. {{ borg_monitoring_repos|d({})|combine({
  123. item.key:
  124. {
  125. "backup_wage": item.value.interval|d(86400)|int,
  126. "backup_cage": (item.value.interval|d(86400)|int+7200)*2
  127. }
  128. }) }}
  129. loop: '{{ borgrepo_repos|dict2items }}'
  130. tags:
  131. - 'monitoring'
  132. - name: 'MONITORING | update facts'
  133. set_fact:
  134. monitoring_facts: >
  135. {{ hostvars[monitoring_host]["monitoring_facts"]
  136. | default({})
  137. | combine({
  138. host_fqdn:
  139. {
  140. "address": ansible_host,
  141. "borg_repos": borg_monitoring_repos
  142. }
  143. }, recursive=True) }}
  144. delegate_to: '{{ monitoring_host }}'
  145. delegate_facts: true
  146. loop: '{{ borgrepo_repos|dict2items }}'
  147. tags:
  148. - 'monitoring'
  149. ...