-
Notifications
You must be signed in to change notification settings - Fork 3
/
WPStager.sh
executable file
·741 lines (649 loc) · 25.4 KB
/
WPStager.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
#!/usr/bin/env bash
#
################################################################################
## WPStager - WordPress Provisioning and Staging Simplified ##
################################################################################
#
# https://github.com/kLOsk/WPStager
# http://www.daniel-klose.com
# Made in Japan with <3
# Licensed under GPL2 (https://github.com/kLOsk/WPStager/blob/master/LICENSE)
#
############################## SpeedUp Config ##################################
## Feel free to change these variables to speed up the provisioning process.
## It is perfectly fine to not change these, or just change the ones you feel like presetting.
## If a config is not preset the script will automatically query during its execution.
MYSQLUSER="change_me" # Your MAMP MySQL user. Default "root"
MYSQLPWD="change_me" # Your MAMP MySQL password. Default "root"
CFAPI="change_me" # Your CloudFlare v4 Api Zone ID (eg: a34ea34aeaea424ae25352) https://api.cloudflare.com/#getting-started-resource-ids (Can be found in Domain Summary when selecting a domain in CloudFlare)
CFSECRET="change_me" # Your CloudFlare Api key. Can be found here: https://www.cloudflare.com/a/account/my-account
CFEMAIL="change_me" # Your CloudFlare e-mail account (e.g. [email protected])
CFDOMAIN="change_me" # The second level domain which is managed by CloudFlare (e.g. stageserver.com)
CFSERVER="change_me" # The IP address of your staging server CF should point the new subdomain to (e.g. 134.12.34.56)
SSWEBDIR="change_me" # The root directory of your webserver. With Apache 2.2 that used to be /var/www but now with 2.4 this is /var/www/html - No trailing slash at the end!
SSMYSQLSERVER="change_me" # Your MySQL Staging Server IP (e.g. localhost (if run on the staging webserver) or e.g. 56.137.45.23)
SSMYSQLUSER="change_me" # Your MySQL Staging Server user
SSMYSQLPWD="change_me" # Your MySQL Staging Server password
SSSSH="change_me" # Your Staging Server SSH address (e.g. 34.23.56.12 or e.g. stageserver.com)
SSSSHUSER="change_me" # Only change this to root. If you use another account rsync won't be able to set the correct permissions for the staging WordPress installation :-/
################################################################################
## All Done! Don't change anything below this line, or hell will break loose! ##
################################################################################
#
#Set Colors (http://natelandau.com/bash-scripting-utilities/)
#
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
purple=$(tput setaf 171)
red=$(tput setaf 1)
green=$(tput setaf 76)
tan=$(tput setaf 3)
blue=$(tput setaf 38)
#
# Headers and Logging
#
e_header() { printf "\n${bold}${purple}========== %s ==========${reset}\n" "$@"
}
e_arrow() { printf "➜ $@\n"
}
e_success() { printf "${green}✔ %s${reset}\n" "$@"
}
e_error() { printf "${red}✖ %s${reset}\n" "$@"
}
e_warning() { printf "${tan}➜ %s${reset}\n" "$@"
}
e_underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
e_bold() { printf "${bold}%s${reset}\n" "$@"
}
e_note() { printf "${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
##Check for packages and OS
type_exists() {
if [ $(type -P $1) ]; then
return 0
fi
return 1
}
is_os() {
if [[ "${OSTYPE}" == $1* ]]; then
return 0
fi
return 1
}
clear
e_header "WPStager - WordPress Provisioning and Staging Simplified"
e_underline "Please keep in mind, that this script makes heavy use of third party software."
if is_os "darwin"; then
e_success "Mac OSX detected"
else
e_error "You are not using a Mac. Please understand that WPStager currently only works on Mac OSX! https://github.com/kLOsk/WPStager"
exit 1
fi
## Check if config file exists
if [ -f "$HOME/.wpstager.cfg" ]
then
e_success "WPStager configuration file found."
e_warning "Reading configuration..."
source $HOME/.wpstager.cfg
else
e_error "WPStager configuration file not found."
fi
if [ -d "/Applications/MAMP/MAMP.app" ]
then
e_success "MAMP installed at /Applications/MAMP/"
e_warning "Please make sure to use the default MAMP htdocs directory at /Applications/MAMP/htdocs"
else
e_error "Error: MAMP is not installed. Please install MAMP from https://www.mamp.info/"
exit 1
fi
# install Xcode Command Line Tools
xcode-select -p &> /dev/null
if [ $? -eq 0 ]; then
e_success "Xcode Command Line Tools installed"
else
e_error "Xcode Command Line Tools not installed"
e_warning "Installing now... (This might take a while)"
# https://github.com/timsutton/osx-vm-templates/blob/ce8df8a7468faa7c5312444ece1b977c1b2f77a4/scripts/xcode-cli-tools.sh
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress;
PROD=$(softwareupdate -l |
grep "\*.*Command Line" |
head -n 1 | awk -F"*" '{print $2}' |
sed -e 's/^ *//' |
tr -d '\n')
softwareupdate -i "$PROD" -v;
fi
# Check for HomeBrew
if type_exists 'brew'; then
e_success "Homebrew detected"
else
e_error "Homebrew has not been installed yet."
printf "Do you want WPStager to install Homebrew for you (http://brew.sh/)? (Y/n)"
read HB
HB=${HB:-y}
if [ "$HB" = "y" ] || [ "$HB" = "Y" ]; then
e_warning "Installing Homebrew..."
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
if [ $? -eq 0 ]; then
e_success "Homebrew installed"
else
e_error "Something went wrong Please visit http://brew.sh/ or get in touch at https://github.com/kLOsk/WPStager"
exit 1
fi
else
e_error "WPStager requires Homebrew to work. Please install it manually from http://brew.sh/"
exit 1
fi
fi
e_warning "Installing rsync and gnu-sed since OSX ships with outdated versions"
brew tap homebrew/dupes &> /dev/null
brew install rsync gnu-sed &> /dev/null
if [ $? -eq 0 ]; then
e_success "rsync and gnu-sed successfully updated"
else
e_error "Something went wrong with homebrew. Please get in touch at https://github.com/kLOsk/WPStager"
exit 1
fi
# Add MAMP MySQL bins to PATH
if type_exists 'mysql'; then
e_success "mysql bin detected"
else
e_warning "Adding MAMP mysql to /usr/local/bin"
sudo ln -s /Applications/MAMP/Library/bin/mysql /usr/local/bin/mysql
fi
if type_exists 'mysqlcheck'; then
e_success "mysqlcheck bin detected"
else
e_warning "Adding MAMP mysqlcheck to /usr/local/bin"
sudo ln -s /Applications/MAMP/Library/bin/mysqlcheck /usr/local/bin/mysqlcheck
fi
if type_exists 'mysqldump'; then
e_success "mysqldump bin detected"
else
e_warning "Adding MAMP mysqldump to /usr/local/bin"
sudo ln -s /Applications/MAMP/Library/bin/mysqldump /usr/local/bin/mysqldump
fi
# Check for Wordmove
if type_exists 'wordmove'; then
e_success "Wordmove detected"
else
e_error "Wordmove has not been installed yet."
e_note "WPStager can install Wordmove for you (globally with sudo). If you want your Ruby Gems to be installed locally only, please install Wordmove manually (https://github.com/welaika/wordmove)"
printf "Install Wordmove globally? (Y/n)"
read WM
WM=${WM:-y}
if [ "$WM" = "y" ] || [ "$WM" = "Y" ]; then
e_warning "Installing Wordmove..."
sudo gem install wordmove
if [ $? -eq 0 ]; then
e_success "Wordmove installed"
else
e_error "Something went wrong. Please visit https://github.com/welaika/wordmove or https://github.com/kLOsk/WPStager"
exit 1
fi
else
e_error "WPStager requires Wordmove to work. Please install it manually from https://github.com/welaika/wordmove"
exit 1
fi
fi
## Sanity check for programs existence
#Global declaration area
declare -r T_CMDS="curl wordmove"
#Sanity check: Test if commands are in $PATH
for t_cmd in $T_CMDS
do
type -P "$t_cmd" >> /dev/null && : || {
echo -e "$t_cmd not found in PATH ." >&2
e_error "Please get in touch at https://github.com/kLOsk/WPStager"
exit 1
}
done
## Check if Apache and MySQL are running
WEBSERVER='httpd'
if ps ax | grep -v grep | grep $WEBSERVER > /dev/null
then
e_success "$WEBSERVER service running, everything is fine"
else
e_error "$WEBSERVER is not running"
e_note "Make sure to start MAMP before running WPStager"
open "/Applications/MAMP/MAMP.app"
exit
fi
DBSERVER='mysql'
if ps ax | grep -v grep | grep $DBSERVER > /dev/null
then
e_success "$DBSERVER service running, everything is fine"
else
e_error "$DBSERVER is not running"
e_note "Make sure to start MAMP before running WPStager"
open "/Applications/MAMP/MAMP.app"
exit
fi
## Fix Apache config for automatic pretty domains http://xxxx.dev
if grep -Fxq "Listen 80" /Applications/MAMP/conf/apache/httpd.conf
then
e_success "Apache listening on port 80"
else
e_error "Apache not configured to listen on port 80"
e_note "Fixing Apache to listen on port 80"
gsed -i.bak '/Listen 8888/i Listen 80' /Applications/MAMP/conf/apache/httpd.conf
if [ $? -eq 0 ]; then
e_success "Apache configured to listen on port 80"
SERVERRESTART="1"
else
e_error "Something went wrong when changing the Apache configuration"
exit 1
fi
fi
if grep -Fxq "#Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf" /Applications/MAMP/conf/apache/httpd.conf
then
e_error "Apache not configured to include dynamic vhosts"
e_note "Fixing Apache to include vhosts"
gsed -i.bak '/httpd-vhosts\.conf/s/^#//g' /Applications/MAMP/conf/apache/httpd.conf
if [ $? -eq 0 ]; then
e_success "Apache configured to include dynamic vhosts"
SERVERRESTART="1"
else
e_error "Something went wrong when changing the Apache configuration"
exit 1
fi
else
e_success "Apache includes dynamic vhosts conf"
fi
if grep -Fxq "#Dynamic Vhost" /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
then
e_success "Apache configured for dynamic vhosts"
else
e_error "Apache not configured for dynamic vhosts"
e_note "Fixing Apache to support dynamic vhosts"
mv /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf.bak
touch /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo "NameVirtualHost *:80" >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo $'\n' >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo "#Dynamic Vhost" >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo "<VirtualHost *:80>" >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo " UseCanonicalName Off" >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo " VirtualDocumentRoot /Applications/MAMP/htdocs/%0" >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo "</VirtualHost>" >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
if [ $? -eq 0 ]; then
e_success "Apache configured for dynamic vhosts."
SERVERRESTART="1"
else
e_error "Something went wrong when changing the Apache configuration"
exit 1
fi
fi
if [ "$SERVERRESTART" = "1" ]; then
e_warning "Apache configuration was changed. Restarting Apache now"
sudo /Applications/MAMP/Library/bin/apachectl stop
sleep 10
sudo /Applications/MAMP/Library/bin/apachectl start
fi
#
## Switch to MAMP htdocs directory
#
cd /Applications/MAMP/htdocs
printf "What would you like to name your new WordPress development domain (i.e. devsite.test)? "
read LOCALDOMAIN
#
## Add the new local development domain name to /etc/hosts
#
sudo sh -c "echo \"127.0.0.1 $LOCALDOMAIN\" >> /etc/hosts"
e_warning "Downloading WordPress Stable, see http://wordpress.org/"
curl -L -O https://wordpress.org/latest.tar.gz
tar -xf latest.tar.gz
mv wordpress "$LOCALDOMAIN"
rm latest.tar.gz
if [ "$MYSQLUSER" = "change_me" ]; then
printf "\nLocal MySQL User: (root)"
read MYSQLUSER
MYSQLUSER=${MYSQLUSER:-root}
fi
if [ "$MYSQLPWD" = "change_me" ]; then
printf "Local MySQL Password: (root)"
read MYSQLPWD
MYSQLPWD=${MYSQLPWD:-root}
fi
printf "What would you like to name your new database? (%s)" "${LOCALDOMAIN%%.*}"
read NEWDB
NEWDB=${NEWDB:-${LOCALDOMAIN%%.*}}
echo "CREATE DATABASE \`$NEWDB\`; GRANT ALL ON \`$NEWDB\`.* TO '$MYSQLUSER'@'localhost';" | /Applications/MAMP/Library/bin/mysql -u"$MYSQLUSER" -p"$MYSQLPWD"
cd "$LOCALDOMAIN"
e_warning "Setup Wordmove for local environment"
# Create the Movefile for Wordmove
touch movefile.yml
cat <<EOT >> movefile.yml
global:
sql_adapter: "default"
local:
vhost: "http://$LOCALDOMAIN"
wordpress_path: "/Applications/MAMP/htdocs/$LOCALDOMAIN" # use an absolute path here
database:
name: "$NEWDB"
user: "$MYSQLUSER"
password: "$MYSQLPWD"
host: "localhost"
EOT
## Big Staging Block
printf "Do you want to use an online Staging Environment? (y/N)"
read STAGING
STAGING=${STAGING:-n}
if [ "$STAGING" = "y" ] || [ "$STAGING" = "Y" ]; then
printf "Do you want to use CloudFlare DNS for automatic subdomain provisioning? (Y/n)"
read CF
CF=${CF:-y}
if [ "$CF" = "y" ] || [ "$CF" = "Y" ]; then
#
#Require a cloudflare account
#
if [ "$CFEMAIL" = "change_me" ]; then
printf "CloudFlare E-Mail Account (i.e. [email protected]): "
read CFEMAIL
fi
if [ "$CFAPI" = "change_me" ]; then
printf "CloudFlare Api Zone ID: "
read CFAPI
fi
if [ "$CFSECRET" = "change_me" ]; then
printf "CloudFlare Api Key: "
read CFSECRET
fi
if [ "$CFDOMAIN" = "change_me" ]; then
printf "CloudFlare administrated Staging Domain (i.e. WPStager.com): "
read CFDOMAIN
fi
if [ "$CFSERVER" = "change_me" ]; then
printf "The IP address CloudFlare will route the new subdomain to (i.e. 34.23.1.34): "
read CFSERVER
fi
printf "What's your staging domain? (%s.%s)" "${LOCALDOMAIN%%.*}" "$CFDOMAIN"
read FULLDOMAIN
FULLDOMAIN=${FULLDOMAIN:-${LOCALDOMAIN%%.*}.$CFDOMAIN}
SUBDOMAIN=${FULLDOMAIN%%.*}
e_warning "Generating DNS Entry with Cloudflare"
curl -X POST 'https://api.cloudflare.com/client/v4/zones/'$CFAPI'/dns_records' \
-H 'X-Auth-Email: '$CFEMAIL \
-H 'X-Auth-Key: '$CFSECRET \
-H 'Content-Type: application/json' \
--data '{
"type": "A",
"name": "'$SUBDOMAIN.$CFDOMAIN'",
"content": "'$CFSERVER'",
"service_mode": "1",
"ttl": "1"
}'
else
printf "What's your staging subdomain (i.e. clientsite.WPStager.com)? "
read FULLDOMAIN
fi
# Requesting Apache webdir. Would be great to automate this!
if [ "$SSWEBDIR" = "change_me" ]; then
printf "\nWhere is the web root directory of your staging server? (/var/www)"
read SSWEBDIR
SSWEBDIR=${SSWEBDIR:-/var/www}
fi
if [ "$SSMYSQLSERVER" = "change_me" ]; then
printf "Staging Server MySQL Host Address (Hit enter if MySQL server runs locally on the staging server): (localhost)"
read SSMYSQLSERVER
SSMYSQLSERVER=${SSMYSQLSERVER:-localhost}
fi
if [ "$SSMYSQLUSER" = "change_me" ]; then
printf "Staging Server MySQL User: (root)"
read SSMYSQLUSER
SSMYSQLUSER=${SSMYSQLUSER:-root}
fi
if [ "$SSMYSQLPWD" = "change_me" ]; then
printf "Staging Server MySQL Password: "
read SSMYSQLPWD
fi
if [ "$SSSSH" = "change_me" ]; then
if [ "$CFSERVER" = "change_me" ]; then
printf "Staging Server SSH Host/IP Address (i.e. 1.2.3.4): "
read SSSSH
else
printf "Staging Server SSH Host Address: (%s)" "$CFSERVER" #Needs fix as %s is change_me with default config and no cloudflare use
read SSSSH
SSSSH=${SSSSH:-$CFSERVER}
fi
fi
if [ "$SSSSHUSER" = "change_me" ]; then
e_warning "SSH user root is required for proper Apache permissions."
e_warning "Permissions need to be fixed manually when not using root :-/"
printf "Staging Server SSH user: (root)"
read SSSSHUSER
SSSSHUSER=${SSSSHUSER:-root}
fi
## Setup Public Keys
## Check if local ssh keys exist
e_warning "Check if public SSH key exists"
if [ -f ~/.ssh/id_rsa.pub ]; then
e_success "Public SSH key exists"
## If it exists check if its added on server. If not add it.
KEY=$(cat ~/.ssh/id_rsa.pub)
ssh -o StrictHostKeyChecking=no -l ${SSSSHUSER} ${SSSSH} "if [ -z \"\$(grep \"$KEY\" ~/.ssh/authorized_keys )\" ]; then echo $KEY >> ~/.ssh/authorized_keys; echo Public SSH key added to staging server; fi;"
else
## If not create
e_error "Public SSH key doesn't exist. Generating now"
ssh-keygen -t rsa
##Copy key to staging server
cat ~/.ssh/id_rsa.pub | ssh -o StrictHostKeyChecking=no -l ${SSSSHUSER} ${SSSSH} "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
fi
## Install virtualhost on staging Server
e_warning "Check if virtualhost is installed on staging server"
if ssh -t $SSSSHUSER@$SSSSH "stat /usr/local/bin/virtualhost &> /dev/null"; then
e_success "Virtualhost installed on staging server"
else
e_error "Virtualhost not installed on staging server"
e_warning "Installing virtualhost on staging server"
ssh -t $SSSSHUSER@$SSSSH "cd /usr/local/bin ; wget -O virtualhost https://raw.githubusercontent.com/RoverWire/virtualhost/master/virtualhost.sh ; chmod +x virtualhost ;"
fi
e_warning "Setup Wordmove for staging environment"
# Append Movefile with staging block
cat <<EOT >> movefile.yml
staging:
vhost: "http://$FULLDOMAIN"
wordpress_path: "$SSWEBDIR/$FULLDOMAIN" # use an absolute path here
database:
name: "$NEWDB"
user: "$SSMYSQLUSER"
password: "$SSMYSQLPWD"
host: "$SSMYSQLSERVER"
# port: "3308" # Use just in case you have exotic server config
exclude:
- ".git/"
- ".gitignore"
- ".sass-cache/"
- "bin/"
- "tmp/*"
- "Gemfile*"
- "Movefile"
- "movefile.yml"
- ".DS_Store"
- "wp-config.php"
- "wp-content/*.sql"
# paths: # you can customize wordpress internal paths
# wp_content: "wp-content"
# uploads: "wp-content/uploads"
# plugins: "wp-content/plugins"
# themes: "wp-content/themes"
# languages: "wp-content/languages"
# themes: "wp-content/themes"
ssh:
host: "$SSSSH"
user: "$SSSSHUSER"
# password: "password" # password is optional, will use public keys if available.
# port: 22 # Port is optional
rsync_options: "-og --chown=www-data:www-data --no-perms --chmod=ugo=rwX"
# gateway: # Gateway is optional
# host: "host"
# user: "user"
# password: "password" # password is optional, will use public keys if available.
# ftp:
# user: "user"
# password: "password"
# host: "host"
# passive: true
# production: # multiple environments can be specified
# [...]
EOT
#Virtualhost creation for Apache on staging server
e_warning "Create Remote Database and setup Virtual Hosts"
ssh -t $SSSSHUSER@$SSSSH "echo 'CREATE DATABASE \`$NEWDB\`; GRANT ALL ON \`$NEWDB\`.* TO $SSMYSQLUSER@$SSMYSQLSERVER;' | /usr/bin/mysql -u$SSMYSQLUSER -p$SSMYSQLPWD ; sudo /usr/local/bin/virtualhost create $FULLDOMAIN $FULLDOMAIN ; sudo chown -R www-data:www-data $SSWEBDIR/$FULLDOMAIN ; sudo chmod -R g+w $SSWEBDIR/$FULLDOMAIN ; sudo rm $SSWEBDIR/$FULLDOMAIN/phpinfo.php ; "
#Create staging wp-config.php as Wordmove doesn't sync wp-config.php
e_warning "Create Staging wp-config.php"
cp -n ./wp-config-sample.php ./wp-config-staging.php
SECRETKEYS=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
EXISTINGKEYS='put your unique phrase here'
printf '%s\n' "g/$EXISTINGKEYS/d" a "$SECRETKEYS" . w | ed -s wp-config-staging.php
DBUSER=$"username_here"
DBPASS=$"password_here"
DBNAME=$"database_name_here"
DBSERVER=$"localhost"
sed -i '' -e "s/${DBUSER}/${SSMYSQLUSER}/g" wp-config-staging.php
sed -i '' -e "s/${DBPASS}/${SSMYSQLPWD}/g" wp-config-staging.php
sed -i '' -e "s/${DBNAME}/${NEWDB}/g" wp-config-staging.php
sed -i '' -e "s/${DBSERVER}/${SSMYSQLSERVER}/g" wp-config-staging.php
rsync -og --chown=www-data:www-data --no-perms --chmod=ugo=rwX wp-config-staging.php $SSSSHUSER@$SSSSH:$SSWEBDIR/$FULLDOMAIN/wp-config.php
rm wp-config-staging.php
fi
## Big Production Pull Block
e_warning "Note: Public key SSH access must be enabled."
e_warning "WPStager will try to generate them for you, but sometimes this does not work"
printf "Do you want to pull data from a production server? (y/N)"
read PRODUCTION
PRODUCTION=${PRODUCTION:-n}
if [ "$PRODUCTION" = "y" ] || [ "$PRODUCTION" = "Y" ]; then
e_warning "Make sure to include http:// OR https://"
printf "What's your production domain (i.e. https://WPStager.com)?"
read PFULLDOMAIN
printf "\nWhere is the root directory of your production server WordPress installation? (e.g. /var/www/sitename)"
read PSWEBDIR
printf "Production Server MySQL Host Address (Hit enter if MySQL server runs locally on the production server): (localhost)"
read PSMYSQLSERVER
PSMYSQLSERVER=${PSMYSQLSERVER:-localhost}
printf "Production Server MySQL Database Name: "
read PSMYSQLDB
printf "Production Server MySQL User: (root) "
read PSMYSQLUSER
PSMYSQLUSER=${PSMYSQLUSER:-root}
printf "Production Server MySQL Password: "
read PSMYSQLPWD
printf "Production Server MySQL Port: (3306) "
read PSMYSQLPORT
PSMYSQLPORT=${PSMYSQLPORT:-3306}
printf "Production Server SSH Host/IP Address (i.e. 1.2.3.4): "
read PSSSH
printf "Production Server SSH User: (root) "
read PSSSHUSER
PSSSHUSER=${PSSSHUSER:-root}
printf "Production Server SSH Port: (22) "
read PSSSHPORT
PSSSHPORT=${PSSSHPORT:-22}
## Setup Public Keys
## Check if local ssh keys exist
e_warning "Check if public SSH key exists"
if [ -f ~/.ssh/id_rsa.pub ]; then
e_success "Public SSH key exists"
## If it exists check if its added on server. If not add it.
KEY=$(cat ~/.ssh/id_rsa.pub)
ssh -o StrictHostKeyChecking=no -l ${PSSSHUSER} ${PSSSH} "if [ -z \"\$(grep \"$KEY\" ~/.ssh/authorized_keys )\" ]; then echo $KEY >> ~/.ssh/authorized_keys; echo Public SSH key added to staging server; fi;"
else
## If not create
e_error "Public SSH key doesn't exist. Generating now"
ssh-keygen -t rsa
##Copy key to production server
cat ~/.ssh/id_rsa.pub | ssh -o StrictHostKeyChecking=no -l ${PSSSHUSER} ${PSSSH} "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
fi
e_warning "Setup Wordmove for production environment"
# Append Movefile with production block
cat <<EOT >> movefile.yml
production:
vhost: "$PFULLDOMAIN"
wordpress_path: "$PSWEBDIR" # use an absolute path here
database:
name: "$PSMYSQLDB"
user: "$PSMYSQLUSER"
password: "$PSMYSQLPWD"
host: "$PSMYSQLSERVER"
port: "$PSMYSQLPORT"
exclude:
- ".git/"
- ".gitignore"
- ".sass-cache/"
- "bin/"
- "tmp/*"
- "Gemfile*"
- "Movefile"
- "movefile.yml"
- ".DS_Store"
- "wp-config.php"
- "wp-content/*.sql"
# paths: # you can customize wordpress internal paths
# wp_content: "wp-content"
# uploads: "wp-content/uploads"
# plugins: "wp-content/plugins"
# themes: "wp-content/themes"
# languages: "wp-content/languages"
# themes: "wp-content/themes"
ssh:
host: "$PSSSH"
user: "$PSSSHUSER"
# password: "password" # password is optional, will use public keys if available.
port: $PSSSHPORT # Port is optional
# rsync_options: "-og --chown=www-data:www-data --no-perms --chmod=ugo=rwX"
# gateway: # Gateway is optional
# host: "host"
# user: "user"
# password: "password" # password is optional, will use public keys if available.
# ftp:
# user: "user"
# password: "password"
# host: "host"
# passive: true
# production2: # multiple environments can be specified
# [...]
EOT
fi
# Create the local wp-config.php in case it doesn't exist yet (which it never should)
if [ -f ./wp-config.php ]; then
open http://"$LOCALDOMAIN"/wp-admin/install.php
else
cp -n ./wp-config-sample.php ./wp-config.php
SECRETKEYS=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
EXISTINGKEYS='put your unique phrase here'
printf '%s\n' "g/$EXISTINGKEYS/d" a "$SECRETKEYS" . w | ed -s wp-config.php
DBUSER=$"username_here"
DBPASS=$"password_here"
DBNAME=$"database_name_here"
sed -i '' -e "s/${DBUSER}/${MYSQLUSER}/g" wp-config.php
sed -i '' -e "s/${DBPASS}/${MYSQLPWD}/g" wp-config.php
sed -i '' -e "s/${DBNAME}/${NEWDB}/g" wp-config.php
open http://"$LOCALDOMAIN"/wp-admin/install.php
fi
if [ "$STAGING" = "y" ] || [ "$STAGING" = "Y" ] || [ "$PRODUCTION" = "y" ] || [ "$PRODUCTION" = "Y" ]; then
clear
e_bold "All done! Now finish your local WordPress installation in the browser (http://$LOCALDOMAIN/wp-admin/install.php)!"
echo
e_bold "After that you can invoke your first Staging, or Production sync! To do so use the following commands from the command line:"
echo
e_bold "For Staging"
e_warning "cd /Applications/MAMP/htdocs/$LOCALDOMAIN"
e_warning "wordmove push --all -e staging"
e_bold "Your new staging site can be accessed at http://$FULLDOMAIN after the first sync."
e_underline "Keep in mind that you need to manually change your DNS server setting when not using the CloudFlare feature!"
echo
e_bold "For Production"
e_warning "wordmove pull --all -e production"
e_underline "This will make an exact copy of your production site in your local dev environment!"
echo
e_note "For more information on how to use Wordmove make sure to visit https://github.com/welaika/wordmove"
else
clear
e_success "All done! Now finish your local WordPress installation in the browser (http://$LOCALDOMAIN/wp-admin/install.php)!"
e_note "For more information on how to use Wordmove make sure to visit https://github.com/welaika/wordmove"
fi
#
# Livereload app seems to have bugs. Can't get it started from bash...
#
# open "livereload:add?path=/Applications/MAMP/htdocs/$LOCALDOMAIN/wp-content"