Skip to content

Instantly share code, notes, and snippets.

@canthony
Created March 3, 2017 08:58
Show Gist options
  • Save canthony/90f603dc40dbcdf60cc2d3ca78459c7f to your computer and use it in GitHub Desktop.
Save canthony/90f603dc40dbcdf60cc2d3ca78459c7f to your computer and use it in GitHub Desktop.
zfs snapshot allows us to take recursive snapshots of a file system (zfs snapshot -r root-pool-name@snapshot). zfs rollback does *not* allow you to restore recursively. This script does; as the comment says, ideas copied from elsewhere and then rewritten into something I could read and maintain a little better. Not a lot, but a little.
#!/usr/bin/env bash
# Originally by: Dr. Martin Menzel www.dr-menzel-it.de
# Modified by Matthew Pherigo https://gist.github.com/mwpher/4493681e9efdcc271830
# Rewritten by Charles Anthony
#
# Recursively restores the zfs filesystems below a given filesystem to a snapshot
# e.g. given
# the-pool
# the-pool/var
# the-pool/var/lib
# the-pool/doris
# and a snapshot of @backup
#
# using this script with the-pool/doris@backup will restore just that one file system
# Using this script with the-pool/var@backup will restore the-pool/var AND the-pool/var/lib
# Using this script with the-pool@backup will restore the-pool. the-pool/var, the-pool/var/lib AND
# the-pool/doris
#
# Accepts just one parameter - the root snapshot reference to restore, in the form of
# the-root-zfs-file-system@snapshot-name
usage() {
echo "Usage: $0 zpool/root@snapshot"
}
if [ $# -ne 1 ]
then
usage;
else
# Patter matches any valid filename character follwed by @ followeding by anything else
regex="^([a-zA-Z0-9/\.\_\-]*)\@(.*)"
if [[ ! $1 =~ $regex ]]
then
usage;
else
# Curious #/} here drops any leading /
pool="${BASH_REMATCH[1]#/}"
root_snapshot="${BASH_REMATCH[2]}"
echo Restoring ${root_snapshot} in ${pool}
while IFS= read -r snapshot; do
echo -n "Rolling Back ${snapshot} : "
# -r : also destroys the snapshots newer than the specified one
# -R : also destroys the snapshots newer than the one specified and their clones
# -f : forces an unmount of any clone file systems that are to be destroyed
zfs rollback -r -R -f ${snapshot}
echo "Done"
done < <(zfs list -H -t snapshot -r $pool | grep "@$root_snapshot" | cut -f 1)
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment