From 48b0335071f7a4dcb074a63f80e8ebfa6a6c00e8 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Thu, 16 Oct 2025 22:58:05 +0200 Subject: [PATCH] Organize and fix the scripts --- tasks/main.yml | 27 ++++++++++++--------- templates/backup-mount.sh.j2 | 2 +- templates/backup-restore-if-necessary.sh.j2 | 6 ++--- templates/backup-restore.sh.j2 | 10 +++----- templates/backup-run.sh.j2 | 6 ++--- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/tasks/main.yml b/tasks/main.yml index bef3981..5c86d65 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -68,10 +68,15 @@ - initiate - connection_sanity +- name: Create the backup scripts direct + file: + path: /root/backup-scripts + state: directory + - name: Create the backup vars script template: src: "backup-vars.sh.j2" - dest: /root/backup-vars.sh + dest: /root/backup-scripts/vars.sh mode: "0400" force: yes backup: yes @@ -79,7 +84,7 @@ - name: Create the backup send script template: src: "backup-send.sh.j2" - dest: /root/backup-send.sh + dest: /root/backup-scripts/send.sh mode: u+rwx force: yes backup: yes @@ -87,7 +92,7 @@ - name: Create the backup mount script template: src: "backup-mount.sh.j2" - dest: /root/backup-mount.sh + dest: /root/backup-scripts/mount.sh mode: u+rwx force: yes backup: yes @@ -97,7 +102,7 @@ - name: Create the backup restore script template: src: "backup-restore.sh.j2" - dest: /root/backup-restore.sh + dest: /root/backup-scripts/restore.sh mode: u+rwx force: yes backup: yes @@ -105,7 +110,7 @@ - name: Create the backup prepare script ansible.builtin.template: src: "backup-scripts/{{inventory_hostname}}.sh.j2" - dest: /root/backup-prepare.sh + dest: /root/backup-scripts/prepare.sh mode: u+rwx backup: yes force: yes @@ -113,7 +118,7 @@ - name: Create is-restore-needed script ansible.builtin.template: src: "backup-is-restore-needed.sh.j2" - dest: /root/backup-is-restore-needed.sh + dest: /root/backup-scripts/is-restore-needed.sh mode: u+rwx backup: yes force: yes @@ -121,7 +126,7 @@ - name: Create the backup run script ansible.builtin.template: src: "backup-run.sh.j2" - dest: /root/backup-run.sh + dest: /root/backup-scripts/run.sh mode: u+rwx backup: yes force: yes @@ -129,7 +134,7 @@ - name: Create the restore-if-needed script ansible.builtin.template: src: "backup-restore-if-necessary.sh.j2" - dest: /root/backup-restore-if-necessary.sh + dest: /root/backup-scripts/restore-if-necessary.sh mode: u+rwx backup: yes force: yes @@ -139,13 +144,13 @@ name: "nightly backup for {{ inventory_hostname }}" minute: 15 hour: 4 - job: "/root/backup-run.sh" + job: "/root/backup-scripts/run.sh" - name: "Restore backup if necessary" - command: /root/backup-restore-if-necessary.sh + command: /root/backup-scripts/restore-if-necessary.sh register: command_output args: - chdir: /root + chdir: /root/backup-scripts - name: "Print command output" debug: diff --git a/templates/backup-mount.sh.j2 b/templates/backup-mount.sh.j2 index b03ac7b..d90a437 100644 --- a/templates/backup-mount.sh.j2 +++ b/templates/backup-mount.sh.j2 @@ -1,4 +1,4 @@ -source ./backup-vars.sh +source ./vars.sh MOUNT_PATH="/mnt/restic" diff --git a/templates/backup-restore-if-necessary.sh.j2 b/templates/backup-restore-if-necessary.sh.j2 index 2887f43..fee9b15 100644 --- a/templates/backup-restore-if-necessary.sh.j2 +++ b/templates/backup-restore-if-necessary.sh.j2 @@ -1,7 +1,7 @@ #!/bin/bash -source ./backup-vars.sh; +source ./vars.sh -if ./backup-is-restore-needed.sh; then - ./backup-restore.sh +if ./is-restore-needed.sh; then + ./restore.sh fi diff --git a/templates/backup-restore.sh.j2 b/templates/backup-restore.sh.j2 index c4da9ed..c3d0cdf 100644 --- a/templates/backup-restore.sh.j2 +++ b/templates/backup-restore.sh.j2 @@ -2,10 +2,10 @@ original_dir=$(pwd) -source ./backup-vars.sh +source ./vars.sh cd "$original_dir" -./backup-mount.sh & +./mount.sh & RESTIC_PID=$! MOUNT_DIR="/mnt/restic" @@ -25,10 +25,8 @@ done # Space-separated list of absolute paths BACKUP_DIR="$MOUNT_DIR/snapshots/latest" -eval "$DIRS_TO_BACKUP_STR" # creates the DIRS_TO_BACKUP array from string - # Iterate over each path in the array -for ORIGINAL_PATH in "${DIRS_TO_BACKUP[@]}"; do +while IFS= read -r ORIGINAL_PATH; do # Skip empty paths [[ -z "$ORIGINAL_PATH" ]] && continue @@ -49,7 +47,7 @@ for ORIGINAL_PATH in "${DIRS_TO_BACKUP[@]}"; do # Use rsync to copy files, preserving permissions, ownership, and timestamps rsync --archive --acls --xattrs --compress --verbose --human-readable --partial --progress "$BACKUP_PATH/" "$ORIGINAL_PATH/" -done +done