diff --git a/backup.sh b/backup.sh index 975ab2f..00b6667 100755 --- a/backup.sh +++ b/backup.sh @@ -9,11 +9,11 @@ nodes_file="nodes.txt" datasets_file="datasets.txt" ### datasets.txt -# [remote dataset] -# [remote dataset] -# [remote dataset] +# [options] +# [options] +# [options] # ... -# recognized options: nosend nosnap(shot) remotezfs= +# recognized options: nosend nosnap(shot) remotezfs= if [ ! -f $nodes_file ]; then echo "No nodes specified."; exit 2; fi if [ ! -f $datasets_file ]; then echo "No datasets specified."; exit 3; fi @@ -48,6 +48,34 @@ run_send_recv() fi } +run_snapshot() +{ + if [ $ZB_TEST_MODE = "TRUE" ]; then + echo zfs snapshot $ZFS_SNAPSHOT_OPTS $dset@$snapid + else + zfs snapshot $ZFS_SNAPSHOT_OPTS $dset@$snapid + fi +} + +run_allow() +{ + if [ $ZB_TEST_MODE = "TRUE" ]; then + echo zfs allow -u $LOCAL_USER $ORIGIN_PERMS $dset + else + zfs allow -u $LOCAL_USER $ORIGIN_PERMS $dset + fi +} + +run_remote() +{ + ssh_cmd="ssh $SSH_SEND_OPTS -o port=$port $user@$addr" + if [ $ZB_TEST_MODE = "TRUE" ]; then + echo $ssh_cmd $remote_cmd + else + $ssh_cmd $remote_cmd + fi +} + set_compression() { case $SEND_COMPRESSED in [tTyY]*) ZFS_SEND_OPTS="$ZFS_SEND_OPTS -c" @@ -57,8 +85,8 @@ set_compression() { populate_datasets() { - while read dataset dataset_options; do - case $dataset in + while read dset dataset_options; do + case $dset in \#*) continue;; esac @@ -74,32 +102,32 @@ populate_datasets() ;; "remotezfs="*) remotezfs=`echo $option|cut -d'=' -f2` - dataset="$dataset:$remotezfs" + dset="$dset:$remotezfs" ;; esac done if [ $send_dataset ]; then - SEND_DATASETS="$SEND_DATASETS $dataset" - debug "Enabled send for $dataset." + SEND_DATASETS="$SEND_DATASETS $dset" + debug "Enabled send for $dset." fi if [ $snapshot_dataset ]; then - SNAPSHOT_DATASETS="$SNAPSHOT_DATASETS $dataset" - debug "Enabled snapshot for $dataset." + SNAPSHOT_DATASETS="$SNAPSHOT_DATASETS $dset" + debug "Enabled snapshot for $dset." fi done < datasets.txt } list_datasets() { - for dataset in $SEND_DATASETS; do - dataset=`echo $dataset|cut -d: -f1` - zfs list -H $dataset + for dset in $SEND_DATASETS; do + dset=`echo $dset|cut -d: -f1` + zfs list -H $dset done } list_remote_datasets() { - while read addr user ssh_port zroot speed; do + while read addr user port zroot speed; do echo "=== $user@$addr $zroot ===" if [ $zroot ]; then case $zroot in @@ -107,22 +135,19 @@ list_remote_datasets() { *) zroot="$zroot/" ;; esac fi - run_cmd=" - sh -c 'for dataset in $SEND_DATASETS; do - dataset=`echo \\$dataset|cut -d: -f2` - zfs list -H $zroot\$dataset; - done'" - if [ $ZB_TEST_MODE = "TRUE" ]; then - echo ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr $run_cmd - else - ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr $run_cmd - fi + remote_cmd=" + sh -c 'for dset in $SEND_DATASETS; do + dset=`echo \\$dset|cut -d: -f2` + zfs list -H $zroot\$dset; + done' + " + run_remote done < sendnodes.tmp } get_latest_snapshot() { - zfs list -t snapshot $dataset | tail -n1 | cut -d'@' -f2 | cut -d' ' -f 1 + zfs list -t snapshot $dset | tail -n1 | cut -d'@' -f2 | cut -d' ' -f 1 } get_latest_remote_snapshot() @@ -133,26 +158,22 @@ get_latest_remote_snapshot() *) zroot="$zroot/" ;; esac fi - ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr \ - "zfs list -t snapshot $zroot$dataset | - tail -n1|cut -d'@' -f2|cut -d' ' -f 1" + remote_cmd="zfs list -t snapshot $zroot$dset|tail -n1|cut -d'@' -f2|cut -d' ' -f 1" + run_remote + } zfs_allow_origin() { - for dataset in $SEND_DATASETS; do - dataset=`echo $dataset|cut -d: -f1` - if [ $ZB_TEST_MODE = "TRUE" ]; then - echo zfs allow -u $LOCAL_USER $ORIGIN_PERMS $dataset - else - zfs allow -u $LOCAL_USER $ORIGIN_PERMS $dataset - fi + for dset in $SEND_DATASETS; do + dset=`echo $dset|cut -d: -f1` + run_allow done } zfs_allow_mirrors() { - while read addr user ssh_port zroot speed; do + while read addr user port zroot speed; do echo "=== $user@$addr $zroot ===" if [ $zroot ]; then case $zroot in @@ -161,34 +182,26 @@ zfs_allow_mirrors() esac fi remote_cmd=" - for dataset in $SEND_DATASETS; do - dataset=`echo \\$dataset|cut -d: -f2` - zfs allow -u $user $MIRROR_PERMS $zroot\$dataset + for dset in $SEND_DATASETS; do + dset=`echo \\$dset|cut -d: -f2` + zfs allow -u $user $MIRROR_PERMS $zroot\$dset done " - if [ $ZB_TEST_MODE = "TRUE" ]; then - echo ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr $remote_cmd - else - ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr $remote_cmd - fi + run_remote done < sendnodes.tmp } create_snapshots() { - snapshot_id="`date -I`-`date +%s`-$LOCAL_TZ" - for dataset in $SNAPSHOT_DATASETS; do - if [ $ZB_TEST_MODE = "TRUE" ]; then - echo zfs snapshot $ZFS_SNAPSHOT_OPTS $dataset@$snapshot_id - else - zfs snapshot $ZFS_SNAPSHOT_OPTS $dataset@$snapshot_id - fi + snapid="`date -I`-`date +%s`-$LOCAL_TZ" + for dset in $SNAPSHOT_DATASETS; do + run_snapshot done } send_latest() { - while read addr user ssh_port zroot speed; do + while read addr user port zroot speed; do echo "=== $user@$addr $zroot ===" if [ $zroot ]; then case $zroot in @@ -196,15 +209,14 @@ send_latest() *) zroot="$zroot/" ;; esac fi - for dataset in $SEND_DATASETS; do - remote_dataset=`echo $dataset|cut -d: -f2` - dataset=`echo $dataset|cut -d: -f1` - debug "Processing $dataset (remote $remote_dataset)." - origin_snapshot=`get_latest_snapshot $dataset` - send_cmd="zfs send $ZFS_SEND_OPTS $dataset@$origin_snapshot" + for dset in $SEND_DATASETS; do + rset=`echo $dset|cut -d: -f2` + dset=`echo $dset|cut -d: -f1` + debug "Processing $dset (remote $rset)." + snap1=`get_latest_snapshot $dset` + send_cmd="zfs send $ZFS_SEND_OPTS $dset@$snap1" pv_cmd="pv -qL ${speed:-$DEF_SPEED}" - recv_cmd="ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr \ - zfs recv $ZFS_RECV_OPTS $zroot$remote_dataset" + recv_cmd="ssh $SSH_SEND_OPTS -o port=$port $user@$addr zfs recv $ZFS_RECV_OPTS $zroot$rset" run_send_recv done done < sendnodes.tmp @@ -212,7 +224,7 @@ send_latest() send_increment() { - while read addr user ssh_port zroot speed; do + while read addr user port zroot speed; do echo "=== $user@$addr $zroot ===" if [ $zroot ]; then case $zroot in @@ -220,16 +232,15 @@ send_increment() *) zroot="$zroot/" ;; esac fi - for dataset in $SEND_DATASETS; do - remote_dataset=`echo $dataset|cut -d: -f2` - dataset=`echo $dataset|cut -d: -f1` - origin_snapshot=`get_latest_snapshot` - remote_snapshot=`get_latest_remote_snapshot` - send_cmd="zfs send $ZFS_SEND_OPTS -i $dataset@$remote_snapshot \ - $dataset@$origin_snapshot" + for dset in $SEND_DATASETS; do + rset=`echo $dset|cut -d: -f2` + dset=`echo $dset|cut -d: -f1` + snap1=`get_latest_snapshot` + snap0=`get_latest_remote_snapshot` + send_cmd="zfs send $ZFS_SEND_OPTS -i $dset@$snap0 $dset@$snap1" pv_cmd="pv -qL ${speed:-$DEF_SPEED}" - recv_cmd="ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr \ - zfs recv $ZFS_RECV_OPTS $zroot$remote_dataset" + recv_cmd="ssh $SSH_SEND_OPTS -o port=$port $user@$addr \ + zfs recv $ZFS_RECV_OPTS $zroot$rset" run_send_recv done done < sendnodes.tmp