Last active
August 29, 2015 14:16
-
-
Save shouptech/89b6c83b68496656a135 to your computer and use it in GitHub Desktop.
A simple btrfs snapshotting script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Takes snapshots in a format that Samba can use with the shadow_copy2 VFS module.