Skip to content

Instantly share code, notes, and snippets.

@freemo
Last active October 22, 2023 11:58
Show Gist options
  • Save freemo/59b420659c9161e27711a5969e5dfcef to your computer and use it in GitHub Desktop.
Save freemo/59b420659c9161e27711a5969e5dfcef to your computer and use it in GitHub Desktop.
Backup script usint btrfs
#!/bin/bash
TIMESTAMP=$(date +%F_%R)
OLD_TIMESTAMP="$1"
echo "new timestamp is $TIMESTAMP"
echo "old time is $OLD_TIMESTAMP"
if [ -z "$OLD_TIMESTAMP" ]
then
read -p "No arguments given, continue? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
fi
if grep -qs '/mnt/backups ' /proc/mounts; then
echo "/mnt/backups mounted already"
else
echo "mounting /mnt/backups"
mount /mnt/backups
fi
if grep -qs '/mnt/sdcard-backups ' /proc/mounts; then
echo "/mnt/sdcard-backups mounted already"
else
echo "mounting /mnt/sdcard-backups"
mount /mnt/sdcard-backups
fi
echo "doing stuff"
#mount /mnt/backups
#mount /mnt/sdcard-backups
#btrfs subvolume create /mnt/backups/$TIMESTAMP
mkdir /mnt/backups/$TIMESTAMP
mkdir /mnt/sdcard-backups/$TIMESTAMP
btrfs subvolume snapshot -r / /mnt/backups/$TIMESTAMP/root
btrfs subvolume snapshot -r /etc /mnt/backups/$TIMESTAMP/etc
btrfs subvolume snapshot -r /home /mnt/backups/$TIMESTAMP/home
btrfs subvolume snapshot -r /var /mnt/backups/$TIMESTAMP/var
btrfs subvolume snapshot -r /var/cache /mnt/backups/$TIMESTAMP/var-cache
btrfs subvolume snapshot -r /var/lib/machines /mnt/backups/$TIMESTAMP/var-lib-machines
btrfs subvolume snapshot -r /var/lib/portables /mnt/backups/$TIMESTAMP/var-lib-portables
if [ -z "$OLD_TIMESTAMP" ]
then
btrfs send /mnt/backups/$TIMESTAMP/root | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send /mnt/backups/$TIMESTAMP/etc | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send /mnt/backups/$TIMESTAMP/home | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send /mnt/backups/$TIMESTAMP/var | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send /mnt/backups/$TIMESTAMP/var-cache | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send /mnt/backups/$TIMESTAMP/var-lib-machines | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send /mnt/backups/$TIMESTAMP/var-lib-portables | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
else
btrfs send -p /mnt/backups/$OLD_TIMESTAMP/root /mnt/backups/$TIMESTAMP/root | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send -p /mnt/backups/$OLD_TIMESTAMP/etc /mnt/backups/$TIMESTAMP/etc | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send -p /mnt/backups/$OLD_TIMESTAMP/home /mnt/backups/$TIMESTAMP/home | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send -p /mnt/backups/$OLD_TIMESTAMP/var /mnt/backups/$TIMESTAMP/var | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send -p /mnt/backups/$OLD_TIMESTAMP/var-cache /mnt/backups/$TIMESTAMP/var-cache | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send -p /mnt/backups/$OLD_TIMESTAMP/var-lib-machines /mnt/backups/$TIMESTAMP/var-lib-machines | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
btrfs send -p /mnt/backups/$OLD_TIMESTAMP/var-lib-portables /mnt/backups/$TIMESTAMP/var-lib-portables | btrfs receive /mnt/sdcard-backups/$TIMESTAMP
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment