Of Backups and BASH

Anybody can save backup copies of all their files somewhere for later retrieval, but when I planned to backup my files, I would not settle for a simple run-of-the-mill backup plan.  I would come up with a new and innovative system full of hack value goodness.  I had the idea of setting up an old computer with a large hard disk and writing a script to execute when the computer booted.  This script would download all of my important files from my other computers and save them in a compressed file identified by the date of the backup.  It would then wait ten or fifteen minutes, time for me to remotely log in and kill the script if I need to make configuration changes on the computer (or retrieve one of the past backups), and shutdown.  In essence, I planned to create a magical box on which I could push a button and have all of my documents backed up without any effort on my part.  It was a great idea, and I couldn’t wait to try it.

Unfortunately, the poor resources at my disposal prevented me from succeeding.  The computer that I wanted to use for this project was very, very old and, as it turns out, unreliable.  I couldn’t get anything to work on it.  In fact, it was such a rotten machine that I just took out the dvd drive (I always seem to have a shortage of those) and threw out the rest of it (and I’m not one to throw out computers at a whim).  It was a sad evening for me, but I did not give up all hope of having my files properly backed up.

The decision was made that I would write a backup script that would run on my desktop  (the one with the largest hard drive).  I finally got around to doing it last night around midnight.  My final product:

#!/bin/bash


now=`date '+%F'`
mkdir "$now" #make a folder for the date
cd "$now"

echo 'making backup directory'
for places in laptop desktop server #make direcoties for each machine
do
mkdir $places
done

#now we have to pick out the important directories in each computer
#we'll copy those directories here, and then we'll compress the whole thing
#include command line options to include the desktop

echo 'copying server files'
scp -r server:/var/www/ server/
echo 'copying files from laptop'
scp -r laptop:/home/saba laptop/

if [ $# == "1" ]
then
if [ $1 == "-d" ]
then
echo '-d option enabled'
echo 'copying files from desktop'
scp -r desktop:/home/saba desktop/
fi
fi
cd ../

tar -cf "$now.tar" "$now"
gzip -r "$now.tar"
rm -r "$now"

exit 0

The script is available for download on my website at http://mankindisone.com/my_stuff/backup.html.  There is also a short description of what the script does and how to make it work for you at the aforementioned link.

As you can probably see, I’ve gotten better at scripting since my updater script last week.  I owe it to a long series of web searches and the reading of many documents last night.  In order to help others who are trying to write a real script for the first time, I’m going to make a BASH scripting super quickstart guide and publish it on the tutorials section of my website.  Expect that to be up in a few days.

Advertisements

2 Responses to “Of Backups and BASH”

  1. Jeff Cooper Says:

    I would highly recommend rsync instead of scp, provided you don’t need encryption (which you don’t).

  2. mankindisone Says:

    An excellent point. I’ll make sure to make that modification before I run the script next. I’ll probably put the update on my site. It would appear that there is some other issue as well. Perhaps due to a symbolic link going back to my home folder or something, files never stop being downloaded from the laptop. I noticed this after the amount of files downloaded had gone to over three times the size of my actual home folder. Clearly this script needs soe modification and optimizatoin, but if applied correctly (only downloading specific directories instead of the whole home directory), it can still be useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: