Moved old zfs-replicate scripts out of the way.
[zfs-nexenta/.git] / zfs-replicate
1 #/bin/bash
2
3 set -x
4
5 # Usage: replicate <hostname> <zfs filesystem>
6 remote=$1
7 remote_fs=$2
8
9 # The ssh connection doesn't find zfs without this.
10 zfs=/usr/sbin/zfs
11
12 # List the snapshots on the remote machine.
13 remote_list=$(mktemp /tmp/replicate.XXXXXX)
14 ssh $remote \
15     $zfs list -H -t snapshot |
16     grep ^${remote_fs}@ |
17     awk '{print$1}' > $remote_list
18
19 # List the snapshots on the local machine.
20 local_list=$(mktemp /tmp/replicate.XXXXXX)
21 $zfs list -H -t snapshot |
22     grep ^backups/${remote_fs}@ |
23     awk '{gsub(/^backups./,"",$1); print$1}' > $local_list
24
25 # See what the most recent snapshot on the remote end is.
26 latest=$(tail -n 1 $remote_list)
27
28 # I did this to make sure that diff would always display the most recent common
29 echo bogus.remote >> $remote_list
30 echo bogus.local  >> $local_list
31 common=$(diff -u $remote_list $local_list | grep '^ ' | tail -n 1)
32
33 ssh $remote $zfs send -R -I${common/*@/@} $latest |
34     $zfs receive -vF -d backups/${remote_fs%/*}
35
36 rm -f $local_list $remote_list