main.yml (1815B)
1 --- 2 - name: Install backup prerequisites 3 ansible.builtin.apt: 4 name: [borgbackup, gzip, cron, util-linux, sudo] 5 state: present 6 update_cache: true 7 policy_rc_d: 101 8 9 - name: Ensure the private SSH directory exists 10 ansible.builtin.file: 11 path: /root/.ssh 12 state: directory 13 owner: root 14 group: root 15 mode: "0700" 16 17 - name: Check SSH key for backups exists 18 stat: 19 path: "/root/.ssh/borg" 20 register: have_ssh_key 21 22 - name: Fail if we do not have an SSH key for the backup server 23 fail: 24 msg: "You need to first run extract-borg-key" 25 when: not have_ssh_key.stat.exists 26 27 - name: Ensure /root/bin/ directory exists 28 file: 29 path: "/root/bin/" 30 state: directory 31 owner: root 32 group: root 33 mode: "0700" 34 35 - name: Place shell script to do backups 36 ansible.builtin.template: 37 src: templates/root/bin/borg-backup.sh 38 dest: /root/bin/borg-backup.sh 39 owner: root 40 group: root 41 mode: "0700" 42 no_log: true 43 diff: false 44 45 - name: Place ssh configuration 46 ansible.builtin.template: 47 src: templates/root/.ssh/config 48 dest: /root/.ssh/borg-config 49 owner: root 50 group: root 51 mode: "0600" 52 53 - name: Check whether we already know the host key of the borg server 54 ansible.builtin.command: 55 cmd: ssh-keygen -F {{ borg_host }} -f /root/.ssh/known_hosts 56 register: known_host 57 changed_when: false 58 failed_when: false 59 60 - name: Add host key for borg server 61 ansible.builtin.shell: 62 cmd: ssh-keyscan {{ borg_host }} >> /root/.ssh/known_hosts 63 when: known_host.rc != 0 64 65 - name: Create cron job to run daily backups 66 ansible.builtin.cron: 67 name: "perform backup" 68 minute: "43" 69 hour: "2" 70 job: "/root/bin/borg-backup.sh" 71 72 - name: Enable the cron service 73 ansible.builtin.systemd: 74 name: cron 75 state: started 76 enabled: true