-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_import.sh
More file actions
executable file
·118 lines (105 loc) · 3.58 KB
/
export_import.sh
File metadata and controls
executable file
·118 lines (105 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/ksh
##########################################################################################
#Author: sanumala
#Date: 04-11-2012
#Description: This script can be used to export and import cdw db2 tables.
#Usage: export_import.sh talesList.txt dbconfig.properties
#Input1: talesList.txt --> contains list of tables seperated line-by-line.
#Input2: dbconfig.properties --> contains properties to connect to source and target db's.
#output: Useful echos on console and DB messages will be loged to /pwd/log_date.log.
##########################################################################################
usage()
{
echo "Usage of this script"
echo "export_import.sh <tables_list>.txt <dbconfig>.properties"
}
validateFileExistance()
{
input="$@"
# make sure file exist and readable
if [ ! -f $input ]; then
echo "$input : does not exists"
exit 1
elif [ ! -r $input ]; then
echo "$input: can not read"
exit 2
fi
}
readAndStoreTableNames()
{
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$tableListFile"
counter=0
while read -r line
do
# Skip Header record.
#test $counter -eq 1 && ((counter=counter+1)) && continue
# use $line variable to process line in processTestCase() function
tables[counter]="$line"
counter=${counter}+1
done
exec 0<&3
}
#This will run multiple threads at a same time to complete extracts fast
exportData()
{
sqlToExecute="$@"
db2 "connect to $SOURCE_DATABASE_NAME user $SOURCE_DATABASE_USER_NAME using $SOURCE_DATABASE_PASSWORD" >> $LOG
db2 "set schema $SOURCE_SCHEMA_NAME" >> $LOG
db2 $sqlToExecute >> $LOG
db2 terminate >> $LOG
#echo "Now Executing ::::: $sqlToExecute" #>> $LOG
}
importData()
{
sqlToExecute="$@"
db2 "connect to $TARGET_DATABASE_NAME user $TARGET_DATABASE_USER_NAME using $TARGET_DATABASE_PASSWORD" >> $LOG
db2 "set schema $TARGET_SCHEMA_NAME" >> $LOG
db2 $sqlToExecute >> $LOG
db2 terminate >> $LOG
echo "Now Executing :::: $sqlToExecute" #>>$LOG
}
##### M A I N S C R I P T S T A R T S H E R E #####
tableListFile=""
dbConfigFile=""
set -A tables
numberOfTables=0
LOG_FILE=`pwd`/log_`date +"%m%d%Y"`.log
# Make sure we get file name as command line argument
if [[ $# -lt 2 ]]; then
usage
exit
else
echo "Execution started at ::: `date +\"%m-%d-%Y %T\"`"
tableListFile="$1"
dbConfigFile="$2"
# make sure file exist and readable
echo "Validating file existance for $tableListFile"
validateFileExistance $tableListFile
echo "Validating file existance for $dbConfigFile"
validateFileExistance $dbConfigFile
#Now Source db config properties and make them available as env variables
. $dbConfigFile
numberOfTables=`cat $tableListFile|wc -l`
echo "Number of tables are $numberOfTables"
#echo "Current working directory is `pwd`"
readAndStoreTableNames
for ((i=0;i<$numberOfTables;i++))
do
exportData "export to `pwd`/${tables[i]}.ixf of ixf select * from $SOURCE_SCHEMA_NAME.${tables[i]}" &
done
#Wait until all backups are done.This very important and dont comment this line
wait
echo "Exports are completed successfully at `date +\"%m-%d-%Y %T\"`"
#Now import data into target
echo "Importing data into target($TARGET_DATABASE_NAME#$TARGET_SCHEMA_NAME) started at `date +\"%m-%d-%Y %T\"`"
for ((i=0;i<$numberOfTables;i++))
do
importData "import from `pwd`/${tables[i]}.ixf of ixf commitcount 10000 insert into $TARGET_SCHEMA_NAME.${tables[i]}" &
done
#Wait until restores are done
wait
echo "Export and import are completed successfully at `date +\"%m-%d-%Y %T\"`. Please refer $LOG_FILE for more details"
fi