Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Posts
    1

    Question Unanswered: URGENT - Help with Backup Script

    DB2 Experts
    I request you to review this backup script and see if the following are intact in the script. Please let me know if there are any changes that I should be doing to this script. Please treat this as urgent.

    Thanks much for any help!!

    1. DB2 DB Backup
    2. Trap the 'Error' or 'Success' of the Backup
    3. eMail the eMail ID a file with detailed message where the 'Success' or 'Failure' of the Backup will be stored in AIX once the Backup completes (Error or Normal completion)
    4. Any other additions that I need to do in the script in order to ensure that the script is in GOOD Shape

    I am looking to move this in to PROD and need your expert validation. Thanks Much.

    Also, this script seems to be failing occasionally with SQL2428N error message. We use NetBackup and do not know why the backup ends reporting 'Log not found' but when we check the log directory, things seem to be intact but for the log which it is looking for... Wierd! . Please help!!

    Backup Shell Script:

    #!/usr/dt/bin/dtksh

    # <db2backup>

    #
    # This program will run Db2 Backup
    #

    USAGE=" $0 backup -s SID -c NB-Class [-p password] [-t Minutes]
    [-m Mail-name] [ -d backup-Directory ]
    $0 remove -s SID -c NB-Class [ -m Mail-address]
    $0 check -s SID [-p password] [-m Mail-address]
    [-u Utilization percent]"

    # <backup> (DB to Disk)
    # This form is used to run DB and requires the SID, and
    # the NetBackup class name as argments.
    # If the db2 OS User account has a password other than,
    # than the -p argment must be supplied along with the correct password.
    # The -t option is the minutes between the remove and DB processes.
    # The default number of Minutes to wait is 10 minutes
    # If -m is specified, and an error happens, the log file will be mailed
    # to Mail-name.
    #
    # <remove> (DB to Tape)
    # This form is used to only remove old archive files that have been backed up.
    # Files older than TAG file named /.<NetBackup Class>
    # See above for options.
    #
    # <check>
    # The third form is used to check the /BKUP/<SID>/log_archive filesystem
    # to see if it is more than 50% full. It requires the SID and optionally
    # the db2 OS User account password. The percentage can be changed by
    # using the -u option above.

    # OS specific df command
    case $(uname) in
    AIX) DF="df -Pk" ;;
    SunOS) DF="df -k" ;;
    esac

    logdir=/BKUP/APPLlogs
    keep=5

    # OUTPUT:
    #
    # Log files are written to <logdir>. Each run of this program creates
    # a new log file named <program>_<process-id>.log. Logs are kept for
    # <keep> days. If the job exits abnormally, a message is sent to STD out
    # if the -m option is used, the message is send to the person specified.

    ################################################## #############################
    # subroutines
    #############

    function get_options
    {
    if [[ $# -gt 0 ]]
    then
    TYPE=$1
    shift
    fi
    while getopts d:c:m:s:t:u: option
    do
    case $option in
    d) BackupDir=$OPTARG
    ;;
    c) NetBackupClass=$OPTARG
    ;;
    m) MailPerson=$OPTARG
    ;;
    p) OraclePassword=$OPTARG
    ;;
    s) Sid=$OPTARG
    ;;
    t) Minutes=$OPTARG
    ;;
    u) UtilizationPercent=$OPTARG
    ;;
    *) print -u2 "Error! Unknown option: $option.\n"
    print -u2 "Usage: $USAGE"
    process_error
    ;;
    esac
    done

    if [[ -z $TYPE ]] # check for TYPE of run (backup check remove)
    then
    print -u2 "Error! You must specify what to do.\n"
    print -u2 "Usage: $USAGE"
    process_error
    fi

    if [[ -z $Sid ]] # check for SID on command-line
    then
    print -u2 "Error! You must specify a SID.\n"
    print -u2 "Usage: $USAGE"
    process_error
    fi
    }

    function do_brremove
    {

    trap process_error 1 2 3 ERR

    # Remove backed up archive files

    typeset Dirs="$@"

    print "\n** Output from $DF $BackupDir **"
    $DF $BackupDir | cat
    print "\n** Output from ls -lR $BackupDir/log_archive **"
    ls -lR $BackupDir/log_archive

    # create directories, if needed

    [[ ! -d $BackupDir/log_archive/old_logs ]] && \
    mkdir -p $BackupDir/log_archive/old_logs

    # Find files and remove them

    if [[ -f /.$NetBackupClass ]]
    then
    print "\n** Deleting Files older than /.$NetBackupClass **"
    ls -l /.$NetBackupClass
    print "\n** Output from find (deletion) **"
    find $Dirs -type f ! -newer /.$NetBackupClass \
    -exec ls -ld {} \; \
    -exec rm {} \;
    find $Dirs -type d ! -newer /.$NetBackupClass \
    -exec ls -ld {} \; \
    -exec rmdir {} \; 2>/dev/null || :
    fi
    }

    function do_move
    {
    trap process_error 1 2 3 ERR

    if [[ -n $(ls $BackupDir/log_archive/*.dbf.Z 2>/dev/null) ]]
    then
    print "\n** Moving *.dbf.Z to old_logs **"
    mv $BackupDir/log_archive/*.dbf.Z \
    $BackupDir/log_archive/old_logs
    fi

    print "\n** Output from $DF $BackupDir **"
    $DF $BackupDir | cat
    }

    function check_processes
    {
    trap process_error 1 2 3 ERR

    procs="bpbkar.* $Class "
    trap process_error 1 2 3 ERR

    if [[ $(ps -ef|egrep "$procs"|grep -vc grep) -gt 0 ]]
    then
    print "\n** Found the following processes **"
    ps -ef|egrep "$procs"|grep -vc grep
    print "$(date) Sleeping..."
    while [[ $(ps -ef|egrep "$procs"|grep -vc grep) -gt 0 ]]
    do
    sleep 30
    done
    print "$(date) Done."
    fi
    }

    function do_DB
    {
    trap process_error 1 2 3 ERR

    # create directories, if needed

    [[ ! -d $BackupDir/DB ]] && \
    mkdir $BackupDir/DB

    db2 backup db ${SID} online to /BKUP/${SID}/DB WITH 6 BUFFERS BUFFER 4096 PARALLELISM 2 COMPRESS EXCLUDE LOGS
    cd /BKUP/$SID/DB;/APPLmnt/$SID/exe/DB -s ${SID} -bm RETRIEVE
    }

    function do_check_archive
    {
    trap process_error 1 2 3 ERR
    }

    function process_error
    {
    exitcode=$?
    trap "" 1 2 3 ERR
    [[ $exitcode -ne 0 ]] && print "\nExitCode=$exitcode"
    print "\n** $host:$prog ** $(date) ** Abort."
    sleep 1
    exec 2>&6
    exec 1>&5
    if [[ -n $MailPerson ]]
    then
    cat $logfile | \
    mailx -s "ERROR! $host:$prog had errors. SID: $SID" $MailPerson
    else
    cat $logfile
    print "\nERROR! $host:$prog had errors. SID: $SID"
    print "Log file: $logfile"
    fi
    exit 11
    }

    ################################################## #############################
    # Main
    ######

    . $HOME/.profile

    umask 000 # create all files 666 and directories 777

    host=$(hostname)
    prog=${0##*/} # get program name. same as progname ??

    logfile=${logdir}/${prog}_$$.log

    # create log directories, if needed
    [[ ! -d $logdir ]] && mkdir -p $logdir

    exec 5>&1 # save stdout
    exec 6>&2 # save stderr
    exec 1> $logfile 2>&1 # direct STD out & STD err to logfile

    trap process_error 1 2 3 ERR

    print "** $host:$prog ** $(date) ** Begin.\n"

    # cleanup old log files
    find $logdir -name ${prog}_\*.log -mtime +${keep} -exec rm {} \;

    typeset -i Minutes=10 # default Minutes to 10 Minutes
    typeset -i Seconds=0

    print "** Processing command line **"
    print "$0 $@\n"
    get_options "$@" # process command-line options

    typeset -u SID=$Sid # SID is all upper case
    typeset -l sid=$Sid # sid is all lower case

    OraclePassword=${OraclePassword:=manager} # default password to manager
    BackupDir=${BackupDir:=/BKUP/$SID} # default backup directory
    UtilizationPercent=${UtilizationPercent:=50} # default disk percent

    ((Seconds=Minutes*60)) # get number of seconds to wait after remove

    if [[ ! -d $BackupDir ]]
    then
    print -u2 "Error! $BackupDir must already exist!"
    process_error
    fi

    case $TYPE in
    removeArchive)
    print "** Running removeArchive **"
    check_processes # sleep, if necessary
    do_brremove $BackupDir/log_archive
    ;;
    removeBackup)
    print "** Running removeBackup **"
    check_processes # sleep, if necessary
    do_brremove $BackupDir/DB
    ;;
    remove)
    print "** Running remove **"
    check_processes # sleep, if necessary
    do_brremove $BackupDir/log_archive $BackupDir/DB
    ;;
    backup)
    print "** Running backup **"
    check_processes # sleep, if necessary
    do_brremove $BackupDir/log_archive $BackupDir/DB
    print "\nSleeping for $Seconds seconds"
    sync
    sleep $Seconds
    do_DB
    ;;
    check)
    print "** Running check archive **"
    do_check_archive
    ;;
    *)
    print -u2 "Error! Invalid TYPE.\n"
    print -u2 "Usage: $USAGE"
    process_error
    ;;
    esac

    print "\n** $host:$prog ** $(date) ** End."
    exit 0


    Cheers
    Ram
    Last edited by ramsf; 01-27-09 at 23:27.

  2. #2
    Join Date
    Apr 2008
    Posts
    51
    A very good backup script is available with this product: http://www.db2topgun.com/dbapf5
    db2topgun.com

  3. #3
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650
    Please treat this as urgent.
    A gentle reminder .. No one in this forum is being paid or has an obligation to respond. Therefore it is never urgent.
    Visit the new-look IDUG Website , register to gain access to the excellent content.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •