Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monthly Backup 1

Status
Not open for further replies.

khalidaaa

Technical User
Jan 19, 2006
2,323
BH
Hi Unixers,

I have a small script that backs up my data on a daily basis. I'm using the same script to backup a monthly backup (full data to be kept for the whole year) and i use this logic for the backup:

Code:
# Check whether today is the first day of the month to do a full monthly backup
#
DOW=`date | awk '{ printf $3 }'`
if [ $DOW -eq 1 ]
then
    LEVEL=0
    BkupType="Monthly"
fi

I'm checking for first of the month but the requirement from the management is to make the backup run in the last day of the month!

Could you please let me know how would i check for the last day of the month?

(one thought was to add to the month one day and check whether the day became 1 which indiciates that i'm in the last day! The other thought is to check for each month (28, 29, 30, 31) thru if statements! but then how would i check for leap years?)

Thanks in advance.

Regards,
Khalid
 
Khalid
Firstly you can tidy up your test to
Code:
if [ $(date +%d) -eq 1 ]
then
 ...
To find the last day of the month look at PHV's excellent FAQ faq80-4800 which allows you to use
Code:
if  [ $(GetDate 1 '+%d') -eq 1 ]
i.e. if tomorrow's date is the first...

Ceci n'est pas une signature
Columb Healy
 
Or if your version of date supports it, you could use:
Code:
if [ `date +%d -d tomorrow` = 01 ]
then
    LEVEL=0
    BkupType="Monthly"
fi

HTH :)
 
Khalid

See also thread822-1404900 where I asked a very similar question

Ceci n'est pas une signature
Columb Healy
 
Thank you very much columb! My statement is this for now:

Code:
DOW=`date | awk '{ printf $3 }'`
last_day_in_month=$(echo $(cal $(date +%m) $(date +%Y)) | awk '{print $NF}')
if [ $DOW -eq $last_day_in_month ]
then
    LEVEL=0
    BkupType="Monthly"
fi

That's exactly what i wanted :) Star Deserved!

Regards,
Khalid
 
You can also use this (On AIX)

TZ=GMT+24 date +%d

to get yesterdays date

then its pretty straight forward to implement

a=`TZ=GMT+24 date +%d`

if [ $a = "1" -o $a = "01" ] ; then
run monthly backup
else
run daily backup
fi

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top