LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-25-2010, 11:33 AM   #1
No_one_knows_me
LQ Newbie
 
Registered: Dec 2009
Posts: 20

Rep: Reputation: 0
Modify date command to show previous year when performing December backups in January


I know the subject is a little verbose, but that is exactly the trouble I am having. I have a script (many thanks to those on LQ who helped me create it) that moves files from one directory to another, based on the numeric date of the file name (i.e., 20091212 would go to the December directory). Now, since this script will be ran at the beginning of the following month (December's files to be tarred and gzipped will be performed the first day of January, and January's in February, etc.), it appears to me that the script will be tricky when it comes time to do the December files in January.

Here's part of the script:

Code:
# Define working directory and target directory.
DIR=/var/log
target=$DIR/month
hostname=`uname -n`

# Switch to working directory.
cd "$DIR"
echo "Entered working directory $DIR"

# Main loop.
for file in 2*.gz; do

     echo "-----------------------"
     echo "Processing file: $file."
     echo

     # Test whether file is actually a file.
     if [[ ! -f $file ]]; then
          echo "$file is not a regular file.  Skipping"
          continue
     fi


     case "${file:5:2}" in
          01)  mkdir -vp $target/January
               mv -v --target-directory $target/January $file
               ;;
          02)  mkdir -vp $target/February
               mv -v --target-directory $target/February $file

####    -----This continues for the remaining months-----

*)   echo "File $file does not contain a valid month number."
               ;;
               # Provides feedback for invalid hits.
     esac

echo "-----------------------"

done

date=`date +%B`

###THIS IS WHERE I'M RUNNING INTO TROUBLE!!!###    
     case "${date}" in

          January) cd $target/December
                   tar -czvf * December.`date +%Y.$hostname
                   ;;
         February) cd $target/January
                   tar -czvf * January.`date +%Y.$hostname
                   ;;
     esac
     
echo "Finished"

exit 0
I can't seem to figure out a way to carry the output of the date command to the next command, and the year for the December files will always be wrong. Anyone out there who can make sense of my needs?
 
Old 01-25-2010, 01:11 PM   #2
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora40
Posts: 6,153

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
December is the only month which will be "wrong".
So we need to do some simple maths.
Try:
Code:
###THIS IS WHERE I'M RUNNING INTO TROUBLE!!!###    
     case "${date}" in

         January) cd $target/December
                   tar -czvf * December.$(( $(date +%Y) - 1 )).$hostname
                   ;;
         February) cd $target/January
                   tar -czvf * January.$(date +%Y).$hostname
Note $(command) is replacing the "backticks": `command` format.
 
Old 01-26-2010, 09:14 AM   #3
No_one_knows_me
LQ Newbie
 
Registered: Dec 2009
Posts: 20

Original Poster
Rep: Reputation: 0
The date modification part works. Now, I seem to have poorly thought out the tarring of all the files in the directory and renaming them to the month.year.hostname format. As I have it now, the script wants to tar all the files in the directory and it looks for the month.date.hostname file to tar, too. I want to tar all the files in the directory and name the tarball month.date.hostname. Will this entail somehow storing the files using something like {} and then give the destination file name? Will I also have to include the tar.gz extension in the renaming of the files, or will it automatically tack on the tar.gz extension?

Last edited by No_one_knows_me; 01-28-2010 at 08:54 AM.
 
  


Reply

Tags
bash, date, script, shell, tar


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i want to know the command which show the date of creation of file abhi_singh4 Red Hat 6 09-16-2011 01:02 AM
vi help, how to modify previous command ufmale Linux - Newbie 1 09-05-2008 01:20 PM
Tar not overwriting previous backups neocookie Linux - General 6 02-13-2007 06:03 AM
Date command - month of year, blank padded? menator Programming 3 06-27-2006 07:00 AM
date command getting previous day swinchen Programming 6 08-23-2004 01:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:58 AM.

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