This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
freebsd:zfs_rep [2014/02/27 09:45] tschulz created |
freebsd:zfs_rep [2017/09/28 21:22] (current) tschulz [On primary node] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== ZFS Replication Script ====== | ====== ZFS Replication Script ====== | ||
+ | ===== On primary node and backup node ===== | ||
+ | * set **pool** | ||
+ | * link to /etc/periodic/daily/ | ||
<file bash /scripts/clean_snaps.sh> | <file bash /scripts/clean_snaps.sh> | ||
#`date -v -1d +"$type%Y-%m-%d"` | #`date -v -1d +"$type%Y-%m-%d"` | ||
Line 21: | Line 24: | ||
</file> | </file> | ||
+ | ===== On primary node ===== | ||
+ | * set **pool**, **host**, and **destination** | ||
+ | * link to /etc/periodic/daily/ | ||
<file bash /scripts/zfs_rep.sh> | <file bash /scripts/zfs_rep.sh> | ||
#!/bin/sh | #!/bin/sh | ||
- | pool="zvol1/vol1" | + | pool="tank/vol1" |
- | destination="zvol1" | + | destination="tank" |
- | host="10.1.0.79" | + | remote_pool="tank/vol1" |
+ | host="192.168.0.10" | ||
- | today=`date +"$type%Y-%m-%d"` | + | today=`date +"%Y-%m-%d"` |
yesterday=`date -v -1d +"$type%Y-%m-%d"` | yesterday=`date -v -1d +"$type%Y-%m-%d"` | ||
+ | #yesterday="2017-01-07" | ||
# create today snapshot | # create today snapshot | ||
- | snapshot_today="$pool@$today" | + | snapshot_today="${pool}@${today}" |
# look for a snapshot with this name | # look for a snapshot with this name | ||
- | if zfs list -H -o name -t snapshot | sort | grep "$snapshot_today$" > /dev/null; then | + | if zfs list -H -o name -t snapshot | sort | grep "${snapshot_today}" > /dev/null; then |
- | echo " snapshot, $snapshot_today, already exists" | + | echo " snapshot, ${snapshot_today}, already exists" |
- | exit 1 | + | # exit 1 |
else | else | ||
- | echo " taking todays snapshot, $snapshot_today" | + | echo " taking todays snapshot, ${snapshot_today}" |
- | zfs snapshot -r $snapshot_today | + | zfs snapshot -r ${snapshot_today} |
fi | fi | ||
# look for yesterday snapshot | # look for yesterday snapshot | ||
snapshot_yesterday="$pool@$yesterday" | snapshot_yesterday="$pool@$yesterday" | ||
- | if zfs list -H -o name -t snapshot | sort | grep "$snapshot_yesterday$" > /dev/null; then | + | snapshot_yesterday_r="$remote_pool@$yesterday" |
- | echo " yesterday snapshot, $snapshot_yesterday, exists lets proceed with backup" | + | snapshot_today_r="$remote_pool@${today}" |
- | zfs send -R -i $snapshot_yesterday $snapshot_today | ssh root@$host zfs receive -Fduv $destination | + | snapshot_start="" |
+ | snapshot_end="" | ||
+ | ss_list_local="`zfs list -H -o name -t snapshot | grep ${pool} | cut -d \"@\" -f2 | sort -r`" | ||
+ | ss_list_remote="`ssh root@${host} \"zfs list -H -o name -t snapshot | grep ${remote_pool} | cut -d \"@\" -f2 | sort -r\"`" | ||
- | echo " backup complete" | + | #echo $snapshot_yesterday |
- | exit 0 | + | if ssh root@$host "zfs list -H -o name -t filesystem | sort | grep \"${remote_pool}\"" > /dev/null; then |
+ | echo " Pool found on remote" | ||
else | else | ||
- | echo " missing yesterday snapshot aborting, $snapshot_yesterday" | + | echo " Pool missing on remote, aborting" |
+ | exit 1; | ||
+ | fi | ||
+ | if zfs list -H -o name -t snapshot | sort | grep "${snapshot_yesterday}" > /dev/null; then | ||
+ | echo " yesterday snapshot, ${snapshot_yesterday}, exists locally" | ||
+ | for l in $ss_list_local | ||
+ | do | ||
+ | for r in $ss_list_remote | ||
+ | do | ||
+ | if [ "$l" == $r ]; then | ||
+ | snapshot_start="$l" | ||
+ | fi | ||
+ | done | ||
+ | if [ "$snapshot_start" != "" ]; then | ||
+ | break | ||
+ | fi | ||
+ | done | ||
+ | if [ "$snapshot_start" != "" ]; then | ||
+ | echo " found common ss at: ${snapshot_start}" | ||
+ | if ssh root@$host "zfs list -H -o name -t snapshot | sort | grep \"${snapshot_today_r}\"" > /dev/null; then | ||
+ | echo " Remote already has today's snapshot aborting sync" | ||
+ | exit 2 | ||
+ | else | ||
+ | echo " proceeding with incremental sync" | ||
+ | echo "zfs send -R -i ${pool}@${snapshot_start} ${snapshot_today} | lzop | ssh root@$host \"lzop -d | zfs receive -Fduv ${destination}\"" | ||
+ | zfs send -R -i ${pool}@${snapshot_start} ${snapshot_today} | lzop | ssh root@$host "lzop -d | zfs receive -Fduv ${destination}" | ||
+ | echo " sync complete" | ||
+ | exit 0; | ||
+ | fi | ||
+ | else | ||
+ | echo " yesterday snapshot, ${snapshot_yesterday_r}, missing on destination" | ||
+ | if ssh root@$host "zfs list -H -o name -t snapshot | sort | grep \"${snapshot_today_r}\"" > /dev/null; then | ||
+ | echo " Remote already has today's snapshot aborting sync" | ||
+ | exit 2 | ||
+ | else | ||
+ | echo " proceeding with full sync" | ||
+ | zfs send ${snapshot_today} | ssh root@$host "zfs receive -Fduv ${destination}" | ||
+ | echo " sync complete" | ||
+ | exit 0; | ||
+ | fi | ||
+ | fi | ||
+ | else | ||
+ | echo " missing yesterday snapshot aborting, ${snapshot_yesterday}" | ||
exit 1 | exit 1 | ||
fi | fi | ||
- | |||
</file> | </file> |