Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

First, I wasn't sure if belonged to StackOverflow or here, but figure this would be more applicable to server administration.

We have a .tar backup file going to an offsite backup server each day as apart of our daily processing. These are relatively large files averaging between 25-30 Gigs. As result, we are only able to keep about 2 weeks of backups. Every Friday (yes, this is Friday), one will ssh to the remote the server, OpenSuse 12.1, and manually delete the files the week prior and keep the current week's data. There is a consistent naming scheme within the file with something like BACKUP<YYYYMMDD><Time stamp>, for example.

BACKUP20121022268498.tar
BACKUP20121022268498.log
BACKUP20121023268498.tar
BACKUP20121023268498.log
BACKUP20121024268498.tar
BACKUP20121024268498.log
BACKUP20121025268498.tar
BACKUP20121025268498.log
BACKUP20121026268498.tar
BACKUP20121026268498.log
BACKUP20121029268498.tar
BACKUP20121029268498.log
BACKUP20121030268498.tar
BACKUP20121030268498.log
BACKUP20121031268498.tar
BACKUP20121031268498.log
BACKUP20121101268498.tar
BACKUP20121101268498.log

and so on.

Most of the time I would use the command rm *20121022* in this example. Sometimes, like in this example, im even able to get away with using rm *201210[22-26]*. Doing a quick google search on muddied the water for me. I'm basically trying set this up so that an intern can log in, and run script from the user directoy.

Is there an easy way to do this? Where do I start here? I'm familiar with using date functions in other languages, but I seem be at an impass as where to start here. I don't do alot of Bash scripting. Thanks.

share|improve this question
    
Any reason you are not compressing the tar files using gzip? –  kernelpanic Nov 2 '12 at 20:29
    
@kernelpanic Technicly, it's a vendor's machine that's being backed up. They keep the hood tightly shut with little control over the process. It's application built on top of AIX. If you only knew the world I live in :( –  hydroparadise Nov 2 '12 at 20:37
1  
@hydroparadise: You said the 'A' word. I think that says it all.... –  Scott Pack Nov 2 '12 at 20:38
1  
Any reason this is not in a cron job ? –  adaptr Nov 5 '12 at 14:40
add comment

2 Answers

up vote 1 down vote accepted

Assuming the timestamps of the files stay consistent you can do this with find.

find /path/to/backups -type f -maxdepth 1 -mtime +7 -delete

This will list all of the files within the directory, without recursing, /path/to/backups that were last modified more than 7 24 hour periods ago and delete them. I would recommend running without the -delete first until you know it'll work for you.

If you instead want to operate on calendar days use the -daystart option. I find this most useful when I have something that rotates files hourly, but I want to base my retention schedule on a calendar. Sometimes the distinction between "day" and "24-hour period" actually matters and using -daystart certainly makes the retention schedule easier to explain.

share|improve this answer
add comment

You can delete files older than 1 week using the following command:

find /path -type f -name "BACKUP*" -mtime +7 -exec rm {} \;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.