oracle定时备份
1.将如下代码复制到文本中,最后将文本后缀名称修改成XXX.bat 批处理文件;
*********************************************************************************************************************************
@echo off
setlocal ENABLEDELAYEDEXPANSION
::读取配置文件
md %windir%\OracleAutoBackup >nul 2>nul
set configFile=%windir%\OracleAutoBackup\config.ini
set i=0
if not exist %configFile% echo.>%configFile%
for /f "delims=" %%x in (%configFile%) do (
if !i!==0 set bak_hou=%%x
if !i!==1 set bak_lot=%%x
if !i!==2 set bak_dir=%%x
if !i! gtr 2 (
set/a gup=!i!-2
set ora[!gup!]=%%x
)
set/a i+=1
)
::取默认值
if "!bak_hou!"=="" set bak_hou=3
echo !bak_hou!|findstr "^[0-9]*$">nul || set bak_hou=3
if "!bak_lot!"=="" set bak_lot=7
echo !bak_lot!|findstr "^[0-9]*$">nul || set bak_lot=7
if "!bak_dir!"=="" set bak_dir=%cd%\数据库备份
for /f "tokens=*" %%x in ("!bak_dir!") do set bak_dir=%%~fx
if not exist !bak_dir! md !val! >nul 2>nul
::去掉格式错误的数据库连接配置项
set j=0
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora[%%i]=
echo !ora_cur!|findstr "\/">nul 2>nul && echo !ora_cur!|findstr "@">nul 2>nul && (
set/a j+=1
set ora[!j!]=!ora_cur!
)
)
)
::进入管理程序
if "%1"=="" goto init
::检查exp命令是否可用
:checkexp
set resultFile=%temp%\%random%.txt
del %resultFile% /q>nul 2>nul
exp a/a@a%random% file=%temp%\%random%.dmp >nul 2>%resultFile%
if exist %resultFile% (
type %resultFile%|find "'exp' 不是内部或外部命令">nul
if !errorlevel!==0 (
del %resultFile%>nul
echo exp命令不可用!程序即将退出!
ping -n 10 127.1 >nul 2>nul
exit
)
del %resultFile%>nul
)
::1.数据库备份
title 备份进程
echo.
echo.
echo 一、正在进行备份……
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora_usr=
set ora_net=
for /f "delims=/" %%a in ('echo !ora_cur!') do set ora_usr=%%a
for /f "delims=@ tokens=2" %%a in ('echo !ora_cur!') do set ora_net=%%a
echo.
echo.
echo %%i.正在备份 !ora_usr!/******@!ora_net!……
md !bak_dir!\!ora_net!__!ora_usr!\ >nul 2>nul
set ftmr=!time: =0!
set bak_cur_dir=!bak_dir!\!ora_net!__!ora_usr!\
for /f "tokens=*" %%x in ("!bak_cur_dir!") do set bak_cur_dir=%%~fx
set bak_cur_fnm=!ora_net!__!ora_usr!__!date:~0,4!!date:~5,2!!date:~8,2!-!ftmr:~0,2!!ftmr:~3,2!
set bakfile=!bak_cur_dir!!bak_cur_fnm!.dmp
set logfile=!bak_cur_dir!!bak_cur_fnm!.log
exp !ora_cur! file="!bakfile!" log="!logfile!"
echo 如果备份成功的话,就进行压缩>nul
if exist "!bakfile!" (
pushd !bak_cur_dir!
set zipfile=
if exist "%ProgramFiles%\winrar\winrar.exe" (
echo 使用WinRAR进行压缩>nul
set zipfile=!bak_cur_fnm!.rar
"%programfiles%\winrar\winrar" a -r "!zipfile!" "!bak_cur_fnm!.dmp" "!bak_cur_fnm!.log"
) else (
echo 使用ZIP指令进行压缩>nul
set zipfile=!bak_cur_fnm!.zip
zip "!zipfile!" "!bak_cur_fnm!.dmp" "!bak_cur_fnm!.log">nul
)
if exist "!zipfile!" (
del /q "!bakfile!"
del /q "!logfile!"
)
popd
) else (
echo 如果不存在备份文件,但有日志文件,则删除日志文件>nul
if exist "!logfile!" del /q "!logfile!"
)
)
)
::2.数据库过期备份删除
echo.
echo.
echo 二、正在清除过期的备份文件……
for /f "tokens=1,2,3 delims=-" %%a in ('echo wscript.echo date-!bak_lot! ^>t~.vbs ^& cscript //nologo t~.vbs ^& del t~.vbs') do (
set y=%%a&set m=%%b&set d=%%c
if %%b lss 10 set m=0%%b
if %%c lss 10 set d=0%%c
)
set DateE=!y!-!m!-!d!
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora_usr=
set ora_net=
for /f "delims=/" %%a in ('echo !ora_cur!') do set ora_usr=%%a
for /f "delims=@ tokens=2" %%a in ('echo !ora_cur!') do set ora_net=%%a
set cur_dir=!bak_dir!\!ora_net!__!ora_usr!
for /f "tokens=*" %%x in ("!cur_dir!") do set cur_dir=%%~fx
echo 检查今天的备份成功了没有 >nul
set fnm_pre=!cur_dir!\!ora_net!__!ora_usr!__!date:~0,4!!date:~5,2!!date:~8,2!-& set today_success=0
dir !fnm_pre!*.dmp !fnm_pre!*.zip !fnm_pre!*.rar /b >nul 2>nul && set today_success=1
if "!today_success!"=="1" (
echo 判断文件夹条件是否满足 >nul
for %%a in (!cur_dir!\*.dmp,!cur_dir!\*.log,!cur_dir!\*.zip,!cur_dir!\*.rar) do (
echo 判断文件名称条件是否满足 >nul
set n=%%a&set n=!n:~-17,-9!&set n=!n:~0,4!-!n:~4,2!-!n:~6,2!
set t=%%~ta
set FileDate=!t:~0,10!
if "!n!"=="!FileDate!" (
echo 判断时间条件是否满足 >nul
if !FileDate! leq %DateE% (
echo %date:~0,10% %time:~0,8% 删除过期备份 %%a
echo %date:~0,10% %time:~0,8% 删除过期备份 %%a>>!cur_dir!\delete.log
del /q "%%a"
)
)
)
) else (
echo %date:~0,10% %time:~0,8% [!ora_net!__!ora_usr!]因为今天的备份没有成功,暂时不删除过期文件!
echo %date:~0,10% %time:~0,8% [!ora_net!__!ora_usr!]因为今天的备份没有成功,暂时不删除过期文件!>>!cur_dir!\delete.log
)
)
)
::3.完成退出
echo.
echo.
echo 三、本次备份操作完成,即将退出。
ping -n 10 127.1 >nul 2>nul
exit
::=================================以下是备份程序=================================
::=================================以下是管理程序=================================
:init
mode con cols=100 lines=40
title Oracle自动备份 - by zhouyou96
color 0e
::复制到 Windows 目录
copy "%~f0" "%windir%\OracleAutoBackup\OracleAutoBackup.bat" >nul 2>nul
::注册计划任务
:regtasks
sc config schedule start= auto >nul 2>nul
at|find "服务尚未启动">nul 2>nul&&(
net start schedule
if not !errorlevel!==0 (
echo Task Scheduler^(计划任务^)服务未能启动,程序即将退出!
pause>nul
goto exit
)
)
set job_tmr=!bak_hou!:00
if !bak_hou! lss 10 set job_tmr=0!bak_hou!:00
at !job_tmr! /every: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 %windir%\OracleAutoBackup\OracleAutoBackup.bat -backup >nul 2>nul
for /f "usebackq" %%i in (`dir %windir%\tasks\at*.job /b/o:d`) do set lastAt=%%i
del %windir%\tasks\Oracle自动备份.job >nul 2>nul
rename %windir%\tasks\!lastAt! Oracle自动备份.job
::保存配置文件
:saveconfig
echo !bak_hou!>%configFile%
echo !bak_lot!>>%configFile%
echo !bak_dir!>>%configFile%
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
echo !ora_cur!>>%configFile%
)
)
::准备数据库配置字符串
set ora_str=
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora_usr=
set ora_net=
for /f "delims=/" %%a in ('echo !ora_cur!') do set ora_usr=%%a
for /f "delims=@ tokens=2" %%a in ('echo !ora_cur!') do set ora_net=%%a
set ora_str=!ora_str!%%i. !ora_usr!/******@!ora_net!;
)
)
::开始
:start
cls
echo --------------------------------------------------------------------------------------------------
echo Oracle自动备份
echo 作者:zhouyou96 QQ:191458000
echo --------------------------------------------------------------------------------------------------
echo 使用操作系统自带的计划任务功能,每天定时运行exp命令导出指定的Oracle数据库并压缩,然后按需删除
echo 已过期的压缩的导出文件,以实现自动备份的功能。
echo 通常,为了便于管理,在我们公司一个oracle用户有且仅有的全权管理一个数据库,因此该用户的登陆名称
echo 其实可以视做为数据库名称。
echo.
echo 1.添加数据库:!ora_str!
echo 2.删除数据库
echo 3.设置文件夹:!bak_dir!
echo 4.几点钟备份:!bak_hou!
echo 5.删除几天前:!bak_lot!
echo 6.立即备份
echo 7.退出
echo.
::选择
:cho
set choice=
set /p choice=请选择:
if not "%choice%"=="" set choice=%choice:~0,1%
if "%choice%"=="1" goto addora
if "%choice%"=="2" goto delora
if "%choice%"=="3" goto setdir
if "%choice%"=="4" goto sethou
if "%choice%"=="5" goto setlot
if "%choice%"=="6" goto nowbak
if "%choice%"=="7" goto exit
echo.
echo =================================================================================================
echo =================================== 请选择1~7,按任意键重选!====================================
echo =================================================================================================
pause>nul
goto start
::添加数据库
:addora
set maxora=0
for %%i in (1,2,3,4,5,6,7,8,9) do (set ora[%%i]>nul 2>nul&&(set maxora=%%i))
set str_result=最多9个!
if not !maxora!==9 (
set/a maxora+=1
set new_ora=
set/p new_ora=请输入(用户名/密码@网络服务名):
set str_result=格式错误!
if not "!new_ora!"=="" (
echo !new_ora!|findstr "\/">nul 2>nul && echo !new_ora!|findstr "@">nul 2>nul && (
set ora[!maxora!]=!new_ora!
set str_result=添加成功,
)
)
)
echo =================================================================================================
echo ==================================== !str_result!按任意键继续!====================================
echo =================================================================================================
pause>nul
if "!str_result!"=="添加成功," goto saveconfig
goto start
::删除数据库
:delora
set str_result=操作错误!
set del_idx=0
set/p del_idx=请输入要删除的序数(1~9):
if not "%del_idx%"=="" set del_idx=%del_idx:~0,1%
if "!del_idx!"=="" set del_idx=0
echo !del_idx!|findstr "^[0-9]*$">nul || set del_idx=0
if not "!del_idx!"=="0" (
set ora[!del_idx!]=
set str_result=删除成功,
)
::去掉格式错误的数据库连接配置项
if "!str_result!"=="删除成功," (
set j=0
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora[%%i]=
echo !ora_cur!|findstr "\/">nul 2>nul && echo !ora_cur!|findstr "@">nul 2>nul && (
set/a j+=1
set ora[!j!]=!ora_cur!
)
)
)
)
echo =================================================================================================
echo ==================================== !str_result!按任意键继续!====================================
echo =================================================================================================
pause>nul
if "!str_result!"=="删除成功," goto saveconfig
goto start
::设置文件夹
:setdir
set new_dir=
set/p new_dir=请输入备份用的文件夹:
if "!new_dir!"=="" set new_dir=%cd%\数据库备份
for /f "tokens=*" %%x in ("!new_dir!") do set new_dir=%%~fx
set bak_dir=!new_dir!
echo =================================================================================================
echo ==================================== 设置成功,按任意键继续!====================================
echo =================================================================================================
pause>nul
goto saveconfig
::几点钟备份
:sethou
set str_result=操作错误!
set new_hou=
set/p new_hou=请输入每天几点钟备份(0~23):
echo !new_hou!|findstr "^[0-9]*$">nul || set new_hou=
if not "!new_hou!"=="" (
if !new_hou! geq 0 (
if !new_hou! leq 23 (
set bak_hou=!new_hou!
set str_result=设置成功,
)
)
)
echo =================================================================================================
echo ==================================== !str_result!按任意键继续!====================================
echo =================================================================================================
pause>nul
if "!str_result!"=="设置成功," goto regtasks
goto start
::删除几天前
:setlot
set str_result=操作错误!
set new_lot=
set/p new_lot=请输入删除几天之前的备份(大于零):
echo !new_lot!|findstr "^[0-9]*$">nul || set new_lot=
if not "!new_lot!"=="" (
if !new_lot! gtr 0 (
set bak_lot=!new_lot!
set str_result=设置成功,
)
)
echo =================================================================================================
echo ==================================== !str_result!按任意键继续!====================================
echo =================================================================================================
pause>nul
if "!str_result!"=="设置成功," goto saveconfig
goto start
::现在备份
:nowbak
start %windir%\OracleAutoBackup\OracleAutoBackup.bat -backup
echo =================================================================================================
echo ==================================== 成功启动,按任意键继续!====================================
echo =================================================================================================
pause>nul
goto start
::退出程序
:exit
::pause>nul
***************************************************************************************************************************************
2.配置网络连接,必须确保能正常远超访问到指定备份数据库的操作系统,或者ping通。
3.安装oracle数据库,并配置好能指定到备份数据的库的连接,可以使用PLSQL Developer做oracle的连接;
1. 配置方法:1.到oracle的安装文件中找到tnsnames.ora,(安装目录:\product\10.2.0\db_1\network\ADMIN\tnsnames.ora,)使用编辑器添加连接信息
例如:
LINUX =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 备份数据库IP地址)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = 备份数据库实例)
)
)
2.使用PLSQL Developer测试是否连接通过
4.设置定时计划,直接复制下来的代码执行XXX.bat,在任务计划中找到该计划,在后面添加一个参数 –backup 设置好备份时间即可