set BACKUP_DIR=G:\test
set ORACLE_USERNAME=scott
set ORACLE_PASSWORD=tiger
set ORACLE_SERVICE=orcl
set BACK_OPTION="owner=%ORACLE_USERNAME%"
set RAR_PATH="E:\Progra~1\WinRAR\RAR.exe"
set RESERVE_DAY=7


set EXP_TIME=%date:~0,10%_%time:~0,2%%time:~3,2%%time:~6,2%
set BACK_NAME=%ORACLE_SERVICE%_%ORACLE_USERNAME%_%EXP_TIME%

set BACK_FULL_NAME=%BACKUP_DIR%\%BACK_NAME%

echo %BACK_FULL_NAME%

rem 开始备份
exp %ORACLE_USERNAME%/%ORACLE_PASSWORD%@%ORACLE_SERVICE% %BACK_OPTION% file="%BACK_FULL_NAME%.dmp" log="%BACK_FULL_NAME%.log"

rem 压缩并删除原有文件
rem RAR a参数添加文件到压缩文件 d删除压缩文件中的文件 f刷新压缩文件中的文件
%RAR_PATH% a -df "%BACK_FULL_NAME%.rar" "%BACK_FULL_NAME%.dmp" "%BACK_FULL_NAME%.log"

rem 删除多少天前的文件
rem forfiles /p %BACKUP_DIR% /m %ORACLE_SERVICE%_%ORACLE_USERNAME%_* /d -%RESERVE_DAY% /c "cmd /c del @file /f"