#!/usr/bin/env sh nodes_file="nodes.txt" ### nodes.txt # # # # ... datasets_file="datasets.txt" ### datasets.txt # # / # / # / # # ... list_active_datasets() { zfs list | tail -n +2 | cut -d' ' -f1|grep ${BG_ROOT} } # compare_datasets() create_datasets() { # should compare_datasets and only create missing or possibly error. for dataset in `cat ${datasets_file}`; do case $dataset in \#*) continue;; esac if [ $BG_TEST_MODE = "TRUE" ]; then echo zfs create -p ${BG_ROOT}/$dataset else zfs create -p ${BG_ROOT}/$dataset fi done } get_latest_snapshot() { zfs list -t snapshot | grep ${BG_ROOT} | tail -n1 | cut -d'@' -f2 | cut -d' ' -f 1 } setup_aggregator() { create_datasets if [ $BG_TEST_MODE = "TRUE" ]; then echo zfs allow -u ${BG_USER} send,snapshot,hold ${BG_ROOT} else zfs allow -u ${BG_USER} send,snapshot,hold ${BG_ROOT} fi } setup_mirror() { if [ $BG_TEST_MODE = "TRUE" ]; then echo zfs allow -u ${BG_USER} send,snapshot,hold ${BG_ROOT} echo zfs allow -u ${BG_USER} \ compression,mountpoint,receive,create,mount ${BG_ROOT} else zfs allow -u ${BG_USER} send,snapshot,hold ${BG_ROOT} zfs allow -u ${BG_USER} \ compression,mountpoint,receive,create,mount ${BG_ROOT} fi } create_snapshot() { id="`date -I`-`date +%s`" if [ $BG_TEST_MODE = "TRUE" ]; then echo zfs snapshot -r ${BG_ROOT}@${id} else zfs snapshot -r ${BG_ROOT}@${id} fi } send_single_snapshot() { BG_NEW_SNAPSHOT=`get_latest_snapshot` tail -n +2 ${nodes_file} > sendnodes.tmp while read BG_REMOTE_ADDR BG_REMOTE_PORT BG_REMOTE_ROOT BG_REMOTE_USER; do case $BG_REMOTE_ADDR in \#*) continue;; esac if [ $BG_TEST_MODE = "TRUE" ]; then echo zfs send -R ${BG_ROOT}@${BG_NEW_SNAPSHOT} echo ssh -i ~/.ssh/id_rsa -o port=${BG_REMOTE_PORT} \ ${BG_REMOTE_USER}@${BG_REMOTE_ADDR} \ zfs receive -dvu ${BG_REMOTE_ROOT} else zfs send -R ${BG_ROOT}@${BG_NEW_SNAPSHOT} | ssh -i ~/.ssh/id_rsa -o port=${BG_REMOTE_PORT} \ ${BG_REMOTE_USER}@${BG_REMOTE_ADDR} \ zfs receive -dvu ${BG_REMOTE_ROOT} fi done < sendnodes.tmp rm sendnodes.tmp if [ $BG_TEST_MODE = "TRUE" ]; then : else echo $BG_NEW_SNAPSHOT > "old-snapshot.txt" fi } send_incremental_snapshot() { BG_NEW_SNAPSHOT=`get_latest_snapshot` echo Will send $BG_NEW_SNAPSHOT #while read BG_REMOTE_ADDR BG_REMOTE_PORT BG_REMOTE_ROOT BG_REMOTE_USER; do # zfs send -R -i `cat old-snapshot.txt` ${BG_ROOT}@${BG_NEW_SNAPSHOT} | # ssh -i ~/.ssh/id_rsa -o port=${BG_REMOTE_PORT} \ # ${BG_REMOTE_USER}@${BG_REMOTE_ADDR} \ # zfs receive -dvu ${BG_REMOTE_ROOT}@${BG_NEW_SNAPSHOT} #done echo $BG_NEW_SNAPSHOT > "old-snapshot.txt" } ################################### MAIN #################################### ### Options set -- `getopt "tl:" "$@"` || { echo "Usage: `basename $0` [-tl] [command]" 1>&2 exit 1 } BG_TEST_MODE=FALSE while :; do case "$1" in -t) BG_TEST_MODE=TRUE echo Test mode. ;; --) shift; break ;; esac shift done read BG_HOST BG_PORT BG_ROOT BG_USER < ${nodes_file} if [ ! -f ${nodes_file} ]; then echo "No nodes specified."; exit 2; fi ### Command case "$1" in list) list_active_datasets ;; create) create_snapshot ;; setup-mirror) setup_mirror ;; setup-aggregator) setup_aggregator ;; send) if [ -f "old-snapshot.txt" ]; then send_incremental_snapshot else send_single_snapshot fi ;; esac