#!/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 "" # Check what OS we're on # OS=`awk -F = '/NAME/{print $2}' /etc/os-release` # echo "OS we're running on is $OS" 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` echo "Mount point is $mount_point" # Check whether target volume is mounted, and mount it if not. if ! mountpoint -q ${mount_point}/; then echo "Mounting the external USB drive." echo "Mountpoint is ${mount_point}" if ! mount ${mount_point}; then 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 mounted backup volume!" exit 1 fi echo "Preparing to rsync differences in home directory to backup USB drive." rsync -azh --exclude '.cache' --exclude 'Videos' --exclude 'Nextcloud' --exclude 'Downloads/isos' --exclude 'VirtualBox VMs' --exclude 'Sync' --exclude '.local/share/Steam' --exclude 'Music' --exclude '.Trash-*' --exclude 'home_backup' --progress /home/tracey/ $mount_point/tracey_home --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