The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Convert String to Date ORatjeuh Shell Programming and Scripting 2 05-06-2008 03:49 AM
Sorting Files by date and moving files in date order rebel64 Shell Programming and Scripting 2 03-11-2008 08:45 AM
Date parsing into string, help! martyb555 Shell Programming and Scripting 5 02-17-2008 06:12 PM
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 12:17 AM
Backup Date & Time EbeyeJJ UNIX for Dummies Questions & Answers 1 09-06-2005 07:14 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 16
Exclamation Backup files per Date String

Guys,

I've got a quick logic question. I'm pretty savvy when it comes to shell scripting, however, I'm at a loss when it comes to writing a simple shell script to backup only certain files out of the month. I have a directory, for example, /data/backups/websites/domain. In each "domain" directory, there are files that are named by the date of when they were created, for example:

Code:
deckerd-2008-04-11.tar.gz
deckerd-2008-04-12.tar.gz
deckerd-2008-04-13.tar.gz
deckerd-2008-04-14.tar.gz
deckerd-2008-04-15.tar.gz
deckerd-2008-04-16.tar.gz
deckerd-2008-04-17.tar.gz
deckerd-2008-04-18.tar.gz
deckerd-2008-04-19.tar.gz
deckerd-2008-04-20.tar.gz
The string is: domain-year-month-day.tar.gz

All I'm wanting to do is backup one file per week, and create a monthly backup (about 4 backups per month). Maybe I should change the string creation to a "day" and just backup "sun" (sunday) for that current month and then backup the backups - that might work. However, I am wanting to run a backup everyday, then at the end of the month, backup the 4 (sometimes just 3) backups of that month and create a monthly backup. The reason being that if I need a daily backup during the month I have it, but after that month is over, I should (maybe) only need a weekly backup.

This is how I want my system to be, and just needed some insight on how to actually get the individual (3 or 4 backups per month) to work. I already have it down how to create the backups and such, but taking what is currently being backed up (30 or 31 backups), and then at the end of the month, do the weekly backups (the part I'm wanting help with).

If anyone has any code snippets or just a few lines of "this could work" or "try this", I'd be very grateful.

Thanks in advanced,
Drew
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-23-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,483
start with this:
Code:
#!/bin/ksh
# print day of week
dow()
{
        perl -e '
                use POSIX qw(strftime);
                $fmt = "%A";  # %a = abbreviated weekday %u = weekday number
                $mday = substr("$ARGV[0]", 8, 2);
                $mon =  substr("$ARGV[0]", 5 ,2);
                $year = substr("$ARGV[0]", 0 ,4);
                $weekday =
                  strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
                print "$weekday";
                ' "$1"
}

for file in $( ls *.tar.gz )
do

      dt=$( echo $file | awk -F'-' { printf("%s-%s-%s", $2, $3, $4)}' )
      day=$( dow $dt )
      if [[ $day = "Sun" ]] ; then
      # do backup here 
      fi
done
Reply With Quote
  #3 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 16
Thanks I'll have a look at this code and see what I can come up with.

Thank you,
Drew
Reply With Quote
  #4 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 16
Getting an error:

Code:
monthlybackup.sh: line 19: unexpected EOF while looking for matching `''
monthlybackup.sh: line 26: syntax error: unexpected end of file
Reply With Quote
  #5 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 16
Fixed the error - continuing to look into this script

Thanks,
Drew
Reply With Quote
  #6 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 16
Aha! Wow. This works perfectly . I wish I knew perl. Something I'll have to look into.

Thanks again.
Drew
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0