-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeersync.sh
executable file
·400 lines (329 loc) · 9.99 KB
/
peersync.sh
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/bash
# PeerSync - a file syncronization tool for clustered hosts
# Author: Roland Lammel <[email protected]>
# (C)2015-2019, NeoTel Telefonservice GmbH & Co KG
VERSION=0.9.2
PEERSYNCCONFIGS="$HOME/.peersync /etc/peersync.conf"
DEBUG=${DEBUG-0}
VERBOSE=${VERBOSE-0}
DRYRUN=1
# User-configurable options, which may be overridden in PEERSYNCCONF
PEER=peer
SYNCROOT=/etc/
SYNCFILES=/etc/peersync.files
RSYNCOPTS="-avhizO --numeric-ids --checksum --delete-after"
SYNCTMPDIR=/tmp
DIFFBIN=diff
DIFFOPTS="-urw"
### FUNCTIONS
debuglog() {
if [ $DEBUG -eq 1 ]; then
echo "[DEBUG] $*"
fi
}
verbose() {
if [ $VERBOSE -eq 1 ]; then
echo "$*"
fi
}
version() {
echo "PeerSync v$VERSION - A peer file syncronisation tool based on rsync"
echo "(C)2016, NeoTel Telefonservice GmbH & Co KG"
}
rsync_peer() {
debuglog "Executing (verbose=$VERBOSE): rsync $RSYNCOPTS --filter=". $SYNCFILES" $SYNCROOT $PEER:$SYNCROOT"
if [ $DRYRUN -eq 1 ]; then
RSYNCOPTS="$RSYNCOPTS --dry-run"
fi
if [ $VERBOSE -eq 1 ]; then
rsync $RSYNCOPTS --filter=". $SYNCFILES" $SYNCROOT $PEER:$SYNCROOT
else
rsync $RSYNCOPTS --filter=". $SYNCFILES" $SYNCROOT $PEER:$SYNCROOT | grep -v '^\.[fdL]\.\.t\.\.\.\.\.\. '
fi
RC=$?
debuglog "Rsync returned exit code $RC"
}
rdiff_peer() {
if [ -z "$DIFFTMPDIR" -o "$DIFFTMPDIR" = "" ]; then
TMPDIR=`mktemp -d /tmp/peersync.XXXX`
else
TMPDIR=$DIFFTMPDIR
fi
if [ ! -d "$TMPDIR" ]; then
verbose "* Create temp. path $TMPDIR for diff"
mkdir -p "$TMPDIR"
else
### Cleanup tmpdir for new compare
rm -rf "$TMPDIR/local"
rm -rf "$TMPDIR/peer"
fi
write_tempfilter $TMPDIR/peerdiff.files
verbose "* Sync local ($SYNCROOT) and remote ($PEER:$SYNCROOT) files to $TMPDIR"
rsync -ahd --filter=". $SYNCFILES" $PEER:$SYNCROOT $TMPDIR/peer/;
rsync -ahd --filter=". $SYNCFILES" $SYNCROOT $TMPDIR/local/;
verbose "* Compare content ($DIFFBIN) in $TMPDIR/peer and $TMPDIR/local"
verbose ""
$DIFFBIN $DIFFOPTS $TMPDIR/peer $TMPDIR/local
if [ $DEBUG -eq 0 ]; then
debuglog "Cleanup tmpdir $TMPDIR"
rm -rf "$TMPDIR"
fi
}
write_tempfilter() {
TMPFILTER=$1
shift
debuglog "* Prepare filelist for remote diff in $TMPFILTER: $*"
echo "# auto-generated peersync compare filter" > $TMPFILTER
if [ $# -gt 0 ]; then
for path in $*; do
debuglog " * Adding: $path"
echo $path | awk -F '/' '{ PATH=""; for (i = 1; i < NF; i++) { PATH=PATH $i "/"; print "+ " PATH }; print "+ " $0 }' >> $TMPFILTER
done
else
rsync -aiO --dry-run --filter=". $SYNCFILES" $SYNCROOT $PEER:$SYNCROOT | awk '{ print $2 }' | awk -F '/' '{ PATH=""; for (i = 1; i < NF; i++) { PATH=PATH $i "/"; print "+ " PATH }; print "+ " $0 }' | sort -u > $TMPFILTER
fi
echo "- *" >> $TMPFILTER
if [ $DEBUG -eq 1 ]; then
cat $TMPFILTER
fi
}
### BASIC CLI PROCESSING
if [ "$1" = "version" ]; then
version
exit 1
fi
if [ "$1" = "help" ]; then
version
cat <<__EOF_HELP
PeerSync
========
A peer file syncronisation tool based on rsync
USAGE
-----
peersync [ config | help | version ]
peersync [OPTIONS] [ show | sync | diff ] [FILTER1 FILTER2 ...]
COMMANDS
--------
show [FILTER1 FILTER2 ...]
Show files, link and directories with differences (in rsync itemize
format), no files will be modified on the peer.
If file filtering rules are specified, these will take precedence and
only files matching this pattern will be considered.
See FILE FILTERING RULES below.
sync [FILTER1 FILTER2 ...]
Sync of files to peer, files on the peer will be over-written
(no backup is generated).
File filtering rules may be specified (see show)
diff [FILTER1 FILTER2 ...]
Compore the file contents of changed file against peer. A local copy
will be compared to a copy of the remote files using the diff command
(default: diff -urw).
File filtering rules may be specified (see show)
config
Show the current peersync configuration and file filter for syncing
version
Show the version of peersync
help
Show help on commands and configuration
When no command is specified "show" is assumed
OPTIONS
-------
-c CONFIGFILE
User configuraiton file from CONFIGFILE
-C Use no configuration file, be sure to specify -p and -s
-p PEER
Use PEER as peer to sync against (usually USER@HOST or HOST)
-s SYNCROOT
Use SYNCROOT as root for syncronisation, overrides configuration
-d Enable DEBUG logging, for debug purposes only
CONFIGURATION FILE
------------------
The configuration for peersync can be placed in $HOME/.peersync or /etc/peersync.conf.
The first file found will be loaded.
Example configuration in /etc/peersync.conf (commented options show defaults):
PEER=myuser@mypeer
SYNCROOT=/etc/
# SYNCFILES=/etc/peersync.files
# RSYNCOPTS=-avhizO --numeric-ids --checksum --delete-after
# DIFFBIN=diff
# DIFFOPTS=-urw
# DIFFTMPDIR=
FILE FILTERING RULES
--------------------
Example file filtering rules in /etc/peersync.files:
- /nginx/ssl/
+ /nginx/***
- *
For SYNCROOT=/etc/ this will sync all nginx configurations of /etc/nginx (and
below), exlcuding the /etc/nginx/ssl directory. All other files in /etc will
also be excluded, due to the "- *" rule.
This example will sync only the "nginx" directory excluding the "nginx/ssl"
sub-directory, all other files and directory are not considered. The example
assumes "/etc" as the synchronization root directory (SYNCROOT=/etc/).
See the INCLUDE/EXCLUDE PATTERN RULES section in the rsync manpage for details.
COPYRIGHT
---------
(C)2015-2019, NeoTel Telefonservice GmbH & Co KG
__EOF_HELP
exit 1
fi
### PREPROCESS COMMANDLINE
debuglog "Preprocessing commandline $* "
while getopts "ds:Cc:p:" opt $*; do
debuglog "Processing option $opt"
case $opt in
s)
debuglog "Setting syncroot to $OPTARG"
FORCESYNCROOT=$OPTARG
;;
C)
debuglog "Setting no configuration file"
PEERSYNCCONF=-
SYNCFILES=-
;;
c)
debuglog "Setting configuration file to $OPTARG"
PEERSYNCCONF=$OPTARG
;;
p)
debuglog "Setting peer to $OPTARG"
FORCEPEER=$OPTARG
;;
d)
DEBUG=1
;;
\?)
echo "Invalid option: -$OPTARG"
;;
esac
done
shift $((OPTIND-1))
CMD=$1
shift
if [ -z "$CMD" ]; then
CMD="show"
fi
### LOAD CONFIGURATION
if [ -z "$PEERSYNCCONF" ]; then
debuglog "* Searching for configuration"
for conf in $PEERSYNCCONFIGS; do
debuglog " - Try loading: $conf"
if [ -r "$conf" ]; then
debuglog "Using configuration - Try loading: $conf"
PEERSYNCCONF=$conf
break
fi
done
fi
if [ "$PEERSYNCCONF" != "-" ]; then
if [ ! -r "$PEERSYNCCONF" ]; then
echo "PeerSync configuration file $PEERSYNCCONF not found. Please create."
echo "See peersync help for details."
exit 1
fi
debuglog "Loading configuration $PEERSYNCCONF"
. "$PEERSYNCCONF"
fi
if [ ! -z $FORCEPEER ]; then
PEER=$FORCEPEER
fi
if [ ! -z $FORCESYNCROOT ]; then
SYNCROOT=$FORCESYNCROOT
fi
debuglog "Using configuration"
debuglog " PEER=$PEER"
debuglog " SYNCFILES=$SYNCFILES"
debuglog " SYNCROOT=$SYNCROOT"
debuglog " RSYNCOPTS=$RSYNCOPTS"
### VERIFY CONFIGURATION IS SET
if [ "$SYNCFILES" != "-" ]; then
if [ -z "$SYNCFILES" ]; then
echo "ERROR: PeerSync file filter rules are not defined."
echo "Please ensure SYNCFILES is set to a file specifying filtering rules"
exit 1
fi
fi
if [ -z "$SYNCROOT" ]; then
echo "ERROR: SYNCROOT is not set. Please ensure SYNCROOT is set to a valid directory."
exit 1
fi
### VERIFY CONFIGURATION IS VALID
if [ "$SYNCFILES" != "-" ]; then
if [ ! -r "$SYNCFILES" ]; then
echo "ERROR: Peersync file filtering rules '$SYNCFILES' not found. Please create."
echo "For instructions see peersync help"
exit 1
fi
fi
if [[ ! "$SYNCROOT" =~ /$ ]]; then
SYNCROOT=$SYNCROOT/
echo "Appending '/' to syncroot: $SYNCROOT"
fi
if [ ! -d "$SYNCROOT" ]; then
echo "ERROR: Specified SYNCROOT '$SYNCROOT' is not a directory or not readable."
exit 1
fi
### PROCESS COMMANDS
# Disable wildcard expension now
set -f
case "$CMD" in
sync | force)
echo "*** Synchronizing and writting files in $SYNCROOT to $PEER"
echo ""
DRYRUN=0
if [ "$1" = "verbose" ]; then
VERBOSE=1
fi
if [ $# -gt 0 ]; then
SYNCFILES=`mktemp`
debuglog "sync using custom filelist $SYNCFILES"
write_tempfilter $SYNCFILES $*
rsync_peer
rm $SYNCFILES
else
rsync_peer
fi
;;
show)
echo "*** Show differences of files in $SYNCROOT against $PEER"
echo ""
DRYRUN=1
if [ "$1" = "verbose" ]; then
VERBOSE=1
fi
if [ $# -gt 0 ]; then
SYNCFILES=`mktemp`
debuglog "show using custom filelist $SYNCFILES"
write_tempfilter $SYNCFILES $*
rsync_peer
rm $SYNCFILES
else
rsync_peer
fi
;;
diff)
echo "*** Show content differences against $PEER"
echo ""
VERBOSE=1
if [ $# -gt 0 ]; then
debuglog "rdiff using custom filelist"
SYNCFILES=`mktemp`
write_tempfilter $SYNCFILES $*
rdiff_peer
rm $SYNCFILES
else
rdiff_peer
fi
;;
config)
echo "*** Show peersync configuration $PEERSYNCCONF"
cat $PEERSYNCCONF
echo ""
echo "*** Show file filtering configuration $SYNCFILES"
cat $SYNCFILES
echo ""
;;
*)
echo "Unknown command $CMD. Please see help for details"
;;
esac