DB2 Backup Script
DB2 Backup Script
- 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)
- 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"
2 - Apply modifications
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"
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.
SQL3149N "43" rows were processed from the input file. "43" rows were
successfully inserted into the table. "0" rows were rejected.