Migrating WordPress Using Shell

Plautus has shell access. Video.


This is deep geek, probably incomprehensible. indi.ca crashed for a day, but the only way to learn about computers is breaking them. This is a guide to moving a modern website from one server to another using command line. This is something I’ve done repeatedly and fucked up repeatedly so I just want to write it down. Note that I don’t really recommend following any of my instructions, use the WordPress Codex. The first step is connecting to both servers and getting a Linux command line. Then you backup the database and all files with about one line of text and place it somewhere. Then you open the other window and wget those files somewhere, unzip them and recreate the directories and databases. Then it should, theoretically, work. I’m doing it from memory and haven’t tested these exact commands.

Command Line

Command line is just connecting to a Linux/Unix machine in one of the oldest ways possible. Under the Windows Start button you can hit ‘Run’ and then type in ‘command’ and it’ll give you Shell access. It basically just lets you look at files and folders and do operations on them. You can do the same stuff with FTP or a graphical user interface, but you can’t do it in 5 minutes.

I use Putty. It’s a 500k program that I keep on a flash disk. You just enter the domain (say, indi.ca) and the username and password your webhost gives you. Should be in an email somewhere. Then you have command line access.

The only commands I know are

ls – give a file list so you know where the hell you are
cd – open a directory, like cd home
cd ~ – take you back to the home directory

Backup The Site

I think tarball used to mean Tape Archive. It’s just a format, like zip. You need to make a copy of your site to move it. First you need to ls and cd until you know your way around and where the WordPress is stored. On Dreamhost it’s all in a directory called ‘public’. You might be buried, could be cd web/public/html, or any number of places. You’ll need to orient yourself. On Dreamhost you know it’s in public, so it’s pretty simple.

tar -cvzf site.tar.gz public/*

That tells it to create a tarball called site.tar.gz and fill it with the everything in my public folder. I’m not sure what -cvzf means but I’m sure it’s important.

Note that I typed ‘public/*’ instead of just public ‘public’. I don’t want the parent folder, I just want the stuff inside. The parent folders have different names on different servers.

Backup The Database

The posts and comments and everything important is in a MySQL database. You need to know the name and username/password for you database. Should be in a mail from your webhost, or you can set via the control panel. It’s also written in wp-config.php in your WordPress folder. Anyways, this command should backup your database. I use this Kirupa post as a guide everytime.

mysqldump -u username -p db_name | gzip > database.sql.gz

It’ll prompt you for the database password. This tells it to backup the MySQL for this user on this database, and to ‘pipe’ it through gzip (a compressor) to a file named database.sql. If you’re on Dreamhost or something where the host is not localhost then you’ll need to add ‘-h hostname’ to the command, after ‘-p’ I guess. This is rare.

Anyways, now if you do an ‘ls’ you should get a list that includes the files database.sql.gz and site.tar.gz. And that’s your WordPress blog.

Pack The Stuff

Right now the files are beneath the public director, which means they’re not public. Note that this is VERY DANGEROUS AND STUPID because you’re basically making all you passwords and stuff on the Internet so you can copy them over in the next 5 minutes and then delete all the evidence. If you forget then you’re quite easily fucked. This will move both files into the public directory (or wherever you want them to be).

mv site.tar.gz database.sql.gz public

Unpack The Stuff

Then you need to collect the stuff from the destination server. So, open that window, which is connected to another Linux/Unix machine entirely, quite possibly in another country from the first machine. Shell into that (if you haven’t before) with the username password and look around till you find the public directory you want to install WordPress into. ‘cd’ into that. Then you can collect the files from the other server. This uses a command called wget.

wget www.oldserver.com/site.tar.gz
wget www.oldserver.com/database.tar.gz

Note that those are two separate commands, you’re getting two files. Then you can unpack the tarballs. This is a two lines more code than you need because I have two separate files, you could make one big tarball of both and zip that.

gunzip site.tar.gz
tar xvf site.tar
gunzip database.tar.gz
tar xvf database.tar

This should give you your full directory structure, which includes all the WordPress folders and your documents and uploads and stuff. And it gives you a file called database.sql. Now you need to load that into the new database.

mysql -u username -pPassword databasename < backup.sql

Note that there’s no space between -p and password. Dunno why. Also you need to add ‘-h hostname’ with the Dreamhost host (if it’s not localhost). You need to setup a database beforehand in the Control Panel and add a user and password, FYI.

Clean Up

Then clean up all the files on both servers. On the destination server type

rm backup.sql
rm backup.sql.gz
rm site.sql.gz

On the original server type

rm backup.sql.gz
rm site.sql.gz

Done

Then you can login to your WordPress and check the plugins and stuff. Everything should sorta be working. There’s no guarantees on the methods above and there’s better documentation on the WordPress Codex and just Googling around.

Before this I used to download everything to my PC via FTP, then reupload everything to the new site. In Sri Lanka this takes hours. Connecting server to server I can move 100 MB or more in like seconds. So these little scraps of command line are useful to now. Command Line is obviously insanely more powerful that this, I’m sure you could do the same thing in two lines of elegant code. But this sorta works for me.

RSS feed | Trackback URI

6 Comments »

Anon
2009-03-12 10:53:07

OMG UR DEEP GEEK-FU IZ STRONG

 
2009-03-15 00:02:46

http://indi.ca/search/
the search bar is missing? =/

 
star
2009-03-16 08:01:01

that’s excellent. i’d be needing it soon =))

 
star
2009-03-16 08:04:42

btw, doesn’t dreamhost provide something like cpanel?

 
Jay
2009-03-17 12:54:06

Another Important command that you probably would have used is the ‘pwd’ command which print the Present Working Directory. It is really important to know where you are in linux/unix as things can easily get deleted or overwritten or cut from important places.

Also there is a short cut to

gunzip site.tar.gz
tar xvf site.tar
gunzip database.tar.gz
tar xvf database.tar

its by using the letter ‘z’ so its becomes tar zxvf . This will unzip and then untar all at the same time. SO u will save two lines from being typed. Another Important thing you should always remember is to create a md5 hash of the archive so after u copy the backup to the new site you run a md5 there as well and compare with the original. This makes sure that things are properly copied and the integrity status put.

you can do this by md5 filename > backup-name.md5
This will create a .md5 containing the hash of your targzip file.

Peace!

 
2010-11-01 09:47:38

everybody wants to get rich but not everybody wants to sweat to get rich ‘

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

email indi AT indi.ca.


Recent Comments


Happy Independence Day (1)

Omr: Interesting. Just wait for the glut of articles about how “we are not really free”, “we are not really independentR 21;, “what do we have to be proud of,” “we would have been better off under the...

Trishaw Economics (3)

Silva: http://pictures .topspeed.com/I MG/jpg/200907/v w-trike-the-bes t-ofw.jpg

the way of the dodo: I have issues with trishaw, it’s pretty much bigotry in my case. Personally i feel that it’s completely beneath my station to even get in a trishaw. and i would definitely take the bus over a trishaw.

Chavie: LOLOL, Hipster trishaw. Good idea. xD

Shirt Happens #typoincolombo (6)

Silva: Cliché. Nothing original here.

Lutronman: Marketing at it’s best. Sri Lankans are clever!

Murshid Ahmed: Wow, Challenge Accepted by Shi(r)tWorks & well played :)

Sri Lanka’s Tamil Royalty (11)

Santhoshis: Lol- Heir to the jaffna Kingdom? Laughed so much reading about him.

Teach Yourself Programming (Codecademy) (7)

Jerry: Java – JavaScript is akin to Car – Carpet

indi: Thanks Mike, corrected

Social Media


Twitter
Facebook