Skip to content

Commit

Permalink
Operate on snapshots by default when the pool is in a DEGRADED state
Browse files Browse the repository at this point in the history
  • Loading branch information
Jehops committed May 14, 2017
1 parent 6fdc170 commit 3cb4d5f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 58 deletions.
10 changes: 5 additions & 5 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
zap plays nice with manually created snapshots or snapshots from other tools. It will only operate on snapshots it creates.

** Synopsis
=% zap snap|snapshot [-dLSv] TTL [[-r] dataset]...=
=% zap snap|snapshot [-DLSv] TTL [[-r] dataset]...=

=% zap rep|replicate [-dFLSv] [[user@]host:parent_dataset [-r] dataset [[-r] dataset]...]=
=% zap rep|replicate [-DFLSv] [[user@]host:parent_dataset [-r] dataset [[-r] dataset]...]=

=% zap destroy [-dlsv] [host[,host]...]=
=% zap destroy [-Dlsv] [host[,host]...]=

=% zap -v | -version | --version=
** Subcommands
Expand All @@ -32,7 +32,7 @@ from the local host are destroyed. If a comma separated list of hosts are speci

*** Subcommand options

=-d= Operate on snapshots, even when the pool is in a DEGRADED state.
=-D= Do not operate on snapshots when the pool is in a DEGRADED state.

=-F= Supply =-F= to =zfs receive=, which destroys remote changes that do not exist on the sending side.

Expand Down Expand Up @@ -61,7 +61,7 @@ and =rep= subcommands.
% zap snap -v 1d -r zroot/usr/home
#+END_SRC

Create snapshots (recursively for tank and zroot/var) even if the pool is DEGRADED. The snapshots will expire after three weeks. Be verbose.
Create snapshots (recursively for tank and zroot/var), but not if the pool is in a DEGRADED state. The snapshots will expire after three weeks. Be verbose.
#+BEGIN_SRC sh
% zap snap -dv 3w -r tank -r zroot/var zroot/usr/home/nox
#+END_SRC
Expand Down
68 changes: 34 additions & 34 deletions zap
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ is_pint () {
return 0
}

# pool_ok [-d] pool
# If the -d option is supplied, consider the DEGRADED state ok.
# pool_ok [-D] pool
# If the -D option is supplied, do not consider the DEGRADED state ok.
pool_ok () {
skip="state: \(DEGRADED\|FAULTED\|OFFLINE\|REMOVED\|UNAVAIL\)"
skip="state: \(FAULTED\|OFFLINE\|REMOVED\|UNAVAIL\)"
OPTIND=1
while getopts ":d" opt; do
while getopts ":D" opt; do
case $opt in
d) skip=$(echo "$skip" | sed "s/DEGRADED\\\|//") ;;
\?) fatal "Invalid pool_ok() option -$OPTARG" ;;
D) skip="state: \(DEGRADED\|FAULTED\|OFFLINE\|REMOVED\|UNAVAIL\)" ;;
\?) fatal "Invalid pool_ok() option: -$OPTARG" ;;
esac
done
shift $(( OPTIND - 1 ))
Expand Down Expand Up @@ -90,7 +90,7 @@ pool_resilver () {
}

ss_st () {
# Using an extended regexp here, because $hn may contains a list of
# Using an extended regexp here, because $hn may contain a list of
# alternatives like awarnach|bravo|phe.
echo "$1" | sed -r "s/^.*@ZAP_(${hn})_//;s/--[0-9]{1,4}[dwmy]$//;s/p/+/"
}
Expand All @@ -115,10 +115,10 @@ usage () {
echo "$*" > /dev/stderr
cat <<EOF > /dev/stderr
usage:
${0##*/} snap|snapshot [-dLSv] TTL [[-r] dataset]...
${0##*/} rep|replicate [-dFLSv] [[user@]host:parent_dataset
${0##*/} snap|snapshot [-DLSv] TTL [[-r] dataset]...
${0##*/} rep|replicate [-DFLSv] [[user@]host:parent_dataset
[-r] dataset [[-r] dataset]...]
${0##*/} destroy [-dlsv] [host[,host]...]
${0##*/} destroy [-Dlsv] [host[,host]...]
${0##*/} -v|-version|--version
EOF
Expand Down Expand Up @@ -176,13 +176,13 @@ warn () {

# ==============================================================================
destroy () {
while getopts ":dlsv" opt; do
while getopts ":Dlsv" opt; do
case $opt in
d) d_opt='-d' ;;
D) D_opt='-D' ;;
l) l_opt=1 ;;
s) s_opt=1 ;;
v) v_opt=1 ;;
\?) fatal "Invalid destroy() option -$OPTARG" ;;
\?) fatal "Invalid destroy() option: -$OPTARG" ;;
esac
done
shift $(( OPTIND - 1 ))
Expand All @@ -201,8 +201,8 @@ destroy () {
for i in $(zfs list -H -t snap -o name); do
if echo "$i" | grep -E -q "$zptn"; then
pool="${i%%/*}"
# Do not quote $d_opt, but ensure it does not contain spaces.
if ! pool_ok $d_opt "$pool"; then
# Do not quote $D_opt, but ensure it does not contain spaces.
if ! pool_ok $D_opt "$pool"; then
warn "Did not destroy $i because of pool state."
elif [ -z "$s_opt" ] && pool_scrub "$pool"; then
warn "Did not destroy $i because $pool is being scrubbed."
Expand All @@ -227,16 +227,16 @@ time could not be determined."
done
}

# rep_parse [-dLSv] [destination [-r] dataset [[-r] dataset]...]
# rep_parse [-DLSv] [destination [-r] dataset [[-r] dataset]...]
rep_parse () {
while getopts ":dFLSv" opt; do
while getopts ":DFLSv" opt; do
case $opt in
d) d_opt='-d' ;;
D) D_opt='-D' ;;
L) L_opt=1 ;;
S) S_opt=1 ;;
v) v_opt='-v' ;;
F) F_opt='-F' ;;
\?) fatal "Invalid rep_parse() option -$OPTARG." ;;
\?) fatal "Invalid rep_parse() option: -$OPTARG." ;;
esac
done
shift $(( OPTIND - 1 ))
Expand All @@ -259,7 +259,7 @@ rep_parse () {
rep "$f" "$dest"
done
;;
\?) fatal "Invalid rep_parse() option -$OPTARG" ;;
\?) fatal "Invalid rep_parse() option: -$OPTARG" ;;
:) fatal "rep_parse() option -$OPTARG requires an \
argument." ;;
esac
Expand All @@ -280,13 +280,13 @@ rep () {
# while getopts ":r" opt; do
# case $opt in
# r) r_opt='-R' ;;
# \?) fatal "Invalid rep() option -$OPTARG" ;;
# \?) fatal "Invalid rep() option: -$OPTARG" ;;
# esac
# done
# shift $(( OPTIND - 1 ))

# Do not quote $d_opt, but ensure it does not contain spaces.
if ! pool_ok $d_opt "${1%%/*}"; then
# Do not quote $D_opt, but ensure it does not contain spaces.
if ! pool_ok $D_opt "${1%%/*}"; then
warn "DID NOT replicate $1 because of pool state."
elif [ -n "$S_opt" ] && pool_scrub "${1%%/*}"; then
warn "DID NOT replicate $1 because '-S' was supplied and the pool is \
Expand All @@ -301,7 +301,7 @@ a resilver in progress."
elif ! val_dest "$2"; then
tdest=$(echo "$2" | tr '[:upper:]' '[:lower:]')
if [ "$tdest" != '-' ] && [ "$tdest" != 'off' ]; then
warn "Invalid remote replication location, $tdest."
warn "Invalid remote replication location: $tdest."
warn "Failed to replicate $1."
fi
elif ! lsnap=$(zfs list -rd1 -tsnap -o name -s creation "$1" \
Expand Down Expand Up @@ -387,15 +387,15 @@ recv -du $F_opt $v_opt $rloc\""
fi
}

# snap_parse [-dSv] TTL [[-r] dataset [[-r] dataset]...]
# snap_parse [-DLSv] TTL [[-r] dataset [[-r] dataset]...]
snap_parse () {
while getopts ":dLSv" opt; do
while getopts ":DLSv" opt; do
case $opt in
d) d_opt='-d' ;;
D) D_opt='-D' ;;
L) L_opt=1 ;;
S) S_opt=1 ;;
v) v_opt=1 ;;
\?) fatal "Invalid snap_parse() option -$OPTARG." ;;
\?) fatal "Invalid snap_parse() option: -$OPTARG." ;;
esac
done
shift $(( OPTIND - 1 ))
Expand All @@ -419,7 +419,7 @@ snap_parse () {
while getopts ":r:" opt; do
case $opt in
r) snap -r "$OPTARG" ;;
\?) fatal "Invalid snap_parse() option -$OPTARG" ;;
\?) fatal "Invalid snap_parse() option: -$OPTARG" ;;
:) fatal "snap_parse: Option -$OPTARG requires an \
argument." ;;
esac
Expand All @@ -441,13 +441,13 @@ snap () {
while getopts ":r" opt; do
case $opt in
r) r_opt='-r' ;;
\?) fatal "Invalid snap() option -$OPTARG" ;;
\?) fatal "Invalid snap() option: -$OPTARG" ;;
esac
done
shift $(( OPTIND - 1 ))

# Do not quote $d_opt, but ensure it does not contain spaces.
if ! pool_ok $d_opt "${1%%/*}"; then
# Do not quote $D_opt, but ensure it does not contain spaces.
if ! pool_ok $D_opt "${1%%/*}"; then
warn "DID NOT snapshot $1 because of pool state!"
elif [ -n "$S_opt" ] && pool_scrub "${1%%/*}"; then
warn "DID NOT snapshot $1 because '-S' was supplied and the pool is \
Expand All @@ -461,7 +461,7 @@ a resilver in progress!"
[ -n "$r_opt" ] && printf "\-r "
echo "$1@ZAP_${hn}_${date}--${ttl}"
fi
# Do not quote $d_opt, but ensure it does not contain spaces.
# Do not quote $r_opt, but ensure it does not contain spaces.
zfs snap $r_opt "$1@ZAP_${hn}_${date}--${ttl}"
fi
}
Expand All @@ -484,7 +484,7 @@ hostptn="^\(\([:alnum:]]\|[[:alnum:]][[:alnum:]\-]*[[:alnum:]]\)\.\)*\([[:alnum:
ipptn="^\(\([0-9]\|[1-9][0-9]\|1[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)\.\)\{3\}\([0-9]\|[1-9][0-9]\|1[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)$"
ttlptn='^[0-9]\{1,4\}[dwmy]$'
unptn="^[[:alpha:]_][[:alnum:]_-]\{0,31\}$"
readonly version=0.6.9
readonly version=0.7.0
zptn="@ZAP_(${hn})_..*--[0-9]{1,4}[dwmy]" # extended re

case $1 in
Expand Down
45 changes: 26 additions & 19 deletions zap.1
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
.Sh SYNOPSIS
.Nm
.Ar snap Ns | Ns Ar snapshot
.Op Fl dLSv
.Op Fl DLSv
.Ar TTL
.Oo Op Fl r
.Ar dataset Oc Ns ...
.Nm
.Ar rep Ns | Ns Ar replicate
.Op Fl dFLSv
.Op Fl DFLSv
.Oo Op Ar user Ns @ Ns
.Ar host Ns : Ns
.Ar parent_dataset
Expand All @@ -23,26 +23,28 @@
.Ar dataset Oc Ns ... Oc
.Nm
.Ar destroy
.Op Fl dlsv
.Op Fl Dlsv
.Oo Ar host
.Oo , Ns Ar host
.Oc Ns ... Oc
.Nm
.Fl v Ns | Ns Fl version Ns | Ns Fl -version
.Sh DESCRIPTION
.Nm
automates the management of zfs snapshots. With a few, simple crontab entries,
automates the management of zfs snapshots. With a few simple crontab entries,
it can be used to create a comprehensive zfs backup system. There are no
configuration files. All parameters are supplied on the command line or in zfs
properties and all snapshot information is stored in snapshot names.
.Pp
.Nm
plays nice with manually-created snapshots or snapshots from other tools. It will only operate on snapshots it creates.
plays nice with manually-created snapshots or snapshots from other tools. It
will only operate on snapshots it creates.
.Pp
If the pool is in a DEGRADED state, then snapshots will not be created,
replicated, or destroyed unless
.Fl d
is used. Snapshots are still created and replicated, but not destroyed when the pool has a resilver in progress or is being scrubbed. Use
If the pool is in a DEGRADED state, then snapshots will still be created,
replicated, and destroyed unless
.Fl D
is used. If the pool is being resilvered or scrubbed, then snapshots will still
created and replicated, but not destroyed. Use
.Fl L ,
.Fl l ,
.Fl S ,
Expand Down Expand Up @@ -73,7 +75,8 @@ set to
.Ss Ar rep Ns | Ns Ar replicate
Use the
.Ar rep
subcommand to replicate datasets. If a destination and datasets are not supplied on the command line, datasets with a destination set in the
subcommand to replicate datasets. If a destination and datasets are not
supplied on the command line, datasets with a destination set in the
.Sy zap:rep
user property are replicated. If the
.Sy canmount
Expand All @@ -95,30 +98,34 @@ specified without any domain information, that is, as returned by
.Ic hostname -s Ns .
.Sh OPTIONS
.Bl -tag -width "12345678"
.It Fl d
Operate on snapshots, even when the pool is in a DEGRADED state.
.It Fl D
Do not operate on snapshots when the pool is in a DEGRADED state.
.It Fl F
Supply
.Ic -F
to
.Ar zfs receive Ns
, which destroys remote changes that do not exist on the sending side.
.It Fl L
Do not operate on snapshots if the pool has a resilver in progress. This is the default for the
Do not operate on snapshots if the pool has a resilver in progress. This is the
default for the
.Ar destroy
subcommand.
.It Fl l
Operate on snapshots, even if the pool has a resilver in progress. This is the default for the
Operate on snapshots, even if the pool has a resilver in progress. This is the
default for the
.Ar snap
and
.Ar rep
subcommands.
.It Fl S
Do not operate on snapshots if the pool is being scrubbed. This is the default for the
Do not operate on snapshots if the pool is being scrubbed. This is the default
for the
.Ar destroy
subcommand.
.It Fl s
Operate on snapshots, even if the pool is being scrubbed. This is the default for the
Operate on snapshots, even if the pool is being scrubbed. This is the default
for the
.Ar snap
and
.Ar rep
Expand All @@ -139,10 +146,10 @@ Recursively create snapshots that will expire after one day. Be verbose.
zap snap -v 1d -r zroot/usr/home
.Ed
.Pp
Create snapshots (recursively for tank and zroot/var) even if the pool is
DEGRADED. The snapshots will expire after three weeks. Be verbose.
Create snapshots (recursively for tank and zroot/var), but not if the pool is in
a DEGRADED state. The snapshots will expire after three weeks. Be verbose.
.Bd -literal -offset indent
zap snap -dv 3w -r tank -r zroot/var zroot/usr/home/nox
zap snap -Dv 3w -r tank -r zroot/var zroot/usr/home/nox
.Ed
.Pp
Create snapshots for datasets with the
Expand Down

0 comments on commit 3cb4d5f

Please sign in to comment.