One of the tasks that comes up frequently when working with WordPress websites is the need to either move a website, or create a fully functional backup. Since we recently moved all of our BraveNewCode sites to another server, I thought I would do a quick tutorial for anyone wishing to do that same.
I’m only going to focus on the procedure for a Linux server and a Mac host computer, since that’s the configuration we use the most.
What Is A WordPress Website
A WordPress website is a collection of files on disk as well as a MySQL database. That’s it. To move a site to a new location, you have to move both.
Step 1 – Backing Up The Database
The very first thing I do when moving a site is to backup the database. My preferred method for doing this is to utilize the command line tools within a Linux shell, but you can also use cPanel.
Assuming you are logged in via ssh, the first thing to do is to look for the wp-config.php file, which is usually in the website’s root directory, or possibly in a wordpress folder. If you open it up, you can find the database credentials, which resemble the following.
define('DB_NAME', 'dsdotcom');
define('DB_USER', 'duane');
define('DB_PASSWORD', 'duanespw');
Typically what I do next is to change to the website’s root directory, and then execute mysqldump, which is a program that comes with MySQL that creates a complete backup of a database in a text file format.
The format of the command is:
mysqldump -u [user] -p [database name] > [output-file]
which in the example above would be:
mysqldump -u duane -p dsdotcom > mywebsite-backup.sql
If you execute that command, you’ll be asked for the database password, which is the same value configured in the DB_PASSWORD variable in wp-config.php.
Assuming that goes well, you’ll end up with a complete backup of the database in mysite-backup.sql.
Step 2 – Backing Up The Files
If you’re using the Linux shell method, at this point go to directory that’s located before your website’s main directory. For example, if your site is at /home/bravenewcode/public_html/mywebsite.com, make sure you’re in the public_html directory. It’s important to be one directory back from your website’s main directory, as this will ensure the .htaccess and other hidden files are properly added to the archive.
At this point you can create a tar file representing all the files on disk by using the command.
tar cvf mybackup.tar mywebsite.com/*
where mywebsite.com/* should be replaced with your website’s directory name. If you put the MySQL database backup in the proper place in Step 1, then it will get added to the archive too.
At this point you can compress the file using gzip, by executing:
gzip mybackup.tar
which will create a file called mybackup.tar.gz.
Step 3 – Delivering The Package
At this point you will have a complete backup of the old site. Hopefully you have shell access to the new server, in which case you can copy the files directory between servers. You can accomplish this using the “scp” command (which stands for secure copy). The format of the copy command is:
scp mybackup.tar.gz user@hostname.com:/path/to/web/root/mybackup.tar.gz
where user is a valid user on the new server, and hostname.com is the name of the server. If the server is using cPanel, the backup file should almost always end up in a public_html directory.
Step 4 – Uncompressing The Files
At this point you can login to the new server via a shell command. Change directories using cd to the location of the backup file, for example by typing:
cd /home/bravenewcode/public_html
Then decompress the file using gunzip:
gunzip mybackup.tar.gz
which will leave you with a mybackup.tar file. Next, you have to expand the backup file, which will put all the files back in their proper location:
tar xvf mybackup.tar
when that is done, you should end up with a completely restored directory structure off of your main webserver root directory, for example public_html/mywebsite.com.
Step 5 – Import The Database
At this point, you have to import the database onto the new server. The backup file that was created in Step 1 should still exist in website’s directory (mywebsite.com), so it’s just a matter or importing it into MySQL. If this is a brand new server, you’ll have to create a new MySQL database using either cPanel or the command line. Assuming you have a new database with valid credentials, you can import the database using the mysql command.
First, change to the website directory:
cd /home/bravenewcode/public_html/mywebsite.com
next, execute the mysql command using the backup file that was created:
mysql -u [database_user] -p [database_name] < [backup-file-name]
or something like this when populated with real data:
mysql -u bnc_duane -p bnc_dsdotcom < mywebsite-backup.sql
You'll be prompted to enter the database password. Once the command is finished, you'll have the database restored on the new site.
Step 6 - Quick Sanity Test
At this point in time, WordPress is installed on the new server and is most likely in a somewhat broken state (due to the fact that the database information on the new server is different than the old server). I purposefully exploit that fact to test to make sure everything is working on the new server.
For example, if the website name is mywebsite.com, and it's been moved from an IP of 24.100.100.100 to 72.100.100.100, you can do a quick test my editing your hosts file. On a Mac, this file is located in /etc/hosts, and contains a list of domain names to map to IP addresses. There's an equivalent on Windows somewhere as well.
In that file you can add a single line, similar to the following:
72.100.100.100 mywebsite.com
Once added to the file, all requests for mywebsite.com in the browser will be redirected to the new IP, and to the new server. The old site will continue to be accessible for every one else, assuming it's still public, but your local configuration (that you set using the hosts file) will force your computer to use the new server.
If all goes well, you'll punch in your website name in a browser after editing the hosts file and see a broken site, most likely due to a Database connection error.
Step 7 - Update Database Information
At this point, edit the wp-config.php file and change the database information to match the information on the new server. As soon as that is done, the website should be completely accessible again from a browser. Once you verify that the website is working properly, you can delete the dummy .sql backup file.
Step 8 - Update DNS Information
At this point, you can probably safely redirect the web traffic from the old server to the new server. If you use a third party nameserver, simply update all the IP information to the new server's address. If the nameserver is located on the new server, you'll have to log into your DNS registrar and point your domain name to the new nameserver.
I personally usually redirect all the IP addresses first, let those propagate, and then only change the namserver information after a day or so of making sure the website looks proper to everyone. It's generally quick to change IP addresses back if there is a problem (assuming the TTL values are low on the DNS entries), but takes a long time to change the nameserver information at the registrar and have it propagate.
There are also few variations on this I'll detail below.
What If I Don't Have Shell Access?
You can definitely still backup and restore a site, it's just more work. First, you'll have to make the backup via cPanel using the phpMyAdmin program. Once inside, there's a tab at the very right hand side called Export. phpMyAdmin makes use of mysqldump as well, so the end result is the same as doing the backup from the command line, but it takes a few more clicks.
In terms of moving the files, you'll probably have to use FTP and proxy everything via a local machine. For example, usually you can drag the whole website folder from the FTP program onto your desktop, and then drag it back to a new FTP connection on the new server. It definitely takes a lot longer this way, since typically home-based Internet connections are slow compared to server-server transfers, but you can still get the job done.
Likewise, if you don't have shell access you'll have to import the .sql file using the Import tab within phpMyAdmin.
Maintenance Mode
While the backup and restoration process is fast, it's not instantaneous. That means if you completed all the tasks above in order, it's possible that you may receive a reader comment on your site after you've already made a database backup. The best option is to install and activate a maintenance mode plugin on the site prior to starting this procedure. Maintenance mode will make it so only administrators can see the actual site, while everyone else will get a "This site is down for maintenance" message.
The one we use most is the Maintenance Mode plugin in the WordPress.org repository. It has a nice feature where you can create a 503.php file in your theme directory and have a completely custom page people will see when maintenance mode is active. We use that to our advantage on client sites where we want a nice presentable, user-friendly screen to appear during the transfer process.
Wow, That Wasn't So Hard
After you get the hang of doing a full backup/restore from the command line, you can move an entire WordPress website in just a few minutes. The important thing is to take your time, be careful, and make sure you have backups on a local machine (of both the files and the database) prior to starting the procedure.
If you use cPanel, it has a backup/restore feature built into it, but in my experience it has failed pretty much every time I've tried to use it (in fact, one time it borked the original server as well, since it redirected all the domains there to the new server, which hadn't properly been set up). So, I much prefer taking a bit more time and moving sites one at a time to make sure it's done properly.