Backup Your Website Via SCP

Backup Your Website Via SCP

This tutorial goes through the steps of backing up your website via SCP from start to finish. We will start with zipping our site directory on the remote server and downloading it, we will then cover creating a mysql dump of the database and downloading it, finally we will cover creating a checksum of the zip and dump on the remote server, checking it against the checksum of our downloaded zip and dump and then removing / deleting the zip and dump on the remote server to free up space. This tutorial only covers creating db dumps of mysql databases.

This is a lengthy tutorial and to repeat this process every time you need to backup your website would be very inefficient, to automate the processes in this tutorial you can download our Simple Auto-Backup Via SSH script here and view our docs for the script here (DOCS COMING SOON). The script is licensed under the Apache 2.0 License, 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, so you can create the zip of your web directory and the MySQL dump.

							
							
					ssh username@host-ip				
			

Zip Your Site's Directory

Now you want to zip your website’s directory. 

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

Downloading the Zipped Directory

Once the directory is zipped we will download the .zip via scp (you can also do this via SFTP, however from my experience SCP is faster), be sure you know the path of the zip file because we will need it when we download it. The first step is to exit the remote server. Then input the following into the terminal. (The /target/path/on/local/computer is the path of the directory on your computer where you want to download the zipped file to).

							
							
					scp username@hostip:/path/of/zipped/file /target/path/on/local/computer 				
			

Create the MySQL Dump File

Next we will create the MySQL db dump, we can do this while the website zip file is being downloaded. Open a new terminal and log back into the remote server via ssh and type this into the terminal:

							
							
					mysqldump -u db_user -p db_name > /home/username/db_backup.sql				
			

Download the Dump to Your Computer

Now we will use scp (secure copy protocol) to backup the dump file to our computer.

Side note: It is possible to ssh into serverA and then ssh into serverB to copy files (in this case our site’s .zip and db dump) from serverB to serverA without having to copy the files first from serverB to your computer and then to serverA.

							
							
					scp username@hostip:/path/to/dumpfile.sql /path/to/target/directory/on/computer				
			

Checking the Integrity of the Backups

Now we will check the integrity of our download to ensure it is the exact same as the original zip file on the remote server, (this is to ensure no damage happened to the zip during download). Note depending on your site’s size and server specs this may take a little while. Log back into the server and input:

							
							
					sha256sum /path/to/zip.zip				
			

Be sure to copy the resulting hash and paste it in a note, we will need it to compare it to the resulting hash of our downloaded zip file. Next we will do the same for our MySQL db dump.

							
							
					sha256sum /path/to/dumpfile.sql				
			

Now after copying the resulting hashes go back to your local computer and generate the hashes of the zip file and db dump and compare them with the hashes of the remote file and remote dump. If they are exactly the same you are good to go, if not, it means they are not the same and you may want to re-download the files / dump that have differing hashes.

Cleanup -Remove the ZIP and Dump Files 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 zip file and the dump file. BE CAREFUL, ONLY DELETE THE ZIP FILE & THE DUMP FILE WE JUST CREATED. This cleans up unused space on the server.

							
							
					rm /path/fo/dumpfile.sql && rm /path/to/zipfile.zip				
			

That’s it you have successfully backed up your website and database via SCP!

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.