Download / Backup Directories & Files Via SCP

Download / Backup Directories & Files Via SCP

This tutorial goes through the steps of backing up files and/or directories on a remote server via scp. All steps included from logging in to the remote server via ssh and zipping the files to the actual copying of the files to your computer. Grab a coffee and enjoy!

Note Regarding SSH Commands

If you are running Linux or Mac, you can enter the ssh commands below directly into the terminal, if you are running Windows, you may need to install PuTTY for running the ssh commands if you have not already. Also when logging into the remote server via ssh / scp if the remote server is running ssh off of a non-standard port (standard is 22) you will need to specify that with -p or -P depending. (For instance -p 8000)

Login Via SSH to the Remote Server

First you have to login to the remote server via ssh to get the directory / file path you wish to download and to zip the directory to download (if you are downloading a directory). 

							
							
					ssh username@host-ip				
			

After running this command you will be prompted for your password. Once you input your password you will be logged in.

Find File / Directory Path & Compress Directories.

To view sub-directories and files within a directory you can use ls (for list) and to move between directories you can use cd (change directory) and to view a specific file use the cat command.

							
							
					ls /directoryname				
			

ls for list

							
							
					cd /directory/path				
			

cd for change directory

							
							
					cat /path/to/filename 				
			

the cat command lets you view the content of files 

Once you find the file or directory you need to download, copy the file path to a note for easy retrieval.  

If you are downloading a directory you may want to zip it. This will make the download go faster and decrease the overall size of the directory. To zip a directory, we use zip -r desirednameofzip.zip  /path/of/directory/to/be/zipped if you do not have the zip utility installed you can install it in linux via sudo apt-get update
sudo apt-get install zip (for Ubuntu / Debian) or use the appropriate command for your distro. It is less likely that you will find your self on a Windows server than a Linux distro, so we will not cover that here. 

							
							
					zip -r desirednameofzip.zip /path/of/directory/to/be/zipped				
			

zip (zips the directory) -r (recursively, includes all files within the directory to be zipped) desirednameofzip.zip (simply the name you want the zip to be called) /path/of/directory/to/be/zipped (the full path to the directory to be zipped, this tells the zip utility which directory is to be zipped)

							
							
					sudo apt-get update && sudo apt-get install zip				
			

Download the Directory / File Via SCP

Now exit the ssh connection to the remote server, just type exit and press enter. 

							
							
					exit				
			

Now we will use scp (secure copy protocol) to copy the zipped directory or the file to our local machine. 

Side note: It is possible to ssh into serverA and then ssh into serverB to copy files from serverB to serverA without having to copy the files first from serverB to your computer and then to serverA.

Type scp username@hostip:/path/to/directory/to/download path/to/local/target/directory

							
							
					scp username@hostip:/path/to/directory/to/download path/to/local/target/directory				
			

scp (copies the files from the server to your computer), username@hostip (your ssh username and your server’s ip address), :/path/to/directory/to/download (this is the zip file you want to download, or the text file or whatever type of file you want to download), path/to/local/target/directory (this is the directory to where you want to download the file to such as home/username/downloads).

Cleanup -Remove the Zipped Directory from the Remote Server-

Once the download is complete you can ssh back into the remote server (just as we did when we first logged in) and delete the zipped directroy file we created. BE CAREFUL, DO NOT DELETE THE WRONG FILE, we only want to delete the .zip file that we downloaded, we do not want to delete the original file or directory. Deleting the zip file we created ensures that we are keeping our server clean and frees up space for ruture use. To remove the .zip file enter: rm nameofzip.zip (This is the sip file we just downloaded).

							
							
					rm nameofzip.zip				
			

rm (removes the file), nameofzip.zip (this is the file to be removed, it should be the zip we created of the directory we desired to download, DO NOT DELETE THE ORIGINAL FILE OR DIRECTORY.

That’s it, we have successfully downloaded our .zip file ( or our .txt, .csv, etc. etc.) to our local computer!

Walter is a tech entrepreneur and in his free time loves fitness, art, writing, and exploring Montana’s outdoors.