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

View File

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