RAID is good.

I’m currently slammed with finals week stuff–my last finals week ever I must add–so I haven’t had a chance to write anything substantial as of late. I had a drive go out recently =(. Luckily–and luck seems to be the theme of the week, I had just reinstalled SuSE 10.2 and set up a RAID 1 mirror for all my data. W00t!

As a result of todays hardware mishaps, I just wrote a little script that copies some stuff from my home directory to a default directory or the directory of my choice. As a result, I figure it should be a great chance to educate some newbie Linux users in the ways of bash shell scripting. Consider:

#!/bin/sh
 
default_dir="/storage/array0/backups"
timestamp=`date +%m-%d-%Y_%H%M`
 
if(test -z "$1") then
    path=$default_dir
else
    # 2>dev/null; black magic supresses output from cd
    if cd $1 2>/dev/null; then
        path=$1
    else
        echo "Bad Directory"
        exit
    fi
fi
 
cd ~
tar cf dev.tar dev
bzip2 dev.tar
mv dev.tar.bz2 ${path}/${timestamp}_dev.tar.bz2
echo "Created backup: ${path}/${timestamp}_dev.tar.bz2"
 
cp "/etc/X11/xorg.conf" .
tar cf config.tar .Xresources .bashrc .emacs xorg.conf
bzip2 config.tar
mv config.tar.bz2 ${path}/${timestamp}_config.tar.bz2
rm xorg.conf
echo "Created backup: ${path}/${timestamp}_config.tar.bz2"

This script is very basic. It simply compresses and copies a single directory called dev and some customized config files from my home directory to another directory of my choice; or a default one given no arguments. It illustrates that you can use any command in a script that you can use in everyday terminal use. The first line of any interest is this one:

timestamp=`date +%m-%d-%Y_%H%M`

Notice the backwards ticks–the little guy on your tilde key; no no, upper left hand side of the keyboard. You need those around any command you wish to evaluate and stick inside a variable or echo. All we do here is call the date command with some format strings. This will result in a timestamp that goes little like: mm-dd-yyyy_hm. Primitive, yet satisfying. The next part is a little more confusing:

if(test -z "$1") then
    path=$default_dir
else
    # 2>dev/null; black magic supresses output from cd
    if cd $1 2>/dev/null; then
        path=$1
    else
        echo "Bad Directory"
        exit
    fi
fi

I’m going to assume that you are at least familiar with basic control structure like if…then…else. If you aren’t, then take a gander over here for a good basic tutorial. Anywho, the usage for our script allows us to pass in a directory that the backups will be copied to if we want too. Our first conditional if(test -z "$1") then will check to see if our script has a parameter passed into it. If not, then we simply assign the path variable to be our default path. If we do have a directory passed in, we need to make sure that it exists before copying anything there. Our nested conditional will handle that problem by trying to cd into the directory. If it fails, we print “Bad Directory” to the console and exit.

The rest of the script is pretty bland. It just tars and bzips a directory and some customized config files, moves them to our target directory, and renames them appending the timestamp we collected earlier to the front. It’s not a robust script, and not a substitute for real backups. Nevertheless, it is fun, educational, and quite useful. Feel free to modify it to your liking.

Leave a Comment