目录
1. 连接数据库
2.记录数据库日志文件路径
3.启用数据库增量备份所需更改的三个参数
4.强制重启,使更改的参数生效
5.重启成功后,需要做一次离线备份
6.重新连接数据库
7.执行set.bat
8.复制日志路径,修改备份脚本(db2backup.bat)
9修改db2list.txt 增加数据库
10.执行备份脚本 db2backup.bat
db2 connect to XXX
db2 get db cfg
db2 update db cfg for STORE using userexit on #启用用户出口
db2 update db cfg for STORE using logretain on #启用归档日志
db2 update db cfg for STORE using trackmod on #启用增量备份功能
(应用程序都与此数据库断开连接之后,更改才会生效)
db2stop force
db2start
直接连接数据库受限制,提示处于报错backpending状态,需要离线备份
db2 backup db XXX to 'f:almbackup/workFloder'
db2 connect to XXX
使用方法:将文件解压在同一个文件夹下,运行set.bat文件,根据提示去输入相应的参数,其中包括数据库备份存放的目录、数据库归档日志目录(作为日志的一个中转站,里面虽然没有数据,但是很重要)、数据库日志最终存放及处理的目录、数据库的名字、数据库实例名;数据库名一定要输入!
将以下脚本保存在txt文档中,重命名为set.bat,并双击执行它
@echo off
setlocal EnableDelayedExpansion
:start
set dbdir1=d:\databackup
set dbdir=
set logdir1=d:\logdatabackup
set logdir=
set bklog1=d:\bklog
set bklog=
set dbinstance1=db2
set dbinstance=
set dbname=
set /p dbdir=请输入您数据库的备份路径:
if defined dbdir ( if !dbdir:~-1!==\ (
set dbdir1=!dbdir:~0,-1! ) else ( set dbdir1=!dbdir! )
)
set dbdir1=!dbdir1: =!
if not exist !dbdir1! mkdir !dbdir1!
xcopy /y backup.bat !dbdir1!
xcopy /y deltbackup.bat !dbdir1!
set /p logdir=请输入您要归档日志的路径:
if defined logdir ( if !logdir:~-1!==\ (
set logdir1=!logdir:~0,-1! ) else ( set logdir1=!logdir! )
)
set logdir1=!logdir1: =!
if not exist !logdir1! mkdir !logdir1!
set /p bklog=请输入您存放归档日志的路径:
if defined bklog ( if !bklog:~-1!==\ (
set bklog1=!bklog:~0,-1! ) else ( set bklog1=!bklog! )
)
set bklog1=!bklog1: =!
if not exist !bklog1! mkdir !bklog1!
set /p dbinstance=请输入该数据库的实例:
if defined dbinstance set dbinstance1=!dbinstance!
set /p dbname=请输入您要备份的数据库:
if not defined dbname ( goto dbset )
goto switch
:switch
if defined dbdir ( if defined logdir ( if defined dbname ( if defined bklog ( goto setdb ) else (
goto choice1 )) else (
goto choice1 )) else (
goto choice1 )) else ( goto choice1 )
:end
exit
:setdb
db2start
db2cmd /c /w "db2 update db cfg for !dbname! using logretain on"
db2cmd /c /w "db2 update db cfg for !dbname! using userexit on"
db2cmd /c /w "db2 update db cfg using trackmod on"
db2cmd /c /w "db2 update db cfg for !dbname! using LOGARCHMETH1 DISK:!logdir!"
db2stop force
db2start
db2cmd /c /w "db2 backup db !dbname! to !dbdir!"
db2stop force
db2start
for /f "eol= tokens=*" %%a in ( 'findstr .* !dbdir1!\backup.bat' ) do (
set str=%%a
set str=!str:$dbdir$=%dbdir1%!
set str=!str:$dbname$=%dbname%!
set str=!str:$bklog$=%bklog1%!
set str=!str:$logdir$=%logdir1%!
set str=!str:$dbinstance$=%dbinstance1%!
echo>>!dbdir1!\backup1.bat !str!
)
for /f "eol= tokens=*" %%a in ( 'findstr .* !dbdir1!\deltbackup.bat' ) do (
set str=%%a
set str=!str:$dbdir$=%dbdir1%!
set str=!str:$dbname$=%dbname%!
set str=!str:$bklog$=%bklog1%!
set str=!str:$logdir$=%logdir1%!
set str=!str:$dbinstance$=%dbinstance1%!
echo>>!dbdir1!\deltbackup1.bat !str!
)
move /y !dbdir1!\backup1.bat !dbdir1!\backup.bat
move /y !dbdir1!\deltbackup1.bat !dbdir1!\deltbackup.bat
at /delete /yes
at 1:00 /every:M,T,W,TH,F,S !dbdir1!\deltbackup.bat
at 1:00 /every:Su !dbdir1!\backup.bat
goto end
:choice1
choice /C ync /M 对于您没有设置的参数将以默认参数来执行,但数据库您必须输入!按y键确认,按n键重置,按c键退出
if errorlevel 3 goto end
if errorlevel 2 goto start
if errorlevel 1 goto setdb
:dbset
set /p dbname=您必须输入数据库名,请输入您要备份的数据库:
if not defined dbname goto dbset
goto switch
设置备份脚本所存放的日志路径
请输入您数据库的备份路径:f:almbackup\workfloder
找不到文件 - backup.bat
复制了 0 个文件
找不到文件 - deltbackup.bat
复制了 0 个文件
请输入您要归档日志的路径:F:\AlmBackup\log
请输入您存放归档日志的路径:C:\DB2\NODE0000\SQL00003\SQLOGDIR
请输入该数据库的实例:db2
请输入您要备份的数据库:STORE
2019-11-18 15:38:04 0 0 SQL1026N 数据库管理器已活动。
SQL1026N 数据库管理器已活动。
2019-11-18 15:38:11 0 0 SQL1064N DB2STOP 处理成功。
SQL1064N DB2STOP 处理成功。
2019-11-18 15:38:14 0 0 SQL1063N DB2START 处理成功。
SQL1063N DB2START 处理成功。
执行完set.bat,复制归档日志路径,其中
蓝色框为set.bat设置存放归档日志路径
红色框为set.bat设置要归档日志路径(找到填写路径会发现根据填写的数据库名脚本又多建了四层目录,复制到最末级的路径)
REM @echo off
REM ---环境配置-------------
set Backup_Log=F:\AlmBackup\db2back.log
REM ---DBlist.txt最后需要增加一个回车,需要备份的数据库清单
SET sPath =F:\AlmBackup\ftprun.log
set DBList=F:\AlmBackup\dblist.txt
set User=XXXXX
set Pwd=XXXXXX
set TargetPath=F:\AlmBackup\
set WorkFloder=F:\AlmBackup\workFloder
set RestFloder=F:\AlmBackup\restFloder
::获取日期 将格式设置为:20110820
set datevar=%date:~0,4%%date:~5,2%%date:~8,2%
::获取时间中的小时 将格式设置为:24小时制
set timevar=%time:~0,2%
if /i %timevar% LSS 10 (
set timevar=0%time:~1,1%
)
::获取时间中的分、秒 将格式设置为:3220 ,表示 32分20秒
set timevar=%timevar%%time:~3,2%%time:~6,2%
set FilePath=%datevar%%timevar%
set CMDHeader=db2 backup db
set CMDTail=user %User% using %Pwd% online to
set CMDTail1=user %User% using %Pwd% online incremental delta to
REM rar.exe绿色压缩软件程序包需要的朋友可以将附件的rar.txt内容下载后改成后缀为exe的文件
set ProgramRar=%~dp0\rar.exe
REM 设置set db2clp=DB20FADE,设置db2clp的值为这个值就可以不需要新建窗口,运行db2命令了
REM 打开另外一个db2cmd的窗口,运行set db2clp,就会显示db2clp的值 记住这个值,就可以在任何已经存在的cmd窗口中
set db2clp=DB20FADE
REM ---检查数据库配置文件------
if not exist %DBList% (
echo %date% %time% %DBList% not found >> %Backup_Log%
exit
)
REM ---创建当日文件夹------
if not exist %TargetPath%%FilePath% (
md %TargetPath%%FilePath% > nul
echo %date% %time% create %TargetPath%%FilePath% folder >> %Backup_Log%
) else (
echo %date% %time% %TargetPath%%FilePath% exist >> %Backup_Log%
)
REM ---输出今天是周几,区分工作日或休息日,以判断是差异备份还是完全备份------
for /f %%k in ('powershell ^(get-date^).DayOfWeek') do (
set dow=%%k
)
echo %dow%
if %dow%==Monday goto :work
if %dow%==Tuesday goto :work
if %dow%==Wednesday goto :work
if %dow%==Thursday goto :work
if %dow%==Friday goto :work
if %dow%==Saturday goto :work
if %dow%==Sunday goto :rest
REM ---差异备份-------------
:work
for /f "tokens=*" %%j in (%DBList%) do (
%CMDHeader% %%j %CMDTail1% %TargetPath%%FilePath% >> %Backup_Log%
echo %date% %time% %%j delbackup done >> %Backup_Log%
)
REM ---压缩数据-------------
%ProgramRar% a -m5 %TargetPath%%FilePath% %TargetPath%%FilePath% > nul
echo %date% %time% compress %TargetPath%%FilePath% done >> %Backup_Log%
REM ---删除压缩前数据-------------
RD /Q /S %TargetPath%%FilePath%
echo %date% %time% delete %TargetPath%%FilePath% done >> %Backup_Log%
REM ---移动到工作日文件夹------
move /y %TargetPath%%FilePath%.rar %WorkFloder% >> %Backup_Log%
echo %date% %time% move %TargetPath%%FilePath%.rar to WorkFloder done >> %Backup_Log%
echo %date% %time% delete %TargetPath%%FilePath% done >> %Backup_Log%
REM ---使用ftp转移数据-----
REM ---判断是否备份成功------
REM ---删除传输数据-------------
REM ---删除7天前的文件------
REM ---set.bat设置完成之后,备份数据库1归档日志数据-----
if not exist C:\DB2\NODE0000\SQL00004\SQLOGDIR\log_%date:~0,4%-%date:~5,2% mkdir C:\DB2\NODE0000\SQL00004\SQLOGDIR\log_%date:~0,4%-%date:~5,2%
for /r F:\AlmBackup\log\DB2\TUSTEST\NODE0000\C0000001) %%i in ( *.LOG ) do (
move /y %%i C:\DB2\NODE0000\SQL00004\SQLOGDIR\log_%date:~0,4%-%date:~5,2%
echo %date% %time% move goodtest logs success>> %Backup_Log%
REM ---备份数据库2归档日志数据-----
if not exist C:\DB2\NODE0000\SQL00003\SQLOGDIR\log_%date:~0,4%-%date:~5,2% mkdir C:\DB2\NODE0000\SQL00003\SQLOGDIR\log_%date:~0,4%-%date:~5,2%
for /r F:\AlmBackup\log\DB2\LIRY\NODE0000\C0000005 %%i in ( *.LOG ) do (
move /y %%i C:\DB2\NODE0000\SQL00003\SQLOGDIR\log_%date:~0,4%-%date:~5,2%
if %errorlevel%==0 ( echo 向日志储存目录移动%%i成功!>> %Backup_Log%) else ( echo 向日志储存目录移动%%i失败!>> %Backup_Log%))
REM ---备份全量数据-------------
:rest
for /f "tokens=*" %%j in (%DBList%) do (
%CMDHeader% %%j %CMDTail% %TargetPath%%FilePath% >> %Backup_Log%
echo %date% %time% %%j backup done >> %Backup_Log%
)
REM ---压缩数据-------------
%ProgramRar% a -m5 %TargetPath%%FilePath% %TargetPath%%FilePath% > nul
echo %date% %time% compress %TargetPath%%FilePath% done >> %Backup_Log%
REM ---删除压缩前数据-------------
RD /Q /S %TargetPath%%FilePath%
echo %date% %time% delete %TargetPath%%FilePath% done >> %Backup_Log%
REM ---移动到休息日文件夹------
move /y %TargetPath%%FilePath%.rar %RestFloder% >> %Backup_Log%
echo %date% %time% move %TargetPath%%FilePath%.rar to RestFloder done >> %Backup_Log%
echo %date% %time% delete %TargetPath%%FilePath% done >> %Backup_Log%
REM ---ftp传输数据-----
REM ---python判断是否备份成功------
REM ---备份数据库1归档日志数据-----
if not exist C:\DB2\NODE0000\SQL00004\SQLOGDIR\log_%date:~0,4%-%date:~5,2% mkdir C:\DB2\NODE0000\SQL00004\SQLOGDIR\log_%date:~0,4%-%date:~5,2%
for /r F:\AlmBackup\log\DB2\TUSTEST\NODE0000\C0000001 %%i in ( *.LOG ) do (
move /y %%i C:\DB2\NODE0000\SQL00004\SQLOGDIR\log_%date:~0,4%-%date:~5,2%
echo %date% %time% move goodtest logs success>> %Backup_Log%
REM ---备份数据库2归档日志数据-----
if not exist C:\DB2\NODE0000\SQL00003\SQLOGDIR\log_%date:~0,4%-%date:~5,2% mkdir C:\DB2\NODE0000\SQL00003\SQLOGDIR\log_%date:~0,4%-%date:~5,2%
for /r F:\AlmBackup\log\DB2\LIRY\NODE0000\C0000005 %%i in ( *.LOG ) do (
move /y %%i C:\DB2\NODE0000\SQL00003\SQLOGDIR\log_%date:~0,4%-%date:~5,2%
if %errorlevel%==0 ( echo 向日志储存目录移动%%i成功!>> %Backup_Log%) else ( echo 向日志储存目录移动%%i失败!>> %Backup_Log%))
ps:先用测试库,其中有许多路径设置的问题,多多注意一下,脚本运行过了是可以用的,其实全备之后最重要的就是归档日志的备份,因为数据库一旦发生灾难性问题,日志也会跟着删除掉,所以只有全备文件还不足以能够恢复到最后日志截止的时间点
分享一下在学习DB2数据库备份的一些参考
DB2在线增量备份 还原增量备份及前滚恢复 https://blog.csdn.net/taikeqi/article/details/2602072
db2备份恢复https://blog.csdn.net/huryer/article/details/80353000