一直想和大家分享一下关于在windows下如何自动化运维业务系统方面的心得及经验,但是由于时间和工作原因一直未能如愿。现将一个简单的自动化定时在线热备db2数据的脚本贴出来与大家共同学习进步。
@echo off
REM Create by fengzhanhai on 2010.12.23 Used by backup database db2
REM ---tasks: 每天晚上24点运行
REM 计划任务命令参照格式:"C:\Program Files\IBM\SQLLIB\BIN\db2cmd.exe" "F:\db2-backup-bbs\script\dbbackup.bat"
REM ---tasks: 每天晚上24点运行
REM 计划任务命令参照格式:"C:\Program Files\IBM\SQLLIB\BIN\db2cmd.exe" "F:\db2-backup-bbs\script\dbbackup.bat"
REM ---环境配置-------------
set Backup_Log=F:\db2-backup-bbs\script\db2back.log
REM ---DBlist.txt最后需要增加一个回车,需要备份的数据库清单
set DBList=F:\db2-backup-bbs\script\dblist.txt
set User=db2admin
set Pwd=your db2 password
set TargetPath=F:\db2-backup\
for /f "tokens=1,2,3 delims=- " %%A in ('date /t') do set FileNameDate=%%A%%B%%C
for /f "tokens=1,2 delims=: " %%A in ('time /t') do set FileNameTime=%%A%%B
set FilePath=%FileNameDate%%FileNameTime%
set CMDHeader=db2 backup db
set CMDTail=user %User% using %Pwd% online to
set Backup_Log=F:\db2-backup-bbs\script\db2back.log
REM ---DBlist.txt最后需要增加一个回车,需要备份的数据库清单
set DBList=F:\db2-backup-bbs\script\dblist.txt
set User=db2admin
set Pwd=your db2 password
set TargetPath=F:\db2-backup\
for /f "tokens=1,2,3 delims=- " %%A in ('date /t') do set FileNameDate=%%A%%B%%C
for /f "tokens=1,2 delims=: " %%A in ('time /t') do set FileNameTime=%%A%%B
set FilePath=%FileNameDate%%FileNameTime%
set CMDHeader=db2 backup db
set CMDTail=user %User% using %Pwd% online to
REM rar.exe绿色压缩软件程序包需要的朋友可以将附件的rar.txt内容下载后改成后缀为exe的文件
set ProgramRar=%~dp0\rar.exe
set ProgramRar=%~dp0\rar.exe
REM ---检查数据库配置文件------
if not exist %DBList% (
echo %date% %time% %DBList% not found >> %Backup_Log%
exit
)
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%
)
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 "tokens=*" %%j in (%DBList%) do (
%CMDHeader% %%j %CMDTail% %TargetPath%%FilePath% >> %Backup_Log%
echo %date% %time% %%j backup done >> %Backup_Log%
)
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 %TargetPath%%FilePath% %TargetPath%%FilePath% > nul
echo %date% %time% compress %TargetPath%%FilePath% done >> %Backup_Log%
%ProgramRar% a %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%
RD /Q /S %TargetPath%%FilePath%
echo %date% %time% delete %TargetPath%%FilePath% done >> %Backup_Log%
exit
-------------------------------------------------------------------------------------------
注:首先你确保已经打开支持在线备份的数据库配置参数:
db2 update db cfg for sample using userexit on 启用用户出口
db2 update db cfg for sample using logretain on 启用归档日志
db2 update db cfg for sample using userexit on 启用用户出口
db2 update db cfg for sample using logretain on 启用归档日志
开启这些参数后,数据库处于backup pending状态,兄弟们记得一定要做数据库的离线全备份哦!
如果不开启上述参数设置你的数据库将不支持热备方式从而在使用该脚本时会导致数据库备份暂挂甚至影响业务的正常运行。
dblist.txt rar.txt 以上附件分别是需要备份的数据库清单及免安装压缩软件
如果不开启上述参数设置你的数据库将不支持热备方式从而在使用该脚本时会导致数据库备份暂挂甚至影响业务的正常运行。
dblist.txt rar.txt 以上附件分别是需要备份的数据库清单及免安装压缩软件