Skip to content

Instantly share code, notes, and snippets.

@evansus
Last active December 16, 2015 02:09
Show Gist options
  • Save evansus/5360427 to your computer and use it in GitHub Desktop.
Save evansus/5360427 to your computer and use it in GitHub Desktop.
CloneZFS - clonezfs.sh to recursively clone a snapshot or dataset
#!/bin/sh
#-----------------------------------------
#
# CloneZFS - sh shell script to recursively create a zfs clone of a dataset
#
# Copyright 2013 Evan Susarret evansus@gmail.com
#
#-----------------------------------------
#-----------------------------------------
#
# Constants
#
#-----------------------------------------
CLONEZFS_ERROR_SRC_MISSING=11
CLONEZFS_ERROR_SRC_SNAPSHOT=10
CLONEZFS_ERROR_DEST_EXISTS=11
CLONEZFS_ERROR_DEST_DELETE=12
CLONEZFS_ERROR_DEST_RENAME=13
CLONEZFS_ERROR_SRC_LISTING=20
#-----------------------------------------
#
# Functions
#
#-----------------------------------------
#-----------------------------------------
# clonezfs_usage
# Print command usage and exit 1
#-----------------------------------------
clonezfs_usage() {
cat << EOF
usage: clonezfs [-bfnq] <dataset@snapshot> <destination>
clonezfs [-bfnq] [-s <snapshotname>] <source> <destination>
[ COMMAND ]
In its first form, clonezfs will recursively clone an existing snapshot
from <source@snapshot> to the <destination> dataset, prompting to
rename, overwrite, or cancel if the destination exists.
The second form of clonezfs creates a snapshot on the <source> dataset
then clones the new snapshot to the <destination> dataset, as if the
first form was called on an existing snapshot.
The new snapshot will be named 'clone-YY-MM-DD-HH-MM', using 'date'
to format the timestamp, unless <snapshotname> is specified by using
the -s flag.
[ ARGUMENTS ]
<source@snapshot> : zpool/mydataset@newsnapshot
If the source dataset or snapshot cannot be located, an error will
be output, unless the -q flag is set.
<source> : zpool/mydataset
This form of the command will create a recursive snapshot:
source@clone-YY-MM-DD-HH-MM
Then will proceed to clone dataset@snapshot to destination.
<destination> : zpool/myclone
If the destination dataset cannot be located, an error will be
output, unless the -q flag is set.
If destination already exists, you will be prompted to overwrite
the destination dataset, rename, or cancel. This behavior can be
overridden by using the -f, -b, or -n flags; -f to overwrite,
-b to backup / rename the dataset, and -n to cancel the operation.
[ OPTIONS ]
-b If <destination> already exists, make a backup by renaming it
to <destination>.backup-YY-MM-DD-HH-MM before proceeding.
If the dataset cannot be renamed, an error will be output
unless -q is set, and clonezfs will exit with status
CLONEZFS_ERROR_BACKUP
-f Force the operation to continue, overwriting <destination> if
it exists. If the destination cannot be overwritten, an
error will be output unless -q is set, and clonezfs will
exit with status CLONEZFS_ERROR_DELETE
-n If <destination> already exists, do not proceed with any action,
and exit with status CLONEZFS_ERROR_EXISTS
EOF
# Error out with generic error 1
exit 1
} # End clonezfs_usage
#-----------------------------------------
# clonezfs_get_children
# Get recursive listing from source
#-----------------------------------------
clonezfs_get_children() {
# Not yet implemented
exit $CLONEZFS_ERROR_SRC_LISTING
} # End clonezfs_get_children
#-----------------------------------------
# clonezfs_make_snapshot
# Make snapshot on source
#-----------------------------------------
clonezfs_make_snapshot() {
# Not yet implemented
exit $CLONEZFS_ERROR_SRC_SNAPSHOT
} # End clonezfs_make_snapshot
#-----------------------------------------
#
# Main
#
#-----------------------------------------
#-----------------------------------------
# Arguments
# Get and validate arguments
#-----------------------------------------
# Show usage and exit
clonezfs_usage
# Should not reach this point
exit 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment