#!/usr/bin/ksh
. $HOME/.profile
#-----------------------------------------------------------------------------#
# Start customize section
#-----------------------------------------------------------------------------#
backup_dir=/db2/backups/others
typeset -i keep_days=21
#phone_home=xxxxxx@aaa.bbb.com
#this_pgm=backup_db
#work_dir=$HOME/xxxxxx
#-----------------------------------------------------------------------------#
# End customize section
#-----------------------------------------------------------------------------#
typeset -i error_count=0
server=`uname -n`
if [[ $1 = '' ]] ; then
echo "Error: database not specified"
exit 99
else
typeset -l dbnamel=$1
typeset -u dbnameu=$1
fi
Exit_error()
{
#mail -s "Errors encountered during backup of $dbnameu on $server" $phone_home < $this_pgm.out
exit 99
}
#cd $work_dir
#echo "`date`: $this_pgm started"
echo "connect to $dbnamel"
db2 -v connect to $dbnamel
sqlcode=$?
if [[ $sqlcode != 0 ]] ; then
echo "Cannot connect to $dbnameu, error code $sqlcode - exiting"
Exit_error
fi
db2 -v quiesce db immediate
db2 -v connect reset
db2 -v deactivate db $dbnamel
echo "backup db $dbnamel to $backup_dir compress"
db2 -v backup db $dbnamel to $backup_dir compress
sqlcode=$?
if [[ $sqlcode != 0 ]] ; then
echo "Backup of $dbnameu failed with error $sqlcode, will unquiesce and exit"
error_count=$error_count+1
else
echo "Backup of $dbnameu created in $backup_dir"
fi
db2 -v connect to $dbnamel
db2 -v unquiesce db
db2 -v connect reset
db2 -v activate db $dbnamel
if [[ $error_count != 0 ]] ; then
echo "Errors encountered, not removing old backups"
Exit_error
else
echo "Following backup images older than $keep_days days have been removed:"
find $backup_dir -name "$dbnameu.*" -mtime +$keep_days -type f -exec ls {} \;
find $backup_dir -name "$dbnameu.*" -mtime +$keep_days -type f -exec rm {} \;
fi
echo "$0 ended successfully"
#mail -s "No errors encountered while backing up $dbnameu on $server" $phone_home < $this_pgm.out