Bash Backup Script

OCPdba.Net

  • Using the script and files below, you can backup selected directories and files to a location
  • You can also have the backup log placed somewhere for your webserver to show the logs

  • config.txt - this stores the configuration for the script
  •    
       ##############################################################################
       # Author:	Ahbaid Gaffoor, OCPdba.Net
       # File:		config.txt
       # Date:		Sunday 24th NOvember 2002
       # Use:		Configuration for backup.sh
       ##############################################################################
       
       # The target directory for backups
       TARGET=/mnt/n/winbox/linuxshare
       
       # Web Server Logging
       WEBSERVER=Y
       WEBDIR=/var/backuplogs
       
             

  • candidates.txt - this stores the directories and files to be backed up
  •    
       ##############################################################################
       # Author:	Ahbaid Gaffoor, OCPdba.Net
       # File:		candidates.txt
       # Date:		Sunday 24th NOvember 2002
       # Use:		Directories and files to be backed up by backup.sh
       ##############################################################################
       
       # Usage:
       # ======
       # d directory
       # f file
       # Any other start character causes the line to be ignored
       
       # ===========
       # Directories
       # ===========
       
       # web logs
       d /var/log/httpd
       
       # scripts
       d /root/backup_scripts
       
       # backup logs
       d /var/backuplogs
       
       # =====
       # Files
       # =====
       
       # website
       f /etc/httpd/httpd.conf
       f /etc/httpd/vhosts.conf
       f /etc/httpd/default.conf
       
       # system
       f /var/log/messages
       
             

  • root.crontab - Sample crontab for root that runs the script at 3:00am daily
  •    
       ##############################################################################
       # Author:	Ahbaid Gaffoor, OCPdba.Net
       # File:		root.crontab
       # Date:		Sunday 24th NOvember 2002
       # Use:		crontab for backup.sh
       #	        To submit: "crontab root.crontab"
       ##############################################################################
       # ------
       # Notes:
       # ------
       #
       # Entry Format: minute, hour, day of month, month, day of week, command
       #
       # 0 is Sunday
       # 1 is Monday
       # 2 is Tuesday
       # 3 is Wednesday
       # 4 is Thursday
       # 5 is Firday
       # 6 is Saturday
       # 
       # minute, hour, day of month, month, day of week, command
       # Sunday = 0
       #
       # backup.sh - Backup directories and files 
       0 3 * * * /root/backup_scripts/backup.sh
       
             

  • backup.sh - The backup script
  •    
       #!/bin/bash
       ##############################################################################
       # Author:	Ahbaid Gaffoor, OCPdba.Net
       # File:		backup.sh
       # Date:		Sunday 24th NOvember 2002
       # Use:		Backup directories and files
       #
       # Notes:        The CONFIG_FILE allows specification of an optional
       #               webserver directory for placing backup logs
       ##############################################################################
       
       
       # Configuration files
       export CONFIG_PATH=/root/backup_scripts
       export CONFIG_FILE=$CONFIG_PATH/config.txt
       export CANDIDATE_FILE=$CONFIG_PATH/candidates.txt
       
       # Target Directory
       export TIMESTAMP=`date +%Y%m%d-%H%M%S`
       export TARGETDIR=`cat $CONFIG_FILE | grep -v ^# | grep ^TARGET | awk -F= '{print $2}'`"/"$TIMESTAMP
       
       # Target Log
       export TARGETLOG=$TARGETDIR"/backup.log"
       
       # Webserver logging info.
       export WEBSERVER=`cat $CONFIG_FILE | grep -v ^# | grep ^WEBSERVER  | awk -F= '{print $2}'`
       export WEBDIR=`cat $CONFIG_FILE | grep -v ^# | grep ^WEBDIR  | awk -F= '{print $2}'`
       export WEBLOG=$WEBDIR"/backup-"$TIMESTAMP".txt"
       
       # Create Target Directory if it does not exist
       if [ ! -d ${TARGETDIR} ]; then
          mkdir -p $TARGETDIR
       fi
       
       # Usage function
       function usage {
         echo
         echo "backup.sh"
         echo "  - See: $CANDIDATE_FILE for entries"
         echo
       }
       
       
       # Header function
       function header {
          echo "================================================================================="
          echo "Backup Log for `date` on `hostname`"
          echo "================================================================================="
          echo "Config file: $CONFIG_FILE"
          echo "Candidate file: $CANDIDATE_FILE"
          echo "Timestamp is: $TIMESTAMP"
       
         if test "$WEBSERVER" = "Y" ; then
            echo "Webserver logging: ON"
            echo "Webserver log to $WEBDIR"
         else
            echo "Webserver logging: OFF"
         fi
       }
       
       # Footer function
       function footer {
          echo
          echo "================================================================================="
          echo "Backup Log ended at `date`"
          echo "================================================================================="
       }
       
       # List Candidates function
       function list_candidates {
          echo
          echo "================================================================================="
          echo "Backup Candidates:"
          echo "================================================================================="
          cat $CANDIDATE_FILE
          echo "================================================================================="
       }
       
       # Directory backup function
       function backup_directories {
       
         echo
         echo "#################################################################################"
         echo "Backing up directories...."
         echo "#################################################################################"
         echo
       
         for dir in `cat $CANDIDATE_FILE | grep ^d | awk '{print $2}'`
         do
            export SRCDIR=$dir
            export TARFILE=$TARGETDIR"/"`echo $dir | awk -F/ '{print $NF}'`".tar"
       
            if [ ! -f ${TARFILE} ]; then
               echo
               echo "---------------------------------------------------------------------------------"
               echo "Creating $TARFILE for $dir"
               echo "---------------------------------------------------------------------------------"
               tar -cPvf $TARFILE $dir
            else
               echo "---------------------------------------------------------------------------------"
               echo "Adding to existing $TARFILE for $dir"
               echo "---------------------------------------------------------------------------------"
               tar -rPvf $TARFILE $dir
            fi
       
         done
       
       }
       
       # File backup function
       function backup_files {
       
         echo
         echo "#################################################################################"
         echo "Backing up files...."
         echo "#################################################################################"
         echo
       
         export TARFILE=$TARGETDIR"/files.tar"
       
         echo "---------------------------------------------------------------------------------"
         echo "Creating $TARFILE"
         echo "---------------------------------------------------------------------------------"
       
         for fn in `cat $CANDIDATE_FILE | grep ^f | awk '{print $2}'`
         do
       
            if [ ! -f ${TARFILE} ]; then
               tar -cPvf $TARFILE $fn
            else
               tar -rPvf $TARFILE $fn
            fi
       
         done
       
       }
       
       
       # Compress tar files
       function compress_files {
       
         echo 
         echo "#################################################################################"
         echo "Compressing tar files...."
         echo "#################################################################################"
         echo
       
         for tf in `ls $TARGETDIR/*.tar`
         do
            echo "---------------------------------------------------------------------------------"
            echo "Compressing $tf"
            gzip $tf
            ls -al $tf.gz | awk '{print $5" "$6" "$7" "$8" "$9}'
            echo "---------------------------------------------------------------------------------"
            echo
         done
       
       }
       
       #
       # Main call to functions
       #
       if [ $# -ne 0 ]; then
         usage
       else
       
         header 1>>$TARGETLOG 2>>$TARGETLOG
         list_candidates 1>>$TARGETLOG 2>>$TARGETLOG
         backup_files  1>>$TARGETLOG 2>>$TARGETLOG
         backup_directories 1>>$TARGETLOG 2>>$TARGETLOG
         compress_files 1>>$TARGETLOG 2>>$TARGETLOG
       
         # Optional webserver directory
         if test "$WEBSERVER" = "Y" ; then
            echo 1>>$TARGETLOG 2>>$TARGETLOG
            echo "Copying log to $WEBLOG" 1>>$TARGETLOG 2>>$TARGETLOG
            cp $TARGETLOG $WEBLOG
         fi
       
         footer 1>>$TARGETLOG 2>>$TARGETLOG
       
       fi
          
             

    OCPdba.Net