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:
Code:
#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.
#!/bin/bash
#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
touch $FILENAME
#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.
alfred_e_neuman