Wednesday 10 May 2017

Shell script to backup DB2 database even if applications are connected with group owner ID and schedule at specific time

In this document we will use two scripts.
First script backup the DB2 database and second script deletes backup older than 90 days.



1. backup.sh

# This script backup the DB2 database named mydb at the path /home/db2inst1/backups


#!/bin/sh

/home/db2inst1/sqllib/db2profile

/home/db2inst1/sqllib/bin/db2 force applications all
/home/db2inst1/sqllib/bin/db2 deactivate database moit
/home/db2inst1/sqllib/bin/db2 force applications all
/home/db2inst1/sqllib/bin/db2 backup database testdb to "/home/db2inst1/backups"


#db2set DB2COMM=TCPIP


# after creating backup.sh file execute following command to make the file executable.

# chmod -R 775 backup.sh

 2. housekppping.sh

# This script deletes backup files older than the 90 days 

#!/bin/sh

logfile= /home/db2inst1/backups/backup.log

rm -f $logfile

for file in `find  /home/db2inst1/backups  -mtime +90 -type f -name '*.backup' `
do
  echo "deleting: " $file >> $logfile
  rm $file
done

exit 0



# after creating backup.sh file execute following command to make the file executable.

# chmod -R 775 housekppping.sh


Execute following command to schedule your script to run at 1 pm and 10 pm daily 

[root]# crontab -e
0 13 * * * /home/db2inst1/backups/backup.sh
0 22 * * *  /home/db2inst1/backups/backup.sh
0 22 * * *  /home/db2inst1/backups/housekeeping.sh