批量脚本删除旧备份并保留一个备份

时间:2014-04-24 12:11:40

标签: oracle shell batch-file scripting backup

我启动一个脚本--command=STATISTICS --statdata=SAVEPOINTS > C:\Safepoints.txt ,生成这样的输出。

Page count for each save point version:
  0: version=0, status=3, ts=2014-03-18 16:24:51.764, page count=68861
  1: version=1, status=3, ts=2014-03-18 17:49:25.622, page count=68861
  2: version=2, status=3, ts=2014-03-19 05:00:10.467, page count=68925
  3: version=3, status=2, ts=2014-03-20 14:05:53.267, page count=2744
  4: version=4, status=3, ts=2014-03-20 15:08:40.607, page count=68859
  5: version=5, status=3, ts=2014-03-21 05:00:10.527, page count=68926

我的想法是,阅读C:\ Safepoint.txt并检查是否存在多于一个status = 3(完全备份)的文件,而不是保留最新文件,否则启动一个像这样的新命令

--command=DELETE_SAVEPOINTS 0-4

我修改了脚本如下,但它在

之前停留
  

for>之后的循环%mytempfile%

@echo on

set last=-1

set mytempfile=%TEMP%\%random%.out
%ORACLE%\bin\OracleDatabase.bat --dbtype=ORACLE --database=orca --hostname=test.ora.db --port=5645 --user=sa --password=***** --command=STATISTICS --statdata=SAVEPOINTS > %mytempfile%

for /f "tokens=1 delims=: " %%a in ('type %mytempfile% ^| find "status=3" ') do  set /a last=%%a
for /f "tokens=1 delims=: " %%a in ('type %mytempfile% ^| find "status=3" ') do (
if %last% neq %%a (
 %Oracle%\bin\OracleDatabase.bat --dbtype=ORACLE --database=orca --hostname=test.ora.db --port=5645 --user=sa --password=***** --command=DELETE_SAVEPOINTS %%a
)
) 

del /q %mytempfile%

1 个答案:

答案 0 :(得分:0)

我不确定要求删除保存点的内容/时间,但是..

@echo off
set "last=-1"
for /f "tokens=1 delims=: " %%a in ('findstr /l /c:"status=3" safepoint.txt') do set /a "last=%%a-1"
if %last% geq 0 (
    RunWhatEver --command=DELETE_SAVEPOINTS 0-%last%
)

它使用status=3搜索最后一行并检索初始数字。如果它大于或等于0,则找到一行,并调用脚本以删除从0到上一次完全备份之前的保存点的任何内容