A Reflection on Moving Websites

This article was supposed to be originally published 2010-November-20, but was lost due to time constraints.

As I mentioned in my previous post I’ve been working on moving my websites from my old hosting company to my new server at MediaTemple. And after moving several websites from one server to another, I have some advice. For starters – just don’t, it requires a large amount of time. But below is a method of doing it in a streamline process.

So if you don’t already have a backup of your website or a current backup of your website, continue reading – if you do have a current backup skip to step five. There are three ways you can create an backup of your website, the first and probably easiest is to just download everything using your favorite FTP client to your local machine. The second method is to use SCP (Secure Copy) which is supposed to transfer your files directly from one server to another, however my former server had it disabled for a unknown reason – maybe to make it harder for customers to switch hmm? The third and the method we are going to discuss today is create a tarball (an Linux archive) of all the files and directories we want to move. So now on to the gory details…

Requirements

Step One: Preparing Files and Your Workstation

Please Read all steps before following along with guide, there are a few caveats in a few.

First of all, you will need to login to your current web server with your FTP client of choice and have a look around at your files. If you have a simple file structure you may not need to do any house keeping, however if you have a massive site or you’ve done development experiments on your website you may want to do some housekeeping now to save you from a headache later. Housekeeping is only necessary if you are planning on not moving everything to the new server. Once you’re satisfied with your current file structure you can move on to the next step.

Now that you have your website files cleaned up we can create the archive. To start you will need to login to your current server via SSH. Depending upon which operating system you are running on your workstation will determine how you will do this.

Windows

Microsoft Windows operating systems by default do not include a SSH client, so you will need to download one. One of the most popular SSH client’s for Windows is PuTTY. PuTTY is a free implementation of telnet and SSH client for the windows and Unix platforms, you can download the client from the PuTTY Homepage. Once you have downloaded PuTTY you can move on to the next section.

Mac OS X & Linux

Both Mac OS X and Linux include a SSH client out of the box so you can skip to the next section. To access SSH on Mac OS X or Linux you need to open a terminal window. On Mac OS X you can access the terminal by doing a spotlight search for “terminal” and select terminal from the results under applications. If you are running Linux you probably already know where to go, so I leave that as an exercise for the reader.

Step Two: Creating the Archive

The Connection Settings Window for PuTTY on Windows

PuTTY Connection Settings Window

Now that you have the SSH client we can log into the server to create the archive/backup. I will be using screenshots from a Windows based machine running PuTTY, however the command should be the same for Mac OS X and Linux. The first thing we need to do is to login to the server with your credentials. Start by opening up PuTTY and enter your domain name in the field named “host name” and select “SSH” as connection type, then click open.

You may get a security alert saying that the server you are connecting to may not be the correct server, if you’re sure you have entered your details correctly just click yes to the dialog. After you click the security alert you will be prompted for your user name and password to access your server. These credentials are the same as your FTP user, if you are unsure contact your server administrator are hosting company.

PuTTY Console after you login into the site

Since all hosting environments are different, I’m going to set a baseline to work from in this tutorial. So, if you have questions about your particular hosting service, the best place for those answers are directly from the hosting company you are with. So here is the directory structure on my former hosting company’s server:

/home/content/88/8888888/html/

Where anything inside the html folder is publicly accessible from my domain name. This folder is sometimes named: htdocs, webroot, public_html, or html.

PuTTY running the command pwd

PuTTY running command pwd

The first thing you need to do is find out where your ssh client is pointed within the directory structure. To determine where your at, type the following into your ssh client:

pwd

This will return the current working directory as shown in the screenshot to the left.

Which states that I am at /home/content/88/8888888/ which if you don’t understand your web server’s file structure means very little to you. However, there are few other commands that can help you find where your located. The first command you should run is ls which will list the contents of the current directory. In this case, it will return the following:

cgi  html

In this case, html is the directory that contains all of the data files for all my website at my old hosting company. Since we know were the data for the website is located we can now create an archive of the entire site.

Note: If you can not figure out where your website’s files and folders are, you may need to contact your hosting company or a professional for assistance.

Since we are now above our public HTML directory we can create an archive of all the files in one swoop. The reason we are going to create an archive of the website is for two reasons; one large file is faster to download/upload than thousands of tiny files and we don’t forget anything. To create the archive type the follow command and arguments into your ssh client substituting when needed:

tar cvf archive_name.tar html/

In the above command we are call the system’s TAR archiving utility, followed by three arguments; c – create an archive, v – verbose list processed files, and f – filename. After the arguments is the filename of the new archive to be created, please note the file extension .tar, this is required – you may change archive_name to anything you want, but I would avoid spaces and symbols. Last but not least is the directory (or could be a file) to add to the archive that we are creating. Now if you hit enter on that command you will see a list of files that is currently being added to the archive, depending upon the size of your site this may take a few minutes to process all the files.

If you do not need to add any additional files or folders to the archive, we can now compress the archive. Please note that this is an optional step, compressing the archive will just make the file smaller. Run the following command from your ssh client to start compressing the archive:

tar cvfj archive_name.tar.bz2 archive_name.tar

This process will just take a few moments to compress the archive. Now we can move on to downloading the archive to your local machine.

Adding Additional Files & Folders to an Archive

If you cannot get above your web root you may need to add multiple files and folders to a single archive. Below is the file structure I am going to use for this example.

blog/
img/
js/
portfolio/
.htaccess
index.php
robots.txt

In order to add all of the above files and folders to a single archive, you simply run the same command you would have ran in the above section, however you replace the html/ with each one of the directories and files names as shown below.

tar cvf archive_name.tar blog/
tar rvf archive_name.tar img/
tar rvf archive_name.tar js/
tar rvf archive_name.tar portfolio/
tar rvf archive_name.tar .htacess
tar rvf archive_name.tar index.php
tar rvf archive_name.tar robots.txt

Each line above is one command and is executed after the previous command completes, so if you have several files and directories this will take a while and would be error prone. Also notice that first line creates the archive with argument flag -c while the others use the append flag -r.

One thing you could try before going through a lengthy process of adding files and folders one at a time is to try to get above your web root by typing cd .. into your ssh client. You should then should be above your web root, to see if you are, run ls and look for a directory with the name of: html, public_html, or htdocs. If you see a directory with one of those names, try to change to that directory by running cd and see if the containing files and folders are yours. If you are unsure either consult your hosting company or an professional for help.

Once you have added all your files and directories to your archive we can now compress that archive by running the following command:

tar cvfj archive_name.tar.bz2 archive_name.tar

As said in the above section replace archive_name with the appropriate values from the previous step as needed. Now, we can move on to downloading the archive.

Step Three: Downloading the Archive

After you’ve created the archive, you’ll need to download it from the old hosting company to your local machine. However, there is a catch since you made the archive outside of the web root we cannot simply point your web browser to the file to download it – we must use a FTP client to download the archive. So it’s time the fire up your favorite FTP client and log into your old server. How to set up and use a FTP client is outside the scope of this article. I will be using FileZilla on Windows. The process will be similar in other FTP clients, but may be different slightly depending upon your FTP client of choice.

As already stated, open your FTP client and log into your old hosting companies’ server. Now navigate to your web root directory, in this example it would be /home/content/88/8888888/html/. Once you’re at your web root you need to go up one level higher, to do so you just double click on the parent folder of your web root. Now you should be above your web root and see the archive that you created in the previous step, now download the archive archive_name.tar.bz2 to your local machine. The next step we will perform some file cleanup, this step is only required if you included files and folders that you do not plan to move to your new server.

Step Four: Housekeeping Part II

Now that you have downloaded the archive of your entire web site you may need to do some file cleanup. For example you have files and folders that you do not plan to upload to your new server.

If you need to do this you will need either WinRAR of 7zip installed on your computer. What you’re going to do is unpacked the archive you created to a working directory on your local machine. Using an archive utility is outside the scope of this article. Once you have unpacked the archive, go through your working directory and do any housecleaning that you wish to perform. Once you’re done with your housekeeping tasks you will need to repack the archive, in order to do so use your archive utility to create a new tar.bz2 archive of your working directory – please note do not store any files in the working directory that you do not plan to upload to the new server. Once you are happy with your freshly cleaned archive we can now upload the archive and unpack it to the new hosting companies servers.

Step Five: Upload and Decompress

We are almost done we just need to upload and unpacked our data. To get started with this we will need to upload the archive to a directory above the web root on the new server. To start uploading open your FTP client and log into your new server and navigate to the parent directory of your web root and start uploading the archive. Once the archive has been successfully uploaded to the new server we can move on to unpacking.

In order to unpack the archive we’re going to need to access our new web server via an SSH client. So pop open an SSH client to your new web server and navigate to the parent directory of your web root, you should see the archive you have uploaded. In order to unpack the archive you are going to need to run the following commands:

tar xvfj acrchive_name.tar.bz2

Please note, this command will extract the files and folders to /html/ directory in this case. The reason that it is, on the former hosting server all the files and folders were held in a directory called /html, so if your web root directory name is different between your two servers you will need to rename the working directory in step four – Housekeeping Pt. 2 before repacking the archive to the new name of your web root.

Now if everything went to plan your website has been moved to your new hosting provider, if possible you could use a access domain if provided by your hosting provider. If your hosting provider does not provide access domains, but however you do have a static IP address you could use that instead. Your last alternative is to pushed the changes live and hope for the best.

The last step you need to do in transferring a web site to new hosting provider is to update your DNS servers to the new hosting providers DNS servers. In order to do this you must go to your domain registrar control panel and update the DNS servers there. You can get the new DNS servers from your new hosting provider, you can also usually find the DNS servers in the introduction e-mail you received when signing up for the new hosting provider. If you can’t find your new dns servers addresses, contact your hosting provider.

Conclusions

I hope this guide has been useful, and I wish you the best of luck while transferring your website(s) to a new hosting provider.

Word of advice, research your potential hosting providers thoroughly before committing – because moving web sites is a pain.

P.S. If you need help contact your hosting provider or you could hire me.

Share your experience in moving websites from hosting companies in the comments or if you think this could be improved some how, drop me a line.

Resources