|
57 | 57 | recurse: yes |
58 | 58 | register: dir_files |
59 | 59 |
|
| 60 | + # Remove backup.old folder if it exists. |
| 61 | + - name: "Remove '{{ local.destination }}.old'" |
| 62 | + file: |
| 63 | + path: "{{ local.destination }}.old" |
| 64 | + state: absent |
| 65 | + |
60 | 66 | # Use mv because Ansible copy & delete takes a lot longer. |
61 | 67 | - name: "Moving '{{ local.destination }}' to '{{ local.destination }}.old'" |
62 | 68 | shell: "mv '{{ local.destination }}' '{{ local.destination }}.old'" |
|
201 | 207 |
|
202 | 208 | # Create snapshot |
203 | 209 |
|
204 | | - - name: "Snapshot | Wait for 5 seconds before creating snapshot" |
205 | | - wait_for: |
206 | | - timeout: 5 |
| 210 | + - name: Create Snapshot |
| 211 | + block: |
| 212 | + |
| 213 | + - name: "Snapshot | Wait for 5 seconds before creating snapshot" |
| 214 | + wait_for: |
| 215 | + timeout: 5 |
| 216 | + |
| 217 | + - name: Snapshot | Display snapshot destination |
| 218 | + debug: |
| 219 | + msg: "Creating snapshot of '/opt/' in '{{ backup_root_path }}/' ..." |
| 220 | + |
| 221 | + - name: Snapshot | Create btrfs snapshot |
| 222 | + shell: 'btrfs subvolume snapshot / /btrfs/snapshots/root' |
| 223 | + when: (snapshot_type == 'btrfs') |
| 224 | + |
| 225 | + - name: Snapshot | Display location of '/opt/' folder in snapshot |
| 226 | + debug: |
| 227 | + msg: "Backup will now archive folders from '{{ backup_root_path }}/opt/'" |
| 228 | + |
207 | 229 | when: (use_snapshot) |
208 | 230 |
|
209 | | - - name: Snapshot | Create btrfs snapshot |
210 | | - shell: 'btrfs subvolume snapshot / /btrfs/snapshots/root' |
211 | | - when: (misc.snapshot) and ((root_fstype is defined) and (root_fstype == 'btrfs')) |
212 | 231 |
|
213 | 232 | # Start Plexdrive and containers when snapshot is enabled |
214 | 233 |
|
|
239 | 258 |
|
240 | 259 | when: (use_snapshot) |
241 | 260 |
|
242 | | - - name: "Archiving '{{ backup_root_path }}/opt' to '{{ local.destination }}/opt/'" |
| 261 | + - name: "Get list of all folders in '{{ backup_root_path }}/opt/'" |
| 262 | + find: |
| 263 | + paths: "{{ backup_root_path }}/opt" |
| 264 | + recurse: no |
| 265 | + file_type: directory |
| 266 | + register: opt_folders_temp |
| 267 | + |
| 268 | + - name: Create 'opt_folders' variable |
| 269 | + set_fact: |
| 270 | + opt_folders: [] |
| 271 | + |
| 272 | + - name: Add folder list to 'opt_folders' variable |
| 273 | + set_fact: |
| 274 | + opt_folders: "{{ opt_folders }} + [ '{{ item.path }}' ]" |
| 275 | + with_items: "{{ opt_folders_temp.files }}" |
| 276 | + loop_control: |
| 277 | + label: "{{ item.path }}" |
| 278 | + |
| 279 | + - name: "Archiving '{{ backup_root_path }}/opt/' folders into '{{ local.destination }}/'" |
243 | 280 | shell: | |
244 | | - find {{ backup_root_path }}/opt/ -mindepth 1 -maxdepth 1 -type d -execdir \ |
245 | 281 | tar \ |
246 | 282 | --ignore-failed-read \ |
247 | 283 | --warning=no-file-changed \ |
248 | 284 | --warning=no-file-removed \ |
249 | 285 | --exclude-from '{{ (backup_excludes_list is defined and backup_excludes_list.stat.exists) | ternary(playbook_dir + '/backup_excludes_list.txt', playbook_dir + '/roles/backup/files/backup_excludes_list.txt') }}' \ |
250 | | - -cf "{{ local.destination }}/opt/"{}.tar {} \; \ |
251 | | - 2> /home/{{ user }}/logs/backup/cloudbox_backup_tar_stderr.log |
| 286 | + -cf '{{ local.destination }}/opt/{{ item | basename }}.tar' -C '{{ item | dirname }}' './{{ item | basename }}' \ |
| 287 | + 2> /dev/null |
252 | 288 | args: |
253 | 289 | executable: /bin/bash |
254 | 290 | warn: no |
| 291 | + with_items: "{{ opt_folders }}" |
| 292 | + loop_control: |
| 293 | + label: "'{{ item }}' --> '{{ local.destination }}/opt/{{ item | basename }}.tar'" |
255 | 294 |
|
256 | 295 | - name: Snapshot | Delete btrfs snapshot |
257 | | - command: "btrfs subvolume delete /btrfs/snapshots/root" |
| 296 | + shell: btrfs subvolume delete /btrfs/snapshots/root &> /dev/null || true |
258 | 297 | when: (use_snapshot) and (snapshot_type == 'btrfs') |
259 | | - ignore_errors: yes |
260 | 298 |
|
261 | 299 | - name: Check if tar files were created |
262 | 300 | find: |
|
448 | 486 | state: absent |
449 | 487 | when: (dir_files2.matched|int != 0) and (not local.enable) |
450 | 488 |
|
451 | | - - debug: |
| 489 | + - name: Backup Status - Success |
| 490 | + debug: |
452 | 491 | msg: "Backup Completed Successfully." |
453 | 492 |
|
454 | 493 | rescue: |
455 | 494 | - name: Snapshot | Delete btrfs snapshot |
456 | | - command: "btrfs subvolume delete /btrfs/snapshots/root" |
| 495 | + shell: btrfs subvolume delete /btrfs/snapshots/root &> /dev/null || true |
457 | 496 | when: (use_snapshot) and (snapshot_type == 'btrfs') |
458 | | - ignore_errors: yes |
| 497 | + |
| 498 | + - name: "Reset folder ownership" |
| 499 | + shell: "chown -R {{ user }}:{{ user }} {{ local.destination }}/" |
| 500 | + args: |
| 501 | + warn: no |
459 | 502 |
|
460 | 503 | - name: Start plexdrive and containers |
461 | 504 | block: |
|
464 | 507 | systemd: |
465 | 508 | name: plexdrive |
466 | 509 | state: started |
467 | | - when: (plexdrive_service.stat.exists) and (plexdrive_service_running) |
| 510 | + when: (plexdrive_service is defined) and (plexdrive_service.stat.exists) and (plexdrive_service_running) |
468 | 511 |
|
469 | 512 | - name: "Wait for 5 seconds before starting containers" |
470 | 513 | wait_for: |
|
481 | 524 | systemd: |
482 | 525 | name: cloudplow |
483 | 526 | state: started |
484 | | - when: (cloudplow_service.stat.exists) and (cloudplow_service_running) |
| 527 | + when: (cloudplow_service is defined) and (cloudplow_service.stat.exists) and (cloudplow_service_running) |
485 | 528 |
|
486 | | - - debug: |
| 529 | + - name: Backup Status - Failure |
| 530 | + debug: |
487 | 531 | msg: 'Backup terminated due to an error' |
488 | 532 |
|
489 | 533 | - name: "Pushover Message: Backup terminated due to an error" |
|
499 | 543 | state: absent |
500 | 544 |
|
501 | 545 | - name: "Reset logs folder ownership." |
502 | | - shell: "chown -R {{ user }}:{{ user }} /home/{{ user }}/logs/" |
| 546 | + shell: "chown -R {{ user }}:{{ user }} '/home/{{ user }}/logs/'" |
503 | 547 | args: |
504 | 548 | warn: no |
0 commit comments