导航

一个oracle自动备份的脚本

Posted on 2011-03-15 22:05  datalife  阅读(330)  评论(0编辑  收藏  举报
@echo off
setlocal ENABLEDELAYEDEXPANSION

::读取配置文件
echo 正在读取配置文件...
set "base64=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
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
        call :base64_str "%%x"
        set ora[!gup!]=!val!
    )
    set/a i+=1
)
cls

::取默认值
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 jason QQ:59006558
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
        call :str_base64 "!ora_cur!"
        if not "!val!"=="" echo !val!>>%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                                     作者:jason QQ:59006558
echo  --------------------------------------------------------------------------------------------------
echo    使用操作系统自带的计划任务功能,每天定时运行exp命令导出指定的Oracle数据库并压缩,然后按需删除
echo  已过期的压缩的导出文件,以实现自动备份的功能。
echo    通常,为了便于管理,在我们公司一个oracle用户有且仅有的全权管理一个数据库,因此该用户的登陆名称
echo  其实可以视做为数据库名称。
echo  功能:* 将设置保存到配置文件并加密
echo        * 自动添加计划任务按计划备份
echo        * 自动删除已过期的备份文件和日志
echo        * 将备份文件压缩到rar或zip,并删除dmp
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


::=====================================  字符串加密 by zhouyou96 =====================================
::1.用法call str_base64 "abc" 
::2.返回 echo %val%
::=====================================  字符串加密 by zhouyou96 =====================================
:str_base64
::先求出16进制值
call :str_hex "%~1"
set hex=%val%
set val=
::遍历计算
:str_base64_sub
if not defined hex goto :eof
for /l %%a in (1,1,3) do (
        set/a "n=2*(%%a-1)"
        call set x=%%hex:~!n!,2%%
        set "c%%a=0x!x!"
        if "!x!" equ "" set c%%a=0       
)
set hex=%hex:~6%
set/a ec1=%c1%">>"2
set/a ec2=%c1%"<<"4"|"%c2%">>"4
set/a ec3=%c2%"<<"2"|"%c3%">>"6
set/a ec4=%c3%
for /l %%a in (1,1,4) do set/a ec%%a=!ec%%a!"&"63
if %c2% equ 0 set ec3=64
if %c3% equ 0 set ec4=64
for /l %%a in (1,1,4) do call set "val=%%val%%%%base64:~!ec%%a!,1%%"
goto :str_base64_sub
goto :eof
::获取字符串的16进制代码
::1.用法call str_hex "abc"
::2.返回 echo %val%
:str_hex
set val=
if "%~1"=="" goto :eof
set tmp1=%temp%\%random%.tmp
set tmp2=%temp%\%random%.tmp
echo %~1 >%tmp1%
::利用fsutil创建一个一样大小的文件
for /f "tokens=*" %%x in ("%tmp1%") do set size=%%~zx
fsutil file createnew %tmp2% %size% >nul 2>nul
for /f "tokens=3" %%i in ('fc/b %tmp2% %tmp1%^|findstr "^0000"') do call set "val=%%val%%%%i"
set val=%val:~0,-6%
del %tmp1%
del %tmp2%
goto :eof


::=====================================  字符串解密 by zhouyou96 =====================================
::1.用法call base64_str "YWJj"
::2.返回 echo %val%
::=====================================  字符串解密 by zhouyou96 =====================================
:base64_str
set val=
if "%~1"=="" goto :eof
::初始化变量
set es=%~1
set hex=
set hex2=
set ec1=
set ec2=
set ec3=
set ec4=
set c1=
set c2=
set c3=
set c4=
set f1=
set f2=
set f3=
set f4=
set/a base64_i=0
::遍历计算
:base64_str_sub
    set "o=!es:~%base64_i%,1!"
    if "%o%" equ "" goto :hex-hex2 >nul 2>nul
    call :b64-h1by1 >nul 2>nul
    set/a base64_i+=1
goto :base64_str_sub
goto :eof

:b64-h1by1
for /l %%a in (0,1,64) do (
    if "!base64:~%%a,1!" equ "%o%" (
        call :dec-hex %%a
        set hex=!hex!!r%%a!
        goto :eof
    )
)
goto :eof
:dec-hex
if "%~1" equ "" goto :eof
set "r%1="
set "h=0123456789ABCDEF"
set/a q=%1 >nul 2>nul
:1c
set/a r=%q%%%16 >nul 2>nul
set/a q=%q%/16 >nul 2>nul
call set r=%%h:~%r%,1%%
set r%1=%r%!r%1!
if not %q% equ 0 goto :1c
if %1 leq 0xf set r%1=0!r%1!
goto :eof

:hex-hex2
if not defined hex goto :hex2-str
for /l %%a in (1,1,4) do (
        set/a "n=2*(%%a-1)"
        call set x=%%hex:~!n!,2%%
        set "ec%%a=0x!x!"
        if "!x!" equ "40" (set ec%%a=0& set f%%a=0)       
)
set hex=%hex:~8%
set/a c1=%ec1%"<<"2"|"%ec2%">>"4 >nul 2>nul
set/a c2=%ec2%"<<"4"|"%ec3%">>"2 >nul 2>nul
set/a c3=%ec3%"<<"6"|"%ec4% >nul 2>nul
for /l %%a in (1,1,3) do (
        set/a c%%a=!c%%a!"&"255 >nul 2>nul
        call :dec-hex !c%%a!
)
if "%f3%" equ "0" set "c2="
if "%f4%" equ "0" set "c3="
for /l %%a in (1,1,3) do call set "hex2=%%hex2%%%%r!c%%a!%%"
goto :hex-hex2

:hex2-str
set vbs=%tmp%\tt.vbs
if exist %vbs% goto :runVBS
:createVBS
set t=^>^>%vbs% echo
%t%h=wscript.arguments(0)
%t%s=""
%t%for i=1 to len(h) step 2
%t%        a=cint("&h"^&mid(h,i,2))
%t%        if a^>160 then
%t%                a=cint("&h"^&mid(h,i,4))
%t%                i=i+2
%t%        end if
%t%        s=s^&chr(a)       
%t%next 
%t%wscript.stdout.write(s)
:runVBS
for /f %%a in ('cscript//nologo %vbs% %hex2%') do set "val=%%a"
goto :eof