-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprimemover.sh
2079 lines (1482 loc) · 56.7 KB
/
primemover.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
# PrimeMover.io
# Universal WordPress VPS Migration Assistant
# Copyright 2018 PrimeMover.io - K. Patrick Gallagher
# Easily move WordPress sites between two different servers managed by GridPane, ServerPilot, RunCloud and Others...
# You'll need to already have manually built your sites at RunCloud and have WordPress successfully running there BEFORE trying to move sites in from other sources.
# ServerPilot site build code (via API) is already built but needs to be reintegrated to this work.
source ~/.bash_profile
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root, exiting!!!"
exit 1
fi
# Check is PV is installed, and install if needed...
if ! type "pv" > /dev/null; then
echo "PV was not installed... fixing..."
apt -y install pv
fi
mkdir -p /var/tmp/primemover
ipaddress=$(curl http://ip4.ident.me 2>/dev/null)
MigrateType=$1
MeImCounting() {
echo "This is all very VERY aplha right now. Use at your own risk."
echo " "
echo "All kinds of things might be broken. It's a work in progress and we'll get it hammered out shortly."
echo " "
echo "Please feel free to help out."
echo " "
echo "Best of luck! Drop me a line at patrick at gridpane dot com"
echo " "
echo "You need to have already created SSH keys on both your source server and your destination server and shared them between the two."
echo " "
echo "This all automatically works if you're using GridPane because we (try, at least, to) kick all of the asses."
}
MeImCounting
CommandVariablesCheck() {
#THIS IS OLD AND WILL SOON BE KILLED!!!
#THIS IS OLD AND WILL SOON BE KILLED!!!
#THIS IS OLD AND WILL SOON BE KILLED!!!
# Checking correct startup variables
if [ -z "$1" ] || [ -z "$2" ]
then
echo " "
echo " "
echo " ***************************************"
echo "******* ERROR - MISSING VARIABLES *******"
echo " ***************************************"
echo " "
echo " "
echo "Command line variables required: "
echo " "
echo " 1.) URL/ALL "
echo " 2.) Target IP address "
echo " 3.) *OPTIONAL* Source API token (GridPane servers only - ServerPilot and RunCloud API support coming soon) "
echo " 4.) *OPTIONAL* Target API token (GridPane servers only - ServerPilot and RunCloud API support coming soon) "
# LOTS of work needs to be added in here to make this work seamlessly between SP and RC nodes, and between RC and RC, and between SP and SP, and... you get the point.
echo " "
echo " "
exit 187;
else
# Set all the primary variables - Additional Variables - REQUIRED - But we'll get these soon... appname username finaldomain
site_to_clone=$1
remote_IP=$2
sourcetoken=$3
targettoken=$3
fi
}
# Install WP-CLI - Makes everything so much easier!!!
CheckWPcli() {
if [ -f /usr/local/bin/wp ]
then
echo "WP-CLI is already installed, making sure it's the most current version..."
yes | wp cli update --allow-root
else
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
fi
if [ -f /usr/local/bin/wp-completion.bash ]
then
if grep -q "source /usr/local/bin/wp-completion.bash" ~/.bash_profile; then
echo "WP-CLI Bash Completion already set..."
else
echo "source /usr/local/bin/wp-completion.bash" >> ~/.bash_profile
fi
else
wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash
mv wp-completion.bash /usr/local/bin/wp-completion.bash
echo "source /usr/local/bin/wp-completion.bash" >> ~/.bash_profile
fi
}
CheckWPcli
ServerPilotShell() {
#Check if ServerPilot API is Installed...
if [ -f "/usr/local/bin/serverpilot" ]
then
echo "ServerPilot API Already Installed!"
sed -i 's/printf "%-20s"/printf "%-30s"/g' /usr/local/bin/serverpilot #Fixes the column bleed issue...Just making sure here!
source ~/.bash_profile
else
#install jq
sudo apt-get -y install jq
curl -sSL https://raw.githubusercontent.com/kodie/serverpilot-shell/master/lib/serverpilot.sh > /usr/local/bin/serverpilot
chmod a+x /usr/local/bin/serverpilot
sed -i 's/printf "%-20s"/printf "%-25s"/g' /usr/local/bin/serverpilot #Fixes the column bleed issue...
echo "Enter ClientID from ServerPilot Account..."
read clientID
echo "Enter API Key from ServerPilot Account..."
read APIkey
printf '\nexport serverpilot_client_id="'$clientID'"\nexport serverpilot_api_key="'$APIkey'"' >> ~/.bash_profile && source ~/.bash_profile
fi
}
# Check is SSH key present, if not make it so... This currently only applies to migrating IN to GridPane servers...
# Disabled by default because while I may be a total egotistical prick I recognize that you're MUCH more likely to be running on a SP or RC node than a GridPane managed box.
DoSSH() {
if [ "$y" = "1" ]
then
echo "An error was detected during a previous function, skipping the site packaging step for this site..."
return 1
fi
remote_IP=$1
ipaddress=$(curl http://ip4.ident.me 2>/dev/null)
if [ -f /root/.ssh/id_rsa.pub ]
then
echo "Local SSH Keys exist..."
echo ""
else
echo "We need to create a public key pair..."
echo "CREATING SSH!!!"
#echo -e "\n\n\n" | ssh-keygen -t rsa -b 4096
ssh-keygen -t rsa -b 4096 -N "" -f /root/.ssh/id_rsa
fi
echo "Checking remote connection to $remote_IP..."
sshcheck=$(ssh-keygen -F $remote_IP 2>&1)
if [ $? -eq 0 ]
then
echo "Remote host is already in the Known Hosts file..."
echo ""
else
echo "Adding remote system at $remote_IP..."
ssh-keyscan $remote_IP >> /root/.ssh/known_hosts
echo "Remote host $remote_IP added to the known hosts file..."
fi
sshtatus=$(ssh -o BatchMode=yes -o ConnectTimeout=5 $remote_IP echo ok 2>&1)
if [ "$sshtatus" == "ok" ]
then
echo "Remote SSH Test Successful..."
else
if [[ $MigrateType == "RC2GP" ]] || [[ $MigrateType == "SP2GP" ]] || [[ $MigrateType == "GP2GP" ]]
then
curl -F "ssh_key=@/root/.ssh/id_rsa.pub" -F "source_ip=$ipaddress" -F "failover_ip=$remote_IP" https://my.gridpane.com/api/pair-external-servers?api_token=$gridpanetoken
echo "Remote host added to the Known Hosts file..."
echo ""
else
echo "Attempting to connect to remote system at $remote_IP..."
ssh-copy-id -i ~/.ssh/id_rsa.pub root@$remote_IP
fi
fi
}
GetSPUserAppDetails() {
appholder=word$appnameCOL
appname=$(echo ${!appholder})
runholder=word$runtimeCOL
php=$(echo ${!runholder})
appidhold=word$appidCOL
appid=$(echo ${!appidhold})
serverhold=word$serveridCOL
serverid=$(echo ${!serverhold})
datehold=word$datecreatedCOL
datecreated=$(echo ${!datehold})
userhold=word$sysuserCOL
sysuserid=$(echo ${!userhold})
echo ""
echo ""
echo "Application Name/Folder is $appname"
echo "PHP Version is $php"
echo "User ID is $sysuserid"
serverpilot sysusers $sysuserid > /var/tmp/primemover/source-user-name.txt
serverCOL=$(awk -v name='serverid' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-user-name.txt)
#echo "serverid Column is $serverCOL"
usernameCOL=$(awk -v name='name' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-user-name.txt)
#echo "UserName Column is $usernameCOL"
userIDCOL=$(awk -v name='id' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-user-name.txt)
#echo "UserID Column is $userIDCOL"
sed '1d' /var/tmp/primemover/source-user-name.txt > /var/tmp/primemover/tmpfile; mv /var/tmp/primemover/tmpfile /var/tmp/primemover/source-user-name.txt
if [ $usernameCOL -eq 1 ]
then
currentuser=$(awk '{print $1}' /var/tmp/primemover/source-user-name.txt)
elif [ $usernameCOL -eq 2 ]
then
currentuser=$(awk '{print $2}' /var/tmp/primemover/source-user-name.txt)
else
currentuser=$(awk '{print $3}' /var/tmp/primemover/source-user-name.txt)
fi
currentuser=$(echo $currentuser|tr -d '\n')
echo "System User Name for this App is $currentuser"
}
PushToSP() {
#echo "Creating New System User $currentuser on Target Server $targetserver..."
targetID=$(serverpilot find servers lastaddress=$targetserver id)
if [[ $currentuser == "serverpilot" ]]
then
echo "Default serverpilot user already exists on remote system..."
serverpilot find sysusers serverid=$targetID > /var/tmp/primemover/new-server-users.txt
sed -r -n -e /$currentuser/p /var/tmp/primemover/new-server-users.txt > /var/tmp/primemover/new-user-details.txt
newuserIDCOL=$(awk -v name='id' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/new-server-users.txt)
#echo "New User ID Column is $newuserIDCOL"
if [[ $newuserIDCOL -eq 1 ]]
then
newuserID=$(awk '{print $1}' /var/tmp/primemover/new-user-details.txt)
elif [[ $newuserIDCOL -eq 2 ]]
then
newuserID=$(awk '{print $2}' /var/tmp/primemover/new-user-details.txt)
else
newuserID=$(awk '{print $3}' /var/tmp/primemover/new-user-details.txt)
fi
else
MakeSPUser
fi
echo "Packaging up site..."
#TARBALL THE SITE
PackageSite
echo "Default WP admin credentials are user $admin_user with email address $admin_email with pass $admin_password..."
echo "Getting ready to build site $appdomain for application $appname for user ID $newuserID on PHP version $php ..."
serverpilot apps create $appname $newuserID $php '["'$appdomain'","www.'$appdomain'"]' '{"site_title":"'$appname'","admin_user":"'$admin_user'","admin_password":"'$admin_password'","admin_email":"'$admin_email'"}'
echo "Waiting for remote site build to complete..." #Add error checking here by routing that ^^^ output to a variable and checking it
sleep 5
scp $sitepack root@$remote_IP:/srv/users/$currentuser/apps/$appname/primemover-$appname-migration-file.gz
sleep 1
echo "Running remote restoration process..."
if [[ $run == "1" ]]
then
ssh root@$remote_IP "sleep 3 && wget https://www.dropbox.com/s/1wpxv8kr9bfqz8i/primemover.sh && mv primemover.sh /usr/local/bin/primemover && chmod +x /usr/local/bin/primemover && sleep 1 && tar -xzf /srv/users/$currentuser/apps/$appname/primemover-$appname-migration-file.gz -C /srv/users/$currentuser/apps/$appname/public/ --overwrite && cd /srv/users/$currentuser/apps/$appname/public && echo $finaldomain > source.domain && primemover restore" < /dev/null
else
ssh root@$remote_IP "sleep 3 && wget https://www.dropbox.com/s/1wpxv8kr9bfqz8i/primemover.sh && mv primemover.sh /usr/local/bin/primemover && chmod +x /usr/local/bin/primemover && sleep 1 && tar -xzf /srv/users/$currentuser/apps/$appname/primemover-$appname-migration-file.gz -C /srv/users/$currentuser/apps/$appname/public/ --overwrite && cd /srv/users/$currentuser/apps/$appname/public && echo $finaldomain > source.domain && primemover restore" < /dev/null
fi
sleep 1
echo "Remote restoration done... right?"
}
GetSPApps() {
echo "These are all of the local apps we'll be moving..."
echo ""
cat /var/tmp/primemover/source-applications.txt
sysuserCOL=$(awk -v name='sysuserid' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-applications.txt)
runtimeCOL=$(awk -v name='runtime' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-applications.txt)
appnameCOL=$(awk -v name='name' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-applications.txt)
serveridCOL=$(awk -v name='serverid' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-applications.txt)
datecreatedCOL=$(awk -v name='name' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-applications.txt)
appidCOL=$(awk -v name='id' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/source-applications.txt)
}
PickTargetSP() {
serverpilot servers > /var/tmp/primemover/server-list.txt
echo "Here's our raw SP Server details for all connected nodes..."
cat /var/tmp/primemover/server-list.txt
serverCOL=$(awk -v name='name' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/server-list.txt)
#echo "Server Column is located: Column $serverCOL..."
ipaddressCOL=$(awk -v name='lastaddress' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/server-list.txt)
#echo "IP Address Column is located: Column $ipaddressCOL..."
idCOL=$(awk -v name='id' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/server-list.txt)
#echo "ID Column is located: Column $idCOL..."
awk -v col=name 'NR==1{for(i=1;i<=NF;i++){if($i==col){c=i;break}} print $c} NR>1{print $c}' /var/tmp/primemover/server-list.txt > /var/tmp/primemover/server-names.txt
awk -v col=lastaddress 'NR==1{for(i=1;i<=NF;i++){if($i==col){c=i;break}} print $c} NR>1{print $c}' /var/tmp/primemover/server-list.txt > /var/tmp/primemover/server-ips.txt
awk -v col=id 'NR==1{for(i=1;i<=NF;i++){if($i==col){c=i;break}} print $c} NR>1{print $c}' /var/tmp/primemover/server-list.txt > /var/tmp/primemover/server-ids.txt
echo ""
echo "Please keep in mind: THIS CAN POTENTIALLY BE DESTRUCTIVE!!!"
echo ""
echo "###########################################################"
echo "######## Beginning Migration and Provisioning... #########"
echo "######## Here are your available Servers #########"
echo "###########################################################"
echo ""
rownumber=0
cp /var/tmp/primemover/server-names.txt /var/tmp/primemover/server-names.tmp
sed -i -e "1d" /var/tmp/primemover/server-names.tmp
sed -i -e "1d" /var/tmp/primemover/server-ips.txt
sed -i -e "1d" /var/tmp/primemover/server-ids.txt
while IFS=" " read -r entrydetail
do
rownumber=$((rownumber+1))
currentIP=$(cat /var/tmp/primemover/server-ips.txt | awk '{print $"$ipaddressCOL"; exit}')
currentID=$(cat /var/tmp/primemover/server-ids.txt | awk '{print $"$idCOL"; exit}')
if [[ $currentIP == $ipaddress ]]
then
#Don't display this machine... it's obviously the source.
sourcerow=$((rownumber+1))
sourceserver=$entrydetail
sourceID=$currentID
sourceIP=$currentIP
else
echo "Server #$rownumber ... Named: $entrydetail ... with IP Address of $currentIP"
fi
sed -i -e "1d" /var/tmp/primemover/server-ips.txt
done < "/var/tmp/primemover/server-names.tmp"
echo ""
echo "Enter Target Server By Number..."
read targetServer
#targetServer=$((targetServer+1)) #Increment the server line number by 1 to accomodate the heading line within the source output
serveridsource=$(cat /var/tmp/primemover/server-ids.txt)
servernames=$(cat /var/tmp/primemover/server-names.txt)
targetID=$(echo "$serveridsource" | sed -n "$targetServer"p)
remote_IP=$(serverpilot find servers id=$targetID lastaddress)
echo ""
echo ""
echo "The target server has an ID of... $targetID... with IP Address $remote_IP"
echo ""
echo "The source server has an ID of... $sourceID... with IP Address $sourceIP"
echo ""
echo "These are all of the Source Applications on $sourceserver..."
echo ""
serverpilot find apps serverid=$(serverpilot find servers name=$sourceserver id) > /var/tmp/primemover/source-applications.txt
GetSPApps
# echo "systemuserid Column: $sysuserCOL - runtime Column: $runtimeCOL - AppName Column: $appnameCOL - ServerID Column: $serveridCOL - Date Column: $datecreatedCOL - AppID Column is $appidCOL"
echo ""
echo "To begin initializing ALL apps press ENTER... NOTE: User passwords will be reset on target node!"
read desiredapps
}
SPtoSP() {
# The intention here is to move sites from a ServerPilot node to another ServerPilot node
PickTargetSP
if [ -z "$desiredapps" ]
then
sed '1d' /var/tmp/primemover/source-applications.txt > /var/tmp/primemover/tmpfile; mv /var/tmp/primemover/tmpfile /var/tmp/primemover/source-applications.txt
echo "Copying ServerPilot Sites..."
run=0
while IFS=" " read -r word1 word2 word3 word4 word5 word6
do
((run++))
GetSPUserAppDetails
cd /srv/users/$currentuser/apps/$appname/public
if ! $(wp core is-installed --allow-root);
then
echo "This is not a valid WordPress install, skipping this app!"
else
echo "Proceeding..."
SingleSPDomain
echo "The Final Domain for this application is $finaldomain"
appdomain=$finaldomain
PushToSP
fi
done < "/var/tmp/primemover/source-applications.txt"
fi
}
MakeSPUser() {
echo "Creating New System User $currentuser on Target Server $targetserver..."
serverpilot sysusers create $targetserver $currentuser
serverpilot find sysusers serverid=$SPRemoteIP > /var/tmp/primemover/new-server-users.txt
sed -r -n -e /$currentuser/p /var/tmp/primemover/new-server-users.txt > /var/tmp/primemover/new-user-details.txt
newuserIDCOL=$(awk -v name='id' '{for (i=1;i<=NF;i++) if ($i==name) print i; exit}' /var/tmp/primemover/new-server-users.txt)
#echo "New User ID Column is $newuserIDCOL"
if [[ $newuserIDCOL -eq 1 ]]
then
newuserID=$(awk '{print $1}' /var/tmp/primemover/new-user-details.txt)
elif [[ $newuserIDCOL -eq 2 ]]
then
newuserID=$(awk '{print $2}' /var/tmp/primemover/new-user-details.txt)
else
newuserID=$(awk '{print $3}' /var/tmp/primemover/new-user-details.txt)
fi
randpass=$(openssl rand -base64 12)
echo "New User $currentuser on Server $targetserver has ID $newuserID"
serverpilot sysusers update $newuserID password $randpass
echo "... and now has new random password $randpass"
}
BuildSPSite() {
#MEH... This is gonna go.
serverpilot apps create $appname $newuserID $php '["'$appdomain'","www.'$appdomain'"]' '{"site_title":"'$appname'","admin_user":"'$admin_user'","admin_password":"'$admin_password'","admin_email":"'$admin_email'"}'
}
RCtoSP() {
echo "Confirming that we have the ServerPilot shell tools..."
ServerPilotShell
echo "Getting all local RunCloud site domains..."
rcDomains
echo ""
echo "Are we moving all of these sites to the same server? Please enter YES or NO..."
read SameSPforAll < /dev/tty
if [[ $SameSPforAll == "YES" ]] || [[ $SameSPforAll == "yes" ]] || [[ $SameSPforAll == "Yes" ]] || [[ $SameSPforAll == "Y" ]] || [[ $SameSPforAll == "y" ]]
then
SameServer="yes"
echo "What is the remote IP of the target ServerPilot server?"
read targetserver < /dev/tty
#remote_IP="$targetserver"
DoSSH $targetserver
else
echo "We'll gather a different IP address for each site during the migration..."
fi
while read -r appname site_to_clone username rootfolder count
do
echo "Starting with site $site_to_clone from $rootfolder..."
dots=$(echo "$site_to_clone" | awk -F. '{ print NF - 1 }')
if [[ $site_to_clone == "staging."* ]] && [[ $dots -ge 2 ]]
then
echo "This is a staging site..."
ShipOnly
# NEED WORK HERE!!!
elif [[ $site_to_clone == "canary."* ]] && [[ $dots -ge 2 ]]
then
echo "This is a UpdateSafely site, skipping..."
else
# FIND PHP HERE!!!
if [ -f "/etc/php56rc/fpm.d/$appname.conf" ]
then
echo "PHP56RC file found... setting PHP to verison 5.6"
php="php5.6"
elif [ -f "/etc/php70rc/fpm.d/$appname.conf" ]
then
echo "PHP70RC file found... setting PHP to verison 7.0"
php="php7.0"
elif [ -f "/etc/php71rc/fpm.d/$appname.conf" ]
then
echo "PHP71RC file found... setting PHP to verison 7.1"
php="php7.1"
else
echo "No PHP file found... defaulting to PHP7.0"
php="php7.0"
fi
if [[ $SameServer == "yes" ]]
then
echo "We're using the same IP address $targetserver for all sites..."
else
echo "What is the remote IP of the target ServerPilot server for site $site_to_clone?"
echo "You'll need the root password for the remote ServerPilot system and root password login access will need to be ON."
read targetserver < /dev/tty
#remote_IP="$targetserver"
DoSSH $targetserver
fi
currentuser=$username
if [[ $currentuser = "runcloud" ]]
then
echo "Current user is runcloud, switching to serverpilot..."
currentuser=serverpilot
else
echo "We'll need to build this user $currentuser on the remote ServerPilot system..."
fi
#Make sure we don't have any underscores...
echo $appname > tempfile
appname=$(sed 's/\_/-/g' tempfile)
rm tempfile
appdomain=$site_to_clone
PushToSP
sleep 1
echo "Running remote restoration process..."
fi
#echo "Getting next site..."
done <"/var/tmp/primemover.domains.tmp2"
echo "All sites processed!"
}
StartDomainLogging() {
if [ -f /var/tmp/primemover.domains.tmp ]
then
rm /var/tmp/primemover.domains.tmp
fi
touch /var/tmp/primemover.domains.tmp
echo ""
echo "The following sites have been located on this server..."
echo "************************************************************************************************************************"
printf '%-20s %-40s %-20s %-30s %-30s\n' "APPLICATION" "DOMAIN" "USER" "LOCATION"
echo "************************************************************************************************************************"
}
SingleRCDomain() {
sourcedomain=$(awk '/server_name/,/;/' /etc/nginx-rc/conf.d/$appname.d/main.conf)
sourcedomain=$(echo "$sourcedomain" | sed 's/\S*\_name\S*//g')
sourcedomain=$(echo "${sourcedomain//;}") # Drop trailing semicolon
sourcedomain2=$(echo "$sourcedomain" | sed 's/\S*\www\S*//g')
rootfolder=$(awk '/root/,/;/' /etc/nginx-rc/conf.d/$appname.d/main.conf) # Grab root folder location
rootfolder=$(echo $rootfolder | awk '{print $2;}')
rootfolder=$(echo "${rootfolder//;}") # Drop trailing semicolon
if [ ${#sourcedomain2} -lt 4 ]
then
finaldomain=$sourcedomain
else
finaldomain=$sourcedomain2
fi
if [ ${#finaldomain} -lt 3 ]
then
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "NO DOMAIN!!!" "UNKNOWN" "$rootfolder ****SKIPPING****"
else
if [ -d $rootfolder ]
then
cd $rootfolder
cd ../..
username=$(basename $PWD)
domaincount=$(echo $finaldomain | wc -w)
if [ $domaincount == "1" ]
then
grid=work
else
#echo "This site has more than one domain! We're only able to process the first URL..."
finaldomain=$(echo $finaldomain | awk '{print $1;}')
fi
finaldomain=$(echo "$finaldomain" | sed "s/ //g")
dots=$(echo "$finaldomain" | awk -F. '{ print NF - 1 }')
if [ $dots -ge 2 ]
then
if [[ $finaldomain == "staging."* ]]
then
if [[ $restore == "yes" ]]
then
echo "Final domain for this site: $finaldomain"
else
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "$finaldomain (STAGING)" $username $rootfolder
fi
else
if [[ $restore == "yes" ]]
then
echo "Final domain for this site: $finaldomain"
else
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "$finaldomain (SUBDOMAIN)" $username $rootfolder
fi
fi
else
if [[ $restore == "yes" ]]
then
echo "Final domain for this site: $finaldomain"
else
printf '%-20s %-40s %-20s %-30s %-30s\n' "$appname" $finaldomain $username $rootfolder
fi
fi
echo "$appname $finaldomain $username $rootfolder ${#finaldomain}" >> /var/tmp/primemover.domains.tmp
else
printf '%-20s %-40s %-20s %-30s %-30s\n' "$appname" $finaldomain "UNKNOWN!!!" "SITE ROOT FOLDER DAMAGED OR MISSING! ****SKIPPING****"
fi
fi
}
rcDomains() {
search_dir="/etc/nginx-rc/conf.d"
StartDomainLogging
for entry in "$search_dir"/*
do
if [[ $entry == *".conf" ]]
then
# Skipping app .conf file...
grid=work
else
#echo "Entry is $entry..."
appname=$(basename $entry)
appname=$(echo "${appname//.d}")
SingleRCDomain
fi
done
echo "************************************************************************************************************************"
echo ""
echo ""
echo "PLEASE CONFIRM THIS LIST OF SITES LOOKS CORRECT... PRESS CTRL-Z to CANCEL if there is an error!!!"
echo ""
echo ""
read -t 10 -n 1 -s -r -p "Press any key to confirm or wait ten seconds..." ;
echo ""
sort -k5 -n /var/tmp/primemover.domains.tmp > /var/tmp/primemover.domains.tmp2
}
SingleSPDomain() {
sourcedomain=$(awk '/server_name/,/;/' /etc/nginx-sp/vhosts.d/$appname.conf)
sourcedomain=$(echo "$sourcedomain" | sed '/server_name/d')
sourcedomain=$(echo "$sourcedomain" | sed '/server-/d')
sourcedomain=$(echo "$sourcedomain" | sed '/;/d')
sourcedomain=$(echo "$sourcedomain" | awk '!a[$0]++')
sourcedomain=$(echo "$sourcedomain" | sed "s/ //g")
sourcedomain2=$(echo "$sourcedomain" | sed '/www./d')
rootfolder=$(awk '/root/,/;/' /etc/nginx-sp/vhosts.d/$appname.conf) # Grab root folder location
rootfolder=$(echo "${rootfolder//;}") # Drop trailing semicolon
rootfolder=$(echo "${rootfolder//root }") # Drop root descriptor
if [ ${#sourcedomain2} -lt 4 ]
then
finaldomain=$sourcedomain
else
finaldomain=$sourcedomain2
fi
if [ ${#finaldomain} -lt 3 ]
then
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "NO DOMAIN!!!" "UNKNOWN" "$rootfolder ****SKIPPING****"
else
if [ -d $rootfolder ]
then
cd $rootfolder
cd ../../..
username=$(basename $PWD)
domaincount=$(echo $finaldomain | wc -w)
if [ $domaincount == "1" ]
then
grid=work
else
#echo "This site has more than one domain! We're only able to process the first URL..."
finaldomain=$(echo $finaldomain | awk '{print $1;}')
fi
finaldomain=$(echo "$finaldomain" | sed "s/ //g")
dots=$(echo "$finaldomain" | awk -F. '{ print NF - 1 }')
if [ $dots -ge 2 ]
then
if [[ $finaldomain == "staging."* ]]
then
if [[ $restore == "yes" ]]
then
echo "Final domain for this site: $finaldomain"
else
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "$finaldomain (STAGING)" $username $rootfolder
fi
else
if [[ $restore == "yes" ]]
then
echo "Final domain for this site: $finaldomain"
else
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "$finaldomain (SUBDOMAIN)" $username $rootfolder
fi
fi
else
if [[ $restore == "yes" ]]
then
echo "Final domain for this site: $finaldomain"
else
printf '%-20s %-40s %-20s %-30s %-30s\n' "$appname" $finaldomain $username $rootfolder
fi
fi
echo "$appname $finaldomain $username $rootfolder ${#finaldomain}" >> /var/tmp/primemover.domains.tmp
else
printf '%-20s %-40s %-20s %-30s %-30s\n' "$appname" $finaldomain "UNKNOWN!!!" "SITE ROOT FOLDER DAMAGED OR MISSING! ****SKIPPING****"
fi
fi
}
spDomains() {
search_dir="/etc/nginx-sp/vhosts.d"
StartDomainLogging
for entry in "$search_dir"/*
do
if [[ $entry != *".conf" ]]
then
grid=work
else
appname=$(basename $entry)
appname=$(echo "${appname//.conf}")
SingleSPDomain
fi
done
echo "************************************************************************************************************************"
echo ""
echo ""
echo "PLEASE CONFIRM THIS LIST OF SITES LOOKS CORRECT... PRESS CTRL-Z to CANCEL if there is an error!!!"
echo ""
echo ""
read -t 10 -n 1 -s -r -p "Press any key to confirm or wait ten seconds..." ;
echo ""
sort -k5 -n /var/tmp/primemover.domains.tmp > /var/tmp/primemover.domains.tmp2
}
cpDomains() {
search_dir="/home"
StartDomainLogging
for entry in "$search_dir"/*
do
if [[ -d "$entry"/etc ]]
then
cd $entry/etc/*/
appname=$(basename $PWD)
SingleCPDomain
else
echo "This doesn't appear to be a live Apache site..."
fi
done
echo "************************************************************************************************************************"
echo ""
echo ""
echo "PLEASE CONFIRM THIS LIST OF SITES LOOKS CORRECT... PRESS CTRL-Z to CANCEL if there is an error!!!"
echo ""
echo ""
read -t 10 -n 1 -s -r -p "Press any key to confirm or wait ten seconds..." ;
echo ""
sort -k5 -n /var/tmp/primemover.domains.tmp > /var/tmp/primemover.domains.tmp2
}
SingleCPDomain() {
sourcedomain="$appname"
rootfolder="$entry/public_html/" # Grab root folder location
finaldomain="$appname"
if [ ${#finaldomain} -lt 3 ]
then
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "NO DOMAIN!!!" "UNKNOWN" "$rootfolder ****SKIPPING****"
else
if [ -d $rootfolder ]
then
cd $rootfolder
cd ..
username=$(basename $PWD)
dots=$(echo "$finaldomain" | awk -F. '{ print NF - 1 }')
if [ $dots -ge 2 ]
then
if [[ $finaldomain == "staging."* ]]
then
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "$finaldomain (STAGING)" $username $rootfolder
else
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "$finaldomain (SUBDOMAIN)" $username $rootfolder
fi
else
printf '%-20s %-40s %-20s %-30s %-30s\n' "$appname" $finaldomain $username $rootfolder
fi
echo "$appname $finaldomain $username $rootfolder ${#finaldomain}" >> /var/tmp/primemover.domains.tmp
else
printf '%-20s %-40s %-20s %-30s %-30s\n' "$appname" $finaldomain "UNKNOWN!!!" "SITE ROOT FOLDER DAMAGED OR MISSING! ****SKIPPING****"
fi
fi
}
SingleGPDomain() {
sourcedomain=$(awk '/server_name/,/;/' /etc/nginx/sites-available/$appname)
sourcedomain=$(echo "${sourcedomain//;}") # Drop trailing semicolon
finaldomain=$(echo $sourcedomain | awk '{ $1=""; print}')
domaincount=$(echo $finaldomain | wc -w)
if [ $domaincount == "1" ]
then
grid=work
else
#echo "This site has more than one domain! We're only able to process the first URL..."
finaldomain=$(echo $finaldomain | awk '{print $1;}')
fi
rootfolder="/var/www/$appname/htdocs"
username="www-data"
if [ ${#finaldomain} -lt 3 ]
then
printf '%-20s %-40s %-20s %-30s %-30s\n' $appname "NO DOMAIN!!!" "UNKNOWN" "$rootfolder ****SKIPPING****"
else
if [ -d $rootfolder ]
then
domaincount=$(echo $finaldomain | wc -w)
if [ $domaincount == "1" ]
then
grid=work
else
#echo "This site has more than one domain! We're only able to process the first URL..."