FSBackup: Automate tar-Based Backups

By Mitch Stuart
Copyright © 2003-2005 FullSpan Software  -  Usage subject to license
Software Version: 1.6  -  Document Version: $Revision: 1.5 $, $Date: 2005/04/16 07:59:16 $

Contents

1. Introduction

2. Features and Limitations

3. Using FSBackup

3.1. Required Parameters

3.2. Directories

3.3. Operation

3.4. Options

3.5. Backup File Selection

3.6. File Exclusion

4. Installation

4.1. Linux

4.2. Windows

5. Alternate Notification Configuration

6. Programming and Testing Information

1. Introduction

FSBackup is a shell script that automates the execution of backups using the tar archiving utility. It is designed for use by technically knowledgable users (programmers, system administrators, or other power users), for backing up a small network or individual computers.

Here is an example of how FSBackup can be used to backup a small network of Windows and Linux computers:

Getting FSBackup

2. Features and Limitations

FSBackup offers the following features: FSBackup has some limitations:

3. Using FSBackup

Run FSBackup with the following command line syntax:
  fsbackup.sh type bkname defdir destdir [options]
For example:
  fsbackup.sh full jdoe-backup /home/jdoe/etc/backup-defs /home/jdoe/backup
FSBackup creates a tar.gz file containing the files you specify. It uses GNU tar and gzip.

3.1. Required Parameters

The first 4 parameters are required and must be given in the order shown above.

3.2. Directories

FSBackup uses several directories under destdir. These subdirectories are created if they do not exist yet:

3.3. Operation

FSBackup creates the backup file bkname-timestamp-type.tar.gz and the log file bkname-timestamp-type.log in the directory destdir/current. For example, if the backup is named backup-websrv, then the filenames for a full backup would look like:
  backup-websrv-2003-04-09-011500-full.tar.gz
  backup-websrv-2003-04-09-011500-full.log
The current directory contains a maximum of one full backup for each bkname, plus any incremental and/or differential backups that have been created since that full backup. When a new full backup is created, FSBackup examines the destdir/current directory to see if there are any existing backups for the same bkname. If so, those files are moved to the destdir/archive directory before beginning the new full backup.

3.4. Options

The following options are supported and can be specified in any order:

3.5. Backup File Selection

Create a backup definition file called bkname-list.sh in the defdir directory. For example, if bkname is jdoe-backup then the backup definition file would be defdir/jdoe-backup-list.sh.

This file should contain a set of commands to generate the list of files and/or directories to back up. The definition file will be source'd (not invoked) to generate the list of files. It can be as simple or complex as needed. It can simply echo a list of files, or it can run one or more programs to generate the list. The list must be in the format of one file or directory per line, as consumed by the tar --files-from option.

Here is a very simple definition file:

  echo 'somefile'
  echo 'somedir'
Here is a more complex file:
  #!/bin/bash
  cd /home/user1/data
  echo -e '-C\n/home/someuser/data'
  echo -e "file1\ndir2"
  
  cd /home/user2/data
  echo '-C'
  echo '/home/user2/data'
  find . -maxdepth 1 -not -name 'db' -not -name '.' -printf '%P\n'
  ls -t db/checkpoint.* | head -1
Here are some tips for creating a backup definition file:

  1. The simplest technique is to just echo a file or directory name (e.g., echo 'somefile'). You can also echo a list of names, as long as you insert newlines so that each name appears on its own line when the script is run (e.g., echo -e "file1\ndir2").

  2. The #!/bin/bash line is optional. Because the bkname-list.sh file will be sourced, not invoked, it is not required. However, you may want to include this line to simplify running the file as a standalone script for testing, so you do not have to prefix the script name with 'source ' or '. '.

  3. Similarly, the cd commands are not needed for FSBackup, but they allow the bkname-list.sh script to be run for testing, so you can see the file list that will be generated when FSBackup runs the script.

  4. The -C commands are a tar feature that allow you to instruct tar to change directories. For example, let's say you need to backup two files that are in separate directory trees (i.e., their only common parent directory is /):
      /home/jane/a/b/c/d/hello.txt
      /var/db/x/y/foo.log
    Without the -C flag, you would have to run FSBackup from the / directory and use a backup definition file like this:
      echo 'home/jane/a/b/c/d/hello.txt'
      echo 'var/db/x/y/foo.log'
    The tar file will store the files with all the path information shown, that is, 'home/jane/a/b/c/d/hello.txt' and 'var/db/x/y/foo.log'.

    But let's say you don't want to store all the parent directories. With the -C option, you could use a definition file like this:

      cd /home/jane/a/b/c
      echo '-C'
      echo '/home/jane/a/b/c'
      echo 'd/hello.txt'
      
      cd /var/db/x
      echo '-C\n/var/db/x'
      echo 'y/foo.log'
    With this file, tar will store the files as 'd/hello.txt' and 'y/foo.log'. Obviously this example is simplified, but you can see that the -C option is handy (in fact, essential) when you want to 'factor out' intervening directories.

    In the output from your -list.sh script, the -C needs to appear on one line, and then the directory on the next line.

  5. tar does not support wildcards in file or directory names that you specify for inclusion in the tar file. Therefore, to use wildcards you should use ls, find, or some other command in your backup definition file, to expand the wildcards to specific names that tar will process. tar does support wildcards for files that you want to exclude - this is discussed below.

  6. To test your backup definition file, first run it (or source it) as a shell script, and examine the output. The list of files and directories that you see is what will be fed to tar when you run FSBackup. Then, do a test backup and examine the resulting log file and tar.gz file, to ensure that the files you intended are included.

3.6. File Exclusion

Optionally, you can create a file called bkname-exclude.txt in the defdir directory. This file should contain a list of filenames or filename patterns to exclude from the backup. The format of the file is one pattern per line, as consumed by the tar --exclude-from option. For example:
  *.bak

4. Installation

4.1. Linux

To use FSBackup on Linux, simply copy the fsbackup.sh file to a directory of your choice.

FSBackup may work with other flavors of *nix, but it has only been tested on Linux.

4.2. Windows

To use FSBackup on Windows, you will need to install the Cygwin package, which includes Windows versions of the *nix programs that FSBackup uses (bash, tar, ls, and so on). Once you have installed Cygwin, you will be able to use FSBackup on Windows just as you do on Linux.

There are other *nix command packages like Cygwin that run on Windows, and FSBackup may work with those, but it has only been tested with Cygwin.

Email notifications in Windows
If you want to use the -notify option to have FSBackup send an email summary of each backup, you will need to find a Windows version of the mail utility. Assuming you have Cygwin installed, here is one way to make the mail utility available on Windows:

  1. Use the Cygwin installer to download and install the sSMTP package if you do not have it installed already (it is not included in the default Cygwin base package).

  2. Use the ssmtp-config command to configure sSMTP for your mail server (here is some information on sSMTP configuration).

  3. Create a script called mail to invoke the ssmtp utility, and put it in a directory on your path. An example mail.sh script is included in the download package for FSBackup, or you can view the script here.

  4. Test the mail script from the command line. Once you get this working, you now have a bare-bones mail utility that has sufficient functionality to be used for the FSBackup -notify option.

5. Alternate Notification Configuration

As described earlier, FSBackup uses the mail command to send email notification. If you prefer to use a different command to send email or other notification, you can set the FSBACKUP_MAIL environment variable. If this variable is set, when you request notification FSBackup will issue the command:
   $FSBACKUP_MAIL <summary> <destination>
Where summary is a one-line summary of the backup statistics, and destination is the notification address specified on the FSBackup command line.

For example, I like to use my JMailSend program to send email notifications, because it is written in Java and therefore cross-platform, and it is easy to configure. I set FSBACKUP_MAIL like this:

   export FSBACKUP_MAIL="$HOME/bin/jmailsend-backup-summary.sh"
And my jmailsend-backup-summary.sh is something like this:
   java -jar "$JMAILSEND_JAR" "$JMAILSEND_PROPERTIES" "$1" "$2" <<EOT
   EOT

6. Programming and Testing Information

FSBackup is a bash script that uses several standard *nix commands (tar, find, awk, and so on).

It has been tested on Linux and on the Cygwin environment under Windows.

You can use the fsbackup-test.sh script included in the FSBackup download package to test FSBackup in your environment. The script takes several minutes to run, because it contains some sleep commands to allow time to elapse for some of the tests.

tar Limitations There are a couple of limitations in the way that the tar utility works that had an effect on the way FSBackup was written: