Backup files per Date String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Backup files per Date String
# 1  
Old 04-23-2008
Error 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
# 2  
Old 04-23-2008
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

# 3  
Old 04-23-2008
Thanks I'll have a look at this code and see what I can come up with.

Thank you,
Drew
# 4  
Old 04-23-2008
Getting an error:

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

# 5  
Old 04-23-2008
Fixed the error Smilie - continuing to look into this script Smilie

Thanks,
Drew
# 6  
Old 04-23-2008
Aha! Wow. This works perfectly Smilie. I wish I knew perl. Something I'll have to look into.

Thanks again.
Drew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract count of string in all files and display on date wise

Hi All, hope you all are doing well! I kindly ask you for shell scripting help, here is the description: I have huge number of files shown below on date wise, which contains different strings(numbers you can say) including 505001 and 602001. ... (14 Replies)
Discussion started by: VasuKukkapalli
14 Replies

2. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

3. Shell Programming and Scripting

Find and copy files based on todays date and search for a particular string

Hi All, I am new to shell srcipting. Problem : I need to write a script which copy the log files from /prod/logs directory based on todays date like (Jul 17) and place it to /home/hzjnr0 directory and then search the copied logfiles for the string "@ending successfully on Thu Jul 17". If... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

4. Shell Programming and Scripting

Faster Line by Line String/Date Comparison of 2 Files

Hello, I was wondering if anyone knows a faster way to search and compare strings and dates from 2 files? I'm currently using "for loop" but seems sluggish as i have to cycle through 10 directories with 10 files each containing thousands of lines. Given: -10 directories -10 files... (4 Replies)
Discussion started by: agentgrecko
4 Replies

5. Shell Programming and Scripting

mv files add date string

I have a requirement where I need to move files that are being written too. I am unable to use sed since I believe this will stop the files from getting written. There is a directory containing about 50 unique files, the date is the same on all of them. I need to change the date string on each... (1 Reply)
Discussion started by: jaysunn
1 Replies

6. Shell Programming and Scripting

How to backup latest date files?

Hi, I have below two situations to handle, 1. I have list of files with file names having date&time. I have to backup to old date files. say I have below files in a directory, 1. XX123_20101004010101.dat 2. XX124_20101004010201.dat 3. XX121_20101003010101.dat 4.... (6 Replies)
Discussion started by: smr_rashmy
6 Replies

7. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

8. Filesystems, Disks and Memory

backup of files for a specific date

In Linux Advance server I want to write one script for backing up files for a specific date like 24/07/2008. (3 Replies)
Discussion started by: akm9999
3 Replies

9. Shell Programming and Scripting

backup of files for a specific date

I want to write script for backing up archive logs files for specific date. please give me idea for that. (2 Replies)
Discussion started by: akm9999
2 Replies

10. UNIX for Dummies Questions & Answers

Backup Date & Time

:confused: I'm not really sure about the default backup date & time in our Unix system and I would like to change it to a convienient time...how do I do that? Please help? (1 Reply)
Discussion started by: EbeyeJJ
1 Replies
Login or Register to Ask a Question