2018-10-03 20:27:03 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
#Script to back up /home/tracey to external USB drive
|
|
|
|
|
#http://www.bobulous.org.uk/misc/rsync-backup.html
|
2022-03-27 21:02:44 -05:00
|
|
|
date=$(date +%Y-%m-%d)
|
2019-01-05 12:45:29 -06:00
|
|
|
log_file='/home/tracey/backuplogs/usb-rsync-backup-'${date}'.txt'
|
2018-10-03 20:27:03 -05:00
|
|
|
|
|
|
|
|
echo "#####"
|
|
|
|
|
echo ""
|
2020-03-14 21:02:44 -05:00
|
|
|
|
2022-03-27 21:02:44 -05:00
|
|
|
|
|
|
|
|
# shellcheck source-path=SCRIPTDIR
|
2020-03-14 21:02:44 -05:00
|
|
|
source ~/git/shell-scripts/check_os.sh
|
|
|
|
|
|
2022-03-03 19:39:26 -06:00
|
|
|
# Get the mount point by uuid
|
2020-10-14 21:36:44 -05:00
|
|
|
#UUID=ff0b3c83-853d-452f-abb6-48069e337446
|
|
|
|
|
#mount_point=`findmnt -rn -S UUID=$UUID -o TARGET`
|
|
|
|
|
|
|
|
|
|
# For dedicated Linux Backup drive
|
2022-03-27 21:02:44 -05:00
|
|
|
LABEL="LinuxLapBack"
|
|
|
|
|
BLOCKID="/dev/disk/by-label/$LABEL"
|
2020-03-14 21:02:44 -05:00
|
|
|
|
2018-10-03 20:27:03 -05:00
|
|
|
# Check whether target volume is mounted, and mount it if not.
|
2022-03-27 21:02:44 -05:00
|
|
|
# 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
|
2018-10-03 20:27:03 -05:00
|
|
|
echo "Mounting the external USB drive."
|
2022-03-27 21:02:44 -05:00
|
|
|
# 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
|
2018-10-03 20:27:03 -05:00
|
|
|
fi
|
2022-03-27 21:02:44 -05:00
|
|
|
else
|
|
|
|
|
echo "Drive is already mounted."
|
2018-10-03 20:27:03 -05:00
|
|
|
fi
|
|
|
|
|
# Target volume **must** be mounted by this point. If not, die screaming.
|
2022-03-27 21:02:44 -05:00
|
|
|
# If mounted set the mount point
|
|
|
|
|
FINDMNT_OUTPUT1=$(findmnt -S LABEL=$LABEL)
|
|
|
|
|
echo "NEW findmnt output is:"
|
|
|
|
|
echo "$FINDMNT_OUTPUT1"
|
|
|
|
|
if [ "$FINDMNT_OUTPUT1" = "" ]; then
|
2022-03-03 19:39:26 -06:00
|
|
|
echo "Mounting failed! Cannot run backup without a mounted backup volume!"
|
2018-10-03 20:27:03 -05:00
|
|
|
exit 1
|
2022-03-27 21:02:44 -05:00
|
|
|
else
|
|
|
|
|
mount_point=$(echo "$FINDMNT_OUTPUT1" |awk '{getline; getline; print $1;}';)
|
2018-10-03 20:27:03 -05:00
|
|
|
fi
|
2022-03-27 21:02:44 -05:00
|
|
|
# echo "mount point before perm check is"
|
|
|
|
|
# echo "$mount_point"
|
2018-10-03 20:27:03 -05:00
|
|
|
|
2022-03-27 21:02:44 -05:00
|
|
|
|
|
|
|
|
if [ "$mount_point" = "" ]; then
|
|
|
|
|
echo "ERROR: Mount point is not defined!"
|
|
|
|
|
exit 1
|
2022-03-03 19:39:26 -06:00
|
|
|
else
|
2022-03-27 21:02:44 -05:00
|
|
|
# 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
|
2022-03-03 19:39:26 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check whether target directory exists. If not need to use sudo to make it
|
|
|
|
|
|
2022-03-27 21:02:44 -05:00
|
|
|
if [ ! -d "$mount_point/tracey_home" ]; then
|
|
|
|
|
echo "Creating backup directory"
|
2022-03-03 19:39:26 -06:00
|
|
|
fi
|
|
|
|
|
|
2020-06-07 14:20:10 -05:00
|
|
|
echo "Preparing to rsync differences in home directory to backup USB drive."
|
2018-10-03 20:27:03 -05:00
|
|
|
|
2022-03-27 21:12:20 -05:00
|
|
|
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"
|
2022-03-15 20:47:40 -05:00
|
|
|
|
2022-03-27 21:02:44 -05:00
|
|
|
sudo rsync -azh --progress /var/lib/libvirt/images "$mount_point"/var --delete 2>&1 | tee "$log_file"
|
2022-03-15 20:47:40 -05:00
|
|
|
|
2022-03-27 21:02:44 -05:00
|
|
|
sudo rsync -azh --progress /var/lib/libvirt/qemu "$mount_point"/var --delete 2>&1 | tee "$log_file"
|
2018-10-03 20:27:03 -05:00
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
echo "Backup Complete"
|
2018-10-03 20:29:13 -05:00
|
|
|
echo "Log of this backup is in $log_file"
|
2019-01-04 18:18:00 -06:00
|
|
|
echo "Now deleting backup logs over 7 days"
|
2018-10-03 20:27:03 -05:00
|
|
|
echo "####"
|
2019-01-04 18:18:00 -06:00
|
|
|
|
2019-01-05 12:45:29 -06:00
|
|
|
find /home/tracey/backuplogs -iname 'usb-rsync-backup-*' -mtime +7 -type f -delete
|