Created
January 27, 2016 15:29
-
-
Save nickadam/0439d5234ac76c1b25a3 to your computer and use it in GitHub Desktop.
BTRFS Snapshot
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 | |
# Parse arguments: | |
SOURCE=$1 | |
TARGET=$2 | |
SNAP=$3 | |
COUNT=$4 | |
QUIET=$5 | |
# Function to display usage: | |
usage() { | |
scriptname=`/usr/bin/basename $0` | |
cat <<EOF | |
$scriptname: Take and rotate snapshots on a btrfs file system | |
Usage: | |
$scriptname source target snap_name count [-q] | |
source: path to make snaphost of | |
target: snapshot directory | |
snap_name: Base name for snapshots, to be appended to | |
date "+%F_%H-%M-%S_%Z" | |
count: Number of snapshots in the timestamp-@snap_name format to | |
keep at one time for a given snap_name. | |
[-q]: Be quiet. | |
Example for crontab: | |
15,30,45 * * * * root /usr/local/bin/btrfs-snapshot / /.btrfs quarterly 4 -q | |
0 * * * * root /usr/local/bin/btrfs-snapshot / /.btrfs hourly 8 -q | |
Example for anacrontab: | |
1 10 daily_snap /usr/local/bin/btrfs-snapshot / /.btrfs daily 8 | |
7 30 weekly_snap /usr/local/bin/btrfs-snapshot / /.btrfs weekly 5 | |
@monthly 90 monthly_snap /usr/local/bin/btrfs-snapshot / /.btrfs monthly 3 | |
EOF | |
exit | |
} | |
# Basic argument checks: | |
if [ -z $COUNT ] ; then | |
echo "COUNT is not provided." | |
usage | |
fi | |
if [ ! -z $6 ] ; then | |
echo "Too many options." | |
usage | |
fi | |
if [ -n "$QUIET" ] && [ "x$QUIET" != "x-q" ] ; then | |
echo "Option 4 is either -q or empty. Given: \"$QUIET\"" | |
usage | |
fi | |
# check that source exists | |
if [ ! -d "$SOURCE" ]; then | |
if [ -z $QUIET ]; then | |
echo "$SOURCE does not exist or is not mounted" | |
fi | |
exit 1 | |
fi | |
# $max_snap is the highest number of snapshots that will be kept for $SNAP. | |
max_snap=$(($COUNT -1)) | |
# Clean up older snapshots: | |
for i in `ls $TARGET|sort |grep @${SNAP}|head -n -${max_snap}`; do | |
cmd="/sbin/btrfs subvolume delete $TARGET/$i" | |
if [ -z $QUIET ]; then | |
echo $cmd | |
fi | |
$cmd >/dev/null | |
done | |
# Create new snapshot: | |
cmd="/sbin/btrfs subvolume snapshot $SOURCE $TARGET/`date "+%F_%H-%M-%S_%Z-@${SNAP}"`" | |
if [ -z $QUIET ]; then | |
echo $cmd | |
fi | |
$cmd >/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment