LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-15-2013, 10:03 AM   #1
alfred_e_neuman
LQ Newbie
 
Registered: Jan 2013
Posts: 11

Rep: Reputation: Disabled
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:

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

Last edited by alfred_e_neuman; 01-15-2013 at 12:11 PM.
 
Old 01-15-2013, 11:06 AM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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?
 
Old 01-15-2013, 11:35 AM   #3
alfred_e_neuman
LQ Newbie
 
Registered: Jan 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-15-2013, 11:59 AM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

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

Code:
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.
 
Old 01-15-2013, 12:10 PM   #5
alfred_e_neuman
LQ Newbie
 
Registered: Jan 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Of course! That solves that. Thanks so much!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration