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
#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'
backup_dir_mount='/data/LinuxBack'
echo "#####"
echo ""
# Check what OS we're on
# OS=`awk -F = '/NAME/{print $2}' /etc/os-release`
# echo "OS we're running on is $OS"
# shellcheck source-path=SCRIPTDIR
source ~/git/shell-scripts/check_os.sh
# 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`
# For dedicated Linux Backup drive
# LABEL="LinuxLapBack"
mount_point=/data/LinuxBack
echo "Mount point is $mount_point"
LABEL="LinuxLapBack"
BLOCKID="/dev/disk/by-label/$LABEL"
# 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 "Mountpoint is ${mount_point}"
if ! mount ${mount_point}; then
# echo "Mountpoint is ${mount_point}"
# if ! mount ${mount_point}; then
if udisksctl mount -b "$BLOCKID"; then
echo "Mounted successfully at $mount_point"
else
echo "An error code was returned by mount command!"
exit 5
else echo "Mounted successfully.";
fi
else echo "${mount_point} is already mounted.";
fi
# Target volume **must** be mounted by this point. If not, die screaming.
if ! mountpoint -q ${mount_point}/; then
echo "Mounting failed! Cannot run backup without a mounted backup volume!"
echo "$?"
exit 1
fi
else
echo "Drive is already mounted."
fi
# Target volume **must** be mounted by this point. If not, die screaming.
# 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!"
exit 1
else
mount_point=$(echo "$FINDMNT_OUTPUT1" |awk '{getline; getline; print $1;}';)
fi
# echo "mount point before perm check is"
# echo "$mount_point"
# 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 ]
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 $backup_dir_mount
sudo chown tracey.users "$mount_point"
fi
fi
# Check whether target directory exists. If not need to use sudo to make it
if [ ! -d "$backup_dir_mount/tracey_home" ]; then
echo "Creawting backup directory"
if [ ! -d "$mount_point/tracey_home" ]; then
echo "Creating backup directory"
fi
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 "Backup Complete"