-
Notifications
You must be signed in to change notification settings - Fork 1
/
extsync
executable file
·161 lines (142 loc) · 3.96 KB
/
extsync
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
# Synchronise subtrees between remote hosts using rsync.
USAGE="Usage: $0 -r <REMOTE> [-l <LOCAL>] [OPTIONS] <DIRECTION> <DIR> [<DIR> ...]"
abspath() { cd "$1"; pwd; cd "$OLDPWD"; }
run_d() { test ! -r "$1" || "./$1"; }
abort() { echo >&2 "$2"; exit $1; }
DRYRUN=false
CONFIRM=true
rrsync() {
local wrap=$1
shift 1
local run=env
local failmsg="$0: rsync failed"
if [ -r "$wrap" ]; then
run="./$wrap"
failmsg="$0: ./$wrap failed"
fi
if $DRYRUN; then
echo "${PS4}rsync $@"
return
fi
if $CONFIRM; then
"$run" rsync -n "$@" 2>&1 | less
read -p "Execute this run? [y/N] " x
test "$x" = "y" || return 1
fi
"$run" rsync "$@" || abort 1 "$failmsg"
}
RFLAGS="-acvxz --no-g --no-o --delete"
runsync() {(
local direc="$1"
local dir_l="$2"
local dir_r="$3"
cd "$dir_l"
case "$direc" in
u|up)
run_d extsync.pre_u.rc || abort 1 "$0: ./extsync.pre_u.rc failed"
rrsync extsync.wrap_u.rc $RFLAGS -u "$dir_l/" "$dir_r/"
;;
d|down)
rrsync extsync.wrap_d.rc $RFLAGS -u "$dir_r/" "$dir_l/"
run_d extsync.post_d.rc || abort 1 "$0: ./extsync.post_d.rc failed"
;;
esac
)}
type colordiff >/dev/null 2>&1 && DIFF=colordiff || {
echo >&2 "colordiff(1) not found; using diff(1) instead"
DIFF=diff
}
rundiff() {
local direc="$1"
local dir_l="$2"
local dir_r="$3"
local tempdir="$(mktemp -d)"
local cleanup="rm -rf $tempdir"
trap "$cleanup" HUP INT QUIT TERM KILL
rsync $RFLAGS -q "$dir_l/" "$tempdir/"
rsync $RFLAGS -q "$dir_r/" "$tempdir/"
case "$direc" in
u|up)
$DIFF -Nru "$tempdir/" "$dir_l/" | less -R
;;
d|down)
$DIFF -Nru "$dir_l/" "$tempdir/" | less -R
;;
esac
$cleanup
}
run() {
local base_l="$1"
local base_r="$2"
local cmd="$3"
local direc="$4"
local dir="$5"
# resolve path
if ! test -d "$dir"; then
if ! test -d "$base_l/$dir"; then
echo >&2 "not a directory: $dir or $base_l/$dir"
return 1
else
dir="$base_l/$dir"
fi
fi
local dir_l="$(abspath "${dir:-.}")"
test "${dir_l#$base_l/}" != "$dir_l" || { echo >&2 "not under local base $base_l: $dir"; return 1; }
local dir_r="$base_r/${dir_l#$base_l/}"
$cmd "$direc" "$dir_l" "$dir_r"
}
BASE_L="${HOME:-}"
RUNCMD=runsync
while getopts r:l:dnyh o; do
case $o in
r ) BASE_R="$OPTARG";;
l ) BASE_L="$OPTARG";;
d ) RUNCMD=rundiff;;
n ) DRYRUN=true;;
y ) CONFIRM=false;;
h )
cat <<-EOF
$USAGE
Synchronise subtrees between remote hosts using rsync.
This should be largely compatible with unison(1) when using the same BASE
directories (called "roots" in unison), but is a bit easier to automate.
This command runs several instances of ssh(1). It is recommended that you
use a remote Host where you've configured a ControlPath; see ssh_config(5)
for details. Before running this script, run \`ssh -M \$host\` to set up the
master connection, and this script will then run through that. (This tip is
useful for unison too.)
Arguments:
DIRECTION Up = sync local to remote; Down = sync remote to local.
DIR Directory to sync. If \$DIR = \$LOCAL/\$RELATIVE,
then \$DIR will be synced to/from \$REMOTE/\$RELATIVE.
Options:
-r REMOTE Path to remote base dir, in the form accepted by ssh(1).
-l LOCAL Path to local base dir, \$HOME by default.
-n Print the command to be run, rather than running it.
-d Print a diff of what rsync would apply. Practically,
this syncs to a temporary directory then runs diff(1) on
it. This is slower but prettier than unison(1).
-y Skip confirmation request.
-h This help text.
EOF
exit 1
;;
\? ) echo $USAGE; exit 1;;
esac
done
shift `expr $OPTIND - 1`
DIREC="$1"
shift
case "$DIREC" in
u|up);;
d|down);;
*)
abort 2 "specify a direction, up or down."
;;
esac
test -n "$BASE_R" || abort 2 "no remote selected; use -r or BASE_R="
test -n "$*" || abort 2 "no paths selected"
for D in "$@"; do
run "$BASE_L" "$BASE_R" "$RUNCMD" "$DIREC" "$D"
done