initial commit

This commit is contained in:
Dave Eddy 2015-11-23 00:35:23 -05:00
commit 9d6d5c9366
2 changed files with 122 additions and 0 deletions

54
README.md Normal file
View File

@ -0,0 +1,54 @@
ZFS Snapshot All
================
Recursively snapshot all zpools
Examples
--------
Snapshot all zpools with the prefix "foo"
# ./zfs-snapshot-all foo
zfs snapshot -r goliath@foo_1448256528
zfs snapshot -r zones@foo_1448256528
Snapshot all zpools with the prefix "bar" without actually executing
any snapshot commands (dry-run)
# ./zfs-snapshot-all -n bar
zfs snapshot -r goliath@bar_1448256538 # dry-run: no action taken
zfs snapshot -r zones@bar_1448256538 # dry-run: no action taken
Snapshot the pools given as arguments, "goliath", "zones", and "fake"
with the prefix "foo"
# ./zfs-snapshot-all foo goliath zones fake
zfs snapshot -r goliath@foo_1448256549
zfs snapshot -r zones@foo_1448256549
zfs snapshot -r fake@foo_1448256549
cannot open 'fake': dataset does not exist
# echo $?
1
Usage
-----
usage: zfs-snapshot-all [-hnv] <name> [[pool1] pool2 ...]
recursively snapshot all zpools
examples
# zfs-snapshot-all foo
snapshot all zpools with the prefix foo
# zfs-snapshot-all bar pool1 pool2
snapshot zpools pool1 and pool2 with the prefix bar
options
-h print this message and exit
-n dry-run, don't actually create snapshots
License
-------
MIT License

68
zfs-snapshot-all Executable file
View File

@ -0,0 +1,68 @@
#!/usr/bin/env bash
#
# script to recursively snapshot all zpools
#
# Author: Dave Eddy <dave@daveeddy.com>
# Date: November 20, 2015
# License: MIT
usage() {
local prog=${0##*/}
cat <<-EOF
usage: $prog [-hn] <name> [[pool1] pool2 ...]
recursively snapshot all zpools
examples
# $prog foo
snapshot all zpools with the prefix foo
# $prog bar pool1 pool2
snapshot zpools pool1 and pool2 with the prefix bar
options
-h print this message and exit
-n dry-run, don't actually create snapshots
EOF
}
dryrun=false
while getopts 'hn' option; do
case "$option" in
h) usage; exit 0;;
n) dryrun=true;;
*) usage; exit 1;;
esac
done
shift "$((OPTIND - 1))"
# prefix to use for snapshots
name=$1
if [[ -z $name ]]; then
echo 'error: name must be specified as the first argument' >&2
exit 1
fi
# optional list of pools
shift
pools=("$@")
if (( ${#pools[@]} == 0 )); then
# argument list was empty, use all pools
while read -r zpool; do
pools+=("$zpool")
done < <(zpool list -Ho name)
fi
now=$(date +%s)
code=0
for zpool in "${pools[@]}"; do
snapshot=${zpool}@${name}_${now}
echo -n "zfs snapshot -r $snapshot"
if $dryrun; then
echo ' # dry-run: no action taken'
else
echo
zfs snapshot -r "$snapshot" || code=1
fi
done
exit "$code"