shell-scripts/local_backup.sh

56 lines
1.8 KiB
Bash
Raw Normal View History

#!/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"
2020-06-06 16:28:31 -05:00
mount_point='/run/media/tracey/LinuxBack'
elif [[ "$OS" = "Ubuntu" ]]
then
echo "Setting mount point for Ubuntu"
2020-06-06 16:28:31 -05:00
mount_point='/media/tracey/LinuxBack'
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."
2020-06-06 16:28:31 -05:00
rsync -azh --exclude '.cache' --exclude 'Videos' --exclude 'Nextcloud' --exclude 'Downloads/isos' --exclude 'VirtualBox VMs' --exclude 'Sync' --exclude '.local/share/Steam' --exclude 'Music' --progress /home/tracey/ $mount_point 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