HowTo Make & Schedule Daily & Weekly Backup Scripts
General Overview




Daily Scripts

Overview:

For what I wanted to do to work most efficiently, I created two scripts to perform my daily backups, this will allow me to include the daily into the weekly backup without writing any more script than necessary. Both scripts are simple shell scripts, that are well commented, to make them easier to understand.

The Work:

The first script contains formatting, the location of output files, environment variables, and a procedure to save a set number of old backup files. In this example, I am saving six old daily backup files, plus the most recent giving me a one week daily history. If you are using a tape drive, you would not need to do this, it would be necessary to just simply use the number of tapes you want to keep in a rotation. You should also look into using an expiration date on the media, that will cause it to error, instead of overwriting the wrong tape.

This particular script is formatted to allow for an e-mail log message. Depending on your preference, you may want to drop this, and send a log to a file. If you schedule it as a cron job (explained later), your root account will receive an e-mail of any messages the system logs during the execution of the cron job.

#!/bin/sh
#

# Format Mail Header To Allow Piped Output Mailed To User
echo "From root@MyDomain.Org"
echo "From: Daily Backup"
echo "To: root@MyDomain.Org"
echo "Subject: Daily BackUp Log"
echo "MIME-Version: 1.0"
echo "Content-Type: TEXT/PLAIN; charset=US-ASCII"

echo ""
# Begin Body of Mail Message, and begin to do the work
echo "Backup Job Started....";date

# Set PATH Variable For Shell Script, and export it to subsequent
# called scripts.
PATH=/usr/bin:/usr/local/bin:/bin ; export PATH

# Set Script Variables, exporting some to subsequent called scripts.
DIR=/data2/backups ; export DIR # Change this to the directory you want to
                                # send your scripts to.
DIR2=/data2/backups/scripts ; export DIR2 # Change this to the directory 
                                          # where you are keeping the 
                                          # scripts.
TAR=bk.daily.tar ; export TAR
BZ2=$TAR.bz2
CMDC="tar -cvf" ; export CMDC
CMD="tar -rvf" ; export CMD
# The count variable tells the script how many old backups to save.
# This must be 3 or greater for this script file to work.
COUNT=6
# The following derivations are necessary to adjust the case
# statement later to account for different values of the count
# variable.
CASE1=$COUNT
CASE2=$(($CASE1-1))

# Check For Directory to write backup files to.  If it doesn't exist,
# create it.
echo ""
echo "Created Directories...."
if [ -d $DIR ]
  then
 else
   mkdir $DIR
   chmod 0760 $DIR
fi

# Delete Oldest Daily If Exists.
# And Rename Susbiquent Dailies to Older Dailies.

echo ""
echo "Removed Files...."
until [ $COUNT -eq 0 ]
 do
  case "$COUNT" in
    $CASE1)
      if [ -e $DIR/$BZ2.old$COUNT ]
        then
	       rm -v $DIR/$BZ2.old$COUNT
      fi
      ;;
    $CASE2)
      echo ""
      echo "Renamed Files...."
      until [ $COUNT -lt 1 ]
        do
          if [ -e $DIR/$BZ2.old$COUNT ]
            then
              mv -v $DIR/$BZ2.old$COUNT $DIR/$BZ2.old$(($COUNT+1))
          fi
          COUNT=$(($COUNT-1))
      done
      COUNT=$(($COUNT+2))
      ;;
    1)
      if [ -e $DIR/$BZ2 ]
        then
        mv -v $DIR/$BZ2 $DIR/$BZ2.old$COUNT
      fi
      ;;
  esac
  COUNT=$(($COUNT-1))
done

# Get Files For Archive and Archive
echo ""
echo "Creating Archive File and Adding Files To Archive File....";date
# Execute the script file that contains the list of files to backup.
/$DIR2/daily.bk

# BZip2 The Newly Created Archive
echo ""
echo "Compressing $DIR/$TAR With BZip2....";date
# Use the bzip2 compression algorithm to compress your backup file.
bzip2 -8 $DIR/$TAR

# Stamp the end of the Log with the completion time.
echo ""
echo "Archive Completed";date

The second script is very simple, it uses variables from the first script to lauch a series of commands that create a tar file of the files you are backing up. However, since it uses variables exported from the first file, it cannot be run by itself. The main reason for creating this second file was to allow this to be included during the weekly script, as well as to make it easier to add and remove files.

#!/bin/sh
#

# Imported Variables
# CMDC is the command to create a new tar file.
# CMD is the command to add a new file or group of files to an existing 
#     tar file.
# DIR is the directory where your backup files are being saved.
# TAR is the name of the tar file to create (this file will be removed 
#     after it has been compressed).

# Files To Archive Daily
$CMDC $DIR/$TAR /etc/fstab
$CMD $DIR/$TAR /etc/hosts
$CMD $DIR/$TAR /etc/inetd.conf
$CMD $DIR/$TAR /etc/master.passwd
$CMD $DIR/$TAR /etc/namedb
$CMD $DIR/$TAR /etc/named.conf
$CMD $DIR/$TAR /etc/ntp.drift
$CMD $DIR/$TAR /etc/passwd
$CMD $DIR/$TAR /etc/rc.conf
$CMD $DIR/$TAR /etc/services
$CMD $DIR/$TAR /usr/local/apache/conf/httpd.conf
$CMD $DIR/$TAR /etc/crontab
$CMD $DIR/$TAR /var/backups
$CMD $DIR/$TAR /mysql