Db2 : restore a database from an instance to another and set grant to administer database

This post describe how to restore a database backup from an instance to another and set grant; I assume you have successfully installed Db2 and configured a new instance.

Step 1 restore db:

Use this command to restore db*:

db2 restore db <dbname> from <backupdir> ON <datapath> NEWLOGPATH <tranlogpath>

<dbname> = original database name
<backupdir> = path where backup is located (if more then one backup is present you must specify timestamp with comand TAKEN AT)
<datapath> = where you want to store your database path tipically /home/<instancename>/
<tranlogpath> = where you want to store tranlog (I use : /home/<instancename>/tranlog)

* A database backup contains settings for path so if you move Db from one server to another you have to specify new filesystem path

Step 2 grant :

Use this command to set grant:

db2 CONNECT TO <dbname> user <databaseowner> using <databaseownerpassword>;
db2 GRANT DBADM,CREATETAB,BINDADD,CONNECT,CREATE_NOT_FENCED_ROUTINE,IMPLICIT_SCHEMA,LOAD,CREATE_EXTERNAL_ROUTINE,QUIESCE_CONNECT,SECADM ON DATABASE TO USER <newuser>;
db2 CONNECT RESET;

<dbname> = original database name
<databaseowner> = grant must be set from the old db owner, to do this I usually create a new user whit old name on new server.
<databaseownerpassword> = password
<newuser> = new owner

 


Example:

Immagine you have a server named server1 with instance db2inst1 and a database named wpsdb.
Now immagine you have to move a wpsdb to server2 with instance name db2wps.
What you have to do is :

– Restore db: db2 restore db wpsdb from /backup ON /home/db2wps NEWLOGPATH /home/db2wps/tranlog
– create user db2inst1 on server2:(banally “useradd db2inst1” and “passwd db2inst1” to set password)
– grant:

db2 CONNECT TO wpsdb user db2inst1 using password
db2 GRANT DBADM,CREATETAB,BINDADD,CONNECT,CREATE_NOT_FENCED_ROUTINE,IMPLICIT_SCHEMA,LOAD,CREATE_EXTERNAL_ROUTINE,QUIESCE_CONNECT,SECADM ON DATABASE TO USER db2wps
db2 CONNECT RESET

Step by step htadmin config to use with Websphere Application Server

Here you can find step by step configuration for use IHS htadmin

Step 1 generate user :

/opt/ibm/HTTPServer/bin/htpasswd -cm /opt/ibm/HTTPServer/conf/admin.passwd htadmin

Step 2 edit admin.conf :

vi /opt/ibm/HTTPServer/conf/conf/admin.conf

#Change listen port:
Listen 8008

#Change running user
User wwwrun
Group www

#Set serverName
ServerName servername.mydomain.com

Step 3 add user and group to run htadmin:
groupadd www
useradd wwwrun -g www

Step 4 run htadmin:

/opt/ibm/HTTPServer/bin/adminctl start

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

How to export Tivoli Directory Server to .ldif file

This script is simply and usefull to backup all users in a tivoli directory server to an ldif file.
When schedule by crontab it’s possible to add in command line destination folder by $1 param,
so you can have different versions of backup.

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/sh
 
NOW=$(date +"%u")
LPATH=/tmp/juve_full_backup$NOW.ldif
RPATH=/backup/ldap/$1
LOG=/var/log/bckLdap$1"-"$NOW".log"
 
echo "------------------"Backup LDAP on DOW $NOW"----------------------------------">LOG
echo `date`>>$LOG
 
mkdir $RPATH
/opt/IBM/ldap/V6.3/sbin/idsdb2ldif -I juvetds1 -o $LPATH  >$LOG
mv $LPATH $RPATH

Setup First Instance DB2

Taken from db2 Infocenter these step let you configure first db2 instance on linux.

1) Create Users for db2:

groupadd -g 999 db2iadm1
groupadd -g 998 db2fadm1
groupadd -g 997 dasadm1

2) Create users for each group:

useradd -u 1004 -g db2iadm1 -m -d /home/db2inst1 db2inst1
useradd -u 1003 -g db2fadm1 -m -d /home/db2fenc1 db2fenc1
useradd -u 1002 -g dasadm1 -m -d /home/dasusr1 dasusr1

3) Set initial password:

passwd db2inst1
passwd db2fenc1
passwd dasusr1

4) Create Instance:

/opt/ibm/db2/V9.7/instance/dascrt -u dasusr1
/opt/IBM/db2/V9.7/instance/db2icrt -a server -u db2fenc1 db2inst1
/opt/ibm/db2/V9.7/cfg/db2ln

5) Setup Comunication:

cp /etc/services /root/services.BCK
echo db2c_db2inst1 50001/tcp >> /etc/services
su – db2inst1
db2 update database manager configuration using svcename  db2inst1
db2set DB2COMM=tcpip

6) Restart DB:

db2stop
db2start