Move your Wordpress blog using SSH

Moving your blog from one webhost to another can be a herculean task especially if your wp-contents folder is big or you have a large number of posts. If you are doing a manual transfer by downloading the files and then re-uploading them then it can take a really long time. Also backing up large databases is not a very good experience in itself either. In such cases, its recommended that you look for an alternative approach. One such method is SSH. Moving your blog via SSH can be a painless experience as you won’t have to download or upload any files.

For the following tutorial consider this scenario:

Old Server url: http://olddomain.com/ Path: /home/olduser/public_html/olddomain/

New Server url: http://domain.com/ Path: /home/newuser/public_html/domain/

Step 1 – Take Backup

Now using Putty (on Windows) or Mac’s built in Terminal go through the following commands on your old server after logging in:

cd public_html/olddomain
mysqldump --add-drop-table -h host -u db_user -p db_name > olddomainwp.sql
tar -cvf olddomain.rar ./
exit

You will be asked for your mysql password used to connect. Now we have our blog files backed up in an archive named olddomain.rar and the database in a file olddomainwp.sql.

Step 2 – Import Data at New Host

Now login to the new server using SSH and enter the following commands:

cd public_html/domain
wget http://olddomain.com/olddomain.rar
tar -xvf  olddomain.rar
mysql -h host -u db_user -p db_name < domain.sql
rm olddomain.rar olddomain.sql
exit

Here we download the rar file to the new host using wget command. We don’t need to download the sql file because that was included in the archive which we created on the old host. We then extract all the files using tar command and then import the database. Using sed command we replace all entries of www.olddomain.com with www.domain.com. And at the end after verifying that everything is working properly, we can delete the downloaded files.

Step 3 – Edit Database Settings

Its Time to edit the wp-config.php file to edit the database settings according to the new host. You can edit the file right from your SSH terminal by using Vim Editor. To invoke the editor type

vi ./wp-config.php

You will see something like this screen

Press the ‘I’ key to enter into ‘insert mode’ and arrow keys to move around the file. Once you have made your edits, press the Esc key to exit ‘insert mode’. To exit Vim type ‘:’ (without the quotes) and then type ‘wq’ and press enter, this will save your changes and quit Vim.

Step 4 – Fix Urls in Database

We now need to change the old urls saved inside the database so that attachments and links keep working like they were working on the old server. Now we can use some simple MySQL commands for this but the safest method is to use a Search and replace tool written by Devs at Interconnect.com. This is because at times the url of your site might have been saved inside a serialized array in the database. When you perform simple search and replace via an SQL command, it only replaces the url but does not take care of the fact that length of the string might change if your new url is shorter or longer than the old url and your site might break. Now this might happen rarely but one should never take a risk. Download the tool from Interconnect.com or their you can grab the latest trunk from their github page.

Now extract the files to the root directory of your wordpress blog on the new server. Now you can run this tool inside a browser by going to this url: http://newdomain.com/searchreplacedb2.php or you can run this via command line(Command line version is available only via their github page) using the file searchreplacedb2cli.php as the following command

./searchreplacedb2cli.php -h localhost -u root -d test -c utf\-8 -s "olddomain.com" -r "newdomain.com"

Note

  1. MySQL commands on Shell: You may need to perform some sql commands too as per the following points. You can either perform them via phpMyAdmin or by running them straight away via command prompt. For that enter the following command
    mysql -u username -p

    You will be asked to enter your password and then something like below will come up.

    Now you can enter MySQL commands normally at the prompt. Make sure each command ends with a semi-colon(;).

  2. If you want to keep your wordpress install in a different directorybut blog’s url different, then use the following MySQL command instead to change the siteurl and home.
    UPDATE wp_options SET option_value = 'http://domain.com' WHERE option_name = 'home';
    UPDATE wp_options SET option_value = 'http://domain.com/wp' WHERE option_name = 'siteurl'

    ‘siteurl’ here refers to the location of the actual WP installation files where ‘home’ refers to the public blog url.

  3. If you are getting User permissions erroron accessing the dashboard, most probably you changed the table prefix from the default one and probably set incorrectly. Implement the following command to rectify it.
    UPDATE `{new prefix}_usermeta` SET `meta_key` = REPLACE( `meta_key` , '{old prefix}_', '{new prefix}_' ); UPDATE `{new prefix}_options` SET `option_name` = '{new prefix}_user_roles' WHERE `option_name` = '{old prefix}_user_roles';
  4. Permalinks Update: If your blog was in a directory before shifting or shifting to a directory then you will need to save your permalinks again after logging into your dashboard panel so that it works correctly. Make sure its writable. If its not, then you can paste the code provided on the Permalinks settings page.
  5. Traffic Redirect: You want people to get redirected from the old url automatically to the new url so that you don’t lose your traffic. For that enter the following code in your .htaccess file on the old host
    RewriteEngine on
    RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

3 Comments


  1. Actually i dont know why the SSH is present in my CPanel, but now only came to know its feature and how it works,
    Thanks for your valuable work with its screenshot, it really helpful,


    1. Glad I could be of help. SSH is a very powerful tool. Google it out and you will learn more.


  2. Transferring of mysql db’s via ssh is much quicker to do in this situation, instead of having to import the db.

    $ mysqldump db-name | ssh [email protected] mysql db-name

Comments are closed.