Target Audience: Newbies to Moderately Advanced.
Why: When transferring a website from one server to another, it is inefficient to download the files from your old server to your desktop computer or laptop and then upload it again to the new server especially if the file is several hundred megabytes in size. SCP will allow you to skip downloading the file and just send it directly to the new server.
SCP, being based on the secure shell protocol, is also a secure way to transfer files. This is a great method if you want to transfer data from server to server on a regular basis.
Alternatives: Nothing can give you a better grasp of what SCP can do than by checking the alternatives to transferring files eg. FTP, SFTP, TFTP, wget.
What You Need:
- SSH access on both target and source servers.
- Basic familiarity with linux commands tar, cp, mv.
- My Desktop OS while making this tutorial is Windows, so 3rd party software you will need is Putty or another SSH Client.
Step 1: Tar the source files and directories.
Login to the source server and go to the root directory where your files are located. It is a good idea to use TAR to archive all the files and directories into one file to decrease the possibility of errors.
Here we will create a tarball of my website iheartbf.com from my server ‘crimson’, which I will transfer to its new server ‘kaijuhost’.
The syntax is such: tar cvzf {name_of_file.tar.gz} {directory} eg.
sudo tar cvzf iheartbf.tar.gz iheartbf.com/
This will create a tarball of all files and directories under the directory iheartbf.com/. Here is the result when you type ls -l.
There is now a iheartbf.tar.gz, a 45mb. file.
Step 2: SCP
SCP syntax is such:
scp {nameoffile.tar.gz} [email protected]:/some/directory
The default ssh port is 22, but in my particular case the port is 2222. Thus here is the command when transferring iheartbf.tar.gz to its new server kaijuhost.com.
scp -P 2222 iheartbf.tar.gz [email protected]:
Note that I did not enter a specific folder for the file to go in, I just left the destination at ‘:’, which means the file will end up at the user’s home directory on the destination server.
The file transfer only took a few seconds. To check, login at the destination host (kaijuhost.com in my case) and do a ls -l.
Step 3: Check
There you go. From there you can untar it into its proper directory.
Further Stuff You Want To Know:
- You can exclude certain files from a Tarball by using the ‘–exclude’ switch, useful if you want to keep large or useless files from being added to your tarball eg. To exclude files ending in the word *.mysql.gz from my example above, I would use
sudo tar cvzf iheartbf.tar.gz iheartbf.com/ --exclude='*.mysql.gz
‘ where * is any filename. - Always check if you have appropriate disk space when creating a tarball. If you run out of space while making it, all sorts of weird things happen to your server which is hard to troubleshoot.
- Always check if you have appropriate disk space at the target server. If the target server runs out of space whilst transferring file(s), all sorts of weird things happen again.
- A complete list of all SCP Syntax is here.
- A great tutorial on tar can be found here.