1
0

control ZFS pool with ZPOOL envvar; simplify code

This commit is contained in:
michele
2014-03-20 13:45:16 +01:00
parent 60edb5e5ab
commit 418f021c23
2 changed files with 44 additions and 52 deletions

View File

@ -1,11 +1,13 @@
#! /bin/sh #! /bin/sh
# change this to the name of your ZFS pool. # change this to the name of your ZFS pool. Or set ZPOOL envvar at runtime
zpool="zroot" zpool=${ZPOOL:-"zroot"}
# name of backup to take
bkname=${1:-"default"}
# make a backup collection having 1 full and N-1 incremental backups. # make a backup collection having 1 full and N-1 incremental backups.
# set to 1 to avoid incrementals and always take full dumps. # set to 1 to avoid incrementals and always take full dumps.
max_incremental=4 max_incremental=${2:-"4"}
usage () { usage () {
echo "Usage:" echo "Usage:"
@ -18,23 +20,21 @@ usage () {
echo "UPLOAD_PATH upload generated backup to this path. scp:// or rsync://" echo "UPLOAD_PATH upload generated backup to this path. scp:// or rsync://"
} }
# first argument: name of backup set failmsg () {
if [ $# -lt 1 ] echo $*
then echo
usage usage
exit 1 exit 1
fi }
bkname=$1 verify_zpool () {
if ! echo $bkname | grep -qiE '^[a-z0-9-]+$' zpool status $zpool >/dev/null 2>&1 || failmsg "ZFS pool '$zpool' not found. Fix your \$ZPOOL envvar."
then }
echo "Given bkname is invalid. Want alphanumeric and hyphens."
exit 1 verify_zpool
fi
if echo "$2" | grep -qE '^[1-9][0-9]*$' echo $bkname | grep -qiE '^[a-z0-9]+$' || failmsg "Given bkname is invalid. Want alphanumeric."
then echo "$max_incremental" | grep -qE '^[1-9][0-9]*$' || failmsg "Invalid number of backups sequence steps, want positive integer."
max_incremental=$2
fi
export PATH=$PATH:/usr/local/bin export PATH=$PATH:/usr/local/bin

View File

@ -1,70 +1,62 @@
#! /bin/sh #! /bin/sh
# change this to the name of your ZFS pool. # change this to the name of your ZFS pool. Or set ZPOOL envvar at runtime
zpool="zroot" zpool=${ZPOOL:-"zroot"}
# names of the DATASETs to exclude (datasets, not mountpoints!) # names of the DATASETs to exclude (datasets, not mountpoints!)
# can override this list at runtime with EXCLUDES envvar. # can override this list at runtime with EXCLUDES envvar.
# can extend this list at runtime with EXTRA_EXCLUDES envvar. # can extend this list at runtime with EXTRA_EXCLUDES envvar.
excludes="/usr/ports /usr/src /backups" excludes=${EXCLUDES:-"/usr/ports /usr/src /backups"}
# You do not want to edit anything below here # You do not want to edit anything below here
tstamp=`date +%Y%m%d-%H%M%S` tstamp=`date +%Y%m%d-%H%M%S`
label_pfx="zbk-" label_pfx="zbk-"
usrlabel=$1
maxnum=$2 # expecting optional arguments: $1 -> usrlabel, $2 -> numsnaps
usrlabel=${1:-"default"}
maxnum=${2:-"10"}
usage () { usage () {
echo Usage: echo Usage:
echo "zfssnap [label [maxnum]]" echo "zfssnap [label [maxnum]]"
echo "default label: 'default'" echo "* label: tag name of snapshot (alphanumeric, default 'default')"
echo "default maxnum: infinite" echo "* maxnum: prune old snapshots when more than this snaps exist"
echo "Takes a ZFS snapshot with given custom label."
echo "If maxnum specified, keeps the so-many newest snaps only."
echo echo
echo "Optional envvars:" echo "Optional envvars:"
echo "ZPOOL name of ZFS pool to manage (default 'zroot')"
echo "EXCLUDES list of dataset paths to exclude from snaps. Overrides default." echo "EXCLUDES list of dataset paths to exclude from snaps. Overrides default."
echo "EXTRA_EXCLUDES list of dataset paths to exclude from snaps. Extends default." echo "EXTRA_EXCLUDES list of dataset paths to exclude from snaps. Extends default."
echo "default excluded DATASET paths:" echo "default excluded DATASET paths:"
echo $excludes echo $excludes
} }
failmsg () {
echo $*
echo
usage
exit 1
}
verify_zpool () {
zpool status $zpool >/dev/null 2>&1 || failmsg "ZFS pool '$zpool' not found. Fix your \$ZPOOL envvar."
}
verify_zpool
# build list of dataset paths to exclude # build list of dataset paths to exclude
if [ "x$EXCLUDES" != x ] if [ "x$EXTRA_EXCLUDES" != x ]
then
excludes=$EXCLUDES
elif [ "x$EXTRA_EXCLUDES" != x ]
then then
excludes="$excludes $EXTRA_EXCLUDES" excludes="$excludes $EXTRA_EXCLUDES"
fi fi
# label # label
if [ "x$usrlabel" != "x" ] echo "$usrlabel" | grep -qiE '^([a-z0-9]{1,10})$' || failmsg "Invalid label '$usrlabel'! Quitting."
then
if echo "$usrlabel" | grep -qiE '^([a-z0-9]{1,10})$'
then
label_pfx="$label_pfx$usrlabel" label_pfx="$label_pfx$usrlabel"
else
echo "Invalid label '$usrlabel'! Quitting."
usage
exit 1
fi
else
label_pfx="${label_pfx}default"
fi
# maxnum # maxnum
if [ "x$maxnum" != "x" ] echo "$maxnum" | grep -qE '^[0-9]+$' || failmsg "Invalid maxnum '$maxnum'! Terminating."
then
if ! echo "$maxnum" | grep -qE '^[0-9]+$'
then
echo "Invalid maxnum '$maxnum'! Terminating."
usage
exit 1
fi
fi
# add timestamp to label # add timestamp to label