Moving DB driven sites ( Joomla, Drupal , PHP Nuke & the like or custom) effortlessly using SSH

If you have ever administered a db driven site or one built on CMS like Joomla or Drupal you'll know that it can be a huge pain when it comes to moving hosts. I am putting up this tutorial here as a follow up to the basic SSH commands posts.

SSH makes moving huge sites a effortless process ( learning it can be painful though! ) .

I hope you find this useful!

Here we go.

A few Prerequisites :

1. Both your hosts provide SSH access to their servers.
2. You need a SSH client like Putty installed & ready to go on your machine.
3. If you are doing this for the first time then the below are a must :
i. A large mug of Coffee
ii. A lot of patience

1. Connect to current Server through SSH

So if you are ready, lets start by connecting to your current host using SSH. If you are using Putty start it up put in the your host name, port is usually 22 for SSH & select connection type as SSH.
Once you connect the terminal will pop up. Put in your username & password ( this wont show up anything as you type so dont worry about it being blank) & press enter .

Thats it now you have terminal access to your host.
Now lets first take a backup of the public_html or htdocs or www or whatever root directory your host provides you. In most cases this would be the directory you see as root when you access through FTP.

Ok gotta go to watch Janata Raja...I'll update this later
Ok Back! Lets continue!


2. Take a backup of the root directory.

Now run the following command at the terminal.

Browse & enter the directory that you need to move.

then type

zip -r filename.zip *

Dont forget to end the command with a ; before you press enter to execute it, if you are using putty.

Once you are done, check if the archive has been created by typing in the command below at the terminal.

ls -al

Please note that the command above will create the zip archive in the folder that you are currently in. ls -al will show you the list of files in that folder. you should see the filename.zip file in the file list there. It would be a good idea to check if the file size is decent enough in comparison with your site size. This will give you an idea if the archive is created properly. This isn't a concrete verification , but thats what i normally do.

There we now have a file backup ready. But hold on we are not out of the woods yet! We use a DB driven site remember! So on we go to step three.

3. Take a back up of your mysql database

First of all you need the following data before you begin the backup.

a. host name : this is localhost on most servers, but may be different in case your servers hosts databases on different servers.

b. database name : the name of the db you want to backup

c. Db User : the username that you use to connect to the database

d. Password : The password of the db user you are using.

If you are using Joomla, you can get all this data from the configuration.php file. They are named as $mosConfig_host, $mosConfig_db, $mosConfig_user & $mosConfig_password respectively.

All right now we are ready to go to the terminal to take a mysql dump of your db.

Here's the command you need to construct :

mysqldump -u yourdbuser -p -h yourhostname yourdbname > backup.sql

On executing this command ( & dont forget the ; at the end! ) you will be prompted for your password type in the db password here. Alternatively you can add it in the command itself. after the -p .

Now better tar this sql file as backupsql.zip as well & ( in case it is large which in most cases is bound to be)

Great so if you have got so far you will have 2 zip files ready one with the file backup & another with the database backup.

4. WGET these files to new server

Now we need to use wget to transfer the files to our new server.

Connect to the new server through SSH & type in the command below :

wget http://yourdomain.com/folder/filename.zip

Once thats done

wget http://yourdomain.com/folder/backupsql.zip

This will get both the files to your new server.

5. UnZip the Files

Ok now lets untar the file backup in the root folder. Type in the

unzip backupfiles.tgz

Be sure to do this such that your root folder on the new site is populated with the contents of the archive.

now untar the sql files

unzip backupsql.tgz

6. Restore Database

Before you do this create a new database & a new database user for it. Then use the following command to restore the database.

mysql -u username -p password -h hostname databasename <>Once you are done doing all the above stuff make changes in the configuration file of your site to reflect the new database & location.

Note that for smaller databases it would be easier to create the dump & import using GUI tools like PHPMyadmin

Whew! We are done.. Well at least i hope it is done for you!

I hope you will find this useful.


Comments