On Tue, 2004-04-20 at 08:49, Andrew Conkling wrote:
> I've made a backup script to be executed in my crontab, but it's a bit
> dirty and I'd appreciate it if anyone could help me clean it up a bit.
> I've added comments to indicate what I'd like to do with it (i.e. where
> my scripting skills are inadequate):
> 
> #!/bin/bash
> evolution-1.5 --force-shutdown # I'd like this to execute only if the
> evolution-1.5 program is not running
> export TIME=`clock` # This spits out a lot of time info; I'd like to
> make this a bit terser
> tar
> -cfz /home/andrew/.evolution_files_$TIME.tar.gz /home/andrew/.evolution/* # This 
> command doesn't even work (I pulled it from an archived email).  
> 
> On the last step, I get the error "tar: Options `-[0-7][lmh]' not
> supported by *this* tar".  Google was no help.
> 
> I also wonder if there are any other cool tricks you all find useful in
> your own backup situations?

Hi

I'v attached the two scripts I use, whenever I want to create a backup I
call bu-evo which in turn calls bu.

Keep in mind that I use pop3 so you'll have to edit the command to
create the exclude-list to list the dirs Ron suggested if you're using
IMAP.

HTH

Bram
-- 
# Mertens Bram "M8ram"   <[EMAIL PROTECTED]>   Linux User #349737 #
# SuSE Linux 8.2 (i586)     kernel 2.4.20-4GB      i686     256MB RAM #
#  5:09pm  up 29 days 20:46,  9 users,  load average: 0.22, 0.18, 0.10 #
#!/bin/bash

# add -x to the line above to activate debugging

###########################################################################
#
#	Shell program to backup files and directories.
#
#	Copyright 2002, Mertens Bram <[EMAIL PROTECTED]>.
#
#	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
#!/bin/bash

echo 'shutting down evolution and background processes'

evolution --force-shutdown

echo 'backing up /home/m8ram/evolution/'

/home/m8ram/bin/bu -e -s /home/m8ram/evolution/ -d /data/backup/evolution/

echo 'backing up /home/m8ram/.gconf/apps/evolution/'

/home/m8ram/bin/bu -s /home/m8ram/.gconf/apps/evolution/ -d /data/backup/gconf/

Reply via email to