#!/bin/bash # Thomas Kuschel - create btrfs snapshots # 3.17: 2014-10-27 # switching to readable snapshots if kernel v3.17 -- 3.17.1 found #set -e # todo: to _not_ fire the bug, comment the following line: TESTBUG="on" VERSION="v3.17" # Parse arguments: SOURCE=$1 TARGET=$2 SNAP=$3 COUNT=$4 QUIET=$5 # where the btrfs is found (use command: which btrfs) # BTRFS="/usr/bin/btrfs" # BTRFS_ALT="/usr/local/bin/btrfs" # Function to get path and right version: vercomp() { if [[ $1 == $2 ]] then return 0 fi local IFS=. local i ver1=($1) ver2=($2) # fill empty fields in ver1 with zeros for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) do ver1[i]=0 done for ((i=0; i<${#ver1[@]}; i++)) do if [[ -z ${ver2[i]} ]] then # fill empty fields in ver2 with zeros ver2[i]=0 fi if ((10#${ver1[i]} > 10#${ver2[i]})) then return 1 fi if ((10#${ver1[i]} < 10#${ver2[i]})) then return 2 fi done return 0 } getpath() { scriptname=`/usr/bin/basename $0` path=`/usr/bin/which $scriptname` if [ -z "$path" ] ; then path="/usr/local/bin/" echo "Please install $scriptname to \"$path\"" exit 10 fi path="$path$scriptname" BTRFS=`/usr/bin/which btrfs` if [ -z "$BTRFS" ] ; then echo "btrfs is not installed." exit 11 fi } # Function to display usage: usage() { getpath cat </dev/null done LINUXVERSION=`uname --kernel-release` # echo vercomp $LINUXVERSION "3.17" vercomp $LINUXVERSION "3.17" case $? in 0) READONLY="";; 1) READONLY="";; 2) READONLY="-r";; esac if [ -z $READONLY ]; then echo "Note: btrfs exceptionally creates a read-and-write snapshot with kernel version = 3.17.1" # give it a try for next version... vercomp $LINUXVERSION "3.17.2" case $? in 0) READONLY="-r";; 1) READONLY="-r";; 2) READONLY="";; esac fi # overwrite explicitely the READONLY for testing: if [ -n "$TESTBUG" ] && [ "$TESTBUG" == "on" ]; then echo "Warning: TESTBUG is set!" READONLY="-r" fi # Create new snapshot: cmd="$BTRFS subvolume snapshot $READONLY $SOURCE $TARGET/`date "+%F-%H%M-@${SNAP}"`" if [ -z $QUIET ]; then echo $cmd fi $cmd >/dev/null