Bat脚本学习-6:Oracle自动备份还原脚本

Posted 文刂亻右景彡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bat脚本学习-6:Oracle自动备份还原脚本相关的知识,希望对你有一定的参考价值。

这次注释没写多少,先跳过一些吧

::2.数据库过期备份删除
echo.
echo.
echo 二、正在清除过期的备份文件……

REM wscript.echo是vbs语法,相当于alert
REM >是特殊字符,所以放在 for 命令里面需要使用转义符号^>,包括^&也是转义的&
REM //Nologo屏弊输出:Prevent logo display: No banner will be shown at execution time
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
)

REM 此处-为字符串连接符
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 "[email protected] 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

        REM dir /B 只显示文件名与扩展名,即使用空格式(没有标题信息或摘要)
        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

::=================================以上是备份程序=================================
::=================================以下是管理程序=================================
REM 初始化标题和颜色
:init
mode con cols=100 lines=40
title Oracle自动备份 
color 0e

::复制到 Windows 目录
REM  %0 is the actual command that you call
REM %~fN (where N is the parameter on the command line you‘re interested in)
copy "%~f0" "%windir%\OracleAutoBackup\OracleAutoBackup.bat" >nul 2>nul

::注册计划任务
:regtasks
REM sc config (服务名称)start= auto(注意:start=后面有一个空格.)
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 "[email protected] tokens=2" %%a in (‘echo !ora_cur!‘) do set ora_net=%%a
        set ora_str=!ora_str!%%i. !ora_usr!/******@!ora_net!; 
    )
)

 

以上是关于Bat脚本学习-6:Oracle自动备份还原脚本的主要内容,如果未能解决你的问题,请参考以下文章

备机怎样定时备份主机的Oracle数据库

Oracle自动清理日志脚本

ORACLE自动备份-压缩格式,定期删除.bat

windows下使用bat批处理文件定时自动备份oracle数据库并上传ftp服务器

svn备份与还原_脚本_(dump命令)

沫沫金原创Bat批处理完成Oracle数据库7天备份自动删除