Skip to content

Instantly share code, notes, and snippets.

@incrazyboyy
Created June 14, 2023 10:48
Show Gist options
  • Save incrazyboyy/ad3ec0634588e80e9e7b627b15793fec to your computer and use it in GitHub Desktop.
Save incrazyboyy/ad3ec0634588e80e9e7b627b15793fec to your computer and use it in GitHub Desktop.
Script for easy backup of your /home directory to an external drive utilizing the snapshot capabilities of btrfs.
#!/bin/bash
# config
username=user
backupdir="/media/$username/externalDrive"
name=$(date +Backup\ %Y-%m-%d\ %H:%M)
partuuid="3e45a3de-01" # find your partition uuid by running blkid
# check for root permission
if [[ $UID -ne 0 ]]
then
sudo $0
exit $?
fi
mkdir -p $backupdir
mount PARTUUID=$partuuid $backupdir
if [ ! -d "$backupdir/lastBackup" ]
then
echo "$backupdir/lastBackup does not exist!"
echo "Is the backup drive connected to the pc?"
read
exit 1
fi
read -p "Do you want to perform a backup now? (type \"yes\") " yesno
if [ "$yesno" != "yes" ]
then
exit 2
fi
# temporarily snapshot /home so you can continue working while the backup is running
btrfs subvolume snapshot /home /home/backupSnapshot
# copy to ./lastBackup
rsync -aPEh --delete --stats /home/backupSnapshot/ $backupdir/lastBackup
btrfs subvolume delete /home/backupSnapshot
cd $backupdir
btrfs subvolume snapshot lastBackup "$name"
chown -R $username:$username "$name"
sync
cd /
umount $backupdir && rm -r $backupdir
echo "Backup complete!"
echo "Please unplug the external drive now."
read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment