#!/bin/bash
#chkconfig:2345 81 05
#description:backup cctiw database
#backup database
cd /home/db2inst2/dbscript
rm -df *.log
echo "BACKUP DATABASE... "`date` >> backup.log
#the name of DATABASE
db_name=THPDB
#the days of bakcup
day_keep=7
#the path of backup file
file_path="/home/db2inst2/"
#detail process: 1.detect db backup files to need remain; 2.delete backup invalid files;3.backup new db
begin_filename=$db_name".0.db2inst2.NODE0000.CATN0000."
end_filename="`date +%Y%m%d --date=$day_keep' days ago'`"
filename=$begin_filename$end_filename"*"
#find path
find $file_path -name $begin_filename"*" >> find.log
row_num=`wc find.log -l`
#backup db
echo "========BACKUP FILE========" >> backup.log
echo "========LINE NUM OF find.log : "$row_num >> backup.log
result="`db2 backup database $db_name online to $file_path`"
echo $result >> backup.log
mat=`expr "$result" : ".*Backup successful.*"`
echo $mat >> backup.log
if [ "$mat" -eq 0 ]
then
echo "========Because backup failed, save FILE NAME is "$filename >> backup.log
else
echo "========DELETE FILE NAME is "$filename >> backup.log
rm -df $file_path$filename
fi
echo "========BACKUP DATABASE EXECUTE FINISH========" >> backup.log