Adsense

Wednesday, September 3, 2014

db2 automatic backup configure using crontab in linux.

In this tutorial, let us see the steps how to configure automatic backup in db2 using crontab in linux.  UNIX or Linux cron jobs can be scheduled with the crontab command. In our example, let us configure to take automatic db2 backup on every saturday 10 PM.

1. Create a shell script in a folder (/db2/scripts) that takes online full backup.  For online backup, please ensure that you have enabled online backup.

$  vi backup.ksh

       echo "Online Backup Starts at `date` ... "

        /home/db2inst1/sqllib/bin/db2 backup db dbname online to /db2/backup/ include logs

        echo "Backup completed at `date`"



    If you want to remove old backup files after the current backup is completed, you can include below lines. The following script deletes the backup file which is 7 days older

        echo "Before delete available backups : `ls /db2/backup/`"

        echo "Delete starts"

        find /db2/backup/ -mtime +7 -exec rm {} \;

 echo "Delete completed"

        echo "After Delete available backups : `ls /db2/backup/`"


Save the above file (which is in the folder /db2/scripts) .  Change file mode to execute.

chmod 777 backup.ksh

Manually you can test the above script by ./backup.ksh


Now let us schedule the above task to be executed periodically. Linux command crontab does this job. Now us configure. Run the below command

crontab -e
             -which launches an editing session that allows you to create a crontab file.

In this example, a cron job starts the shell script backup.ksh at 10 p.m.


 To schedule backup,  every Saturday at 10:00 p.m.
#
0  22    * * 6  /usr/bin/su - db2inst1 -c "/db2/scripts/backup.ksh"  
#  


If you want to capture the output in a log file, then the above line can be replaced with the below.
/usr/bin/su - db2inst1 -c "/db2/scripts/backup.ksh > /db2/logs/fullbackup.log"


Now save the cron job. (Esc : wq). Now you are done.

To schedule the above job on every monday to friday at 10.00 PM
30 22 * * 1,2,3,4,5 /usr/bin/su - db2inst1 -c "/db2/scripts/backup.ksh"


To schedule any job (For eg. incremental backup)  twice a day at 11:30 a.m. and at 9:30 p.m. on every monday through saturday, you can wrire something like

30 11,21 * * 1-6  /usr/bin/su - db2inst1 -c "/db2/scripts/backup.ksh"

In the crontab line entry, * indicates all allowed values

crontab -l : to list all scheduled jobs

crontab -r : to remove scheduled jobs

For more on crontab, you can read at http://en.wikipedia.org/wiki/Cron

1 comment:

  1. Please also tell us how to configure backup success and failure script in this process.

    Thanks
    Ahlyan

    ReplyDelete