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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
#!/bin/sh
root=$( realpath "$( dirname "$0" )" )
direction=$1
ALIAS="$root/alias"
EXCLUDES="$root/excludes"
DOTEXCLUDES="$root/.excludes"
FILES="$root/files"
USERS="$root/users"
KEYS="$root/keys"
RSYNC="rsync -a -s --progress --delete"
LOCAL="storage"
host_name() {
local host=$1
local alias_path="$ALIAS/$host"
if [ -f "$alias_path" ]
then
cat "$alias_path"
else
echo "$host"
fi
}
host_prefix() {
local host=$1
local hostname=$( hostname )
local user_path="$USERS/$host.txt"
local user
for local in $LOCAL
do
if [ "$host" = "$local" ]
then
return
fi
done
if [ "$host" = "$hostname" ]
then
return
fi
if [ -f "$user_path" ]
then
user=$( cat "$user_path" )
echo "$user@$host:"
else
echo "$host:"
fi
}
host_key() {
local source_host=$1
local destination_host=$2
local hostname=$( hostname )
local key
local test
for local in $LOCAL
do
if [ "$source_host" = "$local" ] || [ "$destination_host" = "$local" ]
then
return
fi
done
if [ -f "$KEYS/$hostname.txt" ]
then
key=$( eval echo "$( cat "$KEYS/$hostname.txt" )" )
test=$( ssh-add -l | grep "$key" || true )
if [ -z "$test" ]
then
echo -e "Adding SSH key for \e[1;33m$hostname\e[0m:"
ssh-add "$key"
fi
fi
}
host_files_list() {
local host=$1
local qualifier=$2
local host_files_path="$FILES/$host.txt"
local qualifier_host_files_path="$FILES/$qualifier/$host.txt"
local descriptor
if [ -f "$host_files_path" ]
then
cat "$host_files_path" | while read descriptor
do
eval echo "$descriptor"
done
fi
if [ -f "$qualifier_host_files_path" ]
then
cat "$qualifier_host_files_path" | while read descriptor
do
eval echo "$descriptor"
done
fi
}
host_file_path() {
local host_file_descriptor=$1
local descriptor=$( echo "$host_file_descriptor" | sed "s,\([^+]*\)+.*$,\1,g" )
echo "$descriptor" | sed "s,:.*$,,g" | head -n 1
}
host_file_path_files() {
local host_file_descriptor=$1
echo "$host_file_descriptor" | sed -n "s,.*+\(.*\)$,\1,p"
}
host_file_name() {
local host_file_descriptor=$1
shift
local host_file_name
local descriptor=$( echo "$host_file_descriptor" | sed "s,\([^+]*\)+.*$,\1,g" )
local suffix=$( echo "$descriptor" | sed -n "s,[^:]*:\(.*\)$,\1,p" )
local base=$( echo "$descriptor" | sed "s,:.*$,,g" )
local test=$@
local name
if [ -z "$suffix" ]
then
host_file_name=$( basename "$base" )
else
host_file_name=$suffix
fi
if [ -z "$test" ]
then
echo "$host_file_name"
return
fi
for name in "$@"
do
if [ "$host_file_name" = "$name" ]
then
echo "$name"
fi
done
}
host_file_match() {
local destination=$1
local source_file_name=$2
local descriptor
local name
host_files_list "$destination" "destination" | while read descriptor
do
name=$( host_file_name "$descriptor" )
if [ "$source_file_name" = "$name" ]
then
echo "$descriptor"
fi
done
}
host_excludes() {
local host=$1
local name=$2
local excludes_path="$EXCLUDES/$name.txt"
local host_excludes_path="$EXCLUDES/$host/$name.txt"
if [ -f "$excludes_path" ]
then
cat "$excludes_path"
fi
if [ -f "$host_excludes_path" ]
then
cat "$host_excludes_path"
fi
}
usage() {
printf "$0 [source] [destination] (names)\n" >&2
}
sync() {
local source=$1
shift
local destination=$1
shift
local source_prefix
local source_path
local source_files
local source_final
local destination_prefix
local destination_path
local destination_files
local destination_final
local descriptor
local path_file
local name
local match
local dry
local dry_description
set -e
if [ -z "$source" ] || [ -z "$destination" ]
then
usage
exit 1
fi
source_name=$( host_name "$source" )
destination_name=$( host_name "$destination" )
source_prefix=$( host_prefix "$source" )
destination_prefix=$( host_prefix "$destination" )
host_key "$source_name" "$destination_name"
if [ -z "$DRY" ] && [ -z "$NODRY" ] && [ "$1" != "list" ]
then
echo -en "Sync in \e[1;31mdry mode\e[0m: "
read DRY
fi
host_files_list "$source_name" "source" | while read descriptor
do
if [ "$1" = "list" ]
then
host_file_name "$descriptor" | while read name
do
match=$( host_file_match "$destination_name" "$name" )
if [ -z "$match" ]
then
continue
fi
echo -e "\e[1;34m$name\e[0m"
done
continue
fi
name=$( host_file_name "$descriptor" "$@" )
if [ -z "$name" ]
then
continue
fi
match=$( host_file_match "$destination_name" "$name" )
if [ -z "$match" ]
then
continue
fi
if [ -n "$DRY" ]
then
dry="--dry-run"
dry_description=" in \e[1;31mdry mode\e[0m"
fi
source_path=$( host_file_path "$descriptor" )
destination_path=$( host_file_path "$match" )
source_files=$( host_file_path_files "$descriptor" )
destination_files=$( host_file_path_files "$match" )
# Since each source is read separately, there can only be a single file.
if [ -n "$source_files" ]
then
destination_files=$source_files
fi
echo -n "" > "$DOTEXCLUDES"
host_excludes "$destination_name" "$name" >> "$DOTEXCLUDES"
host_excludes "$source_name" "$name" >> "$DOTEXCLUDES"
IFS=$'\n'
for file in "" $destination_files
do
if [ -z "$file" ]
then
if [ -n "$destination_files" ]
then
continue
fi
source_final="$source_prefix$source_path"
destination_final="$destination_prefix$destination_path"
else
source_final="$source_prefix$source_path/$file"
destination_final="$destination_prefix$destination_path/$file"
fi
echo -e "Sync \e[1;34m$name\e[0m from \e[1;33m$source\e[0m to \e[1;33m$destination\e[0m$dry_description"
unset IFS
$RSYNC $dry --exclude-from="$DOTEXCLUDES" "$source_final" "$destination_final"
done
unset IFS
rm "$DOTEXCLUDES"
done
}
sync "$@"
|