Skip to content

Instantly share code, notes, and snippets.

@ctsiaousis
Last active May 22, 2021 13:02
Show Gist options
  • Save ctsiaousis/b9b791dd831923a9dd7d6a9b100d9967 to your computer and use it in GitHub Desktop.
Save ctsiaousis/b9b791dd831923a9dd7d6a9b100d9967 to your computer and use it in GitHub Desktop.
A simple script for automated `rsync` backups to encrypted devices. Called like `./backup.sh /dev/sde1 /myMountPoint/ myUserName`
#!/bin/sh
[[ $# -ne 3 ]] && echo "Please specify device, mount-point and username. Exiting..." && exit
[[ $EUID -ne 0 ]] && echo "Please run as root. Exiting..." && exit
[[ ! -d $2 ]] && echo "Argument 2 must be a directory. Exiting..." && exit
[[ `id $3 &>/dev/null || echo -1` -ne 0 ]] && echo "Not a valid user. Will not push cache to backup. Exiting..."
DIR=${2%/} #remove trailing dash
cryptsetup open $1 backup || { echo "Could not open \"backup\". Exiting..." ; exit; } #backup is the name of cryptsetup
mount -t ext4 /dev/mapper/backup $DIR || { echo "Could not mount \"/dev/mapper/backup\" to \"$DIR\". Exiting..." ; exit; }
sudo rsync -aAXv --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","$DIR/*","/home/$3/lost+found/*","/home/$3/.cache/*","/home/$3/.mozzila/*"} / $DIR && echo "rsync finished properly" || echo "rsync -- ERROR"
umount $DIR || { echo "Error while unmounting. Exiting..." ; exit; }
cryptsetup close backup || { echo "Error while closing \"backup\". Exiting..." ; exit; }
echo Finished and unmounted $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment