I’ve been using WordPress for about six years now and think it’s a truly great piece of technology. I love using it for my blog and have deployed word press on a number of other sites around the web. One of the things I’ve struggled with though is copying a WordPress instance down to my local machine. I could certainly do it, it was just a long and tedious manual process. Copying WordPress locally shouldn’t be so difficult.
- manually login to the remote server and
- compress all the site files
- export the database
- compress the database file
- transport all of the compressed files back to my local machine
- import the database
- create custom SQL statements to change http://oldhost to http://newhost
- unzip all the supporting files locally.
The process was straightforward, but tedious and a waste of time. I would often work on a site that was somewhat out of date or have missing assets as I updated the database without downloading all of the new files.
I’ve been fortunate to meet up with the WordPress Sydney group. Each time I go I’ve learned something new which makes me even more excited about working on my own site. Good Meetups inspire their members to get deeper into the topic. WordPress Sydney is no exception.
The March meeting spoke of moving WordPress installs in between machines. I’m specifically interested in moving my hosted version down to my local Mac. Well, the organizer, introduced WP-CLI as one of the tools in his arsenal. Automation is the key to making everything simpler and more predictable. Now that I had a tool to automate WordPress maintenance, I now had no excuse for an out of date development system.
0.Install an *AMP system
Install a *AMP (Linux, Mac, Windows) set up on your development system to run WordPress. Mac and Linux users have everything they need built-in but a preconfigured solution may be easier. Windows will be a bumpier road, but not impossible.
1. Establish trust between your local and remote systems
The transfer script will need to have access to the production system to download the database. The script needs be able to login with SSH without using a password.
2. Install WP-CLI
WP-CLI needs to be installed on both the host and target systems. WP-CLI does the heavy lifting for us. Installation on my Macintosh was straightforward. However, every web host is different. The installation process for WP-CLI on BlueHost was a bit nonstandard.
3. Install the remote script
This script makes a backup of your database on the remote system. The local script will invoke this script over SSH to get a current copy of the database.
echo 'Exporting WordPress DB' echo '----------------------' rm -r wordpress_db_backup.sql.gz php-cli bin/wp-cli.phar db export --path=public_html/ wordpress_db_backup.sql gzip wordpress_db_backup.sql
The line with php-cli bin/wp-cli.phar is a BlueHost specific workaround for shared hosting. It’s just using the standard database export from WP-CLI.
Test the installation of this script by running the command on the local system.
4. Install the local script
echo 'Export remote database' echo '----------------------' ssh email@example.com backupsite.sh echo 'Copying database files locally' echo '------------------------------' scp firstname.lastname@example.org:wordpress_db_backup.sql.gz path_to_local_website/ gunzip /path_to_local_website/wordpress_db_backup.sql.gz echo 'Syncing remote files locally' echo '----------------------------' rsync -avz -e ssh email@example.com:public_html/* /path_to_local_website/ echo 'Importing database' echo '------------------' cd /path_to_local_website/ wp db import --path=/path_to_local_website/ /path_to_local_website/wordpress_db_backup.sql rm /path_to_local_website/wordpress_db_backup.sql echo 'Reconfiguring URLs' echo '------------------' wp search-replace --path=/path_to_local_website/ www.danradigan.com localhost
The big win is using rsync to copy all of the new content down incrementally rather than having to version all of the content in Git or have massive content downloads when I refresh the site.
I do have to say, I’m going to miss the WordPress Sydney group. Their great bunch of folks with good passion and insight about the WordPress platform.