Vous êtes sur la page 1sur 4

How to economize time with new method of db2 backup

Database backup is a way to protect and restore a database. It is performed to


ensure a companies compliance with business regulations and to maintain and ensure
access to critical/essential business data in case a backout or technical outage.
This document will take care backup about deploy modification with the Data
Definition Language (DDL) and Data Manipulation Language (DML).

You will create a shell script using only two commands:

- db2look - generates the DDL statements by object type.


Command all database: db2look -d <databas> -a -e -l -x -o <fileName>.ddl
Command only a table: db2look -d <databas> -z <schema> -t <table> -a -e -x -o
<fileName>.ddl

- db2 export - exports data from a database to one of several external file
formats.
db2 connect to <database>
db2 export to FIEL_01.del of del select * from <table1>
db2 export to FIEL_01.del of del select * from <table2>
...
db2 export to FIEL_01.del of del select * from <tableN>

Note: you will need to know what is objects that you will change at deploy and
insert at script to create ddl file or del file. For example: If you will have add
colunm (DDL) or drop view (DDL), you will create a ddl from table , insert data
(DML), Delete data (DML)

I am using one SAMPLE database:

- Example: I will be simulating the ADD one column and insert the value in the
EMPLOYEE table
1 - Backup the database and table (DDL and Export)
2 - Apply the script
3 - Backout at inicial fase

1 - Backup
1.1 - Creating the Backup_Script.ksh:
#!/bin/ksh
#
# generate the DDL statements for objects in the DBSAMPLE database associated with
all tables and send the output to the dbsample.ddl file:
db2look -d DBSAMPLE -a -e -l -x -o dbsample.ddl
# generate the DDL statements for objects in the DBSAMPLE database associated with
tables that have schema DB2INST1 name EMPLOYEE and send the output to the
EMPLOYEE.ddl file:
db2look -d DBSAMPLE -z DB2INST1 -t EMPLOYEE -a -e -x -o EMPLOYEE.ddl
#
db2 connect to DBSAMPLE
# exporting all data from EMPLOYEE table and send the output to the EMPLOYEE.del
file:
db2 "export to EMPLOYEE.del of del select * from EMPLOYEE"

1.2 - Executing the script:


[db2inst1@oc3658117730 ~]$ ksh Backup_Script.ksh
db2look -d DBSAMPLE -a -e -l -x -o dbsample.ddl
-- Generate statistics for all creators
-- Creating DDL for table(s)
-- Output is sent to file: dbsample.ddl

db2look -d DBSAMPLE -z DB2INST1 -t EMPLOYEE -a -e -x -o EMPLOYEE.ddl


-- Generate statistics for all creators
-- Schema name is ignored
-- The db2look utility will consider only the specified tables
-- Creating DDL for table(s)
-- Output is sent to file: EMPLOYEE.ddl

db2 connect to DBSAMPLE

Database Connection Information

Database server = DB2/LINUXX8664 9.7.0


SQL authorization ID = DB2INST1
Local database alias = DBSAMPLE

db2 export to EMPLOYEE.del of del select * from EMPLOYEE


SQL3104N The Export utility is beginning to export data to file
"EMPLOYEE.del".

SQL3105N The Export utility has finished exporting "43" rows.

Number of rows exported: 43

NOTE: Check if there is enough space to store the backup files.


In case, it will need a backout, we will use the ddl and del files.

2 - Apply modifications

2.1 - Creating the script AddColumn_InsertRegister.ksh


db2 connect to DBSAMPLE
db2 "ALTER TABLE EMPLOYEE ADD NEW_COL VARCHAR(1)"
db2 "reorg table employee"
db2 "UPDATE EMPLOYEE set NEW_COL = 'T' where NEW_COL is null"

2.2 - Executing the script:


[db2inst1@oc3658117730 ~]$ ksh AddColumn_InsertRegister.ksh
db2 connect to DBSAMPLE
Database Connection Information

Database server = DB2/LINUXX8664 9.7.0


SQL authorization ID = DB2INST1
Local database alias = DBSAMPLE

db2 ALTER TABLE EMPLOYEE ADD NEW_COL VARCHAR(1)


DB20000I The SQL command completed successfully.

db2 reorg table employee


DB20000I The REORG command completed successfully.

db2 UPDATE EMPLOYEE set NEW_COL = 'T' where NEW_COL is null


DB20000I The SQL command completed successfully.

3 - Backout
Note: In case to come back the changes, I will need to restore by DDL and Export
files.
3.1 - Creating Backout.ksh
db2 "drop table EMPLOYEE"
db2 -tvf Employee.ddl | tee Employee.out
db2 "import from EMPLOYEE.del of del commitcount 1000 insert into EMPLOYEE"

3.2 - Executing the script:


[db2inst1@oc3658117730 ~]$ ksh Backout.ksh

Database Connection Information

Database server = DB2/LINUXX8664 9.7.0


SQL authorization ID = DB2INST1
Local database alias = DBAPPLY

db2 drop table EMPLOYEE


DB20000I The SQL command completed successfully.

db2 -tvf Employee.ddl | tee Employee.out


CREATE TABLE "DB2INST1"."EMPLOYEE" ( "EMPNO" CHAR(6) NOT NULL , "FIRSTNME"
VARCHAR(12) NOT NULL , "MIDINIT" CHAR(1) , "LASTNAME" VARCHAR(15) NOT NULL ,
"WORKDEPT" CHAR(3) , "PHONENO" CHAR(4) , "HIREDATE" DATE , "JOB" CHAR(8) ,
"EDLEVEL" SMALLINT NOT NULL , "SEX" CHAR(1) , "BIRTHDATE" DATE , "SALARY"
DECIMAL(9,2) , "BONUS" DECIMAL(9,2) , "COMM" DECIMAL(9,2) ) IN "USERSPACE1"
DB20000I The SQL command completed successfully.

ALTER TABLE "DB2INST1"."EMPLOYEE" ADD CONSTRAINT "PK_EMPLOYEE" PRIMARY KEY


("EMPNO")
DB20000I The SQL command completed successfully.

CREATE INDEX "DB2INST1"."XEMP2" ON "DB2INST1"."EMPLOYEE" ("WORKDEPT" ASC) COMPRESS


NO ALLOW REVERSE SCANS
DB20000I The SQL command completed successfully.

ALTER TABLE "DB2INST1"."EMPLOYEE" ADD CONSTRAINT "NUMBER" CHECK (PHONENO >= '0000'
AND PHONENO <= '9999') ENFORCED ENABLE QUERY OPTIMIZATION
DB20000I The SQL command completed successfully.

db2 import from EMPLOYEE.del of del commitcount 1000 insert into EMPLOYEE
SQL3109N The utility is beginning to load data from file "EMPLOYEE.del".

SQL3110N The utility has completed processing. "43" rows were read from the
input file.

SQL3221W ...Begin COMMIT WORK. Input Record Count = "43".

SQL3222W ...COMMIT of any database changes was successful.

SQL3149N "43" rows were processed from the input file. "43" rows were
successfully inserted into the table. "0" rows were rejected.

Number of rows read = 43


Number of rows skipped = 0
Number of rows inserted = 43
Number of rows updated = 0
Number of rows rejected = 0
Number of rows committed = 43
Note: In this case, we could use the drop column, but the lines above were showing
how you can use the fast restore with simple backup!

Vous aimerez peut-être aussi