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

5 Comments »

Comment by Anon
2009-03-12 10:53:07

OMG UR DEEP GEEK-FU IZ STRONG

 
Comment by Fallen
2009-03-15 00:02:46

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

 
Comment by star
2009-03-16 08:01:01

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

 
Comment by star
2009-03-16 08:04:42

btw, doesn’t dreamhost provide something like cpanel?

 
Comment by 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!

 
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


The English Speaking Elite:
  • Mahinda: This is sort of a forum, isn't it? What I mean is that Indi posts something, everyone has a quick read, then starts using this space to air their own views and spread their own message or start their own little (quite often completely unrelated) arguments or whatever... Its got an organic quality, which I think is quite nice......
  • The way of the Dodo: I think we should take some of this stuff and create a forum. That allows for better discussion and more mature dialog. What do you guys think...
  • Mahinda: Just curious... Didn't mean to offend....
Much More Mahinda:
  • Mahinda: Lousy, uninteresting comeback with only mild entertainment value. Where's the wit? Where's the logic/justification to the comment? I'm disappointed in you......
  • David Blacker: Mahinda, I doubt you'd make a good bench press, never mind a press sec....
  • Mahinda: Machang Blacker! Where have you been??! Were you busy washing your hair or something? Oh sorry - you don't have hair, no? So why the offensive attitude? Don't you think I'd make a good press sec? ;)...
The Final Cross:
  • Mahinda: Flyovers, if effectively used, are an excellent method of improving the flow of traffic and congestion. Since you guys don't think the ones that have been built work terribly well towards that objective, lets just blame Mahinda and the Government for this and move on :) Ok. That was just a little joke, but on a serious note, I think development should be well thought out and also implemented in the most cost effective manner. Which...
  • Anon: To add to what Dodo say, before the fly-over there was stop-go traffic at the Dehiwala junction. After the flyover there is a continuous, but slow flow of traffic. Although I use that road only rarely in my limited experience the length of the queue and the time taken to travel is no better after the flyover, and may be slightly worse. Moving the bus stand would be an improvement at Dehiwala, think Nugegoda is somewhat more...
  • Anon: Thanks Shammi, there was a very good interview in the Nation a few years back that opened my eyes to him. Think he did a fair job at the port and according to reports did not take a large "grant" that was in the offing. Agree with Dodo that Mangala's chances of getting elected at the moment look pretty slip....

Related Posts


Daily Mirror on WordPress

Just to note, the Daily Mirror website is now using Wordpress. They're running the Revolution Magazine theme and a bunch of plugins, and overall its pretty well done. Wordpress is technically blogging software, but I've always thought it was well suited to a variety of sites. It's really cool that the Wijeya web team implemented Wordpress on such a large scale project, and did it pretty well. My only issues with the Daily Mirror site

Trinco: Small Miracles

I'm in Trinco, with family. It's raining a lot, but that's good for the cows, whose ribs are showing. I saw one trying to get service in a kade. Another was walking on water. I wash my feet in the Kanniya hot springs, drive out to see the new Kinniya bridge (unrelated). There's a rainbow leading to an offshore island. Sri Lanka is often surreal in a WTF way, but sometimes it's FTW.

New Blog, For Your Prurient Interest

This chap Lefroy has been commenting here. I didn't sort him out from the usual, but he's started his own blog which is pretty funny. So far the posts include pissing on his cousins clothes, sex chats overhead in internet cafes, Sri Lanka's lack of sex education and the gold and silver rules of dealing with women. I daresay as I get old I'm getting older and more conservative with what I say, but this

WordPress Hack: Upload From Post

This is a geeky thing, but I take a lot of code from the Wordpress community and since I cobbled together a crappy solution I figured that I might as well share it. The Wordpress upload screen has always annoyed me cause in order to use it I have to leave the post I'm writing. This hack provides a pop-up link to the upload screen. Installation involves uploading 5 files to your

This Is Pretty Much How I Feel About Colombo

Deshan has a photo print of the beach. As you look closer you can see the garbage. That's the Colombo I know. Above is a photo by Tharindu Amunugama (u4j10.wordpress.com). It also captures a bit of how Colombo feels to me. I like walking the streets. You can see more. Down my street the National Hospital burns medical waste at night. During the rains a girl got swept into a storm drain and died. It's