1825774 Members
2090 Online
109687 Solutions
New Discussion

script

 
SOLVED
Go to solution
Sanjiv Sharma_1
Honored Contributor

script

I have a script which runs Ignite-UX backup every week.

This creates folders like:
/var/opt/ignite/recovery/2004-03-21,04:08
/var/opt/ignite/recovery/2004-03-28,04:09
/var/opt/ignite/recovery/2004-04-04,04:08
/var/opt/ignite/recovery/2004-04-11,04:09
/var/opt/ignite/recovery/2004-04-18,04:09
/var/opt/ignite/recovery/2004-04-25,04:08

I want to write a script which will check the /var/opt/ignite/recovery folder and remove the 2004-XX-XX,0X:XX folder which is dated last month.

Here I would like to remove:
/var/opt/ignite/recovery/2004-03-21,04:08
/var/opt/ignite/recovery/2004-03-28,04:09

Thanks,
Everything is possible
5 REPLIES 5
V.Tamilvanan
Honored Contributor
Solution

Re: script

Hi Sanjiv,

To get the directory.
#find /var/opt/ignite/recovery -type d -a -mtime +30 -exec ls -d {} \;

to delete the direstory
#find /var/opt/ignite/recovery -type d -a -mtime +30 -exec rm -R {} \;
H.Merijn Brand (procura
Honored Contributor

Re: script

Not every month has 30 days. And the FAQ answers to 'How do I print yesterday' cannot be used here. But perl is still my way to go: clean and simple

# date ; perl -MTime::Local -le '@t=localtime;$t[4]--;print scalar localtime timelocal@t'
Tue Apr 27 08:15:30 CEST 2004
Sat Mar 27 08:15:30 2004

But this will fail for 31 March, because 31 February does not exist:

# date ; perl -MTime::Local=timelocal_nocheck -le '@t=localtime;$t[4]--;$t[3]=32;print scalar localtime timelocal_nocheck@t'
Tue Apr 27 08:22:58 CEST 2004
Thu Apr 1 08:22:58 2004

which forced the date to the 32nd before converting back. If that behaviour is acceptable

# perl -MTime::Local=timelocal_nocheck -e'@t=localtime;$t[4]--;@t=localtime timelocal_nocheck@t;$f=sprintf"%4d-%02d-%02d",1900+$t[5],1+$t[4],$t[3];$file=<$f,* >'

Tue Apr 27 08:15:30 CEST 2004
Sat Mar 27 08:15:30 2004

Will give you the file with the date last month in $file

If you rather not look at month', but use a fixed 30 day period,

# perl -e'@t=localtime(time-30*86400);$f=sprintf"%4d-%02d-%02d",1900+$t[5],1+$t[4],$t[3];$file=<$f,* >'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
john korterman
Honored Contributor

Re: script

Hi,
I was just wondering....what would be wrong with this approach:

#!/usr/bin/sh
# Delete older than current month from FOLDER
FOLDER= # path to ignitefolders
CURRENT_YEAR=$(date +%Y)
CURRENT_MONTH=$(date +%m)
DEL_MONTH=""
DEL_YEAR=""
# Construct pattern for deletion
if [ "$CURRENT_MONTH" = "01" ]
then
DEL_MONTH="12"
else
DEL_MONTH=$CURRENT_MONTH
fi
if [ "$DEL_MONTH" = "12" ]
then
DEL_YEAR=$(($CURRENT_YEAR - 1 ))
else
DEL_YEAR=$CURRENT_YEAR
fi
ls -l ${FOLDER}/${DEL_YEAR}-${DEL_MONTH}-*

regards,
John K.
it would be nice if you always got a second chance
Elmar P. Kolkman
Honored Contributor

Re: script

John's solution would work, except for 1 thing: you use CURRENT_MONTH as value for DEL_MONTH.

Change the line DEL_MONTH=$CURRENT_MONTH to
(( DEL_MONTH = $CURRENT_MONTH - 1 ))

and it will work.
Every problem has at least one solution. Only some solutions are harder to find.
john korterman
Honored Contributor

Re: script

Hi again,
Thanks Elmar, you are of course right! Now I wonder how I managed to miss the most important point.

regards,
John K.
it would be nice if you always got a second chance