|
1 | 1 | --- |
2 | | -- name: Synchronize borgmatic files to remote |
3 | | - ansible.posix.synchronize: |
4 | | - src: "{{ role_path }}/files/" |
5 | | - dest: "{{ ansible_user_home_dir }}/borgmatic" |
6 | | - |
7 | | -- name: Check whether ssh config exists |
| 2 | +- name: Check whether ssh cert for borg exists |
8 | 3 | ansible.builtin.stat: |
9 | 4 | path: "{{ playbook_dir }}/vaults/borg/.ssh/id_ed25519" |
10 | 5 | register: ssh_config |
11 | 6 | delegate_to: localhost |
12 | 7 |
|
13 | | -- name: Set fact for whether borg ssh config exists |
14 | | - ansible.builtin.set_fact: |
15 | | - ssh_config_defined: "{{ ssh_config.stat.exists }}" |
| 8 | +- name: Configure borgmatic and borg backups |
| 9 | + when: |
| 10 | + - ssh_config.stat.exists |
| 11 | + - borg_repository is defined |
| 12 | + - borg_encryption_passphrase is defined |
| 13 | + - borg_remote_path is defined |
| 14 | + block: |
| 15 | + - name: Synchronize borgmatic files to remote |
| 16 | + ansible.posix.synchronize: |
| 17 | + src: "{{ role_path }}/files/" |
| 18 | + dest: "{{ ansible_user_home_dir }}/borgmatic" |
16 | 19 |
|
17 | | -- name: Sync local ssh config vault remote |
18 | | - when: ssh_config_defined |
19 | | - become: true |
20 | | - ansible.builtin.copy: |
21 | | - src: "{{ playbook_dir }}/vaults/borg/.ssh/" |
22 | | - dest: "{{ ansible_user_home_dir }}/borgmatic/.ssh" |
23 | | - mode: "0700" |
24 | | - owner: root |
25 | | - group: root |
| 20 | + - name: Sync local ssh config vault remote |
| 21 | + when: ssh_config_defined |
| 22 | + become: true |
| 23 | + ansible.builtin.copy: |
| 24 | + src: "{{ playbook_dir }}/vaults/borg/.ssh/" |
| 25 | + dest: "{{ ansible_user_home_dir }}/borgmatic/.ssh" |
| 26 | + mode: "0700" |
| 27 | + owner: root |
| 28 | + group: root |
26 | 29 |
|
27 | | -- name: Render Borgmatic configuration |
28 | | - become: true |
29 | | - ansible.builtin.template: |
30 | | - src: config.yaml.j2 |
31 | | - dest: "{{ ansible_user_home_dir }}/borgmatic/config.yaml" |
32 | | - mode: "0600" |
33 | | - owner: "{{ ansible_ssh_user }}" |
| 30 | + - name: Render Borgmatic configuration |
| 31 | + become: true |
| 32 | + ansible.builtin.template: |
| 33 | + src: config.yaml.j2 |
| 34 | + dest: "{{ ansible_user_home_dir }}/borgmatic/config.yaml" |
| 35 | + mode: "0600" |
| 36 | + owner: "{{ ansible_ssh_user }}" |
34 | 37 |
|
35 | | - vars: |
36 | | - borg_exclude_patterns: |
37 | | - - /data/backups |
38 | | - borg_exclude_from: [] |
39 | | - borg_install_method: package |
40 | | - borg_user: "{{ ansible_user }}" |
41 | | - borg_source_directories: |
42 | | - - /data |
43 | | - borgmatic_hooks: |
44 | | - before_backup: |
45 | | - - echo "`date` - Starting backup." |
46 | | - mongodb_databases: |
47 | | - - name: all |
48 | | - hostname: datalab-database-1 |
49 | | - port: 27017 |
50 | | - borgmatic_timer: cron |
51 | | - borg_retention_policy: |
52 | | - keep_daily: 30 |
53 | | - keep_weekly: 0 |
54 | | - keep_monthly: 12 |
55 | | - keep_yearly: 4 |
56 | | - borg_one_file_system: true |
57 | | - borgmatic_store_atime: true |
58 | | - borgmatic_store_ctime: true |
59 | | - borg_encryption_passcommand: false |
60 | | - borg_remote_rate_limit: 0 |
61 | | - borg_ssh_command: ssh |
62 | | - borg_lock_wait_time: 5 |
| 38 | + vars: |
| 39 | + borg_exclude_patterns: |
| 40 | + - /data/backups |
| 41 | + borg_exclude_from: [] |
| 42 | + borg_install_method: package |
| 43 | + borg_user: "{{ ansible_user }}" |
| 44 | + borg_source_directories: |
| 45 | + - /data |
| 46 | + borgmatic_hooks: |
| 47 | + before_backup: |
| 48 | + - echo "`date` - Starting backup." |
| 49 | + mongodb_databases: |
| 50 | + - name: all |
| 51 | + hostname: datalab-database-1 |
| 52 | + port: 27017 |
| 53 | + borgmatic_timer: cron |
| 54 | + borg_retention_policy: |
| 55 | + keep_daily: 30 |
| 56 | + keep_weekly: 0 |
| 57 | + keep_monthly: 12 |
| 58 | + keep_yearly: 4 |
| 59 | + borg_one_file_system: true |
| 60 | + borgmatic_store_atime: true |
| 61 | + borgmatic_store_ctime: true |
| 62 | + borg_encryption_passcommand: false |
| 63 | + borg_remote_rate_limit: 0 |
| 64 | + borg_ssh_command: ssh |
| 65 | + borg_lock_wait_time: 5 |
63 | 66 |
|
64 | | -- name: Build borgmatic image |
65 | | - become: true |
66 | | - community.docker.docker_image: |
67 | | - name: datalab-borgmatic |
68 | | - source: build |
69 | | - state: present |
70 | | - force_source: true |
71 | | - build: |
72 | | - path: "{{ ansible_user_home_dir }}/borgmatic" |
| 67 | + - name: Build borgmatic image |
| 68 | + become: true |
| 69 | + community.docker.docker_image: |
| 70 | + name: datalab-borgmatic |
| 71 | + source: build |
| 72 | + state: present |
| 73 | + force_source: true |
| 74 | + build: |
| 75 | + path: "{{ ansible_user_home_dir }}/borgmatic" |
73 | 76 |
|
74 | | -- name: Create borg repository if it does not exist |
75 | | - ansible.builtin.shell: |
76 | | - cmd: docker run --rm --network datalab_backend -v {{ ansible_user_home_dir }}/borgmatic/.ssh:/root/.ssh -v /data:/data datalab-borgmatic borgmatic init --encryption=repokey -c /etc/borgmatic/config.yaml |
77 | | - executable: /bin/bash |
78 | | - register: new_borg_repository |
79 | | - changed_when: '"Repository already exists" not in new_borg_repository.stdout' |
80 | | - failed_when: new_borg_repository.rc != 0 |
| 77 | + - name: Create borg repository if it does not exist |
| 78 | + ansible.builtin.shell: |
| 79 | + cmd: docker run --rm --network datalab_backend -v {{ ansible_user_home_dir }}/borgmatic/.ssh:/root/.ssh -v /data:/data datalab-borgmatic borgmatic init --encryption=repokey -c /etc/borgmatic/config.yaml |
| 80 | + executable: /bin/bash |
| 81 | + register: new_borg_repository |
| 82 | + changed_when: '"Repository already exists" not in new_borg_repository.stdout' |
| 83 | + failed_when: new_borg_repository.rc != 0 |
81 | 84 |
|
82 | | -- name: Perform first backup if borg repo was just created |
83 | | - ansible.builtin.shell: |
84 | | - cmd: docker run --rm --network datalab_backend -v {{ ansible_user_home_dir }}/borgmatic/.ssh:/root/.ssh -v /data:/data datalab-borgmatic # noqa: no-changed-when |
85 | | - executable: /bin/bash |
86 | | - when: new_borg_repository.changed # noqa: no-handler |
| 85 | + - name: Perform first backup if borg repo was just created |
| 86 | + ansible.builtin.shell: |
| 87 | + cmd: docker run --rm --network datalab_backend -v {{ ansible_user_home_dir }}/borgmatic/.ssh:/root/.ssh -v /data:/data datalab-borgmatic # noqa: no-changed-when |
| 88 | + executable: /bin/bash |
| 89 | + when: new_borg_repository.changed # noqa: no-handler |
87 | 90 |
|
88 | | -- name: Add Cron job for borgmatic |
89 | | - ansible.builtin.cron: |
90 | | - name: borgmatic |
91 | | - hour: "2" |
92 | | - minute: "{{ range(0, 59) | random(seed=inventory_hostname) }}" |
93 | | - user: "{{ ansible_user }}" |
94 | | - job: docker run --rm --network datalab_backend -v {{ ansible_user_home_dir }}/borgmatic/.ssh:/root/.ssh -v /data:/data datalab-borgmatic # noqa: line-length |
| 91 | + - name: Add Cron job for borgmatic |
| 92 | + ansible.builtin.cron: |
| 93 | + name: borgmatic |
| 94 | + hour: "2" |
| 95 | + minute: "{{ range(0, 59) | random(seed=inventory_hostname) }}" |
| 96 | + user: "{{ ansible_user }}" |
| 97 | + job: docker run --rm --network datalab_backend -v {{ ansible_user_home_dir }}/borgmatic/.ssh:/root/.ssh -v /data:/data datalab-borgmatic # noqa: line-length |
0 commit comments