I wanted to move some wordpress sites from an old server to a new one and wanted it to be as seamless as possible. I really didn’t want to use an automated plugin - I’ve tried them in the past and they either don’t work (either badly coded or some missing PHP library) or they require a paid subscription for some ongoing backup service that I don’t want.

I decided to do it the old fashioned way, by logging on to the servers via ssh. Incidentally, the steps below can also be used to provide a backup and restore process.

The following assumes that you are connected via ssh as the root user. If not, you may need to amend some of the commands. It also assumes you’ve configured your webserver and MySQL.

Note that any changes to the Wordpress site in between taking the backup and switching to the new server will be lost - obviously, as they aren’t backed up.

Backup the old files

Create a location for the backup

mkdir ~/mysite_backup

Take a dump of the mysql database to the backup folder, replacing hostname, username and database_name with the relevant information and entering the password when prompted

mysqldump -h hostname -u username -p database_name > ~/mysite_backup/db.sql

Copy in the wordpress files, printing out progress as it goes via the verbose flag

cp -v path/to/wordpress/folder ~/mysite_backup/

Then we’ll create a gzipped archive. The options here are:

  • z - use gzip compression
  • c - creates an archive
  • v - verbose, tells us what’s going on while the archive is being generated
  • p - preserve permissions
  • f - lets us specify a filename to write the archive to
tar -zcvpf backup.tar.gz ~/mysite_backup

Copying the backup file to the new server

You can use scp to securely copy the backup file to the new server. This may take a while if your Wordpress installation is particularly large.

scp -v ~/mysite_backup/backup.tar.gz root@new.server.ip.address:/path/to/copy/file/on/new/server

Then delete the backup files from the old server.

rm -rf ~/mysite_backup

Restoring the backup in the new location

Unpack the backup file. The options here are:

  • x - extracts an archive
  • v - verbose, tells us what’s going on while the archive is being generated
  • z - use gzip compression
  • f - lets us specify a filename to read the archive from
cd /path/to/backup/file/on/new/server
tar -xvzf backup.tar.gz

Put the Wordpress files wherever they need to go (determined by your webserver config).

  • r - recursive
  • p - preserve permissions
cp -rp backup/mysite_backup/* /path/to/wordpress/installation/

Create the database from the backup sql file, replacing the hostname, username and database_name with the correct info, and entering the password when prompted

mysql -h hostname -u username -p database_name < backup.sql

Then clear away the backup files

rm -rf backup/*

Then set the owner chown, group chgrp and permissions chmod in accordance with your setup. The following is an example, and not a recommendation. You need to choose something based on your installation.

chown -R root path/to/wordpress/installation
chgrp -R www-data path/to/wordpress/installation
chmod -R 644 path/to/wordpress/installation

Make sure you restart your webserver. In the case of Apache:

service apache2 restart

Testing things out

If you’re feeling cavalier and you’ve already configured your webserver to point to your new files, go ahead and change the DNS settings now. But if you’re a little more cautious, you can use a spare domain or your server’s ip address and configure your webserver to direct traffic to the new Wordpress site. One problem with this is that Wordpress redirects traffic to its own domain, so you need to fool it into thinking the ip address or spare domain is its correct address. This is an option normally changed via the admin panel, but due to the redirect, attempting to change this value via the admin panel will update the old Wordpress installation even if you type in the address of the new installation. To get around this, you can temporarily hard-code the value. Find the wp-admin/wp-config.php file and add he following lines near the beginning:

define('WP_HOME','http://your_test_url');
define('WP_SITEURL','http://your_test_url');

Test away. If all is good, remove the two lines from your wp-config.php file and update your DNS records.