Db2 set online logs and enable autodelete for logs and backup

A simple script to set logs for online backup :

 


#!/bin/sh

#First of all create archive logs dir in the home path of the instance owner.
mkdir -p /$HOME/archiveLog

#Change log settings.
db2 UPDATE DB CFG FOR $1 USING logarchmeth1 "DISK:/data/archiveLog" logprimary 15 logsecond 15 logfilsiz 8000

#Backup Db.
db2 BACKUP DATABASE $1 TO "/backup/db2";

#Update log and backup retention (I set to 2).
db2 update db cfg for $1 using rec_his_retentn 2

#Enable auto delete.
db2 update db cfg for $1 using auto_del_rec_obj on

#I setup backup to maintain the last 2 backup.
db2 update db cfg for $1 using num_db_backups 2

You can copy and paste in a script named “setLogs” give grant and run by instance owner:

./setLogs DatabaseName

Generate command list for all instance’s Dbs

-) Step 1 : this scrip is an example to prune all logs and backup of a db.

#################### prune.sh
db2 connect to $1
db2 PRUNE HISTORY 20131007 AND DELETE

 

-) Step 2 : generate a command list to prune object for all Dbs in the instance

db2 list database directory | grep alias | awk ‘{print “./prune.sh ” $4}’ > cmdlist

Execute command on all Db2 instances

Here the script I use to verify all backup on all dbs on all instances.
I use it with the “db2-check-backup.sh” documented Here

#!/bin/sh
UNIQUE=`date +%s`
LPATH=$(pwd)
INST=/tmp/instances.$UNIQUE
OUT=/tmp/outallinst.$UNIQUE
 
/opt/ibm/db2/V9.7/bin/db2ilist > $INST
 
while read LN; do
 echo "###########"  $LN  >> $OUT
 $LPATH/db2-check-backup.sh $LN >>$OUT
done < $INST
 
cat $OUT
rm $OUT
rm $INST

Check db2 backup

Here a usefull script to check backup on all dbs in a db2 instance.
It can be used with monitoring products as zabbix or nagios.

 
#!/bin/sh
INS=$1
 
NOW=`date -d '1 day ago' '+%Y%m%d'`
 
 
UNIQUE=`date +%Y%m`
DBS=/tmp/dbs.$UNIQUE
OUTZ=/tmp/outfordbs.$UNIQUE
 
su - $INS -c "db2 list database directory" | grep alias | awk '{ print $4 }' &gt; $DBS
 
while read LN; do
  RESULT=$(su - $INS -c " db2 list history backup since $NOW for $LN" | grep 'B  D' -m 1 | awk '{print $3}')
  if [ -z "${RESULT}" ]; then
   RESULT=" : !!!!!!!!!!!!!! !!!!!!!!!!!!!! ERROR"
  fi
  echo $LN $RESULT &gt;&gt; $OUTZ
done &lt; $DBS
 
cat $OUTZ
rm $DBS
rm $OUTZ