From 3358e4ef07500c5ac36abbdb8476327409cf365f Mon Sep 17 00:00:00 2001 From: James Date: Fri, 29 Mar 2024 09:51:17 +0000 Subject: [PATCH] move config out and make retry if fail --- backup.sh | 75 +++++++++++++++++++++++++++++++------------------- config.sh.dist | 17 ++++++++++++ 2 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 config.sh.dist diff --git a/backup.sh b/backup.sh index 8d198e7..79cfc4c 100644 --- a/backup.sh +++ b/backup.sh @@ -1,35 +1,52 @@ #!/bin/bash -if [ `whoami` != 'root' ] ; then + +# Source the configuration file +. config.sh # Adjust the path as necessary + +# Check if the script is run as root +if [ "$(whoami)" != 'root' ]; then echo "Permission Denied" exit -fi +fi -src=/ -dst=/media/tom/d533e3a2-c332-40e6-82a9-9a22f2dded45/Backup -args=( - --exclude /sys - --exclude /proc - --exclude /dev - --exclude /media - --exclude /mnt - --exclude /var/cache/apt/archives - --exclude '/home/*/.cache' - --exclude '/home/*/.local/share/Trash/' - --exclude '/home/*/Downloads' - --exclude /storage -) +# Prepare the log file and start tailing it +touch "$dst/next.log" +tail -n0 --pid="$$" -f "$dst/next.log" & -# do a backup -touch $dst/next.log -tail -n0 --pid="$$" -f $dst/next.log & -rsync "${args[@]}" -av --link-dest=$dst/current $src/ $dst/next 2>&1 >> $dst/next.log || exit - -# success, clean up -date=`date "+%Y-%m-%dT%H:%M:%S"` -mv $dst/next $dst/$date -mv $dst/next.log $dst/$date.log -unlink $dst/current -unlink $dst/current.log -ln -s $dst/$date $dst/current -ln -s $dst/$date.log $dst/current.log +# Loop until rsync is successful or the user decides to abort +while :; do + if rsync "${args[@]}" -av --link-dest="$dst/current" "$src/" "$dst/next" 2>&1 >> "$dst/next.log"; then + echo "Backup successful." + break + else + echo "Backup failed." + # Ask the user for the next step + echo "Do you want to retry, abort, or continue? [r/a/c]" + read -r response + case $response in + r|R) + echo "Retrying..." + ;; + a|A) + echo "Aborting." + exit 1 + ;; + c|C) + echo "Continuing..." + break + ;; + *) + echo "Invalid response. Please answer r, a, or c." + ;; + esac + fi +done +# Success, clean up +date=$(date "+%Y-%m-%dT%H:%M:%S") +mv "$dst/next" "$dst/$date" +mv "$dst/next.log" "$dst/$date.log" +unlink "$dst/current" +unlink "$dst/current.log" +ln -s "$dst/$date" "$dst/current" +ln -s "$dst/$date.log" "$dst/current.log" diff --git a/config.sh.dist b/config.sh.dist new file mode 100644 index 0000000..494cf73 --- /dev/null +++ b/config.sh.dist @@ -0,0 +1,17 @@ +# Source and destination directories +src="/source/" +dst="/backups/" + +# Rsync arguments +args=( + --exclude /sys + --exclude /proc + --exclude /dev + --exclude /media + --exclude /mnt + --exclude /var/cache/apt/archives + --exclude '/home/*/.cache' + --exclude '/home/*/.local/share/Trash/' + --exclude '/home/*/Downloads' + --exclude /storage +)