Updated backup script to look for mount point based on backup drive label

This commit is contained in:
Tracey Clark 2022-03-27 21:02:44 -05:00
commit d705c0e3c6

View file

@ -2,16 +2,14 @@
#Script to back up /home/tracey to external USB drive #Script to back up /home/tracey to external USB drive
#http://www.bobulous.org.uk/misc/rsync-backup.html #http://www.bobulous.org.uk/misc/rsync-backup.html
date=`date +%Y-%m-%d` date=$(date +%Y-%m-%d)
log_file='/home/tracey/backuplogs/usb-rsync-backup-'${date}'.txt' log_file='/home/tracey/backuplogs/usb-rsync-backup-'${date}'.txt'
backup_dir_mount='/data/LinuxBack'
echo "#####" echo "#####"
echo "" echo ""
# Check what OS we're on
# OS=`awk -F = '/NAME/{print $2}' /etc/os-release` # shellcheck source-path=SCRIPTDIR
# echo "OS we're running on is $OS"
source ~/git/shell-scripts/check_os.sh source ~/git/shell-scripts/check_os.sh
# Get the mount point by uuid # Get the mount point by uuid
@ -19,51 +17,73 @@ source ~/git/shell-scripts/check_os.sh
#mount_point=`findmnt -rn -S UUID=$UUID -o TARGET` #mount_point=`findmnt -rn -S UUID=$UUID -o TARGET`
# For dedicated Linux Backup drive # For dedicated Linux Backup drive
# LABEL="LinuxLapBack" LABEL="LinuxLapBack"
BLOCKID="/dev/disk/by-label/$LABEL"
mount_point=/data/LinuxBack
echo "Mount point is $mount_point"
# Check whether target volume is mounted, and mount it if not. # Check whether target volume is mounted, and mount it if not.
if ! mountpoint -q ${mount_point}/; then # Use findmnt because of greater flexibility
# Avoid hard coded mount point
# if ! mountpoint -q ${mount_point}/; then
FINDMNT_OUTPUT=$(findmnt -S LABEL=$LABEL)
if [ "$FINDMNT_OUTPUT" = "" ]; then
echo "Mounting the external USB drive." echo "Mounting the external USB drive."
echo "Mountpoint is ${mount_point}" # echo "Mountpoint is ${mount_point}"
if ! mount ${mount_point}; then # if ! mount ${mount_point}; then
echo "An error code was returned by mount command!"
exit 5 if udisksctl mount -b "$BLOCKID"; then
else echo "Mounted successfully."; echo "Mounted successfully at $mount_point"
else
echo "An error code was returned by mount command!"
echo "$?"
exit 1
fi fi
else echo "${mount_point} is already mounted."; else
echo "Drive is already mounted."
fi fi
# Target volume **must** be mounted by this point. If not, die screaming. # Target volume **must** be mounted by this point. If not, die screaming.
if ! mountpoint -q ${mount_point}/; then # If mounted set the mount point
FINDMNT_OUTPUT1=$(findmnt -S LABEL=$LABEL)
echo "NEW findmnt output is:"
echo "$FINDMNT_OUTPUT1"
if [ "$FINDMNT_OUTPUT1" = "" ]; then
echo "Mounting failed! Cannot run backup without a mounted backup volume!" echo "Mounting failed! Cannot run backup without a mounted backup volume!"
exit 1 exit 1
fi
# This only has to be done once per new ext* partition. Perms should stick after that every time its mounted
if [ -w $backup_dir_mount ]
then
echo "User has write permission for backup directory, proceeding with rsync"
else else
echo "Changing ownership for backup directory - only has to be done once for the drive" mount_point=$(echo "$FINDMNT_OUTPUT1" |awk '{getline; getline; print $1;}';)
sudo chown tracey.users $backup_dir_mount fi
# echo "mount point before perm check is"
# echo "$mount_point"
if [ "$mount_point" = "" ]; then
echo "ERROR: Mount point is not defined!"
exit 1
else
# This only has to be done once per new ext* partition IF mount is not automounted. Perms should stick after that every time its mounted
# if [ -w $mount_point ]
if [ -w "$mount_point" ]
then
echo "User has write permission for backup directory, proceeding with rsync"
else
echo "Changing ownership for backup directory - only has to be done once for the drive"
sudo chown tracey.users "$mount_point"
fi
fi fi
# Check whether target directory exists. If not need to use sudo to make it # Check whether target directory exists. If not need to use sudo to make it
if [ ! -d "$backup_dir_mount/tracey_home" ]; then if [ ! -d "$mount_point/tracey_home" ]; then
echo "Creawting backup directory" echo "Creating backup directory"
fi fi
echo "Preparing to rsync differences in home directory to backup USB drive." echo "Preparing to rsync differences in home directory to backup USB drive."
rsync -azh --exclude '.cache' --exclude 'Videos' --exclude 'Nextcloud' --exclude 'Downloads' --exclude 'VirtualBox VMs' --exclude 'Sync' --exclude '.local/share/Steam' --exclude 'Music' --exclude '.Trash-*' --exclude 'home_backup' --exclude 'SatisfactoryShareSaves' --exclude 'Desktop/S9_backup' --exclude 'mount' --exclude 'isos' --exclude 'Sync' --exclude '.local/share/gvfs-metadata' --exclude 'clearbuilt' --progress /home/tracey/ $mount_point/tracey_home --delete 2>&1 | tee $log_file rsync -azh --exclude '.cache' --exclude 'Videos' --exclude 'Nextcloud' --exclude 'Downloads' --exclude 'VirtualBox VMs' --exclude 'Sync' --exclude '.local/share/Steam' --exclude 'Music' --exclude '.Trash-*' --exclude 'home_backup' --exclude 'SatisfactoryShareSaves' --exclude 'Desktop/S9_backup' --exclude 'mount' --exclude 'isos' --exclude 'Sync' --exclude '.local/share/gvfs-metadata' --exclude 'clearbuilt' --progress /home/tracey "$mount_point"/tracey_home --delete 2>&1 | tee "$log_file"
sudo rsync -azh --progress /var/lib/libvirt/images $mount_point/var --delete 2>&1 | tee $log_file sudo rsync -azh --progress /var/lib/libvirt/images "$mount_point"/var --delete 2>&1 | tee "$log_file"
sudo rsync -azh --progress /var/lib/libvirt/qemu $mount_point/var --delete 2>&1 | tee $log_file sudo rsync -azh --progress /var/lib/libvirt/qemu "$mount_point"/var --delete 2>&1 | tee "$log_file"
echo "" echo ""
echo "Backup Complete" echo "Backup Complete"