Skip to content

Instantly share code, notes, and snippets.

@simonesestito
Created January 10, 2018 18:15
Show Gist options
  • Save simonesestito/dd63d7572bd4f1f2df03704c625f4a1f to your computer and use it in GitHub Desktop.
Save simonesestito/dd63d7572bd4f1f2df03704c625f4a1f to your computer and use it in GitHub Desktop.
BTRFS backup script
#!/bin/bash
# Root user check
if [[ $EUID -ne 0 ]]; then
echo You must be root to execute this script!
exit 1
fi
SNAPS=.snapshots
SNAPS_DIR=/$SNAPS
# Snaps folder check
if [ ! -d $SNAPS_DIR ]; then
mkdir $SNAPS_DIR
fi
btrfs_backup_func(){
DEST=$1
CONFIG=$2
custom_echo "$DEST ($CONFIG) backup started"
# If it already exists, remove it first
if btrfs subvolume list / | grep $SNAPS/$CONFIG; then
btrfs subvolume delete $SNAPS_DIR/$CONFIG
fi
btrfs subvolume snapshot -r $DEST $SNAPS_DIR/$CONFIG \
|| custom_echo "ERROR! Check the output."
custom_echo "$DEST ($CONFIG) backup terminated"
custom_echo "Location: $SNAPS_DIR/$CONFIG"
}
custom_echo(){
echo -e "\033[1;36m==> $*\033[0m"
}
# HOME
btrfs_backup_func /home home
# Empty line
echo
# ROOT
btrfs_backup_func / root
echo
echo
# Print all subvolumes
custom_echo "BTRFS updated subvolumes list:"
btrfs subvolume list /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment