Skip to content

Instantly share code, notes, and snippets.

@uchida
Created June 13, 2012 04:01
Show Gist options
  • Select an option

  • Save uchida/2921800 to your computer and use it in GitHub Desktop.

Select an option

Save uchida/2921800 to your computer and use it in GitHub Desktop.
A replication script for ZFS
#!/usr/bin/env bash
# coding: utf-8
# A replication script for ZFS
# by Akihiro Uchida, CC0 dedicated to the public domain
# see http://creativecommons.org/publicdomain/zero/1.0/
usage="usage: $(basename $0) [-v] src dst"
die() {
echo "$@" >&2
exit 1
}
while getopts ":v" opt; do
case $opt in
v) set -v;;
*) die $usage;;
esac
done
shift $((OPTIND - 1))
if [ $# -ne 2 ]; then
die $usage
fi
src=$1
dst=$2
for filesystem in $src $dst; do
pool=$(echo $filesystem | cut -d/ -f1)
if [ -z "$(zpool list $pool 2>/dev/null)" ]; then
die "zfs pool $pool does not exist!"
fi
mntpt=$(zfs get -H mountpoint $pool | cut -f3)
path=$(echo $filesystem | sed -e "s|$pool|$mntpt|")
if ! [ -e "$path" ]; then
die "path to $filesystem: $path does not exist!"
fi
done
src_snaps=$(zfs list -H -t snapshot | grep "$src" | cut -f1 | cut -d@ -f2)
dst_snaps=$(zfs list -H -t snapshot | grep "$dst" | cut -f1 | cut -d@ -f2)
last_common=$(sort <(echo "$src_snaps") <(echo "$dst_snaps") | uniq -d | tail -1)
present=$(date +"%Y%m%d-%H:%M")
zfs snapshot $src@$present
if [ -z "$lastcommon" ]; then
zfs send $src@$present | zfs recv -F $dst || die
else
zfs rollback $dst@$last_common || die
zfs send -i $src@$last_common $src@$present | zfs recv $dst || die
zfs release keep $src@$last_common
zfs release keep $dst@$last_common
fi
zfs hold keep $src@$present
zfs hold keep $dst@$present
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment