#!/bin/bash #Script to back up /home/tracey to external USB drive #http://www.bobulous.org.uk/misc/rsync-backup.html date=$(date +%Y-%m-%d) log_file='/home/tracey/backuplogs/usb-rsync-backup-'${date}'.txt' echo "#####" echo "" # shellcheck source-path=SCRIPTDIR source ~/git/shell-scripts/check_os.sh # Get the mount point by uuid #UUID=ff0b3c83-853d-452f-abb6-48069e337446 #mount_point=`findmnt -rn -S UUID=$UUID -o TARGET` # For dedicated Linux Backup drive LABEL="LinuxLapBack" BLOCKID="/dev/disk/by-label/$LABEL" # Check whether target volume is mounted, and mount it if not. # 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 if udisksctl mount -b "$BLOCKID"; then echo "Mounted successfully at $mount_point" else echo "An error code was returned by mount command!" 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" 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 # Check whether target directory exists. If not need to use sudo to make it 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" --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" echo "" echo "Backup Complete" echo "Log of this backup is in $log_file" echo "Now deleting backup logs over 7 days" echo "####" find /home/tracey/backuplogs -iname 'usb-rsync-backup-*' -mtime +7 -type f -delete