Enabling automatic backup in Db2

You can enable automatic database backup as part of your disaster recovery strategy to enable Db2 to back up your database both properly and regularly. Doing so will also enable automatic recovery history file pruning. For more information, see the Related information section.

About this task

Important: Enable automatic backup only if separate (not single) storage was selected at deployment. Separate storage selection creates a separate, persistent backup volume under the path /mnt/backup.

Procedure

  1. Exec into the Db2 pod.
  2. Become the Db2 instance owner if you are not the db2inst1 user:
    su - ${DB2INSTANCE}
  3. Create an automatic_backup.sh script.
    Note: This script only runs successfully if a separate, persistent backup volume is mounted under the path /mnt/backup.
    
    cat /db2u/tmp/automatic_backup.sh
    #!/bin/bash 
    [[ -z "${BLUMETAHOME}" ]] && source /etc/profile
    [[ -z ${DB2U_TMP} ]] && DB2U_TMP=/db2u/tmp
    # Set OS env using the Configmap file
    [[ -f ${DB2U_TMP}/os_envar_configmap ]] || \
        /db2u/scripts/update_env_from_configmap.sh --file ${DB2U_TMP}/os_envar_configmap
    . ${DB2U_TMP}/os_envar_configmap
    source /db2u/scripts/include/db2_functions.sh
    [[ ${RESTRICTED} != "true" ]] && export GLOBAL_SUDO_CMD="sudo"
    
    backup_mnt=$(df -TH | awk '/(mnt)?\/backup/ {print $NF}')
    if [[ ! -z "${backup_mnt}" ]]; then
        [[ $(ls -1d ${HOME}/sqllib/tmp/*.xml | wc -l) -gt 0 ]] || ${GLOBAL_SUDO_CMD} cp ${DB2U_SCRIPTS}/xml/* ${HOME}/sqllib/tmp/
    
        mkdir -p /mnt/backup/db2/backup
        db_list=($(get_db_list))
    
        for dbname in "${db_list[@]}"; do
            db2 connect to ${dbname}
            db2 update db cfg for ${dbname} using AUTO_DB_BACKUP ON
    
            db2 "call sysproc.automaint_set_policyfile( 'AUTO_BACKUP', 'DB2AutoBackupPolicy.xml' )"
            db2 "call sysproc.automaint_set_policyfile( 'MAINTENANCE_WINDOW', 'DB2MaintenanceWindowPolicy.xml' )"
        done
    fi
    
    By default, the script creates an AUTO_BACKUP policy with the following configurations:
    
    <BackupCriteria numberOfFullBackups="1"
       timeSinceLastBackup="168"
       logSpaceConsumedSinceLastBackup="6400"
    />
    
    By default, the script creates a MAINTENANCE_WINDOW policy with the following configurations:
    
    <OnlineWindow Occurrence="During" startTime="00:00:00" duration="24">
       <DaysOfWeek>All</DaysOfWeek>
       <DaysOfMonth>All</DaysOfMonth>
       <MonthsOfYear>All</MonthsOfYear>
    </OnlineWindow>
    
    If the preceding default configurations do not meet your backup requirements, you can tune these default settings by editing the following policy files:
    • /mnt/blumeta0/home/db2inst1/sqllib/tmp/DB2AutoBackupPolicy.xml
    • /mnt/blumeta0/home/db2inst1/sqllib/tmp/DB2MaintenanceWindowPolicy.xml
  4. Set permissions on the automatic_backup.sh script:
    chmod +x /db2u/tmp/automatic_backup.sh
  5. Run the script:
    ./db2u/tmp/automatic_backup.sh