#!/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' 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" 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="LinuxBackups" UUID="498d5c30-d9c2-4fcd-ba75-ca417ea5e962" TYPE="ext4" PARTUUID="f1ee7312-41a8-b044-879a-8eef185b0453" mount_point=/data/LinuxBack 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 a mounted backup volume!" 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 echo "Changing ownership for backup directory - only has to be done once for the drive" sudo chown tracey.users $backup_dir_mount 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" 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