-   Programming (
-   -   Bash script help (

alfred_e_neuman 01-15-2013 12:43 PM

Bash script help
Hello, all.

I posted this just a while ago in the Linux General forum when it probably really belongs here. Apologies for the cross-post.

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.


TobiSGD 01-15-2013 12:48 PM

Multiposting is not allowed on LQ. If you think you have posted a thread in the wrong forum use the Report button and ask a moderator to move it to the forum you think fits better instead of creating a new thread.
For now I have asked the moderators of the specific sub-fora to merge your threads here.

alfred_e_neuman 01-15-2013 12:51 PM

Thanks and, again, apologies.

alfred_e_neuman 01-15-2013 01:13 PM

I received a solution to my original post. Thanks for the heads-up on cross-posting.

celticdevildog 01-15-2013 01:13 PM

try changing from


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



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

either way my system shows these dates (in both situations) to be in 2012.

alfred_e_neuman 01-15-2013 02:23 PM

Thanks for your response. That's actually not the part causing the issue. It's:


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


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

by adding %Y to the end of these lines, it solved my problem, as in:


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

All times are GMT -5. The time now is 05:54 AM.