forked from deajan/osync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osync.sh
executable file
·2113 lines (1939 loc) · 63.6 KB
/
osync.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
PROGRAM="Osync" # Rsync based two way sync engine with fault tolerance
AUTHOR="(L) 2013-2014 by Orsiris \"Ozy\" de Jong"
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
PROGRAM_VERSION=0.99RC3+
PROGRAM_BUILD=0807201401
## type doesn't work on platforms other than linux (bash). If if doesn't work, always assume output is not a zero exitcode
if ! type -p "$BASH" > /dev/null
then
echo "Please run this script only with bash shell. Tested on bash >= 3.2"
exit 127
fi
## allow debugging from command line with preceding ocsync with DEBUG=yes
if [ ! "$DEBUG" == "yes" ]
then
DEBUG=no
SLEEP_TIME=1
else
SLEEP_TIME=10
fi
SCRIPT_PID=$$
LOCAL_USER=$(whoami)
LOCAL_HOST=$(hostname)
## Default log file until config file is loaded
if [ -w /var/log ]
then
LOG_FILE=/var/log/osync.log
else
LOG_FILE=./osync.log
fi
## Default directory where to store temporary run files
if [ -w /dev/shm ]
then
RUN_DIR=/dev/shm
elif [ -w /tmp ]
then
RUN_DIR=/tmp
elif [ -w /var/tmp ]
then
RUN_DIR=/var/tmp
else
RUN_DIR=.
fi
## Working directory. Will keep current file states, backups and soft deleted files.
OSYNC_DIR=".osync_workdir"
## Log a state message every $KEEP_LOGGING seconds. Should not be equal to soft or hard execution time so your log won't be unnecessary big.
KEEP_LOGGING=1801
## Correct output of sort command (language agnostic sorting)
export LC_ALL=C
function Log
{
if [ $sync_on_changes -eq 1 ]
then
prefix="$(date) - "
else
prefix="TIME: $SECONDS - "
fi
echo -e "$prefix$1" >> "$LOG_FILE"
if [ $silent -eq 0 ]
then
echo -e "$prefix$1"
fi
}
function LogError
{
Log "$1"
error_alert=1
}
function LogDebug
{
if [ "$DEBUG" == "yes" ]
then
Log "$1"
fi
}
function TrapError
{
local JOB="$0"
local LINE="$1"
local CODE="${2:-1}"
if [ $silent -eq 0 ]
then
echo -e " /!\ ERROR in ${JOB}: Near line ${LINE}, exit code ${CODE}"
fi
}
function TrapUsr1
{
echo "Still doing stuff"
}
function TrapStop
{
if [ $soft_stop -eq 0 ]
then
LogError " /!\ WARNING: Manual exit of osync is really not recommended. Sync will be in inconsistent state."
LogError " /!\ WARNING: If you are sure, please hit CTRL+C another time to quit."
soft_stop=1
return 1
fi
if [ $soft_stop -eq 1 ]
then
LogError " /!\ WARNING: CTRL+C hit twice. Quitting osync. Please wait..."
soft_stop=2
exit 1
fi
}
function TrapQuit
{
if [ $error_alert -ne 0 ]
then
SendAlert
UnlockDirectories
CleanUp
LogError "Osync finished with errros."
exitcode=1
else
UnlockDirectories
CleanUp
Log "Osync finished."
exitcode=0
fi
if ps -p $child_pid > /dev/null 2>&1
then
kill -9 $child_pid
fi
if ps -p $sub_pid > /dev/null 2>&1
then
kill -9 $sub_pid
fi
exit $exitcode
}
function Spinner
{
if [ $silent -eq 1 ]
then
return 1
fi
case $toggle
in
1)
echo -n $1" \ "
echo -ne "\r"
toggle="2"
;;
2)
echo -n $1" | "
echo -ne "\r"
toggle="3"
;;
3)
echo -n $1" / "
echo -ne "\r"
toggle="4"
;;
*)
echo -n $1" - "
echo -ne "\r"
toggle="1"
;;
esac
}
function EscapeSpaces
{
echo $(echo "$1" | sed 's/ /\\ /g')
}
function CleanUp
{
if [ "$DEBUG" != "yes" ]
then
rm -f $RUN_DIR/osync_*_$SCRIPT_PID
fi
}
function SendAlert
{
if [ "$quick_sync" == "2" ]
then
Log "Current task is a quicksync task. Will not send any alert."
return 0
fi
cat "$LOG_FILE" | gzip -9 > $RUN_DIR/osync_lastlog.gz
MAIL_ALERT_MSG=$MAIL_ALERT_MSG$'\n\n'$(tail -n 25 "$LOG_FILE")
if type -p mutt > /dev/null 2>&1
then
echo $MAIL_ALERT_MSG | $(type -p mutt) -x -s "Sync alert for $SYNC_ID" $DESTINATION_MAILS -a $RUN_DIR/osync_lastlog.gz
if [ $? != 0 ]
then
Log "WARNING: Cannot send alert email via $(type -p mutt) !!!"
else
Log "Sent alert mail using mutt."
fi
elif type -p mail > /dev/null 2>&1
then
echo $MAIL_ALERT_MSG | $(type -p mail) -a $RUN_DIR/osync_lastlog.gz -s "Sync alert for $SYNC_ID" $DESTINATION_MAILS
if [ $? != 0 ]
then
Log "WARNING: Cannot send alert email via $(type -p mail) with attachments !!!"
echo $MAIL_ALERT_MSG | $(type -p mail) -s "Sync alert for $SYNC_ID" $DESTINATION_MAILS
if [ $? != 0 ]
then
Log "WARNING: Cannot send alert email via $(type -p mail) without attachments !!!"
else
Log "Sent alert mail using mail command without attachment."
fi
else
Log "Sent alert mail using mail command."
fi
elif type -p sendemail > /dev/null 2>&1
then
if [ "$SMTP_USER" != "" ] && "$SMTP_PASSWORD" != "" ]
then
$SMTP_OPTIONS="-xu $SMTP_USER -xp $SMTP_PASSWORD"
else
$SMTP_OPTIONS=""
fi
$(type -p sendemail) -f $SENDER_MAIL -t $DESTINATION_MAILS -u "Backup alert for $BACKUP_ID" -m "$MAIL_ALERT_MSG" -s $SMTP_SERVER $SMTP_OPTIONS > /dev/null 2>&1
if [ $? != 0 ]
then
Log "WARNING: Cannot send alert email via $(type -p sendemail) !!!"
else
Log "Sent alert mail using sendemail command without attachment."
fi
else
Log "WARNING: Cannot send alert email (no mutt / mail present) !!!"
return 1
fi
if [ -f $RUN_DIR/osync_lastlog.gz ]
then
rm $RUN_DIR/osync_lastlog.gz
fi
}
function LoadConfigFile
{
if [ ! -f "$1" ]
then
LogError "Cannot load configuration file [$1]. Sync cannot start."
exit 1
elif [[ "$1" != *".conf" ]]
then
LogError "Wrong configuration file supplied [$1]. Sync cannot start."
exit 1
else
egrep '^#|^[^ ]*=[^;&]*' "$1" > "$RUN_DIR/osync_config_$SCRIPT_PID"
source "$RUN_DIR/osync_config_$SCRIPT_PID"
fi
}
function CheckEnvironment
{
if [ "$REMOTE_SYNC" == "yes" ]
then
if ! type -p ssh > /dev/null 2>&1
then
LogError "ssh not present. Cannot start sync."
return 1
fi
fi
if ! type -p rsync > /dev/null 2>&1
then
LogError "rsync not present. Sync cannot start."
return 1
fi
}
function GetLocalOS
{
LOCAL_OS_VAR=$(uname -spio 2>&1)
if [ $? != 0 ]
then
LOCAL_OS_VAR=$(uname -v 2>&1)
if [ $? != 0 ]
then
LOCAL_OS_VAR=($uname)
fi
fi
case $LOCAL_OS_VAR in
*"Linux"*)
LOCAL_OS="Linux"
;;
*"BSD"*)
LOCAL_OS="BSD"
;;
*"MINGW32"*)
LOCAL_OS="msys"
;;
*"Darwin"*)
LOCAL_OS="MacOSX"
;;
*)
LogError "Running on >> $LOCAL_OS_VAR << not supported. Please report to the author."
exit 1
;;
esac
LogDebug "Local OS: [$LOCAL_OS_VAR]."
}
function GetRemoteOS
{
if [ "$REMOTE_SYNC" == "yes" ]
then
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
eval "$SSH_CMD \"uname -spio\" > $RUN_DIR/osync_remote_os_$SCRIPT_PID 2>&1"
child_pid=$!
WaitForTaskCompletion $child_pid 120 240
retval=$?
if [ $retval != 0 ]
then
eval "$SSH_CMD \"uname -v\" > $RUN_DIR/osync_remote_os_$SCRIPT_PID 2>&1"
child_pid=$!
WaitForTaskCompletion $child_pid 120 240
retval=$?
if [ $retval != 0 ]
then
eval "$SSH_CMD \"uname\" > $RUN_DIR/osync_remote_os_$SCRIPT_PID 2>&1"
child_pid=$!
WaitForTaskCompletion $child_pid 120 240
retval=$?
if [ $retval != 0 ]
then
LogError "Cannot Get remote OS type."
fi
fi
fi
REMOTE_OS_VAR=$(cat $RUN_DIR/osync_remote_os_$SCRIPT_PID)
case $REMOTE_OS_VAR in
*"Linux"*)
REMOTE_OS="Linux"
;;
*"BSD"*)
REMOTE_OS="BSD"
;;
*"MINGW32"*)
REMOTE_OS="msys"
;;
*"Darwin"*)
REMOTE_OS="MacOSX"
;;
"ssh"*)
LogError "Cannot connect to remote system."
exit 1
;;
*)
LogError "Running on remote >> $REMOTE_OS_VAR << not supported. Please report to the author."
exit 1
esac
LogDebug "Remote OS: [$REMOTE_OS_VAR]."
fi
}
# Waits for pid $1 to complete. Will log an alert if $2 seconds passed since current task execution unless $2 equals 0.
# Will stop task and log alert if $3 seconds passed since current task execution unless $3 equals 0.
function WaitForTaskCompletion
{
soft_alert=0
SECONDS_BEGIN=$SECONDS
if [ "$LOCAL_OS" == "msys" ]
then
PROCESS_TEST_CMD="ps -a | awk '{\$1=\$1}\$1' | awk '{print \$1}' | grep $1"
else
PROCESS_TEST_CMD="ps -p$1"
fi
while eval $PROCESS_TEST_CMD > /dev/null
do
Spinner
EXEC_TIME=$(($SECONDS - $SECONDS_BEGIN))
if [ $((($EXEC_TIME + 1) % $KEEP_LOGGING)) -eq 0 ]
then
Log "Current task still running."
fi
if [ $EXEC_TIME -gt $2 ]
then
if [ $soft_alert -eq 0 ] && [ $2 != 0 ]
then
LogError "Max soft execution time exceeded for task."
soft_alert=1
fi
if [ $EXEC_TIME -gt $3 ] && [ $3 != 0 ]
then
LogError "Max hard execution time exceeded for task. Stopping task execution."
kill -s SIGTERM $1
if [ $? == 0 ]
then
LogError "Task stopped succesfully"
else
LogError "Sending SIGTERM to proces failed. Trying the hard way."
kill -9 $1
if [ $? != 0 ]
then
LogError "Could not stop task."
fi
fi
return 1
fi
fi
sleep $SLEEP_TIME
done
wait $child_pid
return $?
}
# Waits for pid $1 to complete. Will log an alert if $2 seconds passed since script start unless $2 equals 0.
# Will stop task and log alert if $3 seconds passed since script start unless $3 equals 0.
function WaitForCompletion
{
soft_alert=0
if [ "$LOCAL_OS" == "msys" ]
then
PROCESS_TEST="ps -a | awk '{\$1=\$1}\$1' | awk '{print \$1}' | grep $1"
else
PROCESS_TEST="ps -p$1"
fi
while eval $PROCESS_TEST > /dev/null
do
Spinner
if [ $((($SECONDS + 1) % $KEEP_LOGGING)) -eq 0 ]
then
Log "Current task still running."
fi
if [ $SECONDS -gt $2 ]
then
if [ $soft_alert -eq 0 ] && [ $2 != 0 ]
then
LogError "Max soft execution time exceeded for script."
soft_alert=1
fi
if [ $SECONDS -gt $3 ] && [ $3 != 0 ]
then
LogError "Max hard execution time exceeded for script. Stopping current task execution."
kill -s SIGTERM $1
if [ $? == 0 ]
then
LogError "Task stopped succesfully"
else
LogError "Sending SIGTERM to proces failed. Trying the hard way."
kill -9 $1
if [ $? != 0 ]
then
LogError "Could not stop task."
fi
fi
return 1
fi
fi
sleep $SLEEP_TIME
done
wait $child_pid
return $?
}
## Runs local command $1 and waits for completition in $2 seconds
function RunLocalCommand
{
if [ $dryrun -ne 0 ]
then
Log "Dryrun: Local command [$1] not run."
return 1
fi
Log "Running command [$1] on local host."
eval "$1" > $RUN_DIR/osync_run_local_$SCRIPT_PID 2>&1 &
child_pid=$!
WaitForTaskCompletion $child_pid 0 $2
retval=$?
if [ $retval -eq 0 ]
then
Log "Command succeded."
else
LogError "Command failed."
fi
if [ $verbose -eq 1 ] || [ $retval -ne 0 ]
then
Log "Command output:\n$(cat $RUN_DIR/osync_run_local_$SCRIPT_PID)"
fi
if [ "$STOP_ON_CMD_ERROR" == "yes" ] && [ $retval -ne 0 ]
then
exit 1
fi
}
## Runs remote command $1 and waits for completition in $2 seconds
function RunRemoteCommand
{
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
if [ $dryrun -ne 0 ]
then
Log "Dryrun: Local command [$1] not run."
return 1
fi
Log "Running command [$1] on remote host."
eval "$SSH_CMD \"$1\" > $RUN_DIR/osync_run_remote_$SCRIPT_PID 2>&1 &"
child_pid=$!
WaitForTaskCompletion $child_pid 0 $2
retval=$?
if [ $retval -eq 0 ]
then
Log "Command succeded."
else
LogError "Command failed."
fi
if [ -f $RUN_DIR/osync_run_remote_$SCRIPT_PID ] && ([ $verbose -eq 1 ] || [ $retval -ne 0 ])
then
Log "Command output:\n$(cat $RUN_DIR/osync_run_remote_$SCRIPT_PID)"
fi
if [ "$STOP_ON_CMD_ERROR" == "yes" ] && [ $retval -ne 0 ]
then
exit 1
fi
}
function RunBeforeHook
{
if [ "$LOCAL_RUN_BEFORE_CMD" != "" ]
then
RunLocalCommand "$LOCAL_RUN_BEFORE_CMD" $MAX_EXEC_TIME_PER_CMD_BEFORE
fi
if [ "$REMOTE_RUN_BEFORE_CMD" != "" ]
then
RunRemoteCommand "$REMOTE_RUN_BEFORE_CMD" $MAX_EXEC_TIME_PER_CMD_BEFORE
fi
}
function RunAfterHook
{
if [ "$LOCAL_RUN_AFTER_CMD" != "" ]
then
RunLocalCommand "$LOCAL_RUN_AFTER_CMD" $MAX_EXEC_TIME_PER_CMD_AFTER
fi
if [ "$REMOTE_RUN_AFTER_CMD" != "" ]
then
RunRemoteCommand "$REMOTE_RUN_AFTER_CMD" $MAX_EXEC_TIME_PER_CMD_AFTER
fi
}
function CheckConnectivityRemoteHost
{
if [ "$REMOTE_HOST_PING" != "no" ] && [ "$REMOTE_SYNC" != "no" ]
then
$PING_CMD $REMOTE_HOST > /dev/null 2>&1
if [ $? != 0 ]
then
LogError "Cannot ping $REMOTE_HOST"
exit 1
fi
fi
}
function CheckConnectivity3rdPartyHosts
{
if [ "$REMOTE_3RD_PARTY_HOSTS" != "" ]
then
remote_3rd_party_success=0
OLD_IFS=$IFS
IFS=$' \t\n'
for i in $REMOTE_3RD_PARTY_HOSTS
do
$PING_CMD $i > /dev/null 2>&1
if [ $? != 0 ]
then
Log "Cannot ping 3rd party host $i"
else
remote_3rd_party_success=1
fi
done
IFS=$OLD_IFS
if [ $remote_3rd_party_success -ne 1 ]
then
LogError "No remote 3rd party host responded to ping. No internet ?"
exit 1
fi
fi
}
function CreateOsyncDirs
{
if ! [ -d "$MASTER_STATE_DIR" ]
then
mkdir -p "$MASTER_STATE_DIR"
if [ $? != 0 ]
then
LogError "Cannot create master replica state dir [$MASTER_STATE_DIR]."
exit 1
fi
fi
if [ "$REMOTE_SYNC" == "yes" ]
then
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
eval "$SSH_CMD \"if ! [ -d \\\"$SLAVE_STATE_DIR\\\" ]; then $COMMAND_SUDO mkdir -p \\\"$SLAVE_STATE_DIR\\\"; fi\"" &
child_pid=$!
WaitForTaskCompletion $child_pid 0 1800
else
if ! [ -d "$SLAVE_STATE_DIR" ]; then mkdir -p "$SLAVE_STATE_DIR"; fi
fi
if [ $? != 0 ]
then
LogError "Cannot create slave replica state dir [$SLAVE_STATE_DIR]."
exit 1
fi
}
function CheckMasterSlaveDirs
{
if ! [ -d "$MASTER_SYNC_DIR" ]
then
if [ "$CREATE_DIRS" == "yes" ]
then
mkdir -p "$MASTER_SYNC_DIR"
if [ $? != 0 ]
then
LogError "Cannot create master directory [$MASTER_SYNC_DIR]."
exit 1
fi
else
LogError "Master directory [$MASTER_SYNC_DIR] does not exist."
exit 1
fi
fi
if [ "$REMOTE_SYNC" == "yes" ]
then
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
if [ "$CREATE_DIRS" == "yes" ]
then
eval "$SSH_CMD \"if ! [ -d \\\"$SLAVE_SYNC_DIR\\\" ]; then $COMMAND_SUDO mkdir -p \\\"$SLAVE_SYNC_DIR\\\"; fi"\" &
child_pid=$!
WaitForTaskCompletion $child_pid 0 1800
if [ $? != 0 ]
then
LogError "Cannot create slave directory [$SLAVE_SYNC_DIR]."
exit 1
fi
else
eval "$SSH_CMD \"if ! [ -d \\\"$SLAVE_SYNC_DIR\\\" ]; then exit 1; fi"\" &
child_pid=$!
WaitForTaskCompletion $child_pid 0 1800
res=$?
if [ $res != 0 ]
then
LogError "Slave directory [$SLAVE_SYNC_DIR] does not exist."
exit 1
fi
fi
else
if [ ! -d "$SLAVE_SYNC_DIR" ]
then
if [ "$CREATE_DIRS" == "yes" ]
then
mkdir -p "$SLAVE_SYNC_DIR"
if [ $? != 0 ]
then
LogError "Cannot create slave directory [$SLAVE_SYNC_DIR]."
exit 1
else
Log "Created slave directory [$SLAVE_SYNC_DIR]."
fi
else
LogError "Slave directory [$SLAVE_SYNC_DIR] does not exist."
exit 1
fi
fi
fi
}
function CheckMinimumSpace
{
Log "Checking minimum disk space on master and slave."
MASTER_SPACE=$(df -P "$MASTER_SYNC_DIR" | tail -1 | awk '{print $4}')
if [ $MASTER_SPACE -lt $MINIMUM_SPACE ]
then
LogError "There is not enough free space on master [$MASTER_SPACE KB]."
fi
if [ "$REMOTE_SYNC" == "yes" ]
then
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
eval "$SSH_CMD \"$COMMAND_SUDO df -P \\\"$SLAVE_SYNC_DIR\\\"\"" > $RUN_DIR/osync_slave_space_$SCRIPT_PID &
child_pid=$!
WaitForTaskCompletion $child_pid 0 1800
SLAVE_SPACE=$(cat $RUN_DIR/osync_slave_space_$SCRIPT_PID | tail -1 | awk '{print $4}')
else
SLAVE_SPACE=$(df -P "$SLAVE_SYNC_DIR" | tail -1 | awk '{print $4}')
fi
if [ $SLAVE_SPACE -lt $MINIMUM_SPACE ]
then
LogError "There is not enough free space on slave [$SLAVE_SPACE KB]."
fi
}
function RsyncExcludePattern
{
OLD_IFS=$IFS
IFS=$PATH_SEPARATOR_CHAR
for excludedir in $RSYNC_EXCLUDE_PATTERN
do
if [ "$RSYNC_EXCLUDE" == "" ]
then
RSYNC_EXCLUDE="--exclude=\"$excludedir\""
else
RSYNC_EXCLUDE="$RSYNC_EXCLUDE --exclude=\"$excludedir\""
fi
done
IFS=$OLD_IFS
}
function RsyncExcludeFrom
{
if [ ! $RSYNC_EXCLUDE_FROM == "" ] && [ -e $RSYNC_EXCLUDE_FROM ]
then
## Check if the exclude list has a full path, and if not, add the config file path if there is one
if [ "$(basename $RSYNC_EXCLUDE_FROM)" == "$RSYNC_EXCLUDE_FROM" ]
then
$RSYNC_EXCLUDE_FROM=$(dirname $cfgfile)/$RSYNC_EXCLUDE_FROM
fi
RSYNC_EXCLUDE="$RSYNC_EXCLUDE --exclude-from=\"$RSYNC_EXCLUDE_FROM\""
fi
}
function WriteLockFiles
{
echo $SCRIPT_PID > "$MASTER_LOCK"
if [ $? != 0 ]
then
LogError "Could not set lock on master replica."
exit 1
else
Log "Locked master replica."
fi
if [ "$REMOTE_SYNC" == "yes" ]
then
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
eval "$SSH_CMD \"echo $SCRIPT_PID@$SYNC_ID | $COMMAND_SUDO tee \\\"$SLAVE_LOCK\\\" > /dev/null \"" &
child_pid=$!
WaitForTaskCompletion $child_pid 0 1800
if [ $? != 0 ]
then
LogError "Could not set lock on remote slave replica."
exit 1
else
Log "Locked remote slave replica."
fi
else
echo "$SCRIPT_PID@$SYNC_ID" > "$SLAVE_LOCK"
if [ $? != 0 ]
then
LogError "Couuld not set lock on local slave replica."
exit 1
else
Log "Locked local slave replica."
fi
fi
}
function LockDirectories
{
if [ $nolocks -eq 1 ]
then
return 0
fi
if [ $force_unlock -eq 1 ]
then
WriteLockFiles
if [ $? != 0 ]
then
exit 1
fi
fi
Log "Checking for replica locks."
if [ -f "$MASTER_LOCK" ]
then
master_lock_pid=$(cat $MASTER_LOCK)
LogDebug "Master lock pid present: $master_lock_pid"
ps -p$master_lock_pid > /dev/null 2>&1
if [ $? != 0 ]
then
Log "There is a dead osync lock on master. Instance $master_lock_pid no longer running. Resuming."
else
LogError "There is already a local instance of osync that locks master replica. Cannot start. If your are sure this is an error, plaese kill instance $master_lock_pid of osync."
exit 1
fi
fi
if [ "$REMOTE_SYNC" == "yes" ]
then
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
eval "$SSH_CMD \"if [ -f \\\"$SLAVE_LOCK\\\" ]; then cat \\\"$SLAVE_LOCK\\\"; fi\" > $RUN_DIR/osync_remote_slave_lock_$SCRIPT_PID" &
child_pid=$!
WaitForTaskCompletion $child_pid 0 1800
if [ -f $RUN_DIR/osync_remote_slave_lock_$SCRIPT_PID ]
then
slave_lock_pid=$(cat $RUN_DIR/osync_remote_slave_lock_$SCRIPT_PID | cut -d'@' -f1)
slave_lock_id=$(cat $RUN_DIR/osync_remote_slave_lock_$SCRIPT_PID | cut -d'@' -f2)
fi
else
if [ -f "$SLAVE_LOCK" ]
then
slave_lock_pid=$(cat "$SLAVE_LOCK" | cut -d'@' -f1)
slave_lock_id=$(cat "$SLAVE_LOCK" | cut -d'@' -f2)
fi
fi
if [ "$slave_lock_pid" != "" ] && [ "$slave_lock_id" != "" ]
then
LogDebug "Slave lock pid: $slave_lock_pid"
LogDebug "Slave lock id: $slave_lock_pid"
ps -p$slave_lock_pid > /dev/null
if [ $? != 0 ]
then
if [ "$slave_lock_id" == "$SYNC_ID" ]
then
Log "There is a dead osync lock on slave replica that corresponds to this master sync-id. Instance $slave_lock_pid no longer running. Resuming."
else
if [ "$FORCE_STRANGER_LOCK_RESUME" == "yes" ]
then
LogError "WARNING: There is a dead osync lock on slave replica that does not correspond to this master sync-id. Forcing resume."
else
LogError "There is a dead osync lock on slave replica that does not correspond to this master sync-id. Will not resume."
exit 1
fi
fi
else
LogError "There is already a local instance of osync that locks slave replica. Cannot start. If you are sure this is an error, please kill instance $slave_lock_pid of osync."
exit 1
fi
fi
WriteLockFiles
}
function UnlockDirectories
{
if [ $nolocks -eq 1 ]
then
return 0
fi
if [ "$REMOTE_SYNC" == "yes" ]
then
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
eval "$SSH_CMD \"if [ -f \\\"$SLAVE_LOCK\\\" ]; then $COMMAND_SUDO rm \\\"$SLAVE_LOCK\\\"; fi\"" &
child_pid=$!
WaitForTaskCompletion $child_pid 0 1800
else
if [ -f "$SLAVE_LOCK" ];then rm "$SLAVE_LOCK"; fi
fi
if [ $? != 0 ]
then
LogError "Could not unlock slave replica."
else
Log "Removed slave replica lock."
fi
if [ -f "$MASTER_LOCK" ]
then
rm "$MASTER_LOCK"
if [ $? != 0 ]
then
LogError "Could not unlock master replica."
else
Log "Removed master replica lock."
fi
fi
}
###### Sync core functions
## Rsync does not like spaces in directory names, considering it as two different directories. Handling this schema by escaping space.
## It seems this only happens when trying to execute an rsync command through eval $rsync_cmd on a remote host.
## So i'm using unescaped $MASTER_SYNC_DIR for local rsync calls and escaped $ESC_MASTER_SYNC_DIR for remote rsync calls like user@host:$ESC_MASTER_SYNC_DIR
## The same applies for slave sync dir..............................................T.H.I.S..I.S..A..P.R.O.G.R.A.M.M.I.N.G..N.I.G.H.T.M.A.R.E
## tree_list(replica_path, replica type, tree_filename) Creates a list of files in replica_path for replica type (master/slave) in filename $3
function tree_list
{
Log "Creating $2 replica file list [$1]."
if [ "$REMOTE_SYNC" == "yes" ] && [ "$2" == "slave" ]
then
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
ESC=$(EscapeSpaces "$1")
rsync_cmd="$(type -p $RSYNC_EXECUTABLE) --rsync-path=\"$RSYNC_PATH\" $RSYNC_ARGS -8 --exclude \"$OSYNC_DIR\" $RSYNC_EXCLUDE -e \"$RSYNC_SSH_CMD\" --list-only $REMOTE_USER@$REMOTE_HOST:\"$ESC/\" | grep \"^-\|^d\" | awk '{\$1=\$2=\$3=\$4=\"\" ;print}' | awk '{\$1=\$1 ;print}' | (grep -v \"^\.$\" || :) | sort > \"$RUN_DIR/osync_$2_$SCRIPT_PID\" &"
else
rsync_cmd="$(type -p $RSYNC_EXECUTABLE) --rsync-path=\"$RSYNC_PATH\" $RSYNC_ARGS -8 --exclude \"$OSYNC_DIR\" $RSYNC_EXCLUDE --list-only \"$1/\" | grep \"^-\|^d\" | awk '{\$1=\$2=\$3=\$4=\"\" ;print}' | awk '{\$1=\$1 ;print}' | (grep -v \"^\.$\" || :) | sort > \"$RUN_DIR/osync_$2_$SCRIPT_PID\" &"
fi
LogDebug "RSYNC_CMD: $rsync_cmd"
## Redirect commands stderr here to get rsync stderr output in logfile
eval $rsync_cmd 2>> "$LOG_FILE"
child_pid=$!
WaitForCompletion $child_pid $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME
retval=$?
## Retval 24 = some files vanished while creating list
if ([ $retval == 0 ] || [ $retval == 24 ]) && [ -f $RUN_DIR/osync_$2_$SCRIPT_PID ]
then
mv $RUN_DIR/osync_$2_$SCRIPT_PID "$MASTER_STATE_DIR/$2$3"
return $?
else
LogError "Cannot create replica file list."
exit $retval
fi
}
# delete_list(replica, tree-file-after, tree-file-current, deleted-list-file, deleted-failed-list-file): Creates a list of files vanished from last run on replica $1 (master/slave)
function delete_list
{
Log "Creating $1 replica deleted file list."
if [ -f "$MASTER_STATE_DIR/$1$TREE_AFTER_FILENAME_NO_SUFFIX" ]
then
## Same functionnality, comm is much faster than grep but is not available on every platform
if type -p comm > /dev/null 2>&1
then
cmd="comm -23 \"$MASTER_STATE_DIR/$1$TREE_AFTER_FILENAME_NO_SUFFIX\" \"$MASTER_STATE_DIR/$1$3\" > \"$MASTER_STATE_DIR/$1$4\""
else
## The || : forces the command to have a good result
cmd="(grep -F -x -v -f \"$MASTER_STATE_DIR/$1$3\" \"$MASTER_STATE_DIR/$1$TREE_AFTER_FILENAME_NO_SUFFIX\" || :) > \"$MASTER_STATE_DIR/$1$4\""
fi
LogDebug "CMD: $cmd"
eval $cmd
retval=$?
# Add delete failed file list to current delete list and then empty it
if [ -f "$MASTER_STATE_DIR/$1$5" ]
then
cat "$MASTER_STATE_DIR/$1$5" >> "$MASTER_STATE_DIR/$1$4"
rm -f "$MASTER_STATE_DIR/$1$5"
fi
return $retval
else
touch "$MASTER_STATE_DIR/$1$4"
return $retval
fi
}
# sync_update(source replica, destination replica, delete_list_filename)
function sync_update
{
Log "Updating $2 replica."
if [ "$1" == "master" ]
then
SOURCE_DIR="$MASTER_SYNC_DIR"
ESC_SOURCE_DIR=$(EscapeSpaces "$MASTER_SYNC_DIR")