3 # usage: snaprsync <setting>... <positionals>
4 # <setting> is --<name>=<value>
5 # <positionals> are assigned to unused mandatory values in order
7 # rhost device mountpoint localarea
11 rsharedir=/usr/share/chiark-backup
12 retcdir=/etc/chiark-backup
13 rvardir=/var/lib/chiark-backup
23 badusage () { echo >&2 "snaprsync: bad usage: $1"; exit 12; }
24 nb_echo () { (echo "$@"); } # See Debian #382798
25 x () { nb_echo "+ $@"; "$@"; }
26 xspawned () { eval "${1}pid=$!; nb_echo \"+[$!] ($1) &\";"; }
27 xwait () { eval "nb_echo \"+[\$${1}pid] ($1)...\"; wait \$${1}pid;"; }
32 name=${1#--}; name=${name%%=*}
35 rhost|device|mountpoint|localarea);;
36 localprevious|snapkind|rsharedir|retcdir|rvardir|bwlimit);;
37 subdir|rsyncopts|rsynccompress|sshopts|summer);;
38 *) badusage "unknown setting $name";;
43 -*) badusage "unknown option $1" ;;
49 for name in rhost device mountpoint localarea; do
51 if [ "x$value" != x ]; then continue; fi
52 if [ $# = 0 ]; then badusage "no value for setting $name"; fi
57 datefmt='%Y-%m-%d %H:%M:%S Z'
58 rsync="rsync ${bwlimit:+--bwlimit} $bwlimit"
59 export RSYNC_RSH="ssh -o compression=no $sshopts"
60 sshpfx='PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin; export PATH; '
62 ssh $sshopts $rhost "$sshpfx date -u '+$rhost $datefmt start'"
63 ssh $sshopts $rhost "$sshpfx id"
64 ssh $sshopts $rhost "$sshpfx ls -d $rsharedir"
65 ssh $sshopts $rhost "$sshpfx ls -d $rvardir"
67 test -d $localarea || x mkdir $localarea
69 rsumsfile=for-$ournode.sums
70 summer="$summer -ACDbtqfx"
74 trap 'rm -rf $td; exit $rc' 0
77 mkfifo -m 600 $td/sentinel
80 x ssh $sshopts $rhost "$sshpfx $rsharedir/snap-drop $rvardir"
85 echo '$retcdir/snap/$snapkind drop $rvardir' >snap-drop.new
86 mv snap-drop.new snap-drop
88 x ssh $sshopts $rhost "$sshpfx $retcdir/snap/$snapkind snap $rvardir $device $mountpoint"
89 ssh $sshopts $rhost <$td/sentinel 4<&- "
92 date -u '+$rhost $datefmt main'
94 (set +e; read x <&3; kill 0) &
100 date -u '+$rhost $datefmt sumsdone'
104 x $rsync -aHSx$rsynccompress --numeric-ids --delete $rsyncopts \
105 ${localprevious:+--link-dest} $localprevious \
106 $rhost:$rvardir/snap-mount/$subdir $localarea/.
107 date -u "+ $datefmt rsyncdone"
109 exec 3>$localarea,lsums
117 date -u "+ $datefmt sumsdone"
118 x ssh $sshopts $rhost "$sshpfx $rsharedir/snap-drop"
120 if [ "x${localprevious}" != x ] && test -f "$localprevious,rsums"; then
121 cp "$localprevious,rsums" "$localarea,rsums"
124 $rhost:$rvardir/$rsumsfile \
128 date -u "+ $datefmt checking"
131 diff -u <(sed -e 's/^mountpoint/dir /' "$localarea,rsums") \
132 "$localarea,lsums" >"$localarea,sumsdiff"
135 test $diffrc = 0 || test $diffrc = 1
137 date -u "+ $datefmt checked $diffrc"