3e8813d32c723c8752cb1c63aed8c7399dd50e4c
[zfs-ubuntu/.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 # change to match the name of the local backup pool
10 local_pool=backpool
11
12 # The ssh connection doesn't find zfs without this.
13 zfs=/usr/sbin/zfs
14
15 # List the snapshots on the remote machine.
16 remote_list=$(mktemp /tmp/replicate.XXXXXX)
17 ssh $remote \
18     $zfs list -H -t snapshot |
19     grep ^${remote_fs}@ |
20     awk '{print$1}' > $remote_list
21
22 # List the snapshots on the local machine.
23 local_list=$(mktemp /tmp/replicate.XXXXXX)
24 $zfs list -H -t snapshot |
25     grep ^${local_pool}/${remote_fs}@ |
26     awk '{gsub(/^${local_pool}./,"",$1); print$1}' > $local_list
27
28 # See what the most recent snapshot on the remote end is.
29 latest=$(tail -n 1 $remote_list)
30
31 # I did this to make sure that diff would always display the most recent common
32 echo bogus.remote >> $remote_list
33 echo bogus.local  >> $local_list
34 common=$(diff -u $remote_list $local_list | grep '^ ' | tail -n 1)
35
36 ssh $remote $zfs send -R -I${common/*@/@} $latest |
37     $zfs receive -vF -d ${local_pool}/${remote_fs%/*}
38
39 rm -f $local_list $remote_list