Copy a Linux website from server to server

linux_buildingFirst just let me say I am not a Linux expert. Far from it actually. However in the last 6 months I have spent a lot of time working on these systems. 20 Years as a Windows guy did not prep me for what I would discover.

Please note that this is not the only way to do this task, it’s just my preferred method.

Let me also say that I assume no responsibility if you try this and completely hose your site. Consider yourself warned, MAKE A BACKUP of anything you cannot afford to lose before attempting this.

I offered a client of mine to cut her hosting costs considerably by dropping from a dedicated server to a VPS. I mean why would you have a ded box with only 100-200 people a day visiting you?

So my client goes from $160 a month on GoDaddy with horrendous support, to $40 a month with Media Temple (dv).  Same page load speeds, some coin in the bank still, and a hell of a lot better support IMHO.

To do this effectively, I would tarball the site, a zen cart ecommerce jewelry store. There is 5 Gigs of files(a lot of images) and a 9 Meg or so mysql Database, along with a separate wordpress blog database. I copied all the files via SSH and WGET.I prefer this method over FTP download to a local machine and then a re-upload to another server. Saves a  lot of time, while NOT tying up my own connection.

To do this you will need SSH on each machine. Here is what I did.

On the source machine. We need to compress all the files into one compressed file( a tarball) that can transfer across the servers backbone pipes.

  • Open a SSH terminal session. Navigate to the site root. typically /var/www/vhosts/ or username/home/html/
  • from the command line execute tar -cvf site.tar.gz ./

This may take some time to complete depending on the size of your site. When its done you will have your tarball(site.tar.gz) ready to be copied.

On the destination machine.

  • Open a SSH terminal session. Navigate to the site root
  • From the command line execute wget
  • When the file finishes transfering execute tar -xvf site.tar.gz
  • The files will be uncompressed into their original locations, and will have the same permissions as the originals.

Files all transfered? Time to move the data! Let me first point out that I have copied databases via Navicat v8.0, a great product for database manipulation. Not just for mySQL it also works on MSSQL, Oracle, postGRE.sql, and a host of other sql type databases.

I did a backup, and restored it to the new server. Worked well, or so I thought. Then I got the phone call “Hi, Mike! where did all those questions marks come from in my product descriptions?” I was a little miffed, took a peek at the clients site, and sure enough random ??’s we inserted in some odd places in the descriptions. How odd

So I spent a little time trying to figure it out. One of my cohorts mentioned an encoding issue. Suggested using the command line to copy the files from box to box. Ok sounds reasonable.

Nothing I found is quite crystal clear on how to move a small database(<10M) from one mysql server to another from the command line.Sure there is a ton of advice on how to export/import using phpmyadmin. Even a few on how to dump/import via SSH. Yet I wanted to learn to do this the quick way, mysql to mysql, no middle man, no SCP or WGET. NO phpmyadmin.

First I should point out that it goes without saying that you will need shell access on at least one of the machines.  I have SSH and root access on both machines. should be easy right? It is, if you are using straight out of the box *nix servers without anything like Cpanel or Plesk.

I present to my geeky netizens a quick, easy way to copy mySQL data from server to server.

From the source machine execute this as root.

mysqldump –add-drop-table –extended-insert –force –log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost “mysql -uUSER -pPASS NEW_DB_NAME”

Fairly straight forward, and very quick indeed. Obviously you would need change the values of USER, PASS XXX_DB_NAME to match your environment. Hopefully this works for you.

You might get all the way to a password prompt, only to be told ‘permission denied, try again later’ What????? Not so fast Tonto.

If you are using Plesk domain/server management utility, things get way more complicated when trying to do things via the cmd line. In fact too complicated to be easy. That simple command line statement above, It just plain will not work on Plesk managed systems. If that is the case, stick with phpMyadmin web based export/import. You’ll save yourself a lot of headaches. I will cover copying a database from server to server via phpmyadmin, in another blog post.

For example, everyone knows you can create a new database via SSH and the command line, However Plesk is completely ignorant to that fact. if you do not create that database within Plesk, fuhgedaboutit. You’ll never see it in Plesk, your domains files will not have the correct permissions to access it.

Same thing with Plesk permission’s and groups. I like a utility to make management of domains, and email easy. I don’t care for the SSH and file permission issues you are constantly addressing in the trade off.

Maybe its me, maybe Linux/Plesk is every sysadmin’s nightmare. What do you say.

One Reply to “Copy a Linux website from server to server”

Leave a Reply

Your email address will not be published. Required fields are marked *