#!/bin/sh LOCK=/var/lock/rmtbackup.sh DATE=`date '+%Y%m%d-%H%M'` REMOTEHOST=toriyu10 POOL=tank if [ -f $LOCK ]; then echo "lock file is exist" exit fi touch $LOCK backup_zfs() { (df ; zfs list -t filesystem) | ssh -c blowfish $REMOTEHOST "cat >/$POOL/df.txt" echo "Creat snapshot $DATE" zfs snapshot -r $POOL@$DATE if [ $? -ne 0 ]; then zfs destroy -r $POOL@$DATE zfs snapshot -r $POOL@$DATE if [ $? -ne 0 ]; then echo "Error: Snapshot cannot created." rm $LOCK exit fi fi FS=`zfs list -t filesystem -o name -H -r $POOL | grep '/' ` for i in $FS do echo "Sending $i" TARGET=/tmp/rmtbackup.sh_target REMOTE=/tmp/rmtbackup.sh_remote zfs list -t snapshot -Ho name -s creation | grep "$i@" >$TARGET ssh $REMOTEHOST "zfs list -t snapshot -Ho name -s creation | grep $i@" >$REMOTE err=1 #for j in `comm -1 -2 $TARGET $REMOTE|tail -r` COMM=`/usr/local/bin/ruby -e "print File.readlines('$TARGET') & File.readlines('$REMOTE')"|tail -r` for j in $COMM do echo "Rollback $j" ssh $REMOTEHOST "zfs rollback -R $j" if [ $? -ne 0 ]; then continue # rollback出来なければ別のスナップショットを試す fi first=1 for k in `sed -e "1,/${j#*/}/d" $TARGET` # $jより新しいスナップショット do if [ $first -eq 1 ]; then echo "incremental backup from $j to $k" zfs send -i $j $k | ssh -c blowfish $REMOTEHOST "zfs receive -dF $POOL" if [ $? -eq 0 ]; then err=0 fi first=0 else echo "incremental backup from $prev to $k" zfs send -i $prev $k | ssh -c blowfish $REMOTEHOST "zfs receive -dF $POOL" if [ $? -eq 0 ]; then err=0 fi fi prev=$k done if [ $err -eq 0 ]; then break fi done if [ $err -eq 1 ]; then # フルバックアップ echo "incremental backup failed. trying full backup" # echo "destroy $i" # ssh $REMOTEHOST "zfs destroy -r $i" 2>/dev/null first=1 for j in `cat $TARGET` do if [ $first -eq 1 ]; then first=0 echo "send $j" zfs send $j | ssh -c blowfish $REMOTEHOST "zfs receive -d $POOL" else echo "send $j from $prev" zfs send -i $prev $j | ssh -c blowfish $REMOTEHOST "zfs receive -dF $POOL" fi prev=$j done fi done } date zfs list -t filesystem -r $POOL ping -c 1 $REMOTEHOST if [ $? -eq 0 ]; then backup_zfs echo "done" ssh $REMOTEHOST "zpool list" fi date rm $LOCK