move config out and make retry if fail

This commit is contained in:
James 2024-03-29 09:51:17 +00:00
parent 734129e09e
commit 3358e4ef07
2 changed files with 63 additions and 29 deletions

View File

@ -1,35 +1,52 @@
#!/bin/bash #!/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" echo "Permission Denied"
exit exit
fi fi
src=/ # Prepare the log file and start tailing it
dst=/media/tom/d533e3a2-c332-40e6-82a9-9a22f2dded45/Backup touch "$dst/next.log"
args=( tail -n0 --pid="$$" -f "$dst/next.log" &
--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
)
# do a backup # Loop until rsync is successful or the user decides to abort
touch $dst/next.log while :; do
tail -n0 --pid="$$" -f $dst/next.log & if rsync "${args[@]}" -av --link-dest="$dst/current" "$src/" "$dst/next" 2>&1 >> "$dst/next.log"; then
rsync "${args[@]}" -av --link-dest=$dst/current $src/ $dst/next 2>&1 >> $dst/next.log || exit echo "Backup successful."
break
# success, clean up else
date=`date "+%Y-%m-%dT%H:%M:%S"` echo "Backup failed."
mv $dst/next $dst/$date # Ask the user for the next step
mv $dst/next.log $dst/$date.log echo "Do you want to retry, abort, or continue? [r/a/c]"
unlink $dst/current read -r response
unlink $dst/current.log case $response in
ln -s $dst/$date $dst/current r|R)
ln -s $dst/$date.log $dst/current.log 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"

17
config.sh.dist Normal file
View File

@ -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
)