Skip to content

Instantly share code, notes, and snippets.

@shouptech
Last active August 29, 2015 14:16
Show Gist options
  • Save shouptech/89b6c83b68496656a135 to your computer and use it in GitHub Desktop.
Save shouptech/89b6c83b68496656a135 to your computer and use it in GitHub Desktop.
A simple btrfs snapshotting script
#!/bin/bash
set -e
set -u
#set -x
shopt -s extglob
if [ $# -ne 3 ]
then
echo "Usage: $0 /path/to/subvol /path/to/snaps NUM_SNAPSHOTS"
exit 0
fi
SUBVOLUME=$1
SNAPSHOTS=$2
NUM_SNAPS=$3
echo "---- Started processing at $(date) ----"
# Remove trailing slashes, unless it is root
if [ $SUBVOLUME != '/' ]
then
SUBVOLUME="${SUBVOLUME%%+(/)}"
fi
if [ $SNAPSHOTS != '/' ]
then
SNAPSHOTS="${SNAPSHOTS%%+(/)}"
fi
# Take snapshot
btrfs subvolume snapshot -r "$SUBVOLUME" "$SNAPSHOTS/$(date -u +@GMT-%Y.%m.%d-%H.%M.%S)"
# Delete extra snapshots
num_existing=$(ls -1d $SNAPSHOTS/* | wc -l)
if [ $num_existing -gt $NUM_SNAPS ]
then
let over=$num_existing-$NUM_SNAPS
ls -1d $SNAPSHOTS/* | head -n $over | while read s
do
btrfs subvolume delete $s
done
fi
echo ""
exit 0
@shouptech
Copy link
Author

Takes snapshots in a format that Samba can use with the shadow_copy2 VFS module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment