aboutsummaryrefslogtreecommitdiff
path: root/zb-pull
blob: 1f07ca2d7c80f1f829ccb12058fa186a414e0fda (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
60
61
62
#!/bin/sh

source /etc/profile >/dev/null
[ -f "$HOME/.zb-rc" ] && source "$HOME/.zb-rc"

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}\" " | awk '{print $1;}' | while read l ; do
	#link unix timestamps
	unixtime=`timestamp "$l"` || continue
	echo "$unixtime" "$l"
done | sort -n | while read l ; do
	fullname=${l#* }
	snapname=${fullname#*@zb-}

	newest="$snapname"

	if zfs list -t snapshot -H "${local_zfs}@zb-${snapname}" ; then
		newest_common="$snapname"
	fi >/dev/null 2>/dev/null
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