5. Environment
Requirements
• Terminal with bash / zsh / or the like
http://www.windowscentral.com/how-install-bash-shell-command-line-
windows-10
• PHP > 5.3.29
• WordPress 3.7 or later
& SSH access if you wanna do stuff on a remote server
11. Setup SSH
Host andaaws
HostName ec2-xx-xx-xxx-xxx.eu-central-
1.compute.amazonaws.com
User ubuntu
IdentityFile /Users/panda/Projects/_meetup-workshop/wp-
cli/anda.pem
In ~/.ssh/config add the credentials for a host. E.g.
This allows me to ssh into the instance with
ssh andaaws
rather than
ssh ubuntu@ec2-xx-xx-xxx-xxx.eu-central-1.compute.amazonaws.com
-i anda.pem
12. Local to staging
# Export local db
wp db export --allow-root anda-db.sql
# Copy local db to AWS
scp anda-db.sql andaaws:/var/www/html/wp-content/
wait
# Importing the db with WP-CLI directly through SSH
wp --ssh=andaaws/var/www/html/ db import /var/www/html/wp-
content/anda-db.sql
wait
# Do the search-replace
wp --ssh=andaaws/var/www/html/ search-replace 'anda.local'
'ec2-xx-xx-xxx-xxx.eu-central-1.compute.amazonaws.com'
# Done
13. Staging to local
# Export the db on the remote server, through SSH
wp --ssh=andaaws/var/www/html/ db export --allow-root
/var/www/html/wp-content/anda-db.sql
# Copy the db from AWS to local
scp andaaws:/var/www/html/wp-content/anda-db.sql wp-content/
wait
# Import the db
wp db import wp-content/anda-db.sql
wait
# Do the search-replace
wp search-replace ec2-xx-xx-xxx-xxx.eu-central-
1.compute.amazonaws.com' 'anda.local'
# Done
14. Just run the scripts
• Make sure the scripts are in your wp root folder
• Run them
$ . local-db-to-aws.sh
$ . aws-db-to-local.sh
15. Example of another problem
I needed a way for a semi-technical client to
copy the db from live to staging, between two
aws instances.
He should not need to ssh into the server and
perform a long list of commands.
16. Run a script locally
echo "Exporting live db to /var/www/html/wp-content/db-imports/db-
live.sql"
ssh stagingsite "sudo mkdir /var/www/html/wp-content/db-imports"
ssh stagingsite "sudo chmod 777 /var/www/html/wp-content/db-imports"
ssh stagingsite "mysqldump -h aa11xxxxxxxx.yyyyyyyyyyyy.us-east-
1.rds.amazonaws.com -u theuser -P 3306 --password='thepass' --
databases db_name > /var/www/html/wp-content/db-imports/live-
db.sql"
wait
echo "Importing db"
wp --ssh=stagingsite/var/www/html/ db import /var/www/html/wp-
content/db-imports/live-db.sql
wait
echo "Hide site from search engines"
wp --ssh=stagingsite/var/www/html/ option update blog_public 0
echo "Disabling w3 total cache plugin"
wp --ssh=stagingsite/var/www/html/ plugin deactivate w3-total-cache
echo "Doing the search-replace"
wp --ssh=stagingsite/var/www/html/ search-replace ‘www.example.com’
'staging.example.com' --recurse-objects --skip-columns=guid
echo "Done"
ssh stagingsite "sudo rm -rf /var/www/html/wp-content/db-imports/"
$ . dbimport-live-to-staging.sh
17. So what did we learn?
• Core
• Options
• Posts
• Users
• Themes
• Plugins
• Menus
• Widgets
Database Migration