aboutsummaryrefslogtreecommitdiff
path: root/zb-pull
blob: af8f9cdfde4665c9a4bcc82a4879257d4a77ea72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh

if [[ -z "$1" || -z "$2" || -z "$3" ]] ; then
	echo "usage: $0 <ssh_remote> <remote_zfs_object> <local_zfs_object>" >&2
	exit 1
fi

timestamp () {
	t="`echo \"$1\" |sed -ne 's/^.*@zb-//p' |tr 'p_' '+ '`"
	[[ -z "$t" ]] && return 1
	date --date="$t" +%s
	return $?
}


ssh_remote="$1"
remote_zfs="$2"
local_zfs="$3"

# find newest available and newest common snapshot
newest="N/A"
newest_common="N/A"

ssh $ssh_remote "zfs list -t snapshot -r -d 1 -H \"${remote_zfs}\" " | while read l ; do
	#link unix timestamps
	unixtime=`timestamp "$l"` || continue
	echo "$unixtime" "$l"
done | sort -n | while read l ; do
	curtime=${l%% *}
	snapname=${l#* }

	newest="$snapname"

	if zfs list -t snapshot -H "${local_zfs}@zb-${snapname}" >/dev/null ; then
		newest_common="$snapname"
	fi
done

# some checks

if [ "$newest" = "N/A" ] ; then
	echo "$0: no remote snapshots available to pull" >&2
	exit 2
fi

if [ "$newest" = "$newest_common" ] ; then
	echo "$0: nothing new to pull" >&2
	exit 0
fi

if [ "$newest_common" = "N/A" ] ; then
	#do a full send
	ssh $ssh_remote "zfs send \"${remote_zfs}@zb-${newest}\"" | zfs recv -F "${local_zfs}"
	exit $?
else
	#do incremental send
	ssh $ssh_remote "zfs send -I \"${remote_zfs}@zb-${newest_common}\" \"${remote_zfs}@zb-${$newest}\"" | zfs recv -F "${local_zfs}"
	exit $?
fi