Berthou.comA french developper blog | |
DB2 Backup ScriptAfter my news about DB2 backup script under Windows, I give you in this small article a solution to do the same thing under Linux. To Backup a database this IBM/DB2 gives you a graphic interface quite simple to use “Control-Center”, but if you want you include this backup in a batch script you must write a backup script without GUI. In this new I explain how to write a windows script procedure (.bat or .cmd) to backup your DB2 database. In this sample I considered that you must close your database before backup ( It’s possible to do this in “ONLINE” mode but it’s more complexe — include log file, rollforward…–) and I use a database SAMPLE (it is his name) for this test . the goal of this scripts is to :
Warning ! If you want to do a backup between two differents OS you must use db2move tools (for exmple a backup/restore between Windows and Linux not work). DB2 commandTo execute in a script a command you must initialize DB2 working context. In Windows it is made by a db2cmd but in linux/unix you can do this with the execution of db2profile. Commands :
File sample_backup.db2 CONNECT TO sample ; QUIESCE DATABASE IMMEDIATE FORCE CONNECTIONS; CONNECT RESET; BACKUP DATABASE sample TO "/db2_backup" WITH 2 BUFFERS BUFFER 1024 PARALLELISM 1 WITHOUT PROMPTING; CONNECT TO sample; UNQUIESCE DATABASE; CONNECT RESET; TERMINATE ; Warning : If your current user can not be connected to your database to make a backup you must change this user in CONNECT with these parameters. CONNECT TO sample USER monuser USING monpassword; DB2 saves in his catalogue somes informations about each backup. I advise you to clean it often with the command PRUNE HISTORY . PRUNE HISTORY 20071021 AND DELETE; This command will delete from the catalogue the informations on previous backup and will delete associated log files Batch script fileIn my opinion I’m not a Windows batch specialist and according to me the date calculation (delmax) of this script can be amelirated (more simple). #!/bin/sh # +----------------------------------------------------------+ # + Script de sauvegarde DB2 avec gestion de delete + # +----------------------------------------------------------+ # + Ver. + Auteur + Remarques + # +------+----------+----------------------------------------+ # + 1.00 + RBerthou + + # +----------------------------------------------------------+ # definition de l environnement de travail DB2 . /home/db2inst/sqllib/db2profile NBHISTO=12 MINDATE=`date --date "$NBHISTO days ago" +%Y%m%d` REM +- Fin initialisation -+ destdir=/db2_backup db2backupdir=$destdir/SAMPLE.0/DB2/NODE0000/CATN0000 echo "DB2 Backup" db2 -v -f$destdir/sample_backup.db2 -z$destdir/sample_backup.log returnCode=$? if [ "$returnCode" != "0" ] then echo "Erreur BACKUP DB2 : $returnCode" exit $returnCode fi echo " Suppression historique backup DB2 PIB " db2 -v CONNECT TO SAMPLE user myuser using mypassword db2 -v prune history $MINDATE and delete db2 -v connect reset returnCode=$? if [ "$returnCode" != "0" ] then echo "Erreur BACKUP DB2 PRUNE : $returnCode" exit $returnCode fi # Suppression physique des anciens fichiers DELTA=50 while [ $DELTA -gt $NBHISTO ] do MAXDATE=`date --date "$DELTA days ago" +%Y%m%d` rm /db2data/SAMPLE.0.db2inst.NODE0000.CATN0000.$MAXDATE* DELTA=$((DELTA - 1)) done echo "Fin BACKUP DB2 : $returnCode" exit $returnCode Remarques
Links
Introduction to DB2 UDB Scripting on Windows |
CategoriesTagsRecent Posts |
|
| |
3 Responses pour"DB2 backup script on Linux"
Hello!
This script is a great solution. After I backed up my data how can I restore it by a Linux script?
Can u provide me a script?
I’m absolutely new to DB2 and I would like to backup a Quickr for Portal Services server.
Thanks much,
Tamas
Yes, I post a solution in the next days (to restore the last backup)
Hi rberthou ,
Do you have shell script for online db2 database backup.Can you please share it.
I have another query ?
I have AIX 5.3 with DB2 9.1 FP7.I want to take online backup of database to disk.Currently database is not in archive log mode.
How to make db in archive log mode? (logretain/userexit/logarchmeth1)
Can you tell me the best way.
Thanks
J
Ajouter une réponse