Sample Backup Shell Script


The term UNIX is a registered trademark by The Open Group, and is used within this document
for the purpose of generically describing all BSD and System V derived operating systems.


One very important thing you must always do is BACK UP YOUR SYSTEM! Computers crash, even those running a *nix operating system, and Murphy was an optimist when it comes to computers. Therefore, you MUST perform backups, preferably to a different machine or to removable media.

As an example to the UNIX® / Linux community, I have provided a sample script, written for a FreeBSD system with Samba (as a domain controller) and an FTP server and web server installed, to back up all of the most important files on the system. These happen to be the Samba configuration database files (for Samba 3 when using 'TDB' files), the files in '/etc', the files in '/usr/local/etc', and the web server files. It's a foregone conclusion that the other files can be more or less easily restored by 'make world' or its equivalent, or by re-installing a port or package. In some cases an application may have additional important configuration or data files. One example, PostgreSQL, stores its configuration and data files in '/usr/local/pgsql' on a FreeBSD system. Your backup should include these files as well.

This script contains a section that prompts you for 'Y' or 'N', then defaults to 'Y' if you don't press a key within 30 seconds, or if you simply press ENTER. An invalid response re-cycles by calling the script again. And if you don't run it as root, don't worry, the script will detect this and attempt an 'su' to root for you. The basic process is as follows:

  1. prompt for input, delay 30 seconds, proceed if no input so you can 'cron' it
  2. mount the '/backups' device (assumes you have an entry for it in '/etc/fstab')
    This could be an NFS share on another *nix system. If it is simply a directory, you can remove the 'mount' command.
  3. Create directories (if they don't already exists) to form a backup tree that consists of the latest version of the most important files. This is intentionally smaller than the final backup set. If you back up a database server, it would have the database configuration files but NOT the 'live' data files.
  4. Shut down Samba, and run 'tdbbackup' from the appropriate locations, to create a backup of the TDB files. This is probably implementation-dependent enough that you should look carefully at how samba is being stopped and re-started, and to check for the correct directories where the TDB files are located, then make changes as needed for your operating system.
  5. Re-start Samba (verify that '/usr/local/etc/rc.d/samba.sh' is the correct script for your system, and modify as needed).
  6. Copy all of the configuration files, and all of the 'backup TDB files', to approrpiate directories on the backup device.
  7. Copy the 'critical' web files to the appropriate directories on the backup device.
  8. Build a tarball consisting of ALL of the files that should be backed up, including the entire web directory, using a file name that is derived from the current GMT date and time. If you are backing up a database, you should include all of the data files, as well as the configuration files, and shut down the database server until the data files have been added to the tarball (then re-start it as needed).
  9. Compress the tarball, then copy it to the backup device.
  10. Delete any remaining files and do any other necessary cleanup

Backup Script

#!/bin/sh
# backup SAMBA databases and system files

DoCreateDir()
{

# create directory if it does not exist

  local szDir ;
  szDir="$@"

  if ( !( test -d "$szDir" ) )
  then
    echo creating "$szDir"
    mkdir "$szDir" ;
  fi
}


# this script must run as root.  su to root if not already root

  if ( test `id -un` != "root" )
  then
    eval "su -l root -c $0" "$@" ;
    exit ;
  fi

  curdir="`pwd`";

  echo This file will back up the SAMBA databases and copy various system
  echo -n files to the BACKUP machine.  Continue [Y/n] ?
  read -t 30 YN

# if no response within 30 seconds it defaults to 'Y'
  YN=${YN:-"Y"};
  YN=${YN%%[^YyNn]*};
  if ( test -z "$YN" )
  then
    echo -e "Please enter either \"Y\" or \"N\" " ;
    eval "$0" "$@" ;
    exit ;
  fi

# at this point 'YN' contains Y, y, N, or n

  if ( test "$YN" = "N" -o "$YN" = "n" )
  then
    exit ;
  fi

# step 1, shut down samba

  echo Shutting down SAMBA to perform TDB backups
  echo ""
  if ( !( smbcontrol nmbd shutdown && smbcontrol smbd shutdown ) )
  then
    echo ERROR:  Unable to shut down SAMBA - restarting ;
    /usr/local/etc/rc.d/samba.sh start ;
    exit ;
  fi

# back up the SAMBA databases
  echo backing up /var/db/samba
  cd /var/db/samba
  tdbbackup *.tdb

  echo backing up /usr/local/private
  cd /usr/local/private
  tdbbackup *.tdb

# re-start SAMBA
  echo re-starting SAMBA
  /usr/local/etc/rc.d/samba.sh start ;
  echo ""
  echo "SAMBA re-start complete (error check not performed - you should verify it's running)"
  echo ""

# make sure the backup path is mounted
# NOTE: change this to match where you want your backups to go.  In this
#       case, it's an NFS share defined in /etc/fstab that mounts to /backups
  echo Mounting NFS backup path, ignore errors at this point
  mount /backups

# create backup directories if they don't exist
  DoCreateDir "/backups/samba" ;
  DoCreateDir "/backups/samba/private" ;
  DoCreateDir "/backups/etc" ;
  DoCreateDir "/backups/etc.local" ;
  DoCreateDir "/backups/root.bin" ;
  DoCreateDir "/backups/www" ;
  DoCreateDir "/backups/www/data" ;
  DoCreateDir "/backups/www/cgi-bin" ;
  DoCreateDir "/backups/www/include" ;
  DoCreateDir "/backups/www/icons" ;
  DoCreateDir "/backups/www/ftp-root" ;
  DoCreateDir "/backups/www/ftp-root/etc" ;
  DoCreateDir "/backups/www/ftp-root/pub" ;


# copy the TDB files
  echo Backup of TDB files for SAMBA
  cp /var/db/samba/*.tdb.bak /backups/samba
  cp /usr/local/private/*.tdb.bak /backups/samba/private

# copy the /root/bin files (where all of my custom scripts go)
  echo "Backup of /root/bin files"
  cp /root/bin/* /backups/root.bin

# copy the /etc files in their entirety
  echo "Backup of /etc files"
  cp -rf /etc/* /backups/etc

# copy the /usr/local/etc files in their entirety
  echo "Backup of /usr/local/etc files"
  cp -rf /usr/local/etc/* /backups/etc.local

# copy of the 'primary' web site files (adjust these according to your system)
  echo "Backup of primary web site files"
  cp -f /usr/local/www/* /backups/www
  cp -f /usr/local/www/data/* /backups/www/data
  cp -f /usr/local/www/include/* /backups/www/include
  cp -f /usr/local/www/cgi-bin/* /backups/www/cgi-bin
  cp -f /usr/local/www/icons/* /backups/www/icons
  cp -f /usr/local/www/ftp-root/* /backups/www/ftp-root
  cp -f /usr/local/www/ftp-root/etc/* /backups/www/ftp-root/etc
  cp -f /usr/local/www/ftp-root/pub/* /backups/www/ftp-root/pub

# finally, create a TAR archive of all of the backup files and the entire www
# directory tree and copy it to the 'backup' directory directly, using the GMT
# system date as the file name.  The output path '/work' represents a directory
# that is big enough to handle anything.

  szFileName="backup-"`date -u "+%C%y%m%d%H%M%S"`".tar" ;
  echo Writing backup to $szFileName
  echo ""

  echo backing up /root/bin, /etc, and /usr/local/etc
  tar -c -f "/work/$szFileName" /root/bin
  tar -r -f "/work/$szFileName" /etc
  tar -r -f "/work/$szFileName" /usr/local/etc

  echo backing up samba files
  tar -r -f "/work/$szFileName" /var/db/samba/*.tdb.bak
  tar -r -f "/work/$szFileName" /usr/local/private/*.tdb.bak

# for your system, alter this next section accordingly
# the sample is for FreeBSD, which stores its installed
# package info in /var/db/pkg - for linux systems you might
# consider backing up other directories, such as /var/backups
# or /var/lib/dpkg , depending upon where your system stores
# this kind of information.

  echo backing up installed package database (FreeBSD systems)
  tar -r -f "/work/$szFileName" /var/db/pkg

  echo "backing up the web server (all of it this time)"
  tar -r -f "/work/$szFileName" /usr/local/www

  echo Compressing $szFileName
  gzip "/work/$szFileName"

  echo copying to backup directory
  cp -f "/work/$szFileName"".gz" /backups

# cleanup, change directory back, and exit

# remove the temporary file (I am done now)
  rm -f "/work/$szFileName"
  rm -f "/work/$szFileName"".gz"

  echo "Backup is now complete!!"
  echo ""

  cd "$curdir" ;

# the end

©2005-2013 by Stewart~Frazier Tools, Inc. - all rights reserved
Last Update: 6/23/2013
Back to 'Windows to Unix®' page
Back to S.F.T. Inc. main page