Moving or migrating a website to a new server

Recently i’ve been doing a few site migrations, involving taking an old site and moving the site / db & everything to a new host.

So, here’s what I did.

Firstly, I set the domain TTL’s to something really low, like 300 seconds.
This lets us change around NS entries quickly, without having to wait 14000 seconds for the domain to update.

Then, on the old server;

Dump the mysql db:


user@oldserver user $ mysqldump -uusername -ppassword -Q --opt --database database | gzip > database.oldserver.sql.gz

This mysqldump command will Quote table names (-Q) and drop the tables, add locks, add extended inserts and lock tables (–opt) and we’re only dumping the database we want (–database)

Then, rsync the entire site’s www directory, unless you use subversion or something similar (like we do)

(if you put the above .gz file into the site www directory, it will get copied along with the below command, which is good)


user@oldserver www $ rsync -zavP -e ssh user@newserver.com:/var/www/sitename/ /var/www/sitename

(-z is for compress, -a archives, which really means it does heaps of stuff. -v for verbose, -P for show progress)

Also, /var/www/sitename is the directory containing your web files ;)

This will copy everything over to your new box, providing you put the mysql gzip in the same directory.

Now just import your DB, after you’ve uncompressed it of course:


user@oldserver www $ mysql -uuser -ppassword < database.sql

So now you have your site files and db on the new box! woo!

In the meantime, I threw up some RewriteRules to stop customers seeing Bad Things.

Here’s what I used in the apache vhost config:


RewriteEngine On
RewriteCond %{REQUEST_URI} !/maintenance.php
RewriteCond %{REMOTE_ADDR} !^(255.255.255.255|ip.of.client.here|etc.etc.etc.etc)
RewriteRule ^/.*$ http://website.com/maintenance.php [R=302,L]

This assumes you have maintenance.php in the root directory of the site.

Basically, we’re saying to let the ip’s in {REMOTE_ADDR} access the site as usual, but anyone else gets the maintenance page.

I set this up on both the old server and the new server, then swapped the DNS entries to point to the new server.

So now you get the client to check everything is cool, get your developers to fix the stuff that is broken, and once its done, remove the RewriteRules and we’re in business!

Feel free to ask questions about what I did here. I’m probably missing something.



0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image

|

This work is licensed under a Creative Commons License | © doing.nothing.net.nz