Moving A Wordress Site To A New Server
January 3, 2009 – 11:14 pm
I’m in the middle of moving from one web hosting company to another and i thought i’d make a note of how i moved my wordpress installations (there are six of them, including this site). I’ve backed up moved, or copied, a few databases over the last few months, but i haven’t done it enough for the commands to stick in my head without having to refer to documentation. The information is in the WordPress codex, but it’s in two separate documents – one about backing up a database and one about restoring from a backup.
I’ve got ssh access to the old server and the new server, so i used mysqladmin to dump the database in the old server and then to recreate it in the new one. My old webhosting service doesn’t provide any other way of doing it, but the new one is cpanel based and i could use cpanel’s database restore dialog to upload the mysql dump file and recreate the old database. However, it seemed more straightforward to scp the file directly from the old server to the new server, rather than download it to my laptop and then upload it with cpanel. If i’d had cpanel at both ends, i might have used it.
First, though, i copied the wordpress files from the old site to the new one. I wanted to minimize the time between dumping the database from the old site and the new site going live, because that reduces the chances of losing something in transit – a comment or some stats. None of the sites i was moving are particularly busy – and they don’t get many comments, so it wasn’t that critical. However, i still didn’t want to lose any comments in the process. If any of them had a lot of interactive traffic, i would have had to have taken a rather more sophisticated approach to the changoever.
To copy the files, i simply tar’ed and bzip2’ed the top level directory of the domain i was moving – using
tar -cjf novemberechoromeodelta.com.tar.bz2 www/novemberechoromeodelta.com
Then i scp’d that file to the new server and extracted the files from it there, with
tar -xjf novemberechoromeodelta.com.tar.bz2
(The old server runs freeBSD and the new server runs Linux and tar’s the standard archive utility on all Unix-type systems.)
Then i set up the domain with the new server’s cpanel domain configuration system and created the database using cpanel’s mysql database setup dialog. The new server imposed a database name prefix on me which would mean the new database would have a different name and a different username from the old one, so i edited wordpress/wp-config.php and replaced the old database and user names with the new ones. I re-used the old password.
When that was done, it was time to copy the database over. To dump it from the old server, i used this command:
mysqldump --add-drop-table -h localhost -u dbuser -p dbname | bzip2 -c > dbname.mysqldump.sql.bz2
The ‘mysqldump –add-drop-table’ command creates a text file which contains all the sql commands and data you need to create the database tables again from scratch. I scp’ed this to the new server and then recreated the tables in the database with
cat dbname.mysqldump.sql.bz2 | bzip2 -d | mysql -h localhost -u newdbuser -p newdbname
Once this is done, the site is fully operational on the new server. But there’s one more step to do – change the DNS setup so that traffic goes to this site instead of the old one. In my case, this was a question of changing the nameservers in the registrar’s records – which i did through their web interface.
There are two factors that affect how long it will take before the change of address becomes “visible” to anyone who may visit the site. Firstly, there could be a delay in updating the master name servers about the change of DNS (if that’s the way you’re doing it). And secondly, there can be a delay between that happening and someone browsing the web getting the new address.
The reason for that second delay is because entries in DNS zone files have a “TTL” (time to live) associated with them. That TTL is used by name servers around the internet to decide how long to keep an address in their cache before querying that domain’s primary name server for it again. It works in more or less the same way as your web browser’s cache works. That means that if the name server that your local computer is using has only just queried your domain’s primary name server, it will probably be some time before it will check that address again. So it can take a maximum of the TTL time before the change of server address is visible.
It’s hard to say what the TTL is likely to be configured as for any given hosting company, but 14400 seconds (4 hours) seems to be a common default. So if your ISP’s name server has queried your domain’s name server recently, you could have to wait for four hours before your browser will retrieve your site from the new server. If your local name server hasn’t queried it recently, you could get it straight away. So it’s a good idea to leave the old site up for several hours.
If you want to test the new site before you switch the DNS over, you can add your new server’s IP address to the hosts file on your local system. If you use Linux or Mac OSX, that’s /etc/hosts
. If you use Windows XP or similar, it’s %SystemRoot%\system32\drivers\etc\hosts
. Put the following entry into it:
123.456.789.123 yourdomain.com
where “123.456.789.123” is the IP address of your new server and “yourdomain.com” is the domain name of the web site you’re moving. Then when you type the domain name into your browser’s address bar (not the search bar!) the home page you see will be coming from the new server (so long as you’ve done the above steps right). If you’re not sure whether you’re looking at the old site or the new site, you can always add something small to the new site and see if it shows up in your browser. And don’t forget to clear your browser’s cache!
You got a really useful blog. Thanks for sharing your thoughts. Bye, Jesus Walker @ Flyttst