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:

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

start with this:

#!/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

Thanks I'll have a look at this code and see what I can come up with.

Thank you,
Drew

Getting an error:

monthlybackup.sh: line 19: unexpected EOF while looking for matching `''
monthlybackup.sh: line 26: syntax error: unexpected end of file

Fixed the error :slight_smile: - continuing to look into this script :slight_smile:

Thanks,
Drew

Aha! Wow. This works perfectly :). I wish I knew perl. Something I'll have to look into.

Thanks again.
Drew