This repository has been archived by the owner on Nov 20, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo_sync.sh
executable file
·559 lines (514 loc) · 19.3 KB
/
repo_sync.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/bin/bash
####################################################
# GRIM REPO ########################################
####################################################
# repo_auto_sync.sh
#
# Program that checks what needs to be synchronized
# and does the actual synchronization
#
####################################################
if [ -f ~/.grimreporc ]; then
source ~/.grimreporc
else
source `pwd`/.grimreporc
fi
source $GR_PATH/sync_functions.sh
source $GR_PATH/repo_bookkeeper.sh
source $GR_PATH/sql_wrapper.sh
source $GR_PATH/repo_log.sh
while getopts ":hsd:" opt; do
case $opt in
s)
log 1 "-s was triggered, Parameter: $OPTARG"
conflictsolution=$OPTARG;
;;
d)
#-d <verbosity level> was triggered. Valid values:
#0-verbose, 1-warnings/errors only, 2-errors only
log 1 "-d was triggered, Parameter: $OPTARG";
if [[ "$OPTARG" < 0 ]]; then log 2 "Invalid verbosity level -- valid levels: 0,1,2"; exit 1; fi;
if [[ "$OPTARG" > 2 ]]; then log 2 "Invalid verbosity level -- valid levels: 0,1,2"; exit 1; fi;
GR_VERBOSITY_LEVEL=$OPTARG;
;;
\?)
log 2 "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
h)
echo "$0 [-s solution]"
exit 0
esac
done
#do we have any solutions?1
if [ ! -z "$conflictsolution" ]; then
log 0 "got solving answer: $conflictsolution"
solution_filename=`printf "$conflictsolution" | head -n 1 | tail -n 1`
solution_conflict_type=`printf "$conflictsolution" | head -n 2 | tail -n 1`
solution_chksumlocal=`printf "$conflictsolution" | head -n 3 | tail -n 1`
solution_chksumserver=`printf "$conflictsolution" | head -n 4 | tail -n 1`
solution_action=`printf "$conflictsolution" | head -n 5 | tail -n 1`
log 0 "solution_filename $solution_filename"
log 0 "solution conflict_type $solution_conflict_type"
log 0 "olution_chksumlocal $solution_chksumlocal"
log 0 "olution_chksumserver $solution_chksumserver"
log 0 "olution_action $solution_action"
if [ ! -z $solution_action ]; then
got_action=true;
#check for known action types
case "$solution_action" in
COPY_TO_SERVER)
;;
DELETE_SERVER)
;;
COPY_TO_LOCAL)
;;
DELETE_LOCAL)
;;
*)
log 2 "Unknown action: [$solution_action]";
exit 1
esac
fi
fi
#sets the next conflict state
#we only solve one at the time.
#so if allready set, we exit
function set_conflict_state {
if [ ! -z $returnconflict ]; then
#if $returnconflict exists and length > 0
log 1 'one conflict already detected, lets solve it first'
#returnconflict=$1
#exit 0;
else
#else set $return conflict
returnconflict=$1
#exit 0
fi
}
#return conflict parameteres to the caller
function get_conflict_state {
log 0 "repo_sync returning conflict state: $returnconflict"
echo -e $returnconflict
}
#get lists of changes using rsync
#use head and tail to avoid the garbage info from rsync
#s_to_c: what is listed here exists on server, but is either not on, or in a different version on
#client
server_conflicts=`rsync -vrc -n $GR_SERVER:$GR_SERVERROOT $GR_LOCALROOT | tail --lines=+2 | head --lines=-3`
#client_conflicts: What is listed here exists on local, but is either not on, or in a different version on
#server
client_conflicts=`rsync -vrc -n $GR_LOCALROOT $GR_SERVER:$GR_SERVERROOT | tail --lines=+2 | head --lines=-3`
#C_TO_S:
while [ `echo $client_conflicts | wc -w` != 0 ]; do
log 0 "`tree *root`";
#echo Conflict List: $client_conflicts;
#take first line from list of conflicts:
conflict=`echo "$client_conflicts" | head -n 1`
#then remove that line from client_conflicts
client_conflicts=`echo "$client_conflicts" | tail --lines=+2`;
#echo "next conflict=$conflict";
#if it is a directory (which can be identified by the trailing '/')
if echo $conflict | grep -q -P '/$'; then
#it means the dir only exists on local and not on server
#if dir did NOT exist on last sync, it must have been created on local machine
file_existed "$GR_LOCALROOT$conflict";
existed=$?
#did not exist on last sync -- echo "returned $existed";
if [[ $existed == "0" ]]; then
log 1 "$conflict HAS BEEN CREATED LOCALLY";
#dir must have been created on local machine, copy to server
copy_data "$conflict" "$GR_LOCALROOT" "$GR_SERVER:$GR_SERVERROOT";
#update database with the new dir
calculate_dir "$GR_LOCALROOT$conflict";
else
#echo "dir existed on last sync"
#dir must have been deleted on server
has_changed_locally "$GR_LOCALROOT$conflict";
changed=$?
if [[ $changed == "1" ]]; then
#dir deleted on server, but has been changed locally
log 1 "$conflict: DELETED ON SERVER BUT CHANGED LOCALLY"
current_t="DIR_DELETED_SERVER_CHANGED_LOCAL";
chksum_c=`get_checksum "$GR_LOCALROOT$conflict"`
chksum_s="-"; #invalid? - file deleted
#if we got solution, and is it useable. use it. else set conflictstate
log 0 "solution type: $solution_conflict_type"
log 0 "current state: $current_t"
if [ "$solution_conflict_type" == "DIR_DELETED_SERVER_CHANGED_LOCAL" ]; then
log 0 "got solution, checking if we got usable solution"
#chksums correct? -TODO we overwriting/deleting without mutex
#chk chksum for the file/dir to be deleted
case "$solution_action" in
COPY_TO_SERVER)
log 1 "copying $GR_LOCALROOT$conflict to server"
#chksum match?
if [ $solution_chksumlocal == $chksum_c ]; then
#TODO check return code!
copy_data $conflict $GR_LOCALROOT $GR_SERVERROOT
else
log 2 "chksum mismatch for solution";
exit 1
fi #chksum match
;;
DELETE_SERVER)
log 2 "Already deleted on server!";
exit 1;
;;
COPY_TO_LOCAL)
log 2 "Already deleted on server!";
exit 1;
;;
DELETE_LOCAL)
if [ $chksum_c == $solution_chksumlocal ]; then
log 1 "deleting local dir: $GR_LOCALROOT$conflict"
delete_data $conflict $GR_LOCALROOT;
else
log 2 "chksum mismatch for solution";
exit 1
fi #chksum valid
;;
*)
log 2 "Unknown action: [$solution_action]";
exit 1
esac
else #solution_conflict_type not set, eg. user has not set an action for conflict
set_conflict_state "$conflict\nDIR_DELETED_SERVER_CHANGED_LOCAL\n$chksum_c\n$chksum_s";
#give user the choice between copying or delete
fi
else #$changed == "0" -- not changed locally
log 1 "$conflict: DELETED ON SERVER, UNCHANGED LOCALLY - DELETE FROM LOCAL";
#else dir deleted on server and unchanged locally
delete_data "$conflict" "$GR_LOCALROOT"
#update database
delete_dir_entry "$GR_LOCALROOT$conflict";
fi;
fi;
#Delete all sub-entries with dir from client_conflicts, since we solved all
#conflicts with those by either copy, delete or manually
client_conflicts=`echo "$client_conflicts" | grep -P -v "^$conflict"`;
else #it is a file
#if file conflicts on both lists, it means it exists, but have been
#changed one or both places
if echo "$server_conflicts" | grep -q $conflict; then
#file exists in both places
#check if file has changed
has_changed_locally "$GR_LOCALROOT$conflict";
changed=$?;
if [[ $changed == "1" ]]; then
#file changed locally
has_file_changed_remotely "$GR_LOCALROOT$conflict" "$GR_SERVER" "$GR_SERVERROOT$conflict";
remote_changed=$?;
if [[ $remote_changed == "1" ]]; then
#file also changed on server
log 1 "$conflict: FILE CHANGED BOTH LOCALLY AND REMOTELY -- CONFLICT";
chksum_c=`get_checksum "$GR_LOCALROOT$conflict"`
chksum_s=`calc_remote_file_checksum $GR_SERVER "$GR_SERVERROOT$conflict"`
if [ "$solution_conflict_type" == "FILE_CHANGED_BOTH" ]; then
log 0 "got solution, checking if we got usable solution";
#chksums correct? -TODO we overwriting/deleting without mutex
#chk chksum for the file/dir to be deleted
case "$solution_action" in
COPY_TO_SERVER)
log 1 "copying $GR_LOCALROOT$conflict to server";
#chksum match?
if [[ "$solution_chksumlocal" == "$chksum_c" && "$solution_chksumserver" == "$chksum_s" ]]; then
#TODO check return code!
copy_data $conflict $GR_LOCALROOT $GR_SERVERROOT
else
log 2 "chksum mismatch for solution";
fi #chksum match
;;
DELETE_SERVER)
log 2 "changed both places, dont delete server!";
exit 1;
;;
COPY_TO_LOCAL)
if [[ "$solution_chksumserver" == "$chksum_s" && "$solution_chksumlocal" == "$chksum_c" ]]; then
#TODO check return code!
copy_data $conflict $GR_SERVERROOT $GR_LOCALROOT
else
log 2 "chksum mismatch for solution";
fi #chksum match
;;
DELETE_LOCAL)
log 2 "changed both places, dont delete local!";
exit 1
;;
*)
log 2 "Unknown action: [$solution_action]";
exit 1
esac
else
set_conflict_state "$conflict\nFILE_CHANGED_BOTH\n$chksum_c\n$chksum_s";
#give user the choice between copying or delete
fi;
#kij# solve_conflict "$conflict" "$GR_SERVER:$GR_SERVERROOT" "$GR_LOCALROOT"
#either a merging or copying of files has taken place
#in any case we can update database
# calculate_file "$GR_LOCALROOT$conflict"
# echo calculate_file "$GR_LOCALROOT" "$conflict" 1>&2 ;
#delete entry from server_conflicts after resolved conflict
# server_conflicts=`echo "$server_conflicts" | grep -P -v "^$conflict\$"`;
# echo echo "$server_conflicts" | grep -P -v "^$conflict\$" 1>&2 ;
else #file only changed locally only
log 1 "$conflict: FILE CHANGED LOCALLY ONLY -- COPY TO SERVER";
#copy file to server
copy_data "$conflict" "$GR_LOCALROOT" "$GR_SERVER:$GR_SERVERROOT"
#delete entry from server_conflicts, since conflict is solved
server_conflicts=`echo "$server_conflicts" | grep -P -v "^$conflict\$"`;
#update database accordingly
calculate_file "$GR_LOCALROOT$conflict";
fi;
else #file unchanged locally
log 1 "$conflict: FILE CHANGED ON SERVER -- COPY TO LOCAL";
#implied from the diff that it must be changed on server then
#copy from server to local
copy_data "$conflict" "$GR_SERVER:$GR_SERVERROOT" "$GR_LOCALROOT"
#delete entry from server_conflicts, since conflict is solved
server_conflicts=`echo "$server_conflicts" | grep -P -v "^$conflict\$"`;
#update database accordingly
calculate_file "$GR_LOCALROOT$conflict"
fi;
else #file exists on local, not on server
#check if file existed on last sync
file_existed "$GR_LOCALROOT$conflict";
existed=$?
#echo "returned $existed";
if [[ $existed == "0" ]]; then
log 1 "$conflict: FILE CREATED LOCALLY -- COPY TO SERVER";
#file did not exist on last sync, must have been created locally
#copy to server
copy_data "$conflict" "$GR_LOCALROOT" "$GR_SERVER:$GR_SERVERROOT";
#update database with the new dir
calculate_file "$GR_LOCALROOT$conflict";
else #file deleted on server
#check if it has changed locally
has_changed_locally "$GR_LOCALROOT$conflict";
changed=$?;
if [[ $changed == "1" ]]; then
log 1 "$conflict: FILE DELETED ON SERVER BUT MODIFIED LOCALLY -- CONFLICT";
#kij#
chksum_c=`get_checksum "$GR_LOCALROOT$conflict"`;
chksum_s="-";
if [ "$solution_conflict_type" == "FILE_DELETED_SERVER_CHANGED_LOCAL" ]; then
log 0 "got solution, checking if we got usable solution";
#chksums correct? -TODO we overwriting/deleting without mutex
#chk chksum for the file/dir to be deleted
case "$solution_action" in
COPY_TO_SERVER)
log 1 "copying $GR_LOCALROOT$conflict to server";
#chksum match?
if [ $solution_chksumlocal == $chksum_c ]; then
#TODO check return code!
copy_data $conflict $GR_LOCALROOT $GR_SERVERROOT
else
log 2 "chksum mismatch for solution";
exit 1;
fi #chksum match
;;
DELETE_SERVER)
log 2 "File already deleted on server!";
exit 1;
;;
COPY_TO_LOCAL)
log 2 "File already deleted on server!";
exit 1;
;;
DELETE_LOCAL)
if [ $chksum_c == $solution_chksumlocal ]; then
log 1 "deleting local file: $GR_LOCALROOT$conflict";
delete_data $conflict $GR_LOCALROOT;
else
log 2 "chksum mismatch for solution";
exit 1;
fi #chksum valid
;;
*)
log 2 "Unknown action: [$solution_action]";
exit 1
esac
else
set_conflict_state "$conflict\nFILE_DELETED_SERVER_CHANGED_LOCAL\n$chksum_c\n$chksum_s";
#give user the choice between copying or delete
fi
#file deleted on server, but modified locally
# solve_file_deleted_but_changed_locally "$conflict" "$GR_SERVER:$GR_SERVERROOT" "$GR_LOCALROOT";
else #file deleted on server, untouched locally
log 1 "$conflict: FILE DELETED ON SERVER -- DELETE LOCALLY";
#else dir deleted on server and unchanged locally
delete_data "$conflict" "$GR_LOCALROOT"
#update database
delete_file_entry "$GR_LOCALROOT$conflict";
fi;
fi;
fi;
fi; #checking for dir vs file;
done; #end of loop through conflicts
while [ `echo $server_conflicts | wc -w` != 0 ]; do
log 0 "`tree *root`";
log 0 "Server Conflict List: $server_conflicts"
#take first line from list of conflicts:
conflict=`echo "$server_conflicts" | head -n 1`
#then remove that line from server_conflicts
server_conflicts=`echo "$server_conflicts" | tail --lines=+2`;
log 0 "next conflict=$conflict";
#if it is a directory (which can be identified by the trailing '/')
if echo $conflict | grep -q -P '/$'; then
#it means the dir only exists on server and not on client
#if dir did NOT exist on last sync, it must have been created on server
file_existed "$GR_LOCALROOT$conflict";
existed=$?
#echo "returned $existed";
if [[ $existed == "0" ]]; then
log 1 "$conflict HAS BEEN CREATED ON SERVER";
#dir must have been created on server, copy to local
copy_data "$conflict" "$GR_SERVER:$GR_SERVERROOT" "$GR_LOCALROOT";
#update database with the new dir
calculate_dir "$GR_LOCALROOT$conflict";
else #file existed on last sync
#dir must have been deleted on remote
has_dir_changed_remotely "$server_conflicts" "$GR_SERVER" "$GR_SERVERROOT" "$conflict" "$GR_LOCALROOT";
changed=$?
if [[ $changed == "1" ]]; then
log 1 "$conflict: DELETED ON LOCAL BUT CHANGED ON SERVER";
current_t="DIR_DELETED_LOCAL_CHANGED_SERVER";
chksum_c="-"; #invalid? - file deleted
chksum_s=`calc_remote_dir_checksum $GR_SERVER "$GR_SERVERROOT$conflict"`
#if we got solution, and is it useable. use it. else set conflictstate
log 0 "solution type: $solution_conflict_type"
log 0 "current state: $current_t"
if [ "$solution_conflict_type" == "DIR_DELETED_LOCAL_CHANGED_SERVER" ]; then
log 1 "got solution, checking if we got usable solution"
#chksums correct? -TODO we overwriting/deleting without mutex
#chk chksum for the file/dir to be deleted
case "$solution_action" in
COPY_TO_SERVER)
log 2 "Already deleted on local!"
exit 1;
;;
DELETE_SERVER)
if [ $chksum_s == $solution_chksumserver ]; then
log 1 "deleting server dir: $GR_SERVER:$GR_SERVERROOT$conflict";
delete_data $conflict $GR_SERVER:$GR_SERVERROOT;
else
log 2 "chksum mismatch for solution";
exit 1
fi #chksum valid
;;
COPY_TO_LOCAL)
log 1 "copying $GR_SERVERROOT$conflict to local";
#chksum match?
if [ $solution_chksumserver == $chksum_s ]; then
#TODO check return code!
copy_data $conflict $GR_SERVERROOT $GR_LOCALROOT
else
log 2 "chksum mismatch for solution";
exit 1
fi #chksum match
;;
DELETE_LOCAL)
log 2 "Already deleted on local!"
exit 1;
;;
*)
log 2 "Unknown action: [$solution_action]";
exit 1
esac
else #solution_conflict_type not set, eg. user has not set an action for conflict
set_conflict_state "$conflict\nDIR_DELETED_LOCAL_CHANGED_SERVER\n$chksum_c\n$chksum_s";
#give user the choice between copying or delete
fi
else
log 1 "$conflict: DELETED ON LOCAL, UNCHANGED ON SERVER - DELETE FROM SERVER";
#else dir deleted on local machine and unchanged server
#delete the data from server also
delete_data "$conflict" "$GR_SERVER:$GR_SERVERROOT"
#update database
delete_dir_entry "$GR_LOCALROOT$conflict";
fi;
fi;
#Delete all sub-entries with dir from server_conflicts, since we solved all
#conflicts with those by either copy, delete or manually
server_conflicts=`echo "$server_conflicts" | grep -P -v "^$conflict"`;
else #it is a file
#file exists on server and not locally
#check if file existed on last sync
file_existed "$GR_LOCALROOT$conflict";
existed=$?
#echo "returned $existed";
if [[ $existed == "0" ]]; then
#file did not exist on last sync, must have been created on server
log 1 "$conflict: FILE CREATED ON SERVER -- COPY TO LOCAL";
#copy to local
copy_data "$conflict" "$GR_SERVER:$GR_SERVERROOT" "$GR_LOCALROOT";
#update database with the new dir
calculate_file "$GR_LOCALROOT$conflict";
else #file deleted on server
#check if it has changed locally
has_file_changed_remotely "$GR_LOCALROOT$conflict" "$GR_SERVER" "$GR_SERVERROOT$conflict";
changed=$?;
if [[ $changed == "1" ]]; then
#file deleted on local, but modified on server
log 1 "$conflict: FILE DELETED ON LOCALLY BUT MODIFIED ON SERVER -- CONFLICT"
#kij#
chksum_c="-";
chksum_s=`calc_remote_file_checksum $GR_SERVER "$GR_SERVERROOT$conflict"`
if [ "$solution_conflict_type" == "FILE_DELETED_LOCAL_CHANGED_SERVER" ]; then
log 1 "got solution, checking if we got usable solution"
#chksums correct? -TODO we overwriting/deleting without mutex
#chk chksum for the file/dir to be deleted
case "$solution_action" in
COPY_TO_SERVER)
log 2 "File already deleted local!"
exit 1;
;;
DELETE_SERVER)
if [ $chksum_s == $solution_chksumserver ]; then
log 1 "deleting server file: $GR_SERVERROOT$conflict"
delete_data $conflict $GR_SERVERROOT;
else
log 2 "chksum mismatch for solution"
exit 1;
fi #chksum valid
;;
COPY_TO_LOCAL)
log 1 "copying $GR_SERVERROOT$conflict to local"
#chksum match?
if [ $solution_chksumserver == $chksum_s ]; then
#TODO check return code!
copy_data $conflict $GR_SERVERROOT $GR_LOCALROOT
else
log 2 "chksum mismatch for solution"
exit 1;
fi #chksum match
;;
DELETE_LOCAL)
log 2 "File already deleted local!"
exit 1;
;;
*)
log 2 "Unknown action: [$solution_action]"
exit 1
esac
else
set_conflict_state "$conflict\nFILE_DELETED_LOCAL_CHANGED_SERVER\n$chksum_c\n$chksum_s";
#give user the choice between copying or delete
fi
#solve_file_deleted_but_changed_on_server "$conflict" "$GR_SERVER:$GR_SERVERROOT" "$GR_LOCALROOT";
else #file deleted on on local machine, untouched on server
#else file deleted on local machine and unchanged on server
log 1 "$conflict: FILE DELETED LOCALLY -- DELETE FROM SERVER"
#delete the file from server
delete_data "$conflict" "$GR_SERVER:$GR_SERVERROOT"
#update database
delete_file_entry "$GR_LOCALROOT$conflict";
fi;
fi;
fi; #checking for dir vs file;
done; #end of loop through conflicts
get_conflict_state