X Tutup
Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 54c3f77

Browse files
committed
Backup: Now shows progress during tarball creation.
1 parent c3fc6cb commit 54c3f77

File tree

2 files changed

+78
-23
lines changed

2 files changed

+78
-23
lines changed

roles/backup/tasks/main.yml

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
recurse: yes
5858
register: dir_files
5959

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+
6066
# Use mv because Ansible copy & delete takes a lot longer.
6167
- name: "Moving '{{ local.destination }}' to '{{ local.destination }}.old'"
6268
shell: "mv '{{ local.destination }}' '{{ local.destination }}.old'"
@@ -201,14 +207,27 @@
201207

202208
# Create snapshot
203209

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+
207229
when: (use_snapshot)
208230

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'))
212231

213232
# Start Plexdrive and containers when snapshot is enabled
214233

@@ -239,24 +258,43 @@
239258

240259
when: (use_snapshot)
241260

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 }}/'"
243280
shell: |
244-
find {{ backup_root_path }}/opt/ -mindepth 1 -maxdepth 1 -type d -execdir \
245281
tar \
246282
--ignore-failed-read \
247283
--warning=no-file-changed \
248284
--warning=no-file-removed \
249285
--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
252288
args:
253289
executable: /bin/bash
254290
warn: no
291+
with_items: "{{ opt_folders }}"
292+
loop_control:
293+
label: "'{{ item }}' --> '{{ local.destination }}/opt/{{ item | basename }}.tar'"
255294

256295
- name: Snapshot | Delete btrfs snapshot
257-
command: "btrfs subvolume delete /btrfs/snapshots/root"
296+
shell: btrfs subvolume delete /btrfs/snapshots/root &> /dev/null || true
258297
when: (use_snapshot) and (snapshot_type == 'btrfs')
259-
ignore_errors: yes
260298

261299
- name: Check if tar files were created
262300
find:
@@ -448,14 +486,19 @@
448486
state: absent
449487
when: (dir_files2.matched|int != 0) and (not local.enable)
450488

451-
- debug:
489+
- name: Backup Status - Success
490+
debug:
452491
msg: "Backup Completed Successfully."
453492

454493
rescue:
455494
- name: Snapshot | Delete btrfs snapshot
456-
command: "btrfs subvolume delete /btrfs/snapshots/root"
495+
shell: btrfs subvolume delete /btrfs/snapshots/root &> /dev/null || true
457496
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
459502

460503
- name: Start plexdrive and containers
461504
block:
@@ -464,7 +507,7 @@
464507
systemd:
465508
name: plexdrive
466509
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)
468511

469512
- name: "Wait for 5 seconds before starting containers"
470513
wait_for:
@@ -481,9 +524,10 @@
481524
systemd:
482525
name: cloudplow
483526
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)
485528

486-
- debug:
529+
- name: Backup Status - Failure
530+
debug:
487531
msg: 'Backup terminated due to an error'
488532

489533
- name: "Pushover Message: Backup terminated due to an error"
@@ -499,6 +543,6 @@
499543
state: absent
500544

501545
- name: "Reset logs folder ownership."
502-
shell: "chown -R {{ user }}:{{ user }} /home/{{ user }}/logs/"
546+
shell: "chown -R {{ user }}:{{ user }} '/home/{{ user }}/logs/'"
503547
args:
504548
warn: no

roles/restore/tasks/main.yml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,23 @@
6565
force: yes
6666
when: (backup_excludes_list.stat.exists)
6767

68-
- name: "Unarchive backup tars to /opt"
69-
shell: |
70-
mv /opt/{{ (item | basename | splitext)[0] }} /opt/{{ (item | basename | splitext)[0] }}_bak 2>/dev/null || :
71-
tar -xf '{{ item }}' -C /opt .
68+
- name: "Backup existing folders in '/opt' to prevent overwriting them"
69+
shell: mv '/opt/{{ (item | basename | splitext)[0] }}' '/opt/{{ (item | basename | splitext)[0] }}_bak' 2>/dev/null || true
70+
args:
71+
warn: false
7272
with_fileglob:
7373
- "{{ local.destination }}/opt/*.tar"
74+
loop_control:
75+
label: "'/opt/{{ (item | basename | splitext)[0] }}' --> '/opt/{{ (item | basename | splitext)[0] }}_bak'"
76+
77+
- name: "Unarchive backup tarballs into '/opt'"
78+
shell: tar -xf '{{ item }}' -C '/opt/'
79+
args:
80+
warn: false
81+
with_fileglob:
82+
- "{{ local.destination }}/opt/*.tar"
83+
loop_control:
84+
label: "{{ item | basename }}"
7485
register: unarchive
7586

7687
- name: "Set '/opt' ownership and permissions"

0 commit comments

Comments
 (0)
X Tutup