Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: Looking for an NT script that will remove obsolete RMAN backups
On 2 Apr 2003 10:54:24 -0800, shoad316_at_hotmail.com (Dave) wrote:
>Hey all, > > I haven't done much windows scripting before so i'm trying to find >a script that will do the following. Just hoping someone out there >has done it before and will save me a few hours of pulling my hair. :) > >Basically i'm looking for a script that will grab obsolete rman >backups and remove them. Its fairly easy and Oracle was nice enough >to provide a Unix script to do this but alas, have forsaken NT. > >If you can help me out, let me know. > >Thanks! >Dave
This works in NT 4 - remove the ECHO in the last section to delete for
real.
All lines indented 3 chars (may wrap in newsreader)
@ECHO off
IF "%2"=="" GOTO :s_help IF "%1"=="/?" GOTO :s_help IF "%OS%"=="Windows_NT" GOTO :s_startecho.
:s_help
echo.
echo DEL_OLDER.cmd days fullpath
echo.
echo For example:
echo DEL_OLDER 20 c:\temp\*.* echo DEL_OLDER 365 C:\temp\*.LOGecho.
:s_start
SETLOCAL
::------------------ FIRST PART ---------------
:: Calculate the filedate of the oldest file to keep
:: given the no of days to subtract before today
SET v_days_older=%1
SET v_source=%2
:: Get the date independent of regional settings
FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (
FOR /f "tokens=2 delims= " %%A IN ('date /t') DO ( SET v_first=%%G SET v_second=%%H SET v_third=%%I SET v_all=%%A ) ) SET %v_first%=%v_all:~0,2% SET %v_second%=%v_all:~3,2% SET %v_third%=%v_all:~6,4%
ECHO Today is: Year: [%yy%] Month: [%mm%] Day: [%dd%]
::calculate new date
SET /a v_new_day=%dd%-%v_days_older%
:s_check_days
:: check days are valid for the current month :::::::::::::::
IF /i %v_new_day% GTR 0 GOTO s_got_date
:: The new date is not in the current month
:: so calculate the previous month
:: if its January the previous_month must be December...
IF /i %mm% EQU 1 (
SET /a yy=%yy%-1 SET mm=12 ) ELSE ( SET /a mm=%mm%-1)
:: note that %mm% now contains LAST month
:: and if appropriate %yy% will contain LAST year
:: now add on the appropriate number of days for the month
CALL :s_%mm%
:: and go back to see if we have used up the days yet
GOTO s_check_days
:: The rest of the batch file is simply calculating
:: the valid number of days for the month given in mm
::30 day months
:s_4
:s_6
:s_9
:s_11
SET /a v_new_day=%v_new_day%+30
GOTO :EOF
::31 day months
:s_1
:s_3
:s_5
:s_7
:s_8
:s_10
:s_12
SET /a v_new_day=%v_new_day%+31
GOTO :EOF
::28/29 day month
:s_2
:: fix for leap year
:: every fourth year is a leap year EXCEPT for century years that
are not divisible by 400.
:: SET/a only supports integers (not REAL numbers) so we * 10 and
/4
:: then test the last digit to see if it divided evenly
:: e.g. 2001 *10 = 20010
:: 20010/4 =5002{.5}
:: we then test the 4th digit (2=not a leap yr, 0=is a leap yr)
:: (you can expect the above technique to fail with some REAL
numbers
:: like 5000{.5}, but luckily this never happens with a leap year)
:: first do a MOD 4
SET /a v_leap=%yy%*10
SET /a v_leap=%v_leap%/4
SET v_leap=%v_leap:~3,1%[]
IF /i NOT %v_leap%==0[] GOTO s_feb
:: then do a MOD 100
::SET /a v_leap=%yy%/10
SET v_leap=%yy:~2,2%[]
IF /i NOT %v_leap%==00[] GOTO s_leapyear
:: If we get here it must be a century year
:: then do a MOD 400
SET /a v_leap=%yy%/40
:: next line is good til the year 3999 only
SET v_leap=%v_leap:~1,1%[]
IF /i NOT %v_leap%==0[] GOTO s_feb
:: leap year = 29 days
:s_leapyear
echo It's a leap year in: %yy%
SET /a v_new_day=%v_new_day%+29
GOTO :EOF
:: Normal February = 28 days
:s_feb
SET /a v_new_day=%v_new_day%+28
GOTO :EOF
:s_got_date
SET dd=%v_new_day%
::ensure the days and months have a leading 0
IF %mm:~0,1==%mm% SET mm=0%mm%
IF %dd:~0,1==%dd% SET dd=0%dd%
ECHO Files older than: Year: [%yy%] Month: [%mm%] Day: [%dd%]
::--------------------- SECOND PART -----------
:: List all files in destination older than this date
:: The command to get a list of files newer than given date
:: XCOPY %v_source% /D:%mm%-%dd%-%yy% /L | FIND "\"
:: To get the opposite of the above, compare every file in the DIR
:: with a list of the newer files and delete all those that don't
match
FOR /f "tokens=*" %%G IN ('XCOPY %v_source% /L ^| FIND "\"') DO CALL :s_match_older "%%G"
goto :eof
:s_match_older
FOR /f "tokens=*" %%G IN ('XCOPY %v_source% /D:%mm%-%dd%-%yy% /L ^|
FIND "\"') DO IF %1=="%%G" (GOTO :eof)
ECHO DELETE the file %1
:: To use this batch for real, edit the line above
:: i.e. change the [ECHO DELETE the file %1] to [DEL %1] or [MOVE
%1] or whatever
::
GOTO :eof
-
Simon Sheppard
http://www.ss64.com
-
Received on Thu Apr 03 2003 - 15:52:52 CST