Using secure copy as a backup method

Those of us who manage Linux servers on a daily basis know what a blessing it can be to have a backup at the ready. Whether we're upgrading system files, modifying source code, or removing old archives, we're always one quick rm -rf / away from losing everything. I'm speaking from experience here.

After (inadvertently) completely torching a server configuration, I decided it was best to have a method for copying files between my Ubuntu VPS hosting my blog and my network server as a quick-and-dirty backup method. From the network server, things would be captured via Time Machine so there would actually be two copies of the data (thus qualifying it as a true backup).

Using secure copy (scp) to move files between Linux/Unix-based computers is a very efficient method, but it does require some thought prior to implementing. You'll need to obtain a public SSH key from the server receiving the data to avoid manual authentication and ensure the entire process goes smoothly. In order to save some time, you can visit the Oracle blog for the proper method to generate these keys.

The core of how scp works is through a cronjob. If you need the basics on how a cronjob functions, again to save time, visit this link. It gives a pretty good rundown of the proper syntax so you don't jack your etc/crontab file up too much. However, before you can set up the job to run your backup script, you must first create it.

Start by launching the vi editor. You'll need to know the location of the files you want to backup in your Linux file system. In my case I wanted to back up the core Ghost files for my blog, as well as two databases. Here's what I saved in my backup.sh file:

#!/bin/bash
# Backup script for Mint and Ghost installs and associated databases

/usr/bin/mysqldump -u root -p***** mint > /root/backups/mint_`date '+%m_%d_%y'`.sql
/bin/tar -zcf /root/backups/ghost_`date '+%m_%d_%y'`.tar.gz /var/www/ghost  
/usr/bin/scp /root/backups/mint_`date '+%m_%d_%y'`.sql netadmin@home.classicyuppie.com:/Users/netadmin/remotebackups  
/usr/bin/scp /root/backups/ghost_`date '+%m_%d_%y'`.tar.gz netadmin@home.classicyuppie.com :/Users/netadmin/remotebackups

(Note: the -p switch in the mysqldump command has been obfuscated for security.)

First, let's break this down a bit more. The first line command copies my Mint database from MySQL and places it into /root/backups/ and names the file "mint" with the date the export was done. The second line creates a compressed TAR archive with the data in the core Ghost directory. Third, the script is instructed to secure copy the database archive created in line 1 and transport it to the other server. The last line does the same thing, but for the Ghost core backup.

You can test your script by executing backup.sh (or whatever you named your backup script) at the command line. Be sure to store the script in the /bin directory so it can be executed like any other binary, otherwise you'll need to change directories to where the file is located to execute it. If you were successful in setting up your SSH public keys in the process I mentioned earlier, you should see the progess bars fly across to a completed status without entering in your sudoers password. To see if the file copy was successful, check the directory you specified the files be scp'd to on the receiving server.

With this process you can backup virtually any file in your Linux file directory. It's an invaluable tool for backing up critical files for restoration later when the time comes (and the time will come).

Happy scripting!