-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpleskinitialsync.sh
1020 lines (939 loc) · 40.2 KB
/
pleskinitialsync.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
#Plesk migration script
#many thanks to abrevick@lw for his cpanel initialsync script, off of which this was based.
ver='nov 19.13'
#=========================================================
#initial setup and global variables
#==================================
test -f /var/didnotrestore.txt && mv /var/didnotrestore.txt{,.`date +%F.%T`.bak}
didnotrestore=/var/didnotrestore.txt
tmpfolder=/var/migrationtemp
logfile=$tmpfolder/migrationlog.txt
didnotbackup=/var/didnotbackup.txt
yesNo() { #generic yesNo function
#repeat if yes or no option not valid
while true; do
#$* read every parameter giving to the yesNo function which will be the message
echo -ne "${yellow}${*}${white} (Y/N)? ${noclr}"
#junk holds the extra parameters yn holds the first parameters
read yn junk
case $yn in
yes|Yes|YES|y|Y)
return 0 ;;
no|No|n|N|NO)
return 1 ;;
*)
echo -e "Please enter y or n."
esac
done
}
red='\e[1;31m'
green='\e[1;32m'
yellow='\e[1;33m'
blue='\e[1;34m'
purple='\e[1;35m'
white='\e[1;37m'
noclr='\e[0m'
#========
#end init
#========
main() {
mainloop=0
clear
while [ $mainloop == 0 ]; do
echo -e "${white}Welcome to the Plesk Migration Tool!
Version: $ver${noclr}
This tool has been tested on Plesk 8+. ymmv."
if [[ ! "${STY}" ]]; then
echo -e "${red}
!!!!!
You are not in a screen session. Please run this script in a screen session!
!!!!!1${noclr}"
fi
echo -e "
${white}Select your migration type:${noclr}
A) Help!
1) Full initial sync (all plesk accounts and all data, one backup file)
2) Client list sync (from /var/userlist.txt)
3) Domain list sync (from /var/domainlist.txt)
4) Full update sync (all homedirs, databases, and mail files)
5) Client list update sync (as 4, but from /var/userlist.txt)
6) Domain list update sync (as 4, but from /var/domainlist.txt)
7) Generate a list of clients for this server to /var/clientlist.txt (to help you make /var/userlist.txt)
8) Generate a list of subscriptions for this server to /var/sublist.txt (to help you make /var/domainlist.txt)
9) Final sync (all domains, databases, and mail files)
0) Quit
"
echo -e "${white}Enter your choice:${noclr}"
read choice
case $choice in
A|a)
clear
echo -e "This tool is designed to migrate data between plesk servers. For most migrations, you will want to use the full initial sync followed by the final sync at a later time after testing is complete. If you need more granularity in syncing (for example, if you are copying one or two clients or one or two domains for a particular client), then you can use the client list sync or domain list sync. It is recommended, if you use either of these two options, to use the client sync, as this will bring across client-level data, which the domain list sync does not. However, if you need to migrate one domain and the client owns more than one domain, the domain list sync will work well for this.
Now for the tl;dr:
The presync functions encompass checking for common third-party applications, downgrading from php 5.3 to php 5.2 if neccessary, lowering the TTL values for hosted DNS, setting up SSH keys, and other important prep tasks for all migrations.
The full sync works by creating a configuration-only backup of the entire server and restoring this on the target. Obviously, this works best with an empty target server. Then, individual domains are synced at a folder level to the target server, the mail folders are copied, and databases are dumped and reimported. The final sync for this task redumps all the databases and updates the folder syncs, then changes the DNS on the old server to match the new server.
The client list sync takes a list of clients and makes client-level configuration backups. These are restored one at a time on the target server, and then databases are dumped and copied, and folders are synced. At the end of the client list loop, the databases are imported on the target server. Separate from this, you are asked if you would like to migrate domains that are not owned by a client (admin domains). These are synced one at a time using the domain-list sync method.
The domain list sync works as the client list sync does, but at a subscription-level backup.
After all types of syncs, the target server is checked for accounts that did not restore correctly, and a list of hosts file entries is generated.
Press enter to return to the menu."
read
clear;;
1)
initialsync
mainloop=1;;
2)
clientlistsync
mainloop=1;;
3)
domainlistsync
mainloop=1;;
4)
updatesync
mainloop=1;;
5)
clientlistupdatesync
mainloop=1;;
6)
domainlistupdatesync
mainloop=1;;
7)
echo -e "One moment..."
sleep 1
mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'SELECT c.login, d.name FROM clients AS c JOIN domains AS d ON d.cl_id = c.id;' > /var/clientlist.txt
clear
echo -e "${yellow}A list of clients on this server was placed in /var/clientlist.txt.${noclr}"
sleep 2;;
8)
echo -e "One moment..."
sleep 1
mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'SELECT name FROM domains JOIN Subscriptions ON Subscriptions.object_id = domains.id;' > /var/sublist.txt
clear
echo -e "${yellow}A list of subscriptions on this server was placed in /var/sublist.txt.${noclr}"
sleep 2;;
9)
finalsync
mainloop=1;;
0)
echo -e "Quitting, quitter!"
exit 0;;
*)
echo -e Please select a valid migration type.
sleep 2
clear
esac
done
echo -e Finished!
exit
}
initialsync() {
echo -e "${purple}Starting full initial sync.${noclr}"
presync
domainexistcheck `mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns psa -e "select name from domains;"`
checkcatpreexisting
createbackup
syncbackup
ipmaptool
restore
dnrcheck
hostsfile
removekey
reminders
}
clientlistsync() {
clientlistfile=/var/userlist.txt
echo -e "${purple}Syncing from $clientlistfile:${noclr}
"
cat $clientlistfile | grep -v ^admin$
if yesNo "This look good to you?"; then
clientcheck
presync
if [[ -f $didnotbackup ]]; then mv $didnotbackup{,.`date +%F.%T`.bak}; fi
if yesNo "Would you like to sync domains owned by the admin user, which would not otherwise be migrated at a client level?"; then
syncadmin
fi
syncclients
dbsyncscript
dnrcheck
hostsfile
removekey
reminders
else
echo -e "${red}Check yoself before you wreck yoself. Quitting.${noclr}"
exit 0
fi
}
domainlistsync() {
domlistfile=/var/domainlist.txt
echo -e "${purple}Syncing from $domlistfile:${noclr}
"
cat $domlistfile
if yesNo "This look good to you?"; then
subcheck
presync
if [[ -f $didnotbackup ]]; then mv $didnotbackup{,.`date +%F.%T`.bak}; fi
domainexistcheck `cat $domlistfile`
checkcatpreexisting
syncdomains
dbsyncscript
dnrcheck
hostsfile
removekey
reminders
else
echo -e "${red}Check yoself before you wreck yoself. Quitting.${noclr}"
exit 0
fi
}
presync() {
echo -e "${purple}Starting pre-backup tasks.${noclr}"
dnscheck
rsyncupgrade
lowerttls
getip
foldersetup
licensecheck
ipcheck
phpupgrade
autoinstaller
}
updatesync() {
echo -e "${purple}Starting full update sync.${noclr}"
rsyncupgrade
getip
databasefinalsync
rsynchomedirs
rsyncemail
removekey
reminders
}
clientlistupdatesync() {
echo -e THIS IS NOT READY YET
exit 0
}
domainlistupdatesync() {
echo -e THIS IS NOT READY YET
exit 0
}
#================
#start final sync
#================
finalsync() {
echo -e "${purple}Starting final sync.${noclr}"
#get options for final sync
getip
if yesNo "Stop services for final sync?"; then
stopservices=1
if yesNo "Restart services after final sync?"; then
restartservices=1
fi
fi
if yesNo 'Copy DNS information from target PSA database? Do not do this unless migrating all users, and DNS is on the local machine.'; then
copydns=1
fi
echo -e Press enter to begin the final sync!
read
rsyncupgrade
#stop services
if [ $stopservices ]; then
echo -e "${white}Stopping Services...${noclr}"
/etc/init.d/httpd stop
/etc/init.d/qmail stop
/etc/init.d/exim stop
else
echo -e "${white}Not stopping services...${noclr}"
fi
#do the sync
databasefinalsync
finalrsynchomedirs
rsyncemail
#copy the dns info back
if [ $copydns ]; then
echo -e "${white}Copying zone file information over from new server...${noclr}"
if [ `ssh -q $target -p$port "ls $tmpfolder/ | grep ^mapfile.txt$"` ]; then
rsync -avHPe "ssh -q -p$port" $target:$tmpfolder/mapfile.txt $tmpfolder/
mv /var/named/chroot/etc/named.conf{,.`date +%F.%T`.bak}
cp -a /var/named/chroot/etc/named.conf{.default,} #this fixes some errors with named not starting after final sync.
cat $tmpfolder/mapfile.txt | grep "\->" | awk '{print "mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -D psa -e \"UPDATE dns_recs SET val=\\\""$1"\\\", displayVal=\\\""$1"\\\" WHERE val=\\\""$4"\\\"\""}' | sh
mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'SELECT name FROM domains' | awk '{print "/usr/local/psa/admin/sbin/dnsmng update " $1 }' | sh
/etc/init.d/named restart
else
echo -e "${red}The IP mapping file ${white}($tmpfolder/mapfile.txt)${red} was not found on the target server! please update the DNS records on your own!${noclr}"
sleep 3
mapfilefail=1
fi
fi
#restart services
if [ $restartservices ]; then
echo -e "${white}Restarting services...${noclr}"
/etc/init.d/httpd start
/etc/init.d/qmail start
/etc/init.d/exim start
else
echo -e "${white}Skipping restart of services...${noclr}"
fi
#display mysqldump errors
if [ -s /tmp/mysqldump.log ]; then
echo -e
echo -e "${red}Errors detected during mysqldumps:${noclr}"
cat /tmp/mysqldump.log
echo -e "${red}End of errors from /tmp/mysqldump.log.${noclr}"
sleep 1
fi
echo -e '${green}Final sync complete, check the screen "dbsync" on the remote server to ensure all databases imported correctly.${noclr}'
if [ $mapfilefail ]; then
echo -e "${red}The mapfile on the target server could not be found! Please remember to update DNS!${noclr}"
fi
removekey
}
databasefinalsync() { #perform just a database sync, reimporting on the target side.
echo -e "${white}Dumping the databases...${noclr}"
test -d $tmpfolder/dbdumps && mv $tmpfolder/dbdumps{,.`date +%F.%T`.bak}
mkdir -p $tmpfolder/dbdumps
ssh -q $target -p$port 'test -d $tmpfolder/dbdumps && mv $tmpfolder/dbdumps{,.`date +%F.%T`.bak}'
mysqldumpver=`mysqldump --version |cut -d" " -f6 |cut -d, -f1`
for db in `mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e "show databases" | egrep -v "^(psa|mysql|horde|information_schema|phpmyadmin.*)$"`; do
mysqldumpfunction
done
#move the dumps to the new server
rsync -avHlPze "ssh -q -p$port" $tmpfolder/dbdumps $target:$tmpfolder/
#start import of databases in screen on target
dbsyncscript
}
mysqldumpfunction() { #argument is 'db' provided by a loop
echo -e "Dumping $db"
if [[ $mysqldumpver < 5.0.42 ]]; then
mysqldump -u admin -p$(cat /etc/psa/.psa.shadow) --add-drop-table $db > $tmpfolder/dbdumps/$db.sql
mysqldump -u admin -p$(cat /etc/psa/.psa.shadow) --force --add-drop-table --log-error=/tmp/mysqldump.log $db > $tmpfolder/dbdumps/$db.sql
fi
}
dbsyncscript () { #create a script to restore the databases on the target server, then run it there in a screen
cat > /var/dbsync.sh <<'EOF'
#!/bin/bash
TMPFOLDER=/var/migrationtemp
LOG=$TMPFOLDER/dbdumps/dbdumps.log
if [ -d $TMPFOLDER/dbdumps ]; then
cd $TMPFOLDER/dbdumps
touch $LOG
echo "Dump dated `date`" > $LOG
test -d $TMPFOLDER/prefinalsyncdbs && mv $TMPFOLDER/prefinalsyncdbs{,.`date +%F.%R`.bak}
mkdir $TMPFOLDER/prefinalsyncdbs
for each in `ls *.sql|cut -d. -f1`; do
echo "dumping $each" |tee -a $LOG
(mysqldump -u admin -p$(cat /etc/psa/.psa.shadow) $each > $TMPFOLDER/prefinalsyncdbs/$each.sql) 2>>$LOG
echo " importing $each" | tee -a $LOG
(mysql -u admin -p$(cat /etc/psa/.psa.shadow) $each < $TMPFOLDER/dbdumps/$each.sql) 2>>$LOG
done
echo "Finished, hit a key to see the log."; read
less $LOG
else
echo "$TMPFOLDER/dbdumps not found"; read
fi
EOF
rsync -aHPe "ssh -q -p$port" /var/dbsync.sh $target:/var/
ssh -q $target -p$port "screen -S dbsync -d -m bash /var/dbsync.sh" &
echo -e "${white}Databases are importing in a screen on the target server. Be sure to check there to make sure they all imported OK.${noclr}"
sleep 2
}
rsynchomedirs() { #sync the docroots of all users, exluding the conf folder. the conf folder holds ip-specific data, which we do not want to migrate.
for each in `mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns psa -e "select name from domains;"`; do
if [ `ssh -q $target -p$port "ls /var/www/vhosts/ | grep ^$each$"` ]; then
echo -e "${purple}Syncing data for ${white}$each${purple}...${noclr}"
rsync -avHPe "ssh -q -p$port" /var/www/vhosts/$each root@$target:/var/www/vhosts/ --exclude=conf >> $logfile 2>&1
rsync -avHPe "ssh -q -p$port" /var/www/vhosts/$each/httpdocs root@$target:/var/www/vhosts/$each/ --update >> $logfile 2>&1
else
echo -e "${red}$each did not restore remotely${noclr}"
echo -e $each >> $didnotrestore
fi
done
}
finalrsynchomedirs() { #as with rsynchomedirs(), but without remote home check.
for each in `mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns psa -e "select name from domains;"`; do
echo -e "${purple}Syncing data for ${white}$each${purple}...${noclr}"
rsync -avHPe "ssh -q -p$port" /var/www/vhosts/$each root@$target:/var/www/vhosts/ --update --exclude=conf >> $logfile 2>&1
rsync -avHPe "ssh -q -p$port" /var/www/vhosts/$each/httpdocs root@$target:/var/www/vhosts/$each/ --update >> $logfile 2>&1
done
}
rsyncemail() { #rsync the whole mail folder
echo -e "${white}Syncing email...${noclr}"
rsync -avHPe "ssh -q -p$port" /var/qmail/mailnames/ root@$target:/var/qmail/mailnames/ --update >> $logfile 2>&1
}
#==============
#end final sync
#=================
#presync functions
#=================
dnscheck() { #check the current DNS settings based on the defined domain list
echo -e "${purple}Checking Current dns...${noclr}"
if [ -f /root/dns.txt ]; then
echo -e "Found /root/dns.txt"
sleep 2
cat /root/dns.txt | sort -n +3 -2 | more
else
if [[ `which dig` ]]; then
yum -y install bind-utils
fi
domainlist=`mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'select name from domains'`
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
echo -e "${yellow}Enter to continue...${noclr}"
read
}
rsyncupgrade () { #upgrade rsync to v3.0 if not already there
RSYNCVERSION=`rsync --version |head -n1 |awk '{print $3}'`
rsyncmajor=`echo $RSYNCVERSION |cut -d. -f1`
if [[ $rsyncmajor -lt 3 ]]; then
echo -e "${purple}Updating rsync...${noclr}"
LOCALCENT=`cat /etc/redhat-release |awk '{print $3}'|cut -d '.' -f1`
LOCALARCH=`uname -i`
if [[ $LOCALCENT -eq 6 ]]; then
rpm -Uvh http://migration.sysres.liquidweb.com//rsync/rsync-3.0.9-1.el6.rfx.$LOCALARCH.rpm
elif [[ $LOCALCENT -eq Enterprise ]]; then
echo -e "${red}RHEL detected! Skipping upgrade of rsync...${noclr}"
else
rpm -Uvh http://migration.sysres.liquidweb.com//rsync/rsync-3.0.0-1.el$LOCALCENT.rf.$LOCALARCH.rpm
fi
else
echo -e "${purple}Rsync already up to date.${noclr}"
fi
}
lowerttls() { #lower the TTLs of the DNS records in the psa database, and reload each domain through dnsmng
echo -e "${purple}Lowering TTL values on local server...${noclr}"
mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -e "insert into misc values ('soa_TTL','300');"
mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -e "update psa.dns_zone set ttl_unit=1, ttl=300;"
mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'select name from domains' | awk '{print "/usr/local/psa/admin/sbin/dnsmng --update " $1 }' | sh
}
getip() { #determine if the target server variable exists from a previous migration
echo -e "${purple}Getting Ip for destination server...${noclr}"
#check for previous migration
ipfile=/root/dest.ip.txt
if [ -f $ipfile ]; then
target=`cat $ipfile`
echo -e "Ip from previous migration found: $target"
getport
if yesNo "Is $target the server you want? Otherwise enter No to input new ip." ; then
echo -e "Ok, continuing with $target"
sshkeygen
else
rm -f /root/dest.port.txt
ipask
fi
else
ipask
fi
sleep 1
}
ipask() { #get the target IP variable
echo -e
echo -n 'Enter destination server IP: ';
read target
echo $target > $ipfile
getport
sshkeygen
}
getport() { #get the target server ssh port
echo -e "Getting ssh port..."
if [ -s /root/dest.port.txt ]; then
port=`cat /root/dest.port.txt`
echo -e "Previous Ssh port found: $port"
else
echo -n "Enter destination server SSH port [default 22]: "
read port
fi
if [ -z $port ]; then
echo -e "No port given, assuming 22"
port=22
fi
echo $port > /root/dest.port.txt
}
sshkeygen() { #quietly create an ssh key if it does not exist and copy it to the remote server
echo -e "${purple}Generating SSH keys...${noclr}"
if ! [ -f ~/.ssh/id_rsa ]; then
ssh-keygen -q -N "" -t rsa -f ~/.ssh/id_rsa
fi
echo -e "${purple}Copying key to remote server...${noclr}"
cat ~/.ssh/id_rsa.pub | ssh -q $target -p$port "cp -rp ~/.ssh/authorized_keys{,.syncbak} ; mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
ssh -q $target -p$port "echo \'Connected!\'; cat /etc/hosts| grep $target "
}
licensecheck() { #check the license of the target server to make sure it has enough domains in it
echo -e "${purple}Checking the target for a sufficiently permissive license key...${noclr}"
numdomains=`mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns psa -e "select name from domains;" | wc -l`
targetlimdom=`ssh -q $target -p$port "/usr/local/psa/bin/keyinfo --list | grep lim_dom\:" | awk '{print $2}'`
echo -e "${white}Source server has $numdomains domains, target license permits $targetlimdom domains.${noclr}" | sed -e 's/\-1/unlimited/g'
if [[ $targetlimdom -eq -1 ]]; then
echo -e "${green}Target license allows for unlimited domains, and can hold all these limes!${noclr}"
elif [[ $numdomains -le $targetlimdom ]]; then
echo -e "${green}Target license seems to have a large enough key to hold all these limes.${noclr}"
else
echo -e "${red}Target server probably cannot hold all these limes! Please upgrade target server license key!${noclr}"
fi
if yesNo "Continue with migration?"; then
echo -e Carrying on...
else
echo -e Do not want...
exit 0
fi
}
ipcheck() { #check to ensure that there are the necessary number of IPs on the target machine
echo -e "${purple}Checking for a sufficient number of IPs...${noclr}"
shared=`/usr/local/psa/bin/ipmanage -l | grep " S " | wc -l`
exclusive=`/usr/local/psa/bin/ipmanage -l | grep " E " | wc -l`
targetshared=`ssh -q $target -p$port "/usr/local/psa/bin/ipmanage -l | grep ' S ' | wc -l"`
targetexclusive=`ssh -q $target -p$port "/usr/local/psa/bin/ipmanage -l | grep ' E ' | wc -l"`
#compare the number of ips on the source and the target machines
echo -e "
${white}On the source machine, there are $shared shared IPs and $exclusive exclusive IPs available.${noclr}"
if [[ $targetshared -ge $shared && $targetexclusive -ge $exclusive ]]; then
echo -e "${white}There are also $targetshared shared IPs on the target machine, and $targetexclusive exclusive IPs. ${green}This seems like it will work.${noclr}"
else
echo -e "${white}But, there are only $targetshared shared IPs and $targetexclusive exclusive IPs on the target machine. ${red}That doesn't seem like it will work.${noclr}"
fi
if yesNo "Do these numbers seem ok to you? Yes to continue, no to exit and recheck things before proceeding."; then
echo -e Proceeding with migration.
else
echo -e Exiting.
exit 0
fi
}
autoinstaller() { #get plesk version on target machine and install pmm
echo -e "${purple}Installing PMM and other utilities on remote server...${noclr}"
ssh -q $target -p$port "/usr/local/psa/admin/bin/autoinstaller --select-release-current --install-component pmm --install-component horde --install-component mailman --install-component backup"
echo -e "${purple}Starting PMM install on the local server...${noclr}"
/usr/local/psa/admin/bin/autoinstaller --select-release-current --install-component pmm --install-component backup
}
phpupgrade() { #check for php 5.2.x and downgrade target if necessary.
echo -e "${purple}Checking for PHP 5.2.x...${noclr}"
phpvernum=`php -v | head -n1 | awk '{print $2}'`
tgphpvernum=`ssh -q $target -p$port "php -v" | head -n1 | awk '{print $2}'`
if [[ `echo $phpvernum | awk -F. '{print $1}'` -eq 5 && `echo $phpvernum | awk -F. '{print $2}'` -eq 2 ]]; then
echo -e "${blue}PHP 5.2.x detected! Current version on source server is $phpvernum.${noclr}"
echo -e "${blue}Current version on target server is $tgphpvernum.${noclr}"
if yesNo "Downgrade php on target server?"; then
echo -e "${purple}Downgrading php to 5.2.17 on target...${noclr}"
ssh -q $target -p$port "rpm -qa | grep atomic |xargs rpm -e ; wget -q -O - http://www.atomicorp.com/installers/atomic.sh | sh "
targetcent=`ssh -q $target -p$port "cat /etc/redhat-release" | awk '{print $3}' | cut -d. -f1`
if [ $targetcent == 6 ]; then
ssh -q $target -p$port "yum -y downgrade php-5.2.17-1.el6.art.x86_64 php-cli-5.2.17-1.el6.art php-common-5.2.17-1.el6.art php-devel-5.2.17-1.el6.art php-gd-5.2.17-1.el6.art php-imap-5.2.17-1.el6.art php-ncurses-5.2.17-1.el6.art php-mcrypt-5.2.17-1.el6.art php-mbstring-5.2.17-1.el6.art php-mysql-5.2.17-1.el6.art php-pdo-5.2.17-1.el6.art php-xml-5.2.17-1.el6.art php-pear php-snmp-5.2.17-1.el6.art php-xmlrpc-5.2.17-1.el6.art; yum -y install php-devel-5.2.17-1.el6.art; /etc/init.d/httpd restart"
elif [ $targetcent == 5 ]; then
ssh -q $target -p$port "yum -y install php-5.2.17-1.el5.art.x86_64 php-common-5.2.17-1.el5.art php-gd-5.2.17-1.el5.art php-imap-5.2.17-1.el5.art php-mbstring-5.2.17-1.el5.art php-mysql-5.2.17-1.el5.art php-pdo-5.2.17-1.el5.art php-xml-5.2.17-1.el5.art; /etc/init.d/httpd restart"
else
echo -e"{$red}Could not determine target OS version! Update this script or use a real OS!${noclr}"
fi
echo -e "${purple}Target server is now using PHP `ssh -q $target -p$port "php -v" | head -n1 | awk '{print $2}'`.${noclr}"
else
echo -e Keeping target version at $tgphpvernum.
fi
else
echo -e PHP 5.2.x not detected.
fi
}
foldersetup() { #set up tmpfolder on both servers
test -d $tmpfolder && mv $tmpfolder{,.`date +%F.%T`.bak}
mkdir -p $tmpfolder/dbdumps
ssh -q -p$port root@$target "test -d $tmpfolder && mv $tmpfolder{,.`date +%F.%T`.bak}"
ssh -q -p$port root@$target "mkdir $tmpfolder"
}
#===================
#end presync scripts
#===================
#start regular sync
#==================
domainexistcheck() { #make sure that domains that exist on the source do not already exist on target. takes input.
echo -e "${purple}Checking for coincidental domains on target server...${noclr}"
catpreexisting=0
for each in $*; do
if [ `ssh -q $target -p$port 'mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e "select name from domains;"' | grep ^$each$` ]; then
echo $each >> /root/preexisting.txt
catpreexisting=1
fi
done
}
checkcatpreexisting(){
if [[ $catpreexisting -eq 1 ]]; then
echo -e "${red}Coincidental domains found between source and target!${noclr}"
cat /root/preexisting.txt
if yesNo "If you continue with the migration, data in these domains will be overwritten! Do you want to continue?"; then
echo -e Continuing...
else
echo -e Exiting...
exit 0
fi
else
echo -e "${green}No coincidental domains found.${noclr}"
fi
}
createbackup() { #verbosely (-v) make a configuration-only (-c) backup on the source server ($tmpfolder/backup.tar)
test -e $tmpfolder/backup.tar && mv $tmpfolder/backup.tar{,.`date +%F.%T`.bak}
echo -e "${purple}Generating full backup to $tmpfolder/backup.tar...${noclr}"
if [[ `cat /usr/local/psa/version | awk '{print $1}' | cut -c1` -eq 8 ]]; then
echo -e "Plesk 8 detected!"
/usr/local/psa/bin/pleskbackup -cvz all $tmpfolder/backup.tar
else
/usr/local/psa/bin/pleskbackup server -v -c --skip-logs --output-file=$tmpfolder/backup.tar
fi
}
syncbackup() { #copy the backup to the target server
echo -e "${purple}Copying backup to the target server...${noclr}"
ssh -q $target -p$port "test -e $tmpfolder/backup.tar && mv $tmpfolder/backup.tar{,.`date +%F.%T`.bak}"
rsync -avHPe "ssh -q -p$port" $tmpfolder/backup.tar $target:$tmpfolder/
}
ipmaptool () { #this will set the ip mapping to $target server ips
echo -e "${purple}Setting IP mapping on target server...${noclr}"
ssh -q $target -p$port "test -e $tmpfolder/mapfile.txt && mv $tmpfolder/mapfile.txt{,.`date +%F.%T`.bak}"
ssh -q $target -p$port "/usr/local/psa/bin/pleskrestore --create-map $tmpfolder/backup.tar -map $tmpfolder/mapfile.txt -ignore-sign"
echo -e "${white}Target machine mapped addresses as follows:${noclr}"
ssh -q $target -p$port "cat $tmpfolder/mapfile.txt" | grep '\->'
if yesNo "Does this look alright to you?"; then
echo -e "${white}Moving on... everything should be hands-off until the sync is complete.${noclr}"
sleep 3
else
echo -e "${red}Go ahead and edit the map file ($tmpfolder/mapfile.txt) on the target server now. I'll wait...${noclr}"
read
echo -e "${white}Continuing... everything should be hands-off until the sync is complete.${noclr}"
sleep 3
fi
}
restore() { #The actual restoration function. will restore the configuration backup at a server level, to include all resellers, clients and domains.
echo -e "${purple}Restoring configuration file...
This part may not be completely verbose...${noclr}"
numdomains=`mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns psa -e "select name from domains;" | wc -l`
#targetmysqlpass=`ssh -q $target "cat /etc/psa/.psa.shadow"`
#ssh -q $target -p$port "test -f /root/.my.cnf && cp -a /root/.my.cnf{,.migrationbak}"
#ssh -q $target -p$port "echo '[client]
#user=admin
#pass=$targetmysqlpass' >> /root/.my.cnf" # this will get passwordless mysql logins temporarily, only way to check the database on the remote server.
#restoreprogress &
#MYSELF=$!
ssh -q $target -p$port "/usr/local/psa/bin/pleskrestore --restore $tmpfolder/backup.tar -level server -map $tmpfolder/mapfile.txt -verbose -ignore-sign"
#kill $MYSELF &> /dev/null
echo -e "${green}
Restore completed! ${purple}Testing restored domain list...${noclr}"
targetnumdomains=`ssh -q $target -p$port 'mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns psa -e "select name from domains;"' | wc -l`
#ssh -q $target -p$port "test -f /root/.my.cnf.migrationbak && mv -f /root/.my.cnf{.migrationbak,}"
echo -e "
${white}There are $numdomains domains on the source server and $targetnumdomains domains on the target server.${noclr}"
#if yesNo "Does this make sense to you?"; then
echo -e Carrying on!
databasefinalsync
rsynchomedirs
rsyncemail
# else
# echo Problem right here... exiting!
# sleep 1
# exit
#fi
}
restoreprogress() { #show the progress of the restore in terms of number of domains restored
while true; do
numrestored=`ssh -q $target -p$port 'mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e "select name from domains" | wc -l'`
echo -ne "${white}Restored ${noclr}$numrestored/$numdomains...\r"
sleep 6
done
}
#================
#end regular sync
#=================
#start client sync
#=================
clientcheck() { #Check the $clientlistfile against real subscriptions on the source server
clientlist=`mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'SELECT c.login, d.name FROM clients AS c JOIN domains AS d ON d.cl_id = c.id;'`
echo -e "${purple}Checking $clientlistfile for bad entries...${noclr}"
rm -f /var/clientcheckfail.txt
for client in `cat $clientlistfile`; do
if [[ ! `echo $clientlist | grep $client` ]]; then
echo $client > /var/clientcheckfail.txt
fi
done
if [[ -f /var/clientcheckfail.txt ]]; then
echo -e "${red}Found non-client entries in $clientlistfile:${noclr}
"
cat /var/clientcheckfail.txt
echo -e "
${red}Please double check these lines and remove them if necessary.${noclr}"
if yesNo "Override bad entries check? Script will fail for invalid users."; then
echo -e "Moving on."
else
echo -e "Exiting"
exit
fi
else
echo -e "${green}Looks good. Moving on.${noclr}"
fi
}
syncadmin() { #sync domains owned by the admin user, which cannot be backed up at a client level
domlistfile=/var/admindomains.txt
mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'SELECT c.login, d.name FROM clients AS c JOIN domains AS d ON d.cl_id = c.id;' | grep ^admin\s* | awk '{print $2}' | sort > $domlistfile
subcheck
syncdomains
}
syncclients() { #make a backup per client and restore on the target machine
for client in `cat $clientlistfile | grep -v ^admin$`; do
clientdomains=`mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'SELECT c.login, d.name FROM clients AS c JOIN domains AS d ON d.cl_id = c.id;' | grep ^$client\s* | awk '{print $2}' | sort`
echo -e "${purple}Checking for coincidental domains owned by ${white}$client${purple}...${noclr}"
domainexistcheck $clientdomains
if [[ $catpreexisting -eq 1 ]]; then
echo -e "${red}Conflicting domains found on target server!${noclr}"
cat /root/preexisting.txt
echo -e "${red}Skipping copy of ${white}$client${red}!! Check $didnotbackup after migration is complete${noclr}"
echo $client >> $didnotbackup
else
echo -e "${purple}Backing up ${white}$client${purple}...${noclr}"
/usr/local/psa/bin/pleskbackup clients-name $client -c --skip-logs --output-file=$tmpfolder/backup.$client.tar
if [[ -f $tmpfolder/backup.$client.tar ]]; then
echo -e "${purple}Transferring $client and making mapfile...${noclr}"
rsync -aHPe "ssh -q -p$port" $tmpfolder/backup.$client.tar root@$target:$tmpfolder/
ssh -q -p$port root@$target "/usr/local/psa/bin/pleskrestore --create-map $tmpfolder/backup.$client.tar -map $tmpfolder/backup.$client.map -ignore-sign"
echo -e "${purple}Executing restore of $client (this can take a while, please be patient...)${noclr}"
ssh -q -p$port root@$target "/usr/local/psa/bin/pleskrestore --restore $tmpfolder/backup.$client.tar -map $tmpfolder/backup.$client.map -level clients -ignore-sign"
restoredtest=`echo $clientdomains | awk '{print $1}'`
restored=`ssh -q -p$port root@$target 'mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e "select name from domains"' | grep ^$restoredtest$` #check to see if a domain exists in target psa db to test restore
if [ $restored ]; then
echo -e "${green}$client restored ok. ${purple}Syncing data...${noclr}"
syncclidatabases
syncclidocroot
syncclimail
else
echo -e "${red}$client did not seem to restore correctly!${noclr}"
echo $client >> $didnotrestore
fi
else
echo -e "${red}Backup of $client failed. Does this client really exist?${noclr}"
echo $client >> $didnotbackup
fi
fi
done
}
syncclidatabases() { # option A, lots of imports
echo -e "${purple}Determining databases for sync...${noclr}"
domdatabases=`mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns psa -e "SELECT domains.name AS domain_name, data_bases.name AS database_name, clients.login FROM data_bases, clients, domains WHERE data_bases.dom_id = domains.id AND domains.cl_id = clients.id ORDER BY domain_name;" | grep [[:space:]]$client$ | awk '{print $2}' | sort -u`
for db in $domdatabases; do
echo -e "Dumping $db for $client..."
mysqldump -u admin -p$(cat /etc/psa/.psa.shadow) $db > $tmpfolder/dbdumps/$db.sql
done
echo -e "${green}Databases for $client complete.${purple} Copying to target server...${noclr}"
rsync -avHPe "ssh -q -p$port" $tmpfolder/dbdumps root@$target:$tmpfolder/ --update
echo $domdatabases >> $tmpfolder/databaselist.txt
}
syncclidocroot() { #sync the docroots tied to a particular client
echo -e "${purple}Syncing docroots for $client...${noclr}"
for each in $clientdomains; do
echo -e $each
if [[ -d /var/www/vhosts/$each ]]; then
rsync -avHPe "ssh -q -p$port" /var/www/vhosts/$each root@$target:/var/www/vhosts/ --exclude=conf >> $logfile 2>&1
rsync -avHPe "ssh -q -p$port" /var/www/vhosts/$each/httpdocs root@$target:/var/www/vhosts/$each/ --update >> $logfile 2>&1
fi
done
}
syncclimail() { #sync mail tied to a particular subscription
echo -e "${purple}Syncing mail for $client...${noclr}"
for each in $clientdomains; do
echo -e $each
if [[ -d /var/qmail/mailnames/$each ]]; then
rsync -avHPe "ssh -q -p$port" /var/qmail/mailnames/$each root@$target:/var/qmail/mailnames/ >> $logfile 2>&1
fi
done
}
#===============
#end client sync
#======================
#start domain list sync
#======================
subcheck() { #Check the $domlistfile against real subscriptions on the source server
if [[ `cat /usr/local/psa/version | awk '{print $1}' | cut -c1` -eq 8 ]]; then
echo -e "Plesk 8 detected! I hope you selected the right domains 'cause I can't check!"
sleep 5
else
sublist=`mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'SELECT name FROM domains JOIN Subscriptions ON Subscriptions.object_id = domains.id;'`
echo -e "${purple}Checking $domlistfile for bad entries...${noclr}"
rm -f /var/subcheckfail.txt
for domain in `cat $domlistfile`; do
if [[ ! `echo $sublist | grep $domain` ]]; then
echo $domain > /var/subcheckfail.txt
fi
done
if [[ -f /var/subcheckfail.txt ]]; then
echo -e "${red}Found non-subscription domains in $domlistfile:${noclr}
"
cat /var/subcheckfail.txt
echo -e "
${red}Please double check these lines and remove them if necessary. If the source server is less than plesk 11, this test might not work at all...${noclr}"
if yesNo "Override bad entries check? Script will fail for invalid domains."; then
echo -e "Moving on."
else
echo -e "Exiting"
exit
fi
else
echo -e "${green}Looks good. Moving on.${noclr}"
fi
fi
}
syncdomains() { #make a backup per subscription and restore on the target machine
for domain in `cat $domlistfile`; do
echo -e "${purple}Backing up ${white}$domain${purple}...${noclr}"
/usr/local/psa/bin/pleskbackup domains-name $domain -c --skip-logs --output-file=$tmpfolder/backup.$domain.tar
if [[ -f $tmpfolder/backup.$domain.tar ]]; then
echo -e "${purple}Transferring $domain and making mapfile...${noclr}"
rsync -aHPe "ssh -q -p$port" $tmpfolder/backup.$domain.tar root@$target:$tmpfolder/
ssh -q -p$port root@$target "/usr/local/psa/bin/pleskrestore --create-map $tmpfolder/backup.$domain.tar -map $tmpfolder/backup.$domain.map -ignore-sign"
echo -e "${purple}Executing restore of $domain (this can take a while, please be patient...)${noclr}"
ssh -q -p$port root@$target "/usr/local/psa/bin/pleskrestore --restore $tmpfolder/backup.$domain.tar -map $tmpfolder/backup.$domain.map -level domains -ignore-sign"
restored=`ssh -q -p$port root@$target 'mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e "select name from domains"' | grep ^$domain$` #check to see if domain exists in psa db to test restore
if [ $restored ]; then
echo -e "${green}$domain restored ok. ${purple}Syncing data...${noclr}"
determineowned
syncdomdatabases
syncdomdocroot
syncdommail
else
echo -e "${red}$domain did not seem to restore correctly!${noclr}"
echo $domain >> $didnotrestore
fi
else
echo -e "${red}Backup of $domain failed. Is this domain a subscription?${noclr}"
echo $domain >> $didnotbackup
fi
done
}
syncdomdatabases() { #determine databases for a domain and copy them across, appending a list file for later mass restoration.
echo -e "${purple}Determining databases for sync...${noclr}"
for owned in $domainowned; do
domdatabases=`mysql -u admin -p$(cat /etc/psa/.psa.shadow) -Ns psa -e "SELECT domains.name AS domain_name, data_bases.name AS database_name FROM data_bases, domains WHERE data_bases.dom_id = domains.id ORDER BY domain_name;" | grep ^$owned | awk '{print $2}' | sort -u`
for db in $domdatabases; do
echo -e "Dumping $db for $owned..."
mysqldump -u admin -p$(cat /etc/psa/.psa.shadow) $db > $tmpfolder/dbdumps/$db.sql
done
echo -e "${green}Databases for $owned complete.${purple} Copying to target server...${noclr}"
rsync -avHPe "ssh -q -p$port" $tmpfolder/dbdumps root@$target:$tmpfolder/
echo $domdatabases >> $tmpfolder/databaselist.txt
done
}
determineowned() { #determine which domains are owned by a subscription based on the backup file, depends on $domain being set
domainowned=`tar -Oxf $tmpfolder/backup.$domain.tar *.xml | grep www\= | tr [:space:] '\n' | grep name | cut -d\" -f2 | sort -u`
}
syncdomdocroot() { #sync the docroots tied to a particular subscription
echo -e "${purple}Syncing docroots for $domain...${noclr}"
for each in $domainowned; do
echo $each
if [[ -d /var/www/vhosts/$each ]]; then
rsync -avHPe "ssh -q -p$port" /var/www/vhosts/$each root@$target:/var/www/vhosts/ --exclude=conf >> $logfile 2>&1
rsync -avHPe "ssh -q -p$port" /var/www/vhosts/$each/httpdocs root@$target:/var/www/vhosts/$each/ --update >> $logfile 2>&1
fi
done
}
syncdommail() { #sync mail tied to a particular subscription
echo -e "${purple}Syncing mail for $domain...${noclr}"
for each in $domainowned; do
echo -e $each
if [[ -d /var/qmail/mailnames/$each ]]; then
rsync -avHPe "ssh -q -p$port" /var/qmail/mailnames/$each root@$target:/var/qmail/mailnames/ >> $logfile 2>&1
fi
done
}
#====================
#end domain list sync
#====================
#start postsync
#==============
dnrcheck() { #Checks to see if there is anything in didnotrestore.txt and displays it here
if [ -f $didnotrestore ]; then
echo -e "${red}Some domains may not have restored:${noclr}"
cat $didnotrestore
echo -e "${yellow}Enter to continue...${noclr}"
read
fi
if [[ $didnotbackup && -f $didnotbackup ]]; then
echo -e "${red}Some domains may not have backed up properly:${noclr}"
cat $didnotbackup
echo -e "${yellow}Enter to continue...${noclr}"
read
fi
}
hostsfile() { #Generate a hosts file on the target machine
echo -e "${purple}Generating hosts file...${noclr}"
cat > /var/pleskhosts.sh <<'EOF'
#!/bin/bash
#awalilko@lw 01/15/13
hostsfile=/var/www/vhosts/default/htdocs/hostsfile.txt
althostsfile=/var/www/vhosts/default/htdocs/hosts.txt
ip=`/usr/local/psa/bin/ipmanage -l | grep " S " | head -n1 | cut -d\: -f2 | cut -d\/ -f1`
#backup hosts file if it exists
if [ -s $hostsfile ]; then
mv $hostsfile{,.bak}
fi
if [ -s $althostsfile ]; then
mv $althostsfile{,.bak}
fi
#create one line for each domain and subdomain
echo "Generating entries..."
for domain in `mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e 'select name from domains' | sort -u`; do
domip=`/usr/local/psa/bin/domain --info $domain | grep address | cut -d\: -f2 | sed -e 's/^[ \t]*//'`;
if [[ $domip ]]; then
echo $domip $domain www.$domain >> $hostsfile;
fi
done
echo "Done!"
#create one line per ip for ipv4, then ipv6
echo "Generating alternate file..."
for domip in `cat $hostsfile | cut -d\ -f1 | sort -u | grep -v [a-zA-Z]`; do
echo $domip `cat $hostsfile | grep $domip | cut -d\ -f2-3 | tr '\n' ' '` >> $althostsfile;
done
for domip in `cat $hostsfile | cut -d\ -f1 | sort -u | grep :`; do
echo $domip `cat $hostsfile | grep $domip | cut -d\ -f2-3 | tr '\n' ' '` >> $althostsfile;
done
echo "Done!"
#correct server to accept requests by IP
echo "Correcting for ModSec..."
if [ -s /etc/httpd/modsecurity.d ]; then
grep 960017 /etc/httpd/modsecurity.d/* -Rl | xargs sed -i '/960017/s/^/\#/g';
service httpd restart;
echo "ModSec corrected! All Set!"
else
echo "ModSec not found! All set!"
fi
#determine if there is a default domain and place the files there
echo "Ensuring that the correct folder is used for the default IP..."
domainfolder=`mysql psa -u admin -p$(cat /etc/psa/.psa.shadow) -Ns -e "select d.name from domains AS d join IP_Addresses as ipa where ipa.ip_address='$ip' and d.id=ipa.default_domain_id"`
if [[ $domainfolder ]]; then
cp $hostsfile /var/www/vhosts/$domainfolder/httpdocs/
cp $althostsfile /var/www/vhosts/$domainfolder/httpdocs/
echo "Copied files to default domain $domainfolder!"
else
echo "Server default folder should work!"
fi
#determine if there is a hostname account and copy the files to that docroot
if [ -d /var/www/vhosts/$(hostname) ]; then
cp $hostsfile /var/www/vhosts/$(hostname)/httpdocs/
cp $althostsfile /var/www/vhosts/$(hostname)/httpdocs/
fi
#output resulting file link
echo
cat $althostsfile
echo
echo "One line per IP at http://$ip/hosts.txt"
echo "One line per domain at http://$ip/hostsfile.txt"
echo
EOF
rsync -aHPe "ssh -q -p$port" /var/pleskhosts.sh $target:/var/
ssh -q $target -p$port "bash /var/pleskhosts.sh"
sleep 2
}