Backup script calculating which type to run


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Backup script calculating which type to run
# 1  
Old 07-30-2010
Question Backup script calculating which type to run

Hi,

Second attempt at this, which I have gone down a different and seemingly long winded route but I don't know what other way to do it.

Task -
Create a template (or skeleton) script for our backup scripts to sit inside.

Background -
We currently run our monthly (every 28 days) and weekly backups manually from the console every Saturday, be it weekly or monthly. We are looking to automate this so we don't need to be around!

Every 4 weeks (28 days) we have our monthly backups. This is the Saturday after our pay day. Sadly, this is not the first Saturday of the month since it's 4 weekly. Here is a few sample dates of Saturdays where we would run our monthly backups (note the last 2, since it's every 28 days we can have 2 monthly backups in 1 month):

- Saturday 24th July 2010
- Saturday 21st August 2010
- Saturday 18th September 2010
- Saturday 16th October 2010
- Saturday 13th November 2010
- Saturday 11th December 2010
- Saturday 8th January 2011
- Saturday 5th February 2011
- Saturday 5th March 2011
- Saturday 2nd April 2011
- Saturday 30th April 2011

What I have so far -

Basically the script I have made so far checks to see if it is a leap year. Then it checks if the lastbkup variable last went over the end of a year and minuses the days in the year so that it has the correct value for the new year. It then either runs a weekly script, or a monthly script depending on results.

The script actually seems to work fine, except for when it is a leap year. When it minuses 366 days, it should really be 365. The next year should minus 366 instead. Example of this -

For arguments sake it is Saturday 7th January 2012 which is day 7 of the year. It is a leap year. The last monthly backup was run 28 days ago, on the 10th December 2011 which is day 344 of the year (Handy day of the year calculator). The script adds 28 to this, to create the nxtbkup variable to get 372. Since it is a leap year it minuses 366 from this to get 6. Although this value should be 7. The script returns a weekly backup which should be a monthly one. Change the minus from 366 to 365? Well as far as I can work out the minus 366 needs to come in somewhere but I can't get my head around where or how it should come in. So I'm stuck! But apart from this, everything seems to work fine.

Files -

date4, date4.log, lastbkup

date4 contents -
Code:
#!/bin/bash
# Rab
# 30th June 2010
# Template script for working out if a job is
# a weekly or monthly one.

#############################
# THE VARIABLES
#############################

# Days between monthly backups
betweenbkups=28

# Day of year of last backup from text file
lastbkup=$(cat lastbkup)

# Next backup day of year
nxtbkup=$(($lastbkup + $betweenbkups))

# Todays day of year. Sed removes first 0
today=$(date +%j | sed 's/^0*//')

# This year in format YYYY
year=$(date +%Y)

# Number of days in the year
no_of_days=$(cal $year |egrep "^[ 0-9][0-9]| [ 0-9][0-9]$" |wc -w)


#############################
# THE GUTS
#############################

# Testing for leap year
if [[ $no_of_days -eq 365 ]]

# If it is not a leap year 365 days in the year
then
        # If the next backup will be past the year mark
        if [ $nxtbkup -gt 365 ]
        then
                # Take away 365 to get true value for the new year
                nxtbkup=`expr $nxtbkup - 365`
                # If it's a Saturday and today is the same as next backup
                if [ $(date +%u) -eq 6 ] && [ $today = $nxtbkup ];
                        then echo Monthly Backup Ran >> date4.log
                             echo on a non leap year with days over 365 >> date4.log
                             echo $nxtbkup > lastbkup
                             else echo Weekly Backup Ran  >> date4.log
                             echo on a non leap year with days over 365 >> date4.log
                             echo $nxtbkup > lastbkup
                fi
        else
        # If the next backup will be within the year and if it's a
        # Saturday and today is the same as next backup
                if [ $(date +%u) -eq 6 ] && [ $today = $nxtbkup ];
                        then echo Monthly Backup Ran >> date4.log
                             echo on a non leap year with days less than 365 >> date4.log
                             echo $nxtbkup > lastbkup
                        else echo Weekly Backup Ran >> date4.log
                             echo on a non leap year with days less than 365 >> date4.log
                             echo $nxtbkup > lastbkup
                fi
        fi

# If it is a leap year (366 days in the year)
else
        # If the next backup will be past the year mark
        if [ $nxtbkup -gt 366 ]
        then
                # Take away 366 to get true value for the new year
                nxtbkup=`expr $nxtbkup - 366`
                # If it's a Saturday and today is the same as next backup
                if [ $(date +%u) -eq 6 ] && [ $today = $nxtbkup ];
                        then echo Monthly Backup Ran >> date4.log
                             echo on a leap year with days more than 366 >> date4.log
                             echo $nxtbkup > lastbkup
                        else echo Weekly Backup Ran >> date4.log
                             echo on a leap year with days more than 366 >> date4.log
                             echo $nxtbkup > lastbkup
                fi
        else
        # If the next backup will be within the year and if it's a
        # Saturday and today is the same as next backup
                if [ $(date +%u) -eq 6 ] && [ $today = $nxtbkup ];
                        then echo Monthly Backup Ran >> date4.log
                             echo on a leap year with days less than 366 >> date4.log
                             echo $nxtbkup > lastbkup
                        else echo Weekly Backup Ran >> date4.log
                             echo on a leap year with days less than 366 >> date4.log
                             echo $nxtbkup > lastbkup
                fi
        fi
fi

Any help, comments, suggestions would be welcomed.

Regards,
Rab
# 2  
Old 07-30-2010
Quote:
For arguments sake it is Saturday 7th January 2012 which is day 7 of the year. It is a leap year. The last monthly backup was run 28 days ago, on the 10th December 2011 which is day 344 of the year (Handy day of the year calculator). The script adds 28 to this, to create the nxtbkup variable to get 372. Since it is a leap year it minuses 366 from this to get 6. Although this value should be 7. The script returns a weekly backup which should be a monthly one. Change the minus from 366 to 365? Well as far as I can work out the minus 366 needs to come in somewhere but I can't get my head around where or how it should come in. So I'm stuck! But apart from this, everything seems to work fine.
I think that when you have incremented by 28 and your new value is greater than the number of days in last year you need to subtract the number of days in last year because the current year is now 2012.
In your example the 366 days will come into play when the incremented value rolls over into 2013 (by which time the current date must be in 2013).
This User Gave Thanks to methyl For This Post:
# 3  
Old 07-30-2010
Hi!
This sounds very complicated. Or maybe I haven't understood the problem description. But You want to run it EVERY fourth saturday?
Wouldn't it help to:
1) Schedule a cron job to run every saturday
2) let it read the "lastbkp" file where You have stored a backup counter
3) if it is four, do Your backup routine, write "1" to the file, and exit
4) if it is less than four, just increment it by one and write it back.

Best regards,
Lakris
This User Gave Thanks to Lakris For This Post:
# 4  
Old 08-03-2010
Thanks Lakris,

Now have a working script Smilie

Really helps to stand back and let someone else see it, instead of doing it the long winded hard way!

Cheers,
Rab
# 5  
Old 08-05-2010
Great, I'm glad to help!
Smilie
/L
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python Script Calculating Average

Can anyone explain what each line of the code does and how it works? I have no experience with python so I am not sure how the arrays and such work. I found this code while looking through the forums. f = open("exams","r") l = f.readline() while l: l = l.split(" ") values = l ... (22 Replies)
Discussion started by: totoro125
22 Replies

2. Shell Programming and Scripting

Calculating script run time in Solaris OS

Hi All, I have written script and wanted to know the run time of it in seconds. i used below logic but am not getting the results in second instead getting error. cat pkloader.sh # if you want to calculate the time in milliseconds then use $(date +%s%N) START_TIME=`date +%s` echo... (2 Replies)
Discussion started by: Optimus81
2 Replies

3. Shell Programming and Scripting

Help with Backup Shell Script for Network Device Configuration backup

HI all, im new to shell scripting. need your guidence for my script. i wrote one script and is attached here Im explaining the requirement of script. AIM: Shell script to run automatically as per scheduled and backup few network devices configurations. Script will contain a set of commands... (4 Replies)
Discussion started by: saichand1985
4 Replies

4. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

5. AIX

calculating the size of the luns using script

Hi This is the lspv output of my server : cbspsdb01 #lspv hdisk0 00c7518d2d512fd4 cdgvg active hdisk1 00c7518d2dcbc9d6 cdgvg active hdisk2 00c7518dcda9199a appvg active hdisk3... (1 Reply)
Discussion started by: samsungsamsung
1 Replies

6. UNIX for Dummies Questions & Answers

anyone know if backup exec 11d agent run on solaris10 x86?

anyone ? i install and it just fail to startup. (0 Replies)
Discussion started by: kakabobo
0 Replies

7. UNIX for Dummies Questions & Answers

How to determine what type of backup I've on the tape

I've tape which I've to use to restore data. The problem is that I don't know what type of backup I have on this type. Can someone help me and tell me how can I determine what type of backup is on the tape? Thanks for your help. (2 Replies)
Discussion started by: fraydey
2 Replies

8. UNIX for Dummies Questions & Answers

Calculating field using AWK, or GAWK script

Hello all, I'm totally new to UNIX/Linux but I'm taking a course in it at my local JC. My question: I have been tasked with writing a gawk script that will create a nicely formatted report. That part I've done ok on...however, the very last thing that must be done is a calculation of a... (4 Replies)
Discussion started by: Trellot
4 Replies

9. Solaris

<drive type unknown> when format command run

Hi, we are running solaris 9 on V280R. Due to power failure, one disk's label and lost its details like drive type etc. Disk details: <SUN72G cyl 14087 alt 2 hd 24 sec 424> single partition on slice 6 with UFS. The below i see when i run "format" command ..... ..... 4.... (1 Reply)
Discussion started by: prvnrk
1 Replies
Login or Register to Ask a Question