先看我们最关心的问题
1.数据导出: db2move DBNAME export -u userName -p password
2.数据导入: db2move DBNAME import -u userName -p password
注意:在linux下导入导出数据时要注意db2codepage的一致性,1386表示GBK,1208表示UTF-8还有注意该数据库账户(如db2inst1)是否具有待导入文件的读写权限
遇到上述问题,参见我的博文:http://blog.csdn.net/fuwencaho/article/details/22539727
执行上面命令后会把数据库test中全部数据提取到当前目录(/home/db2inst1),每个表的内容都存储在一个.ixf文件中,
每个.ixf文件都有一个与之相对应的.msg文件,.msg文件是描述从表中导出数据时的信息.例外还有两个文件,db2move.lst用来
记录.ixf文件,.msg文件与表一一对应.EXPORT.out记录的是导出数据时的屏幕输出.
# Shell programmer : Hu YunQiang.
##########################################################################################
# 程序名称: db2back.sh
# 入口参数: 无
#
# 返回结果: 数据库的备份
#
# 功能描述: ① 备份数据库DBname
#
#
# 程序编码: Hu YunQiang
# 发布日期: 20040915
#
# 更新记录: UserName Date(YYYYMMDD) Note
#
#
##########################################################################################
echo "---------------------------------------------------------------------------------------"
echo "程序名称: $0 编码 : Hu YunQinag"
echo "入口参数: 无"
echo "格式 sh $0 "
echo "Hu YunQinag 版权所有"
echo "---------------------------------------------------------------------------------------"
#######################
# export environment #
#######################
#export DB2INSTANCE=db2inst1
#export HOSTNAME=p630
#export LD_LIBRARY_PATH=/home/db2inst1/sqllib/lib
#export LIBPATH=/home/db2inst1/sqllib/lib
#export PATH=/bin:/usr/bin:/usr/local/bin:/usr/bin/X11:/usr/X11R6/bin:/home/db2inst1/sqllib/bin:/home/db2inst1/sqllib/adm:/home/db2inst1/sqllib/misc:/home/db2inst1/bin
#export CLASSPATH=/home/db2inst1/sqllib/java/db2java.zip:/home/db2inst1/sqllib/java/db2jcc.jar:/home/db2inst1/sqllib/function
#######################
# backupdb main #
#######################
backupdb(){
cd $sourcedir
mkdir $backupfolder
if [ ! -d $backupfolder ]
then
echo "`date +%Y%m%d`:创建备份目录失败"< $bkerrlog
exit
fi
cd $backupfolder
db2move $1 export -u $2 -p $3
cd ..
tar cvf $backupfolder.tar $backupfolder
gzip -9 $backupfolder.tar
}
#######################
# ftp main #
#######################
myftp(){
ftp -n < EOF>
open $desthost
user $user $passwd
binary
prompt off
lcd $sourcedir
cd $destdir
put $filename $filenae
del $rmoldfile
! touch $sigfile
EOF
}
#######################
# rmbkfile main #
#######################
rmbkfile()
{
cd $sourcedir
rm -Rf $backupfolder
}
#######################
#program main #
#######################
#程序公用变量
sourcedir=/db2back
shellpath=/home/db2inst1/backup
sigfile=finish.`date +%Y%m%d`
mainlog=errlog.`date +%Y%m%d`
#backup程序变量
backupfolder=`date +%Y%m%d`.dbname.db2.bak
dbname=dbname
dbuser=dbuser
dbpwd=dbpwd
bkerrlog=bkerr.log.`date +%Y%m%d`
#ftp程序变量
desthost=192.168.0.111
user=db2backup
passwd=db2backup
destdir=/
filename=$backupfolder.tar.gz
ftperrlog=ftperr.log.`date +%Y%m%d`
oldday0=`date +%Y%m%d`
backupdb $dbname $dbuser $dbpwd
if [ $? != 0 ]
then
echo "`date +%Y%m%d`:备份失败" < $mainlog
exit
fi
cd $shellpath
oldday1=`./mydate.sh $oldday0`
rmoldfile=`./mydate.sh $oldday1`.dbname.db2.bak.tar.gz
myftp
if [ $? != 0 ]
then
echo "`date +%Y%m%d`:ftp文件失败" < $mainlog
exit
fi
rmbkfile
if [ $? != 0 ]
then
echo "`date +%Y%m%d`:删除失败" < $mainlog
exit
fi
cd $sourcedir
rm $rmoldfile
说起来比较不好意思,当时不熟悉find -atime -mtime -ctime 的用法,还折腾一个判断日起的shell。后面删除过期备份文件的方法,只要两句话就好了,定义一个变量
n=5 #保留这些天之内的
cd $sourcedir
find . -atime +$n -exec rm -fr {} ;
之后只要把这个脚本加入到对应用户的crontab就好了
$ more generateDDL.sh
#!/usr/bin/ksh
generateFile()
{
FF=/crm/db2inst1/generateDDL/gettable.sql
echo "connect to crmdb;">$FF
echo "SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME LIKE '%IFS%REPORT%';">>$FF
echo "disconnect current;">>$FF
db2 -tf $FF>/crm/db2inst1/generateDDL/gettable.file
}
generateDDL()
{
db2look -d crmdb -e -t $1>>/crm/db2inst1/generateDDL/gettable.ddl
}
generateFile
>/crm/db2inst1/generateDDL/gettable.ddl
for i in `cat /crm/db2inst1/generateDDL/gettable.file`
do
generateDDL $i
done
$