This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialsync.sh
executable file
·1694 lines (1544 loc) · 48.3 KB
/
initialsync.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
#!/bin/bash
#initialsync by [email protected]
ver="Dec 17 2013"
# http://migration.sysres.liquidweb.com/initialsync.sh
# https://github.com/defenestration/initialsync
starttime=`date +%F.%T`
scriptlogdir="/home/temp"
scriptlog="${scriptlogdir}/initialsync.${starttime}.log"
userlistfile=/root/userlist.txt
rsyncflags="-avHl"
mysqldumplog="/tmp/mysqldump.log"
remoteusersfile="/home/temp/remoteusers.txt"
sshargs="" #-t, -qt, -qtt, doesn't matter, all end up causing some sort of problem, best to leave blank and deal with the stdin: is not tty error. -q doesn't cause aproblem but doesn't make it quiet either, its mostly -t
dnr=/root/didnotrestore.txt
dnrold="${dnr}.${starttime}.bak"
#back up current dnr file if it exists, and check dnrold for users that didn't restore later on (in the menu)
if [ -s $dnr ]; then
dnrusers=`cat $dnr`
if ! [ -s $dnrold ]; then
mv $dnr $dnrold
else
> $dnr
fi
> $dnr
fi
> /tmp/remotefail.txt
> /tmp/localfail.txt
> /tmp/migration.rsync.log
> /tmp/userexists.txt
mkdir -p $scriptlogdir
echo $ver > $scriptlog
allcpusers=`/bin/ls -A /var/cpanel/users | grep -v ^root$ | grep -v ^system$`
#colors
nocolor="\E[0m"
black="\033[0;30m"
grey="\033[1;30m"
red="\033[0;31m"
lightRed="\033[1;31m"
green="\033[0;32m"
lightGreen="\033[1;32m"
brown="\033[0;33m"
yellow="\033[1;33m"
blue="\033[0;34m"
lightBlue="\033[1;34m"
purple="\033[0;35m"
lightPurple="\033[1;35m"
cyan="\033[0;36m"
lightCyan="\033[1;36m"
white="\033[1;37m" #a bold white
#background colors: \033[bold;color;background
greyBg="\033[1;37;40m"
#echo in a color function
ec() {
#Usage: ec $color "text"
ecolor=${!1} #get the color
shift #$1 is removed here
#echo the rest
echo -e ${ecolor}"${*}"${nocolor}
}
e2c() {
#press enter to continue
ec lightCyan "Press Enter to continue..."
read
}
logit() {
tee -a $scriptlog
}
yesNo() { #generic yesNo function
#repeat if yes or no option not valid
while true; do
#$* read every parameter given to the yesNo function which will be the message
echo -ne "${yellow}${*}${white} (Y/N)?${nocolor} "
#junk holds the extra parameters yn holds the first parameters
read yn junk
case $yn in
yes|Yes|YES|y|Y)
echo "y" >> $scriptlog
return 0 ;;
no|No|n|N|NO)
echo "n" >> $scriptlog
return 1 ;;
*)
ec lightRed "Please enter y or n."
esac
done
#usage:
#if yesNo 'do you want to continue?' ; then
# echo 'You choose to continue'
#else
# echo 'You choose not to continue'
#fi
}
menutext() {
echo "Version: $ver"
echo
#check for screen session
if [[ ! "${STY}" ]]; then
ec lightRed "Warning! You are not in a screen session!"
echo
fi
#check for didnotrestore now:
if [ -s "$dnrold" ]; then
ec lightRed "Found users that did not restore from a previous sync in $dnrold! Press d to see these users."
echo
logvars dnrusers
fi
ec greyBg "=Initialsync Main Menu=
Select the migration type:"
echo "1) Full sync (from $userlistfile or all users, version matching)
2) Basic sync (all users, no version matching)
3) Single user sync (no version matching, shared server safe)
4) User list sync (from $userlistfile, no version matching)
5) Restore users from $dnr. (no version matching)
8) Database sync - only sync databases for cpanel users, and from /root/dblist.txt.
9) Final sync (from $userlistfile or all users)
0) Quit
Post migration script at /root/postmigration.sh will run after the final sync if found."
}
main() {
echo "Version $ver"
echo "Started $starttime"
#menu options
mainloop=0
while [ $mainloop == 0 ] ; do
clear
menutext
echo -n "Enter your choice: "
read choice
case $choice in
1)
fullsync
mainloop=1 ;;
2)
basicsync
mainloop=1 ;;
3)
singleuser
mainloop=1 ;;
4)
listsync
mainloop=1 ;;
5)
dnrsync
mainloop=1 ;;
8)
dbsync
mainloop=1 ;;
9)
finalsync
mainloop=1 ;;
0)
echo "Bye..."; exit 0 ;;
d)
if [ -s ${dnrold} ]; then
cat ${dnrold}
if yesNo "Do you want to restore these users?"; then
dnrsync
mainloop=1
fi
else
echo "${dnrold} file not found! Quit mashing keys!"
fi
e2c
clear
;;
*)
ec lightRed "Not a valid choice. Also, the game."; sleep 2 ; clear
esac
done
sleep 3
echo
echo "Started at $starttime"
[ $syncstarttime ] && echo "Sync started at $syncstarttime"
[ $syncendtime ] && echo "Sync finished at $syncendtime"
echo "Finished at `date +%F.%T`"
#cleanup
if [ -s /root/dblist.txt ]; then
mv /root/dblist.txt{,.$starttime}
fi
ec lightGreen 'Done!'
exit 0
}
dbsync() {
dbonlysync=1
echo "Database only sync."
userlist=$allcpusers
getip #asks for ip or checks a file to confirm destination
mysqldbfinalsync
}
#sync types
singleuser() {
echo
echo "Single user sync."
singleuserloop=0
while [ $singleuserloop == 0 ]; do
echo -n "Input name of the user to migrate:"
read userlist
if yesNo "Restore to dedicated ip?"; then
forcededip=1
fi
#check for error
sucheck=`/bin/ls -A /var/cpanel/users | grep ^${userlist}$`
logvars userlist sucheck forcededip
if [[ $sucheck = $userlist ]]; then
echo "Found $userlist, restoring..."
singleuserloop=1
#rsyncupgrade
getip #asks for ip or checks a file to confirm destination
accountcheck #if conflicting accounts are found, asks
acctcopy
didntrestore
echo
if yesNo "Remove ssh key from remote server?" ; then
echo "Removing key..."
ssh $sshargs -p$port $ip "rm ~/.ssh/authorized_keys ; cp -rp ~/.ssh/authorized_keys{.initialsyncbak,}"
else
echo "Leaving key."
fi
else
ec lightRed "Could not find $userlist."
fi
done
}
dnrsync() {
echo "DNR sync"
#check the that dnr.starttime file exists, use it for list sync
if [ -s "${dnrold}" ]; then
echo $dnrusers > $userlistfile
listsync
else
echo "Did not find any users in the ${dnrold} file. Try a list sync instead."
fi
}
listsync() {
echo
echo "List sync."
listsyncvar=1
if [ -s $userlistfile ]; then
ec yellow "Found $userlistfile"
sleep 3
userlist=`cat $userlistfile`
logvars userlist
echo "$userlist"
presync
copyaccounts
else
echo "Did not find users in $userlistfile in /root or /home"
sleep 3
fi
}
basicsync(){
echo
echo "Basic Sync started"
previousmigcheck
presync
copyaccounts
}
fullsync() {
echo
echo "Full sync started"
#check versions, run ea, upcp, match php versions, lots of good stuff
previousmigcheck
presync
versionmatching
copyaccounts
}
#Main sync procecures
presync() {
echo "Running Pre-sync functions..."
#get ips and such
sslcertcheck
dnscheck #lets you view current dns
rsyncupgrade
lowerttls
getip #asks for ip or checks a file to confirm destination
dnsclustercheck
accountcheck #if conflicting accounts are found, asks
dedipcheck #asks if an equal amount of ips are not found
mysqlsymlinkcheck
}
versionmatching() {
#only full syncs
echo "Running version matching..."
nameservers
gcccheck #needs to be before upcp, ea
upcp
apacheprepostcheck
phpmemcheck
thirdparty
mysqlcheck
upea
installprogs
phpapicheck # to be ran after ea so php4 can be compiled in if needed
rubygems
matchpear
}
copyaccounts() {
echo "Starting account copying functions..."
acctcopy
didntrestore
mysqlextradbcheck
mysqldumpinitialsync
cpbackupcheck
postmigrationhook
finalchecks
hostsgen
}
previousmigcheck() {
#check for previous migration, and define userlist
echo
echo "Checking for previous migrations..."
#check for userlist file
if [ -s $userlistfile ]; then
echo "$userlistfile found: "
cat $userlistfile
if yesNo "Do you want to use this list from $userlistfile?" ; then
userlist=`cat $userlistfile`
else
echo "Backing up $userlistfile to ${userlistfile}.${starttime}.bak."
mv $userlistfile ${userlistfile}.${starttime}.bak
echo "Selecting all users."
userlist=$allcpusers
fi
else
echo "No previous migration found, migrating all users."
userlist=$allcpusers
fi
ec yellow "Users selected for migration:"
echo $userlist
sleep 3
}
hostsgen() {
echo
echo "Generating hosts file..."
#ssh $ip -p$port "wget -O /scripts/hosts.sh http://migration.sysres.liquidweb.com/hosts.sh ; bash /scripts/hosts.sh"
cat > /scripts/hosts.sh <<'EOF'
#!/bin/bash
#abrevick@lw Nov 21 2011
#obtain hosts file format from a cpanel server for easy testing
hostsfile=/usr/local/apache/htdocs/hosts.txt
hostsfilealt=/usr/local/apache/htdocs/hostsfile.txt
ip=`grep ADDR /etc/wwwacct.conf |cut -f2 -d" "`
if [ -s $hostsfile ]; then
mv $hostsfile{,.bak}
fi
if [ -s /etc/userdatadomains ]; then
#new way for cpanel 11.27+
for ips in `/scripts/ipusage | cut -d" " -f1`; do
sites=`grep $ips /etc/userdatadomains |awk -F== '{print $1}' |cut -d: -f1 |sort |uniq | sed -e 's/\(.*\)/\1 www.\1/g' `;
echo $ips $sites ;
done | tee $hostsfile ;
#one line per domain (purkis way)
> $hostsfilealt
cat /etc/userdatadomains | sed -e 's/:/ /g' -e 's/==/ /g' -e 's/\*/x/g' | while read sdomain user owner type maindomain docroot ip port ; do
echo $ip $sdomain "www."$sdomain >> $hostsfilealt
done
echo
echo "Generated hosts file at http://${ip}/hosts.txt"
echo "One line per domain at http://${ip}/hostsfile.txt"
else
#old way
echo "/etc/userdatadomains not found, using old way."
/scripts/ipusage | sed -e 's/\[mail:.*//g' -e 's/,/ /g' -e 's/\[http://g' -e 's/\]//g' -e 's/\[ftp:.*//g' | sed 's/\ \ /\ /g' | tee $hostsfile
/scripts/ipusage | sed -e 's/\[mail:.*//g' -e 's/,/ /g' -e 's/\[http://g' -e 's/\]//g' -e 's/\[ftp:.*//g' -e 's/\ \ /\ /g' -e 's/\ \([a-zA-Z0-9]\)/\ www.\1/g' | tee -a $hostsfile
echo
echo "Generated hosts file at http://${ip}/hosts.txt."
fi
EOF
rsync -aqHPe "ssh -p$port" /scripts/hosts.sh $ip:/scripts/
ssh $sshargs -p$port $ip "bash /scripts/hosts.sh"
sleep 2
}
dnsclustercheck() {
echo
echo "Checking for DNS clustering..."
if [ -f /var/cpanel/useclusteringdns ]; then
echo 'Local DNS Clustering found!'
localcluster=1
fi
remotednscluster=`ssh $sshargs -p$port $ip "if [ -f /var/cpanel/useclusteringdns ]; then echo \"Remote DNS Clustering found.\" ; fi" `
logvars localcluster remotednscluster
if [ "$remotednscluster" ]; then
echo
echo "DNS cluster on the new server is detected, you shouldn't continue since restoring accounts has the potential to automatically update DNS for them in the cluster. Probably will be better to remove the remote server from the cluster before continuing."
if yesNo 'Do you want to continue?'; then
echo "Continuing..."
else
exit 0
fi
fi
}
sslcertcheck() {
#SSl cert checking.
echo "Checking for SSL Certificates in apache conf."
crtcheck=`grep SSLCertificateFile /usr/local/apache/conf/httpd.conf`
logvars crtcheck
if [ "$crtcheck" ]; then
ec yellow "SSL Certificates detected."
echo
for crt in `grep SSLCertificateFile /usr/local/apache/conf/httpd.conf |awk '{print $2}'`; do
echo $crt; openssl x509 -noout -in $crt -issuer -subject -dates
echo
done
echo
e2c
else
echo "No SSL Certificates found in httpd.conf."
sleep 2
fi
}
dnscheck() {
echo
echo "Checking Current dns..."
if [ -f /root/dns.txt ]; then
echo "Found /root/dns.txt"
sleep 3
cat /root/dns.txt | sort -n +3 -2 | more
else
if [ -f "/root/domainlist.txt" ]; then
mv /root/domainlist.txt /root/domainlist.txt.${starttime}.bak
fi
for user in $userlist; do cat /etc/userdomains | grep $user | cut -d: -f1 >> /root/domainlist.txt; done
domainlist=`cat /root/domainlist.txt`
logvars domainlist
for each in $domainlist; do echo $each\ `dig @8.8.8.8 NS +short $each |sed 's/\.$//g'`\ `dig @8.8.8.8 +short $each` ;done | grep -v \ \ | column -t > /root/dns.txt
cat /root/dns.txt | sort -n +3 -2 | more
fi
e2c
}
lowerttls() {
echo
echo "Lowering TTLs..."
#lower ttls, switched to find command for a lot of domains
#switched to perl as sed -i doesn't exist in old versions of sed
find /var/named/ -name \*.db -exec perl -p -i.lwbak -e 's/^\$TTL.*/\$TTL 300/g' {} \;
find /var/named/ -name \*.db -exec perl -pi -e 's/[0-9]{10}/'`date +%Y%m%d%H`'/g' {} \;
#jwarrens A record reducer:
find /var/named/ -name \*.db -exec perl -pi -e 's/^([\w.\-]+[^\S\n]+)[0-9]+([^\S\n]+IN[^\S\n]+A[^\S\n]+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+.*$)/\1\Q300\E\2/g' {} \;
rndc reload
#for the one time i encountered NSD
nsdcheck=`ps aux |grep nsd |grep -v grep`
logvars nsdcheck
if [ "$nsdcheck" ]; then
if [ `which nsdc` ]; then
echo "Nsd found, reloading"
nsdc rebuild
nsdc reload
fi
fi
}
getip() {
echo
echo "Getting Ip for destination server..."
#check for previous migration, just in case.
ipfile=/root/dest.ip.txt
if [ -f $ipfile ]; then
ip=`cat $ipfile`
echo
ec yellow "Ip from previous migration found: `echo $ip`"
getport
echo
if yesNo "Is $ip the server you want? Otherwise enter No to input new ip." ;then
echo "Ok, continuing with $ip"
sshkeygen
else
rm -rf /root/dest.port.txt
ipask
fi
else
ipask
fi
sleep 1
logvars ip
}
ipask() {
echo
echo -n 'Destination IP: ';
read ip
echo $ip > $ipfile
getport
sshkeygen
}
getport() {
echo
echo "Getting ssh port."
if [ -s /root/dest.port.txt ]; then
port=`cat /root/dest.port.txt`
ec yellow "Previous Ssh port found: ($port)."
else
echo -n "SSH Port [22]: "
read port
fi
if [ -z $port ]; then
echo "No port given, assuming 22"
port=22
fi
echo $port > /root/dest.port.txt
logvars port
}
sshkeygen() {
echo
if ! [ -f ~/.ssh/id_rsa ]; then
echo "Generating SSH key..."
ssh-keygen -q -N "" -t rsa -f ~/.ssh/id_rsa
else
echo "SSH key found."
fi
echo "Copying Key to remote server..."
#create .ssh directory
#create authorized key file if it doesn't exist (for non-existing files) backup the file to syncbak, this will keep existing and blank files.
#don't overwrite existing .initialsyncbak files.
cat ~/.ssh/id_rsa.pub | ssh $sshargs $ip -p$port "mkdir -p ~/.ssh ; if [ ! -f ~/.ssh/authorized_keys ] ; then touch ~/.ssh/authorized_keys ; fi; if [ ! -f ~/.ssh/authorized_keys.initialsyncbak ] ; then cp -rp ~/.ssh/authorized_keys{,.initialsyncbak} ; fi ; cat >> ~/.ssh/authorized_keys"
sshtest=$?
#test exit value of ssh command.
if [[ "$sshtest" > 0 ]]; then
ec lightRed "Ssh connection to $ip failed, please check connection before retrying!"
exit 3
else
ec lightGreen "Ssh connection to $ip succeded!"
ssh $sshargs -p$port $ip "chmod 2755 /usr/bin/screen; chmod 775 /var/run/screen"
fi
}
accountcheck() { #check for users with the same name on each server:
echo
echo "Comparing accounts with destination server..."
ssh $sshargs $ip -p$port "/bin/ls -A /var/cpanel/users/" > $remoteusersfile
for user in $userlist ; do
if [ `grep "^$user$" "$remoteusersfile"` ]; then
echo $user
fi
done >> /tmp/userexists.txt
#check for userexists.txt greater than 0
if [ -s /tmp/userexists.txt ]; then
ec lightRed '\\\Accounts that conflict with the destination server///'
cat /tmp/userexists.txt
ec lightRed '^^^Accounts that conflict with the destination server^^^'
ec yellow "Only put accounts you want to migrate in /root/userlist.txt, or remove the accounts from the destination server."
exit 20
# if yesNo "Y to continue, N to exit."; then
# echo "Continuing..."
# else
# echo "Exiting..."
# exit 0
# fi
else
echo "No conflicting accounts found."
fi
}
dedipcheck() { #check for same amount of dedicated ips
echo
echo "Checking for dedicated Ips."
# If /etc/userdatadomains exists, calculate dedicated IPs based on usage.
# Otherwise uses same functionality as before.
if [[ -f /etc/userdatadomains ]]; then
preliminary_ip_check=`cat /etc/userdatadomains|sed -e 's/:/ /g' -e 's/==/ /g' -e '/\*/d' |cut -d ' ' -f8|tr -d [:blank:]|sort|uniq`
server_main_ip=`cat /etc/wwwacct.conf|grep ADDR|cut -d ' ' -f2`
for preliminary_ips in $preliminary_ip_check; do
if [[ $preliminary_ips != $server_main_ip ]]; then
dedicated_ip_accounts="${dedicated_ip_accounts} $preliminary_ips"
fi
logvars preliminary_ips dedicated_ip_accounts
done
sourceipcount=`echo $dedicated_ip_accounts|sed -e 's/ /\n/g'|wc -l`
else
sourceipcount=`cat /etc/ips | grep ^[0-9] | wc -l`
fi
# Check target server for number of dedicated IPs available
destipcount=`ssh $sshargs $ip -p$port "cat /etc/ips |grep ^[0-9] | wc -l"`
logvars destipcount preliminary_ip_check server_main_ip sourceipcount
echo "==Dedicated Ip Count==
Source (Ips in use): $sourceipcount
Destination : $destipcount"
if (( $sourceipcount <= $destipcount ));then
ec lightGreen "There seems to be enough IPs on the destination server for this migration."
ipcheckquery
else
ec lightRed "The Destination server does not seem to have enough dedicated IPs."
ipcheckquery
fi
sleep 1
}
ipcheckquery(){
ipcheck=0
if yesNo "Override IP check?
yes = Restore accounts to dedicated Ips. (Please ensure there are enough Dedicated IPs)
no = Restore accounts to the Main Shared Ip." ;then
echo "Restoring accounts to dedicated IPs."
ipcheck=1
else
echo "Restoring accounts to the main shared Ip."
ipcheck=0
fi
logvars ipcheck
}
nameservers() {
echo "Current nameservers:"
grep ^NS[\ 0-9] /etc/wwwacct.conf
if yesNo "Set nameservers on remote host?" ;then
grep ^NS[\ 0-9] /etc/wwwacct.conf > /tmp/nameservers.txt
rsync -avHPe "ssh -p$port" /tmp/nameservers.txt $ip:/tmp/
ssh $sshargs $ip -p$port "cp -rp /etc/wwwacct.conf{,.bak} ;
sed -i -e '/^NS[\ 0-9]/d' /etc/wwwacct.conf ;
cat /tmp/nameservers.txt >> /etc/wwwacct.conf "
fi
}
apacheprepostcheck() { #check for pre/post conf files
apachefilelist="post_virtualhost_1.conf
post_virtualhost_2.conf
post_virtualhost_global.conf
pre_main_1.conf
pre_main_2.conf
pre_main_global.conf
pre_virtualhost_1.conf
pre_virtualhost_2.conf
pre_virtualhost_global.conf"
for file in $apachefilelist; do
if [ -s /usr/local/apache/conf/includes/$file ]; then
#file exists and is non-zero size
echo
ec lightCyan "Contents of /usr/local/apache/conf/includes/$file :
================================="
cat /usr/local/apache/conf/includes/$file
ec cyan "================================="
if yesNo "Found extra apache configuration in $file, shown above. copy to new server?"; then
ssh $sshargs -p$port $ip "mv /usr/local/apache/conf/includes/$file{,.bak}"
rsync -avHPe "ssh -p$port" /usr/local/apache/conf/includes/$file $ip:/usr/local/apache/conf/includes/
fi
fi
done
}
phpapicheck() { #run after EA so php4 can be supported
echo
echo "Matching php handlers..."
/usr/local/cpanel/bin/rebuild_phpconf --current > /tmp/phpconf
#check for ea failure message
if [ "`cat /tmp/phpconf`" == "Sorry, php has not yet been configured with EA3 tools" ]; then
echo "EA fail message."
phpapicheck=1
logvars phpapicheck
else
phpver=`grep ^DEFAULT\ PHP /tmp/phpconf |awk '{print $3}'`
php4sapi=`grep ^PHP4\ SAPI /tmp/phpconf |awk '{print $3}'`
php5sapi=`grep ^PHP5\ SAPI /tmp/phpconf |awk '{print $3}'`
phpsuexec=`grep ^SUEXEC /tmp/phpconf |awk '{print $2}'`
logvars phpver
logvars php4sapi
logvars php5sapi
logvars phpsuexec
#php suexec will be either 'enabled' or 'not installed', check if its not enabled. can set the param with 1 or 0 also.
if [ "$phpsuexec" != enabled ]; then
phpsuexec=0
logvars phpsuexec
fi
#check if phpver is 4 or 5, old EA versions will fail the rebuild_phpconf command
case $phpver in
[45])
ssh $sshargs $ip -p$port "/usr/local/cpanel/bin/rebuild_phpconf --current > /tmp/phpconf.`date +%F.%T`.txt ;/usr/local/cpanel/bin/rebuild_phpconf $phpver $php4sapi $php5sapi $phpsuexec "
;;
*) echo "Got unexpected output from /usr/local/cpanel/bin/rebuild_phpconf --current, skipping..."
phpapicheck=1
logvars phpapicheck
;;
esac
fi
}
phpmemcheck(){
echo
echo "Checking php memory limit..."
phpmem=`php -i |grep ^memory_limit |cut -d" " -f3`
rphpmem=`ssh $sshargs $ip -p$port 'php -i |grep ^memory_limit |cut -d" " -f3'`
logvars phpmem
logvars rphpmem
if [ $phpmem ]; then
if [ $rphpmem ]; then
if [[ $phpmem != $rphpmem ]]; then
phpmemcmd=`echo 'sed -i '\''s/\(memory_limit\ =\ \)[0-9]*M/\1'$phpmem'/'\'' /usr/local/lib/php.ini'`
logvars phpmemcmd
ssh $sshargs $ip -p$port "cp -rp /usr/local/lib/php.ini{,.bak} ; $phpmemcmd ; service httpd restart"
else
echo "Old memorylimit $phpmem matches new $rphpmem, skipping..."
fi
else
echo "Remote php memory_limit not found."
phpmemcheck=1
logvars phpmemcheck
fi
else
echo "Local php memory_limit not found."
phpmemcheck=1
logvars phpmemcheck
fi
}
thirdparty() {
#look for random apps to install here, they are installed in installprogs
echo
echo "Checking for 3rd party apps..."
#Check for ffmpeg
ffmpeg=`which ffmpeg`
#Check for Imagemagick
imagick=`which convert`
#memcache
memcache=`ps aux | grep -e 'memcache' | grep -v grep | tail -n1 `
#java
java=`which java 2>1 /dev/null`
#postgresql
postgres=`ps aux |grep -e 'postgres' |grep -v grep |tail -n1`
#other stuff, say if it needs to be installed at the end
xcachefound=`ps aux | grep -e 'xcache' | grep -v grep | tail -n1`
eaccelfound=`ps aux | grep -e 'eaccelerator' | grep -v grep |tail -n1`
nginxfound=`ps aux | grep -e 'nginx' |grep -v grep| tail -n1`
lswsfound=`ps aux | grep -e 'lsws' | grep -v grep | tail -n1`
logvars ffmpeg imagick memcache java postgres xcachefound eaccelfound nginxfound
}
mysqlcheck() {
#mysql
echo
echo "Checking mysql versions..."
smysqlv=`grep -i mysql-version /var/cpanel/cpanel.config | cut -d= -f2`
dmysqlv=`ssh $sshargs $ip -p$port 'grep -i mysql-version /var/cpanel/cpanel.config | cut -d= -f2'`
logvars smysqlv dmysqlv
echo "Source: $smysqlv"
echo "Destination: $dmysqlv"
if [ $smysqlv == $dmysqlv ]; then
ec green "Mysql versions match."
else
ec red "Mysql versions do not match."
fi
if yesNo "Change remote server's Mysql version?"; then
mysqlverloop=0
while [ $mysqlverloop == 0 ]; do #asking for user input, so check for errors.
echo -e "Please input desired mysql version, either 5.1 or 5.5, or n to cancel: " #should really be upgrading to these newer versions, older than 5.0 isn't supported in cpanel 11.36, older than 5.1 isn't supported in 11.40
read newmysqlver
case $newmysqlver in
5.1|5.5)
ec green "New server's mysql will be changed to $newmysqlver"
mysqlup=1
mysqlverloop=1;;
n)
echo "Canceling mysql version change."
mysqlverloop=1;;
*)
ec red "Incorrect input, try again." ;;
esac
done
phpvr=`ssh $sshargs $ip -p$port "php -v |head -n1 |cut -d\" \" -f2"` #get remote php version now since mysql will not allow us to check later.
logvars phpvr mysqlup newmysqlver smysqlv
fi
sleep 1
}
mysqlextradbcheck() { #find dbs created outside of cpanel, with potential to copy them over.
#skip this fucntion if the username prefix is disabled.
dbprefixvar=`grep database_prefix /var/cpanel/cpanel.config `
logvars dbprefixvar
if ! [ "$dbprefixvar" = "database_prefix=0" ]; then
echo
echo "Checking for extra mysql databases..."
mkdir -p /home/temp/
mysql -e 'show databases' |grep -v ^cphulkd |grep -v ^information_schema |grep -v ^eximstats |grep -v ^horde | grep -v leechprotect |grep -v ^modsec |grep -v ^mysql |grep -v ^roundcube |grep -v ^Database | grep -v ^logaholicDB | grep -v ^performance_schema |grep -v '*' > /home/temp/dblist.txt
#still have user_ databases, filter those.
cp -rp /home/temp/dblist.txt /home/temp/extradbs.txt
#get all users here, not userlist.
for user in $allcpusers ; do
sed -i -e "/^$user\_/d" /home/temp/extradbs.txt
done
#check for non zero filesize
if [ -s /home/temp/extradbs.txt ];then
echo "Extra databases Detected (/home/temp/extradbs.txt):"
cat /home/temp/extradbs.txt |more
#offer to migrate
if yesNo 'Copy these databases to the new server? (adds to /root/dblist.txt)' ; then
cat /home/temp/extradbs.txt >> /root/dblist.txt
fi
#clear the extra dbs file so it wont interfere in future migrations.
> /home/temp/extradbs.txt
fi
else
echo
echo "Detected user database prefixing is disabled in WHM. Might want to set this up on the new server, accounts should migrate fine though."
e2c
fi
}
mysqlsymlinkcheck() {
echo
echo "Checking if Mysql was moved to a different location..."
#test if symbolic link
if [ -L /var/lib/mysql ]; then
ec red "Warning, /var/lib/mysql is a symlink! Grepping for datadir in my.cnf:"
grep datadir /etc/my.cnf
echo "You may want to relocate mysql on the new server (if it isnt already) before continuing."
if yesNo 'Yes to continue, no to exit.'; then
echo "Continuing..."
else
echo "Exiting."
exit 0
fi
else
echo "Mysql is in the default location."
fi
}
gcccheck() {
echo 'Checking for gcc on new server, because some newer storm servers dont have gcc installed so EA and possibly other things will fail to install.'
gcccheck=$(ssh $sshargs -p$port $ip "rpm -qa gcc")
logvars gcccheck
if [ "$gcccheck" ]; then
echo "Gcc found, continuing..."
else
echo 'Gcc not found, running "yum install gcc" on remote server. You may have to hit "y" then Enter to install.'
sleep 3
ssh $sshargs -p$port $ip "yum -y install gcc"
fi
}
upcp() {
echo
echo "Checking Cpanel versions..."
#upcp if local version is higher than remote
cpver=`cat /usr/local/cpanel/version`
rcpver=`ssh $sshargs $ip -p$port "cat /usr/local/cpanel/version"`
if [[ $cpver > $rcpver ]]; then
echo "This server has $cpver"
echo "Remote server has $rcpver"
if yesNo "Run Upcp on remote server?" ; then
echo "Upcp will be ran when the sync begins."
upcp=1
#ssh $ip -p$port "/scripts/upcp"
else
echo "Okay, fine, not running upcp."
fi
else
echo "Found a higher version of cpanel on remote server, continuing."
fi
logvars cpver rcpver upcp
sleep 1
}
upea() {
echo
echo "Copying over WHM packages and features..."
#Copy Cpanel packages
rsync -aqHe "ssh -p$port" /var/cpanel/packages/ $ip:/var/cpanel/packages/
#Copy features
rsync -aqHe "ssh -p$port" /var/cpanel/features/ $ip:/var/cpanel/features/
#find php versions to judge whether or not ea should be run
phpv=`php -v |head -n1|cut -d" " -f2`
#check if the var is set by the mysql function
if ! [ $phpvr ]; then
phpvr=`ssh $sshargs $ip -p$port "php -v |head -n1 |cut -d\" \" -f2"`
fi
echo "
Available software versions on remote server:"
ssh $sshargs -p $port $ip "/scripts/easyapache --latest-versions"
if [[ $phpv < 5.3 ]];then
ec red "If the php version does not match any of the above, you should manually run EA!"
fi
echo "Source: $phpv"
echo "Dest : $phpvr"
if yesNo "Want me to run EA on remote server?" ;then
ea=1
unset mysqlupcheck
else
echo 'Just trying to help :/'
skippedea=1
if yesNo "Want to copy over the easyapache config to the new server?"; then
echo 'ok!'
rsync -aqHe "ssh -p$port" /var/cpanel/easy/apache/ $ip:/var/cpanel/easy/apache/
fi
fi
sleep 1
}
installprogs(){
proglist="ffmpeg
imagick
memcache
java
upcp
mysqlup
ea
postgres"
echo
ec lightGreen "Heres what we found to install:"
for prog in $proglist; do
if [ "${!prog}" ] ; then
echo "${prog}"
fi
done
sleep 3
ec lightGreen "Ready to begin installing and start the initial sync!"
e2c
#lwbake,plbake
ec yellow "Installing lwbake and plbake"
ssh $sshargs $ip -p$port "wget -O /scripts/lwbake http://layer3.liquidweb.com/scripts/lwbake;
chmod 700 /scripts/lwbake
wget -O /scripts/plbake http://layer3.liquidweb.com/scripts/plBake/plBake
chmod 700 /scripts/plbake"
#java
if [ "$java" ];then
ec yellow "Java found, installing..."
ssh $sshargs $ip -p $port "/scripts/plbake java"
fi
#upcp
if [ $upcp ]; then
ec yellow "Running Upcp..."
sleep 2
ssh $sshargs $ip -p$port "/scripts/upcp"
fi
#mysql
if [ $mysqlup ]; then
ec yellow "Reinstalling mysql..."
#mysql 5.5 won't start if safe-show-database and skip-locking are in my.cnf
remotecpanelversion=` ssh $sshargs $ip -p$port "cat /usr/local/cpanel/version"` #cant set variables in the next script for some reason
ssh $sshargs $ip -p$port "
sed -i.bak /mysql-version/d /var/cpanel/cpanel.config ;
echo mysql-version=$newmysqlver >> /var/cpanel/cpanel.config ;
cp -rp /etc/my.cnf{,.bak} ;
if [ $newmysqlver > 5 ]; then
sed -i -e /safe-show-database/d /etc/my.cnf
sed -i -e /skip-locking/d /etc/my.cnf
fi
cp -rp /var/lib/mysql{,.bak} ;
if [ $remotecpanelversion > 11.36.0 ]; then
/usr/local/cpanel/scripts/check_cpanel_rpms --targets=MySQL50,MySQL51,MySQL55 --fix
else
/scripts/mysqlup --force
fi"