How to migrate a website from one hosting to another via sftp with zero Downtime

How to migrate a website from one hosting to another superfast via sftp

If you use control panel or wordpress, then you have easy to use features to migrate or transfer a website from one hosting to another. But contrary to popular belief, I find it far easier and faster to transfer a site from one hosting to another via command line. Not that I don't use control panels though. But if you have command line skills, you may be better off and quicker issuing few commands than a gui. In this post, let's take a look at how we can do a website migration easily via sftp.

This process involves the following steps:

  • Transfer your database

  • Transfer your Website file

  • Update DNS

Transferring your database

Transferring your database requires four simple steps:

  1. ssh into your old server

    Open a terminal(in Linux or MAC) and just issue the command below:

    ~$ ssh root@oldhosting.com -p 22

    Replace 22 with your custom post number incase it is not the default one that is 22. In case of windows, just launch putty and proceed.

  2. Export your database as .sql file

    You can do this by issueing the following command:

    ~$ mysqldump -u root -p db_name > db_name.sql

    This command should be enough. For more details on this you may refer this post.

  3. Transfer the .sql file to new server

    For this first you need move from first server prompt to new server prompt. You can do this by issueing the following command:

    ~$ sftp  -oPort=22 root@newserver.com

    This command will open an sftp prompt in the new server. And as usual, 22 is the default port and you have to replace it with your custom port, if any. Then in order to transfer the .sql file, the command is:

    newserver> put databse.sql
  4. Import the database in the new server

    This is a two-step process: first, create the database:

    mysql> CREATE DATABASE new_database;

    Exit the mysql prompt and issue the command below:

    newserver> mysql -u root -p new_database < databse.sql

Transferring your website files

Transferring your website files is even simpler. In the Old server, first compress your website files as shown below:

~$ tar -cvzf website.tar.gz public_html

Then transfer this compressed file to your new server exactly the same way as you did with your .sql file above. Then in the new server, uncompress your website file with the command:

~$ tar xvzf website.tar.gz

Before you issue this command, make sure you are in the ssh mode in the new server and not in the ftp mode.

Update DNS

Now that you have successfully transferred all the necessary files to your new hosting, you need to point your doman name to the ip address of the new server. Doing this is pretty easy if you already have your DNS server up and live for your new hosting. All you have to do is to just open you domain name control panel and update your domain name servers.

Now the question is what if you don't have a domain name server for your new hosting? In that case, if you use licensed control panels then their tech support will help you set up one. But if you don't use any control panel then you can refer this post to set up one yourself manually.