-   Linux - General (
-   -   Bash script help (

alfred_e_neuman 01-15-2013 11:03 AM

Bash script help
Hello, all.

A while ago, I created a script to archive nightly database backups. As I've constructed it, it archives the files from the two months ago. Here's the script:


#Automates the monthly archiving of production database
#Created by D. Yioulos, 10_5_2011

#This script archives production database backups from the month before last
to NAS device Netdisk 219, then deletes those backups, leaving last month's
backups for quick retreival purposes, if necessary.  It is executed via a
cron job which is executed on the 15th of each month.


#our variables
#first day of two months ago
FIRST_DAY=`date -d "-2 month -$(($(date +10#"%d")-1)) days" +"%b%e"`

#first day of last month
LAST_DAY=`date -d "-1 month -$(($(date +10#"%d")-1)) days" +"%b%e"`

#name file to be used by tar
FILENAME="dbbackup"`date --date="2 month ago" +%Y%m`".txt"

#date part of tar file name
FILEDATE=`date --date="2 month ago" +%Y_%m`

#create file to be used by tar

#create start and end dates of backup directories to be tarred
touch --date "$FIRST_DAY" /tmp/start
touch --date "$LAST_DAY" /tmp/end

#identify the directories to be tarred and add them to the file to be used by tar
find /data/nightly_data_backup -type d -newer /tmp/start -not -newer /tmp/end -print | sort > $FILENAME

#start portmap service
/etc/init.d/portmap "start" > /dev/null 2>&1

#open connection to NAS
mount -t nfs netdisk219:/mnt//vol001/volume02/Archive/Database /backup

#back up the database directories to NAS
tar -czf /backup/$FILEDATE".tar.gz" --files-from $FILENAME

#close connection to NAS
umount /backup

#stop portmap service
/etc/init.d/portmap "stop" > /dev/null 2>&1

#remove backed up driectories from server
xargs rm -Rf < /root/$FILENAME

#clean up
rm -f /tmp/start /tmp/end
rm -f /root/$FILENAME

This all worked fine until this month. However, now that we've crossed into 2013, these constructs to create /tmp/start and tmp/end are not working as I'd hope:

FIRST_DAY=`date -d "-2 month -$(($(date +10#"%d")-1)) days"
LAST_DAY=`date -d "-1 month -$(($(date +10#"%d")-1)) days" +"%b%e"`

Rather than creating those files with a creation date of "Nov 1 2012" and "Dec 1 2012", the dates' year is 2013. This, of course, will be a problem in January and February of each succeeding year.

I suppose I could leave off the year creation part, but I'd feel much more comfortable including the year. Can anyone help with a solutions?

Many thanks.


Kustom42 01-15-2013 12:06 PM

First, please edit your post and put your script within the "CODE" tags.

Why are you not just using the find with the one of the time tests to find the files that are older than say 30 or 60 days?

alfred_e_neuman 01-15-2013 12:35 PM

Sorry about not using the code tags in the original post.

If I used "find with the one of the time tests to find the files that are older than say 30 or 60 days", since the number of days in the months varies, I'd archive the wrong set of data. The way I've written the script, I get a discrete month of files.

Kustom42 01-15-2013 12:59 PM

If you want to stick to hard coded dates, consider adding another variable which is simply:


date -d "-1 month -$(($(date +10#"%d")-1)) days" +"%b%e %Y"
All it does is add the year to your FIRST_DAY and LAST_DAY variables, the date binary can do the math and get you the proper year.

alfred_e_neuman 01-15-2013 01:10 PM

Of course! That solves that. Thanks so much!

All times are GMT -5. The time now is 12:54 PM.