#!/bin/bash # add -x to the line above to activate debugging ########################################################################### # # Shell program to backup files and directories. # # Copyright 2002, Mertens Bram . # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # This software is part of the LinuxCommand.org project, a site for # Linux education and advocacy devoted to helping users of legacy # operating systems migrate into the future. # # You may contact the LinuxCommand.org project at: # # http://www.linuxcommand.org # # Description: # # # # Usage: # # bu [ -h | --help ] [ -e | --evolution ] # ( -s | --source ) SOURCE ( -d | --destination ) DEST/ # # Options: TODO fix this # # -h, --help Display this help message and exit. # # -e, --evolution Create a backup of evolution # # -s, --source file or directory to backup # # -d, --destination directory where the backup will be # created # # # Revisions: # # 08/07/2002 File created by lc_new_script v.2.0.7 # 28/01/2004 0.7 changed the code that checks the command line # options # 13/02/2004 0.8 fixed 'undeclared variables' bug # cleaned up code # 19/03/2004 0.9 added code to create EXCLUDELIST if it doesn't # exist # 20/03/2004 0.10 added usage and help message # improved code added in version 0.9 # # TODO # # Clean up code # Add further testing to test_source and test_target # Send all output to stderr or stdout? # Check signal_exit: call *_exit? # Reformat usage and help # remove code that checks backup from graceful_exit # bu now reports an error when --help is called # ########################################################################### ########################################################################### # Constants ########################################################################### SOURCE_DIR= TARGET_DIR= PROGNAME=$(basename $0) VERSION="0.10" EXT=.tar.bz2 SPLIT= COUNTER=1 DATE=$(date +'%Y%m%d') # The name of the backup is in YYYYMMDD-format BU_EVOLUTION= EXCLUDELIST=/data/backup/evolution/exclude-index CREATE_EXCLUDE= ########################################################################### # Functions ########################################################################### function error_exit { ##### # Function for exit due to fatal program error # Accepts 1 argument # string containing descriptive error message ##### echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2 exit 1 } function graceful_exit { ##### # Function called for a graceful exit # Tests whether or not the backup was created # No arguments ##### if [ -f ${TARGET_DIR}/${DATE}${SPLIT}${EXT} ]; then echo "backup ${TARGET_DIR}/${DATE}${SPLIT}${EXT} created" exit else echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2 echo "backup ${TARGET_DIR}/${DATE}${SPLIT}${EXT} has not been created" exit 1 fi } function signal_exit { ##### # Handle termination signals # Arguments: # 1 signal spec (required) ##### # Fatal error if required arguments are missing if [ "bu" = "" ]; then error_exit "signal_exit: missing argument 1" fi case bu in INT) echo "bu: Aborted by user" # TODO call int_exit? exit ;; TERM) echo "bu: Terminated" # TODO call term_exit? exit ;; *) error_exit "Terminated on unknown signal" ;; esac } # end of signal_exit function term_exit { ##### # Function to perform exit if termination signal is trapped # No arguments ##### echo "${PROGNAME}: Terminated" graceful_exit } function int_exit { ##### # Function to perform exit if interrupt signal is trapped # No arguments ##### echo "${PROGNAME}: Aborted by user" exit } function usage { ##### # Function to display usage message # No arguments ##### echo "Usage: ${PROGNAME} [ [-h | --help] | ( -e | --evolution ) [ -s | --source ] SOURCE [ -d | --destination ] DEST/ ]" } function helptext { ##### # Function to display help message for program # No arguments ##### usage cat <<- -EOF- -h | --help This help message. -e | --evolution Use this option when creating a backup of ~/evolution/ . When using this option the files in the excludelist are not backed up. -s | --source SOURCE This option is (obviously) required. This dir will be tar'd, compressed with bzip2 and backed up to DEST. -d | --destination DEST/ This option is also (obviously) required. This is the dir where the backup will be created. For this reason this dir must be writable. If it does not exist this script will try to create it. -EOF- } function test_source { ##### # Function to check that source directory exists # No arguments ##### # TODO test whether dir is readable? if [ ! -d ${SOURCE_DIR} ]; then echo "Source directory ${SOURCE_DIR} does not exist!" 1>&2 exit 1 fi } function test_target { ##### # Function to check that target directory exists, and create it if it does not # No arguments ##### # TODO: test whether or not creating dir was succesful # test whether dir is writable? if [ ! -d ${TARGET_DIR} ]; then echo "Target directory ${TARGET_DIR} does not exist!" 1>&2 echo "Creating ${TARGET_DIR}" 1>&2 mkdir -p ${TARGET_DIR} fi } function bu_normal { ##### # Function to perform the actual backup # No arguments ##### echo "Creating backup of ${SOURCE_DIR}" tar -cjvf ${TARGET_DIR}/${DATE}${SPLIT}${EXT} ${SOURCE_DIR} graceful_exit } function create_excludelist { echo "${EXCLUDELIST} does not exist!" if [ "$CREATE_EXCLUDE" = "1" ]; then echo "Could not create ${EXCLUDELIST}" echo "NO BACKUP CREATED!!!" echo "Create the file ${EXCLUDELIST} by running:" echo "find ${SOURCE_DIR} -name "mbox.*" > ${EXCLUDELIST}" echo "find ${SOURCE_DIR}mail/pop -name "cache" >> ${EXCLUDELIST}" echo "echo "${SOURCE_DIR}cache" >> ${EXCLUDELIST}" echo "and rerun ${PROGNAME}" exit 1 else echo "creating ${EXCLUDELIST} ..." find ${SOURCE_DIR} -name "mbox.*" > ${EXCLUDELIST} find ${SOURCE_DIR}mail/pop -name "cache" >> ${EXCLUDELIST} echo "${SOURCE_DIR}cache" >> ${EXCLUDELIST} CREATE_EXCLUDE=1 bu_evolution fi } function bu_evolution { ##### # Function to perform the actual backup # first check if the file already exists # exclude 'mbox.*' and certain cache dirs # ! Do NOT exclude ~/evolution/pop3/_account_/cache ! # No arguments ##### if [ -f ${EXCLUDELIST} ]; then echo "Creating backup of ${SOURCE_DIR}" tar --exclude-from=${EXCLUDELIST} -cjvf ${TARGET_DIR}/${DATE}${SPLIT}${EXT} ${SOURCE_DIR} graceful_exit else create_excludelist fi } ########################################################################### # Program starts here ########################################################################### ##### Command Line Processing ##### while [ "$1" != "" ]; do case $1 in -s | --source ) shift SOURCE_DIR=$1 ;; -d | --destination ) shift TARGET_DIR=${1%/} ;; -e | --evolution ) BU_EVOLUTION=1 ;; -h | --help ) helptext graceful_exit ;; * ) usage exit 1 esac shift done ##### Main Logic ##### test_source test_target # Perform the backup: check if backup already exists while [ $COUNTER -lt 10 ]; do if [ -f ${TARGET_DIR}/${DATE}${SPLIT}${EXT} ]; then echo "you already created a backup today! ${TARGET_DIR}${DATE}${SPLIT}${EXT}" echo "creating extra backup..." COUNTER=$((COUNTER + 1)) SPLIT=-${COUNTER} else if [ "$BU_EVOLUTION" = "1" ]; then bu_evolution else bu_normal fi COUNTER=10 exit # TODO can this exit be removed? fi done graceful_exit