forked from jplew/SyncDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncdb
executable file
·1452 lines (1138 loc) · 46.7 KB
/
syncdb
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
#!/bin/bash -u
# ***********************************************
#
# syncDB v0.3.1
# by JP Lew (jplew.com)
#
# SyncDB is bash deploy script meant to take the tedium out of synchronizing
# the local and remote versions of a Wordpress site. It allows developers
# working in a local environment (eg. MAMP) to rapidly "push" or "pull" changes
# to or from the production server with a single command.
#
# ***********************************************
config_file=syncdb-config
temp_file=syncdb-temp
if [[ -f $config_file ]]
then
#source the config file
. $config_file
else
echo "${red}$msgError$reset: Could not locate $config_file."
exit 1
fi
if [[ -f $temp_file ]]
then
#source the temp file. The temp file is used once per "session". It serves to avoid prompting the user for questions they've already answered. It is created during the first prompt, then deleted after the search and replace.
. $temp_file
fi
# ========================================
# OPTIONAL VARIABLES (you almost certainly won't have to touch these)
# ========================================
# This is the arbitrary name and path to your backups folder. Located in your site's root directory.
l_bak_dir=.bak
r_bak_dir=.bak
# Arbitrary name for the dump files which will be transferred across servers.
l_bak_name2=latest-local.mssql
r_bak_name2=latest-remote.mssql
# $l_site_dir is the absolute path to your local site. This is automatically
# deduced based on the directory into which you moved SyncDB.
l_site_dir=${PWD}
# $l_web_dir is the name of your site's root directory (same as $l_site_dir,
# but with the preceding directory path stripped off).
l_web_dir=${PWD##*/}
# Path expansions.
l_bak_path2=$l_site_dir/$l_bak_dir/$l_bak_name2
l_upload_path=$l_site_dir/$l_upload_dir
r_upload_path=$r_web_dir/$r_upload_dir
r_site_dir=$HOME/$r_web_dir
# The --port flag has to be uppercase for `scp`, lowercase for `ssh`. sigh.
ssh_port_define="-p $r_port "
scp_port_define="-P $r_port "
ssh_port=${r_port:+$ssh_port_define}
scp_port=${r_port:+$scp_port_define}
# ========================================
# GLOBAL CONSTANTS (no need to customize)
# ========================================
# pretty colours
if [ "$TERM" != "dumb" ]; then
red=$(tput setaf 1 || tput AF 1)
green=$(tput setaf 2 || tput AF 2)
yellow=$(tput setaf 3 || tput AF 3)
blue=$(tput setaf 4 || tput AF 4)
magenta=$(tput setaf 5 || tput AF 5)
cyan=$(tput setaf 6 || tput AF 6)
reset=$(tput sgr0)
bold=$(tput bold)
else
red=
green=
yellow=
blue=
magenta=
cyan=
reset=
bold=
fi
msgSuccess='Success:'
msgError='Error:'
# Date placeholders (for naming the SQL dump files)
today=$(date +"%y%m%d")
now=$(date +"%y%m%d-%H%M")
# Search-Replace-DB
search_script_name=srdb.class.php
cli_script_name=srdb.cli.php
srdb_path=https://raw.githubusercontent.com/interconnectit/Search-Replace-DB/master/
search_script_path=$srdb_path$search_script_name
cli_script_path=$srdb_path$cli_script_name
syncdb_path=https://raw.githubusercontent.com/jplew/SyncDB/master/syncdb
# initialize variable. Used because we need to test the SSH connection only once.
checked=
# ====================================
# FUNCTIONS
# ====================================
# ---------------------------------------------------------
# help ()
# Print list of valid SyncDB commands.
# ---------------------------------------------------------
help () {
cat <<EOF
${blue}SyncDB List of Commands$reset
• auto_update
• backup_local_db
• backup_remote_db (remote only)
• do_remote_backup
• do_remote_replace
• do_search_replace_remote
• download_remote_db
• help
• pull
• push
• replace_local_db
• replace_remote_db (remote only)
• rsync_pull
• rsync_push
• search_replace_local
• search_replace_remote (remote only)
• test_ssh
• upload_local_db
• upload_script
EOF
exit 0
}
# ---------------------------------------------------------
# auto_update ()
# Check if the github hosted version of SyncDB is newer (different) from this
# version. If so, it will download the newer one and ask the user to relaunch.
# This function is enabled by default, but can be disabled by changing
# auto_update=false in syncdb-config.
# ---------------------------------------------------------
auto_update () {
if command -v curl >/dev/null 2>&1
then
curl_exists=true
else
if command -v wget >/dev/null 2>&1
then
wget_exists=true
else
echo >&2 "${red}$msgError$reset Could not run auto-update, your system has neither curl nor wget installed."
fi
fi
echo "${blue}Checking for updates to SyncDB...$reset"
#run a diff command to test the difference between the github syncdb versus
#the local one. We use basename rather than hard-coding the filename,
#syncdb, in case the user is using a different name, eg. syncdb-live,
#mysyncdb, etc...
needs_updating=$(diff <(curl -s $syncdb_path) ./$(basename $0))
if [[ "$needs_updating" ]]
then
echo "There is a newer version of SyncDB available. Would you like to update?"
OPTIONS="Yes No"
select opt in $OPTIONS
do
echo
case $opt in
"Yes")
echo "Downloading newer verson of SyncDB..."
#backup the old syncdb, just in case
cp $(basename $0) $(basename $0).bak
if [[ ${curl_exists-} ]]
then
#-s is silent mode, -o specifies a filename of your choosing
curl -s $syncdb_path -o $(basename $0)
elif [[ ${wget_exists-} ]]
then
#-q is quiet mode, -O specifies the filename
wget -q --no-check-certificate -O $(basename $0) $syncdb_path
fi
echo "${green}${msgSuccess}$reset Update complete. Please relaunch SyncDB."
echo
break
;;
"No")
echo "You chose not to update. To disable auto-updating, set auto_update=false in syncdb-config."
echo
break
;;
*) echo "Invalid option, please type 1 or 2.";;
esac
done
else
echo "Your version of SyncDB is up-to-date."
fi
cat <<EOT > $temp_file
auto_update_checked=true
EOT
}
# ---------------------------------------------------------
# test_ssh ()
# Check if the SSH connection is working.
# ---------------------------------------------------------
test_ssh () {
echo "${blue}Checking if SSH connection is working...$reset"
echo
echo "ssh -q $ssh_port$r_user@$r_host exit"
#first test method
ssh -q $ssh_port$r_user@$r_host exit
if [ $? == 0 ] # check if the SSH connection is working
then
echo
echo "${green}${msgSuccess}$reset SSH connection is working."
echo
else
echo "Test failed. Trying again..."
echo "ssh -q -o BatchMode=yes -o ConnectTimeout=5 ${ssh_port}$r_user@$r_host echo ok 2>&1"
return_code=$(ssh -q -o BatchMode=yes -o ConnectTimeout=5 ${ssh_port}$r_user@$r_host echo ok 2>&1)
#echo return code is $return_code
#second test method, in case first one fails. For some reason a
#functioning Arvixe connection returns an error code of 1 for the above
#test, even if it's working
if [[ $return_code == 'ok' ]] # check if the SSH connection is working
then
echo
echo "${green}${msgSuccess}$reset SSH connection is working."
else
echo "$red$msgError$reset could not connect to host. Confirm that the SSH connection, $yellow$r_user$reset@$yellow$r_host$reset, is working."
exit 1
fi
fi
echo
}
# ---------------------------------------------------------
# get_local_db_details ()
# Extract the local database details from wp-config.php
# ---------------------------------------------------------
get_local_db_details () {
echo
#echo "${blue}Fetching local database login details...$reset"
# Change directory to site root (where local-config.php and wp-config.php are located)
if [ ! -d $l_site_dir ] ; then
echo "${red}$msgError$reset: Directory $l_site_dir does not exist"
echo
exit
else
cd $l_site_dir
fi
# If local-config.php is not found (if you're using the default Wordpress
# architecture rather than using WP-Skeleton like I recommend, then search
# for the local DB info in wp-config.php
if [[ -f local-config.php ]]
then
local_config_file=local-config.php
elif [[ -f wp-config.php ]]
then
local_config_file=wp-config.php
else
echo "${red}$msgError$reset: Could not locate local-config.php or wp-config.php"
exit 1
fi
#This tells us how many times DB_USER has been set in the config file.
number_of_environments=$(grep -c "'DB_USER', '[^']*'.*" $local_config_file)
#grep returns "filename.php:3" if there's 3 hits. We want to strip away
#everything up to the colon and just save the number
number_of_environments=$(echo $number_of_environments | sed -e 's/.*://')
#This detects if there's anything weird going on in the config file. If there
#are more than one instances of DB_USER, that's weird.
if [[ $number_of_environments < 2 ]]
then
return_local_standard
elif [[ ${local_chosen-} != "true" ]]
then
return_local_nonstandard
fi
#Local paths
l_bak_name1=$now-$l_db_name.mssql
l_bak_path1=$l_site_dir/$l_bak_dir/$l_bak_name1
}
# ---------------------------------------------------------
# return_remote_nonstandard ()
# Some wp-config files have multiple sets of configuration variables, one
# for each environment (local, dev, production/live etc...).
# This function collects all the login info it can find, organizes it into
# sets, auto-detects which one is the local set, and if necessary asks the user
# which remote server they want to sync with.
# ---------------------------------------------------------
return_remote_nonstandard () {
echo "SyncDB has detected multiple remote environment configurations in your config file."
# If local-config.php is not found (if you're using the default Wordpress
# architecture rather than using WP-Skeleton like I recommend, then search
# for the local DB info in wp-config.php
if [[ -f local-config.php ]]
then
local_config_file=local-config.php
elif [[ -f wp-config.php ]]
then
local_config_file=wp-config.php
else
echo "${red}$msgError$reset: Could not locate local-config.php or wp-config.php"
exit 1
fi
#grep the config file for all relevants hits, and saves them to their
#respective category. All hits are delimited by spaces.
all_db_name=$(sed -n "s/.*DB_NAME', '\([^']*\)'.*/\1/p" wp-config.php)
all_db_user=$(sed -n "s/.*DB_USER', '\([^']*\)'.*/\1/p" wp-config.php)
all_db_pass=$(sed -n "s/.*DB_PASSWORD', '\([^']*\)'.*/\1/p" wp-config.php)
all_db_host=$(sed -n "s/.*DB_HOST', '\([^']*\)'.*/\1/p" wp-config.php)
#This splits the previous values into arrays
name_array=(${all_db_name// / })
user_array=(${all_db_user// / })
pass_array=(${all_db_pass// / })
host_array=(${all_db_host// / })
#Checking if DB_HOST was empty for any environments. This means it
#wasn't hard-coded, and is using _ENV{DATABASE-SERVER} instead. Rather than
#leave it blank, we want to assign it the value "unset"
for (( i = 0 ; i < $number_of_environments ; i++ ))
do
#when -u is set for bash, one valid way to test the existence of a
#variable, without getting an "unbound variable" error, is to set #default value to empty like this ${variable-e
if [ -z "${host_array[$i]-}" ]
then
db_host=${DATABASE_SERVER:=unset}
if [[ $db_host ]]
then
host_array[$i]=$db_host
fi
fi
done
#env_array will be the final list of remote environments
declare -a env_array=()
#for each environment detected, let's test if it's local by checking if
#anything is set to "root". We're assuming no-one's using "root" as their remote
#login or password.
for (( i = 0 ; i < $number_of_environments ; i++ ))
do
#add the current environment to the array
env_array[$i]=$i
#this will create array set_0, set_1, set_2 etc... which will reorganize all the grepped values into their respective environment
eval "declare -a "set_${i}"=( ${name_array[$i]} ${user_array[$i]} ${pass_array[$i]} ${host_array[$i]} )"
#because we're using arrays we have to use some indirect referencing trickery to iterate properly
_set="set_$i"[@]
set=( "${!_set}" )
#k is a separate counter to iterate through the four environment variables
k=0
for j in "${set[@]}"
do
#out of all the environments, let's set the default to 0
local_env=0
if [ $j == "root" ]
then
#hopefully only one of the environments is using root as a login
local_env=$i
fi
#remove the local environment from the final array, so the user doesn't
#have to consider it.
unset env_array[$local_env]
#don't print the local details, and if we're remote, we only need to
#output these once, when the correct remote settings are selected
if [[ "$i" -ne "$local_env" ]]
then
#Display the title, "Environment #x", only once, on the first
#pass through the current environment's config values
[[ $k == 0 ]] && echo " ENVIRONMENT #$i"
header=
case $k in
0 )
header="DB_NAME" ;;
1 )
header="DB_USER" ;;
2 )
header="DB_PASS" ;;
3 )
header="DB_HOST" ;;
esac
echo " $header: $j"
((k++))
fi
done
echo
done
echo
#Now that we've narrowed down the possibilities of what might be a remote
#server, prompt the user to pick which one to synchronize
echo "Synchronize with which remote server?"
select m in "${env_array[@]}"
do
echo
echo "You chose Environment #$m as your remote server."
echo
remote_env=$m
#SyncDB will only use one set of remote environment variables, so set them
#here based on the user's choice
remote_env_ref_0=set_$remote_env[0]
remote_env_ref_1=set_$remote_env[1]
remote_env_ref_2=set_$remote_env[2]
remote_env_ref_3=set_$remote_env[3]
r_db_name=${!remote_env_ref_0}
r_db_user=${!remote_env_ref_1}
r_db_pass=${!remote_env_ref_2}
r_db_host=${!remote_env_ref_3:-$db_host}
echo "============================================================"
echo " REMOTE DATABASE DETAILS"
echo "============================================================"
echo
echo "remote DB_NAME is: $r_db_name"
echo "remote DB_USER is: $r_db_user"
echo "remote DB_PASSWORD is: $r_db_pass"
echo "remote DB_HOST is: $r_db_host"
echo
# a flag which indicates whether the user has selected a remote environment already. This is so it doesn't pester the user repeatedly with this request.
remote_chosen=true
cat <<EOT >> $temp_file
r_db_name=${!remote_env_ref_0}
r_db_user=${!remote_env_ref_1}
r_db_pass=${!remote_env_ref_2}
r_db_host=${!remote_env_ref_3:-$db_host}
remote_chosen=true
EOT
break
done
echo
}
# ---------------------------------------------------------
# return_local_nonstandard ()
# If multiple environments are specified in the same wp-config file, this
# function extracts the set of parameters it feels correspond to the local
# environment.
# ---------------------------------------------------------
return_local_nonstandard () {
#grep the config file for all relevants hits, and saves them to their
#respective category. All hits are delimited by spaces.
all_db_name=$(sed -n "s/.*DB_NAME', '\([^']*\)'.*/\1/p" wp-config.php)
all_db_user=$(sed -n "s/.*DB_USER', '\([^']*\)'.*/\1/p" wp-config.php)
all_db_pass=$(sed -n "s/.*DB_PASSWORD', '\([^']*\)'.*/\1/p" wp-config.php)
all_db_host=$(sed -n "s/.*DB_HOST', '\([^']*\)'.*/\1/p" wp-config.php)
#This splits the previous values into arrays
name_array=(${all_db_name// / })
user_array=(${all_db_user// / })
pass_array=(${all_db_pass// / })
host_array=(${all_db_host// / })
#Checking if DB_HOST was empty for any environments. This means it
#wasn't hard-coded, and is using _ENV{DATABASE-SERVER} instead. Rather than
#leave it blank, we want to assign it the value "unset"
for (( i = 0 ; i < $number_of_environments ; i++ ))
do
#when -u is set for bash, one valid way to test the existence of a
#variable, without getting an "unbound variable" error, is to set
#default value to empty like this ${variable-}
if [ -z "${host_array[$i]-}" ]
then
db_host=${DATABASE_SERVER:=unset}
[[ $db_host ]] && host_array[$i]=$db_host
fi
done
#env_array will be the final list of remote environments
declare -a env_array=()
#for each environment detected, let's test if it's local by checking if
#anything is set to "root". We're assuming no-one's using "root" as their remote
#login or password.
for (( i = 0 ; i < $number_of_environments ; i++ ))
do
#add the current environment to the array
env_array[$i]=$i
#this will create array set_0, set_1, set_2 etc... which will reorganize all the grepped values into their respective environment
eval "declare -a "set_${i}"=( ${name_array[$i]} ${user_array[$i]} ${pass_array[$i]} ${host_array[$i]:-} )"
#because we're using arrays we have to use some indirect referencing trickery to iterate properly
_set="set_$i"[@]
set=( "${!_set}" )
#k is a separate counter to iterate through the four environment variables
k=0
for j in "${set[@]}"
do
#out of all the environments, let's set the default to 0
local_env=0
if [ $j == "root" ]
then
#hopefully only one of the environments is using root as a login
local_env=$i
fi
#remove the local environment from the final array, so the user doesn't
#have to consider it.
unset env_array[$local_env]
done
done
#Now that we've figured out which environment is the local one, let's set all
#the variables to use for the local mysql operations
local_env_ref_0=set_$local_env[0]
local_env_ref_1=set_$local_env[1]
local_env_ref_2=set_$local_env[2]
local_env_ref_3=set_$local_env[3]
l_db_name=${!local_env_ref_0}
l_db_user=${!local_env_ref_1}
l_db_pass=${!local_env_ref_2}
l_db_host=${!local_env_ref_3}
local_chosen=true
echo "============================================================"
echo " LOCAL DATABASE DETAILS"
echo "============================================================"
echo
echo "local DB_NAME is: $l_db_name"
echo "local DB_USER is: $l_db_user"
echo "local DB_PASSWORD is: $l_db_pass"
echo "local DB_HOST is: $l_db_host"
echo
}
# ---------------------------------------------------------
# return_local_standard ()
# A standard wp-config file has one set of environment variables declared. A
# non-standard has more than one. This function fetches the local database
# login details from a normal config file.
# ---------------------------------------------------------
return_local_standard () {
#grep the config file for the MySQL login details
l_db_name=$(sed -n "s/.*DB_NAME', '\([^']*\)'.*/\1/p" $local_config_file)
l_db_user=$(sed -n "s/.*DB_USER', '\([^']*\)'.*/\1/p" $local_config_file)
l_db_pass=$(sed -n "s/.*DB_PASSWORD', '\([^']*\)'.*/\1/p" $local_config_file)
l_db_host=$(sed -n "s/.*DB_HOST', '\([^']*\)'.*/\1/p" $local_config_file)
}
# ---------------------------------------------------------
# return_remote_standard ()
# Get the remote database login details. Used if the config file has only one
# environment specified.
# ---------------------------------------------------------
return_remote_standard () {
#grep the config file for the MySQL login details
r_db_name=$(sed -n "s/.*DB_NAME', '\([^']*\)'.*/\1/p" wp-config.php)
r_db_user=$(sed -n "s/.*DB_USER', '\([^']*\)'.*/\1/p" wp-config.php)
r_db_pass=$(sed -n "s/.*DB_PASSWORD', '\([^']*\)'.*/\1/p" wp-config.php)
r_db_host=$(sed -n "s/.*DB_HOST', '\([^']*\)'.*/\1/p" wp-config.php)
}
# ---------------------------------------------------------
# get_remote_db_details ()
# Extract the remote database details from wp-config.php
# ---------------------------------------------------------
get_remote_db_details () {
#echo "${blue}Fetching remote database login details...$reset"
# Change directory to site root (where local-config.php and wp-config.php are located)
if [ ! -d $r_site_dir ] ; then
echo "${red}$msgError$reset: Directory $r_site_dir does not exist"
echo
exit 1
else
cd $r_site_dir
fi
if [[ ! -f wp-config.php ]]
then
echo "${red}$msgError$reset: Could not locate wp-config.php"
exit 1
fi
#This tells us how many times DB_USER has been set in the config file.
number_of_environments=$(grep -c "'DB_USER', '[^']*'.*" wp-config.php)
#grep returns "filename.php:4" if there's 4 hits. We want to strip away
#everything up to the colon and just save the number
number_of_environments=$(echo $number_of_environments | sed 's/.*://')
#echo no. of envs = $number_of_environments
echo
#This detects if there's anything weird going on in the config file. If there
#are more than one instances of DB_USER, that's weird.
if [[ $number_of_environments > 1 ]]
then
return_remote_nonstandard
else
return_remote_standard
fi
}
# ---------------------------------------------------------
# backup_remote_db ()
# Dump the remote database and back it up
# ---------------------------------------------------------
backup_remote_db () {
check_if_chosen
echo "${blue}Backing up remote database...$reset"
cd $HOME/$r_web_dir
if [ ! -d $r_bak_dir ]
then
mkdir $r_bak_dir
fi
cd $r_bak_dir
r_bak_name1=$now-$r_db_name.mssql
echo "mysqldump -u$r_db_user -p$r_db_pass -h $r_db_host $r_db_name | bzip2 -c > ${r_bak_name1}.bz2"
mysqldump -u$r_db_user -p$r_db_pass -h $r_db_host $r_db_name | bzip2 -c > ${r_bak_name1}.bz2
if [[ $? -eq 1 ]]
then
echo ${red}$msgError$reset Could not backup remote database.$reset
exit 1
else
echo
echo "${green}$msgSuccess$reset Remote database dumped to: $(pwd)/${r_bak_name1}.bz2 and $(pwd)/${r_bak_name2}.bz2"
echo
fi
#echo "cp ${r_bak_name1}.bz2 ${r_bak_name2}.bz2"
cp ${r_bak_name1}.bz2 ${r_bak_name2}.bz2
echo
}
# ---------------------------------------------------------
# download_remote_db ()
# Download the most recent remote database dump file via scp
# ---------------------------------------------------------
download_remote_db () {
echo "${blue}Downloading remote database...$reset"
echo
if [ ! -d $l_bak_dir ] ; then
mkdir $l_bak_dir
fi
cd $l_bak_dir
echo "scp -q $scp_port$r_user@$r_host:~/$r_web_dir/$r_bak_dir/${r_bak_name2}.bz2 ."
set -o pipefail
scp -q "$scp_port"$r_user@$r_host:~/$r_web_dir/$r_bak_dir/${r_bak_name2}.bz2 .
if [ $? == 0 -a -f ${r_bak_name2}.bz2 ]; then
echo
echo "$green$msgSuccess$reset Remote database downloaded to: $(pwd)/${r_bak_name2}.bz2"
else
echo "$red$msgError$reset Download did not work. Could not find $(pwd)/${r_bak_name2}.bz2"
exit 1
fi
}
# ---------------------------------------------------------
# backup_local_db ()
# Back up the local database.
# ---------------------------------------------------------
backup_local_db () {
get_local_db_details
echo "${blue}Backing up the local database...$reset"
cd $l_site_dir
if [ ! -d $l_bak_dir ]
then
mkdir $l_bak_dir;
fi
cd $l_bak_dir
echo "${l_mysqldump:-mysqldump} -u$l_db_user -p$l_db_pass -h $l_db_host $l_db_name | bzip2 -c > ${l_bak_name1}.bz2"
echo
set -o pipefail
"${l_mysqldump:-mysqldump}" "-u$l_db_user" "-p$l_db_pass" -h "$l_db_host" "$l_db_name" | bzip2 -c > "${l_bak_name1}.bz2"
if [[ $? -eq 0 ]]
then
echo
echo "$green$msgSuccess$reset Local database backed up to: $(pwd)/${l_bak_name1}.bz2"
else
echo
echo "$red$msgError$reset Database was not backed up. There is a problem with your local MySQL details. Exiting."
echo
exit 1
fi
#echo "cp ${l_bak_name1}.bz2 ${l_bak_name2}.bz2"
echo
cp ${l_bak_name1}.bz2 ${l_bak_name2}.bz2
echo
}
# ---------------------------------------------------------
# replace_local_db ()
# Drop and recreate the local database.
# ---------------------------------------------------------
replace_local_db () {
echo "${blue}Dropping local database...$reset"
echo
get_local_db_details
echo
"${l_mysql:-mysql}" "-u$l_db_user" "-p$l_db_pass" -h "$l_db_host" --show-warnings >/dev/null 2>&1<<EOF
show databases;
create database if not exists $l_db_name;
drop database $l_db_name;
create database $l_db_name;
EOF
mysql_check_local
echo
echo
echo "${blue}Replacing contents of local database...$reset"
echo
cd $l_site_dir/$l_bak_dir
set -o pipefail
if [ -f "${r_bak_name2}.bz2" ]
then
bunzip2 < "${r_bak_name2}.bz2" | "${l_mysql:-mysql}" "-u$l_db_user" "-p$l_db_pass" -h "$l_db_host" "$l_db_name" --show-warnings
if [[ $? -eq 0 ]]
then
echo "$green$msgSuccess$reset Local database replaced with contents from remote database."
else
echo
echo "$red$msgError$reset There was an error replacing the contents of the local database."
echo
exit 1
fi
else
echo "$red$msgError$reset Remote backup file, "${r_bak_name2}".bz2, does not exist in $(PWD)"
exit 1
fi
echo
}
# ---------------------------------------------------------
# do_check_for_dirs ()
# Login and verify that the remote paths are set correctly
# ---------------------------------------------------------
do_check_for_dirs () {
echo "Checking for existence of remote directories..."
ssh -q ${ssh_port}"$r_user"@"$r_host" "if [[ ! -d "$r_web_dir" ]]; then echo "Remote directory does not exist: $r_web_dir"; exit 1; fi; cd "$r_web_dir"; if [[ ! -d "$r_bak_dir" ]]; then mkdir "$r_bak_dir"; fi;"
checked=true
}
# ---------------------------------------------------------
# upload_script ()
# Upload a copy of sync script to FTP
# ---------------------------------------------------------
upload_script () {
echo "${blue}Uploading sync script...$reset"
cd "$l_site_dir"
# check if we've already checked the remote path structure. If not, check now
[[ "$checked" ]] || do_check_for_dirs
#NOTE: SyncDB used to check if there was any difference between the remote version of syncdb and the local one, for the sake of performance. This part has been commented out for now, because I think we need to upload the temp_file every time, at least.
# Compare the local version of SyncDB with the remote version. If there's a difference, upload and write over the remote one.
#difference=$(ssh $ssh_port$r_user@$r_host cat $r_web_dir/$r_bak_dir/$(basename $0) 2>/dev/null | diff ./$(basename $0) - )
#echo
#if [[ "$difference" ]]
#then
#echo "scp -q $scp_port$(basename $0) $config_file $r_user@$r_host:~/$r_web_dir/"
#echo
#scp -q ${scp_port}$(basename $0) $config_file $r_user@$r_host:~/$r_web_dir/
#echo
#echo "$green$msgSuccess$reset Uploaded $(basename $0) and $config_file to: $r_web_dir."
#else
#echo "SyncDB script already exists on remote server."
#fi
#we only need to upload the temp_file if it exists.
if [[ -f $temp_file ]]
then
upload_temp=$temp_file
fi
echo "scp -q $scp_port$(basename $0) $config_file ${upload_temp-} $r_user@$r_host:~/$r_web_dir/"
echo
scp -q ${scp_port}$(basename $0) $config_file ${upload_temp-} $r_user@$r_host:~/$r_web_dir/
echo
echo "$green$msgSuccess$reset Uploaded $(basename $0) and $config_file to: $r_web_dir."
echo
}
# ---------------------------------------------------------
# do_remote_backup ()
# Login to the remote server via SSH and call backup_remote_db(). Meant to be
# run locally.
# ---------------------------------------------------------
do_remote_backup () {
[[ "$checked" ]] || do_check_for_dirs
echo "ssh -qt $ssh_port$r_user@$r_host cd $r_web_dir; chmod +x $(basename $0); ./$(basename $0) backup_remote_db;"
ssh -qt $ssh_port$r_user@$r_host "cd $r_web_dir; if [ ! -f .bashrc -a -f /etc/profile ]; then . /etc/profile; fi; ./$(basename $0) backup_remote_db;"
}
# ---------------------------------------------------------
# upload_local_db ()
# Upload local scripts and database backup file to the remote server
# ---------------------------------------------------------
upload_local_db () {
echo "${blue}Uploading dump file to remote server...$reset"
echo
# check if we've already checked the remote path structure. If not, check now
[[ "$checked" ]] || do_check_for_dirs
cd $l_site_dir/$l_bak_dir
if [[ -f "${l_bak_path2}.bz2" ]]
then
echo "scp -q $scp_port${l_bak_path2}.bz2 $r_user@$r_host:~/$r_web_dir/$r_bak_dir"
scp -q ${scp_port}${l_bak_path2}.bz2 $r_user@$r_host:~/$r_web_dir/$r_bak_dir
else
echo "$red$msgError$reset Could not locate the local dump file at $(pwd)/${l_bak_name2}.bz2."
echo
exit 1
fi
echo
#test whether the file was actually uploaded or not
#echo "if ssh $ssh_port$r_user@$r_host test -e $r_web_dir/$r_bak_dir/${l_bak_name2}.bz2"
if ssh -q ${ssh_port}"$r_user"@"$r_host" test -e "$r_web_dir"/"$r_bak_dir"/${l_bak_name2}.bz2
then
echo
echo "$green$msgSuccess$reset Dump file uploaded to: $r_web_dir/$r_bak_dir/${l_bak_name2}.bz2"
else
echo
echo "$red$msgError$reset Upload failed. The file "$r_web_dir"/"$r_bak_dir"/"${l_bak_name2}".bz2 could not be found on the remote server."
exit 1
fi
echo
}
# ---------------------------------------------------------
# mysql_check_local ()
# Check if the local database is empty.
# ---------------------------------------------------------
mysql_check_local () {
echo "${blue}Checking if the local database was really emptied...$reset"
get_local_db_details
db_exists=$("${l_mysql:-mysql}" -s "-u$l_db_user" "-p$l_db_pass" -h "$l_db_host" --batch --skip-column-names -e "SHOW DATABASES LIKE '$l_db_name'" | grep $l_db_name)
if [[ ! "$db_exists" ]]
then
echo
echo "${red}$msgError${reset} "${l_mysql:-mysql}" could not connect to database $l_db_name. Check that your login details are set correctly and that the database exists. Exiting."
exit 1;
fi
rows=$("${l_mysql:-mysql}" "-u$l_db_user" "-p$l_db_pass" -h "$l_db_host" --show-warnings -Bse "select count(distinct \`table_name\`) from \`information_schema\`.\`columns\` where \`table_schema\` = '$l_db_name'")
if [ "$rows" == "0" ]; then
echo "$green$msgSuccess$reset The database $l_db_name is empty, it has $rows rows."
else
echo "$red$msgError$reset Database $l_db_name is not empty, it still has $rows rows."
exit 1
fi
}
# ---------------------------------------------------------
# mysql_check_remote ()
# Check if the remote database is empty.
# ---------------------------------------------------------
mysql_check_remote () {
echo "${blue}Checking if the remote database was really emptied...$reset"
check_if_chosen
db_exists=$(mysql "-u$r_db_user" "-p$r_db_pass" -h "$r_db_host" -BNse "SHOW DATABASES LIKE '$r_db_name'" | grep $r_db_name)