A simple backup script

It’s been some time since I have needed to do this, but I finally sat down last night and wrote a backup script so that I can keep my system backed up somewhere.  This script is designed to rsync your user’s ~ and root’s ~ to a Luks encrypted device.

I wanted to share the script after I had completed it.  You can find it here:
https://strikerttd.fedorapeople.org/backup-public.sh.txt

Basically, the script opens the Luks device, mounts it, runs rsync, creates a tarball of the fresh rsync data on the same Luks device and then un-mounts it before locking it again.  You will be prompted a few times to continue as it prepares and will be prompted for the Luks password during the un-lock phase.

#!/bin/bash

## Personalized RSYNC and TAR script by Striker Leggette
## Contact: striker@terranforge.com / striker on Freenode

backupUUID=<UUID of device goes here> #Example: backupUUID=f81d4fae-7dec-11d0-a765-00a0c91e6bf6

## See all Luks devices on the system: # blkid | grep crypto_LUKS | awk ‘{print $2}’

normalUser=<Username goes here> #Example: normalUser=bob

echo
echo “Starting your PRaTs..”
echo
echo “Will do the following:”
echo
echo “1. # cryptsetup luksOpen $(blkid | grep $backupUUID | cut -f1 -d”:”) $backupUUID”
echo “2. # mkdir -p /mnt/$backupUUID”
echo “3. # mount /dev/mapper/$backupUUID /mnt/$backupUUID”
echo “4. Two rsync commands to sync /home/$normalUser and /root to /mnt/$backupUUID/$(hostname -s)/”
echo “5. Tar the current backup of /mnt/$backupUUID/$(hostname -s)/ to /mnt/$backupUUID/$(hostname -s)_$(date +%F).tar.gz”
echo “6. # umount /mnt/$backupUUID”
echo “7. # cryptsetup luksClose /dev/mapper/$backupUUID”
echo

read -p “Ready? [y/n] ” answer
if [[ $answer = y ]] ; then
echo
echo “The current Luks devices attached to”
echo “the system have the following UUIDs:”
echo
echo “$(blkid | grep crypto_LUKS | awk ‘{print $2}’)”
echo
echo “We think the UUID of the backup drive is:”
echo “$backupUUID”
echo “…if this is incorrect, please fix the script.”
echo
echo “Attempting to unlock the backup drive now…”
cryptsetup luksOpen $(blkid | grep $backupUUID | cut -f1 -d”:”) $backupUUID
echo
echo “Attempting to mount the backup drive to:”
echo ” /mnt/$backupUUID”
mkdir -p /mnt/$backupUUID
mount /dev/mapper/$backupUUID /mnt/$backupUUID
#if [ ?$ != 0 ] ; then
# echo “Whoops! Check to make sure something not mounted there already…”
# echo
# echo “Locking device back…”
# cryptsetup luksClose /dev/mapper/$backupUUID
# exit
#else
echo
read -p “Everything is in order! Ready? [y/n] ” answer
if [[ $answer = y ]] ; then
######## RSYNC ########
mkdir -p /mnt/$backupUUID/$(hostname -s)/
#### Normal User
rsync -vrupREAXogt –progress –delete \
/home/$normalUser \
–exclude=/home/$normalUser/Downloads/NotBackedUp \
–exclude=/home/$normalUser/.local/share/Steam \
–exclude=/home/$normalUser/.cxoffice \
–exclude=/home/$normalUser/.wine \
/mnt/$backupUUID/$(hostname -s)/ #
#### Root Backup
rsync -vrupREAXogt –progress –delete \
/root \
/mnt/$backupUUID/$(hostname -s)/ #
######## RSYNC ########
echo
echo “###################################”
echo “######## RSYNC Backup Done ########”
echo “###################################”
echo
echo “Archiving backup to daily tarball:”
echo “/mnt/backup/$(hostname -s)_$(date +%F).tar.gz”
echo
######## TAR ########
tar czvpf /mnt/$backupUUID/$(hostname -s)_$(date +%F).tar.gz \
/mnt/$backupUUID/$(hostname -s)/ #
######## TAR ########
echo
echo “################################”
echo “######## Archiving Done ########”
echo “################################”
else
echo “Guess not…”
fi
#fi
echo
echo “Un-mounting /mnt/backup…”
umount /mnt/$backupUUID
echo
echo “Locking the Luks device…”
cryptsetup luksClose /dev/mapper/$backupUUID
echo
echo “All done!”
else
echo “Well, come back when you are!”
fi

You will need to modify the first couple of lines to suit your own needs, giving the script your Luks device’s UUID and then your username, but it should work nonetheless.  There’s a few missing pieces, including pre-existing mount checking and the possible addition of logging the script to a file, but basic functionality is there.