#!/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 if [[ "$OS" =~ .*Solus.* ]] then echo "Setting mount point for Solus" mount_point='/run/media/tracey/Backup' elif [[ "$OS" = "Ubuntu" ]] then echo "Setting mount point for Ubuntu" mount_point='/media/tracey/Backup' else echo "Unknown OS!" fi 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 backup volume!" exit 1 fi echo "Preparing to transfer differences in home directory to backup USB drive using rsync." rsync -azh --exclude '.cache' --exclude 'Videos' --exclude 'Nextcloud' --exclude 'Downloads/isos' --exclude 'VirtualBox VMs' --exclude 'Sync' --exclude 'Downloads/torrents' --exclude '.local/share/Steam' --exclude 'Music' --progress /home/tracey/ /media/tracey/Backup/tracey/ 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