Link to home
Start Free TrialLog in
Avatar of twolines
twolinesFlag for United States of America

asked on

Handy backup rotation bash script

I have bunch of files named xxx-20111201.gz, xxx-20111202.gz, ..., xxx-20111231.gz, ...., xxx-20120830.gz.

I need to rotate this files leaving only two last backups in each month.
The problem is that I do not always know if there will be backups for 30, 31st of each month. There is a possibility that all of them will be missing except for example couple for the first and second day of the month than I need to keep this two avalable  backups for this month.

Is there any handy way to do this rotation in a bash script?
Avatar of ozo
ozo
Flag of United States of America image

Do you want to delete all but the last two files in each month?
Avatar of twolines

ASKER

yep
perl -e ' /(.*)\d\d\.gz/ and ++$n{$1}>2 and unlink for reverse <*.gz>'
find /path/to/the/backups -name "xxx*.gz" -mtime +3
The above should return all backups that are older than three days.
Adding -exec rm {}\; to the above find line

How often are you considering running this cleanup script?
I've requested that this question be closed as follows:

Accepted answer: 500 points for arnold's comment #a38348953

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Was there any problem with the answer in http:#a38348937 ?
As I understand the question, http:#38348953 does not seem to satisfy its requirements, while http:#a38348937 does.
As I understand the question, http:#38348953 does not seem to satisfy its requirements, while http:#a38348937 does.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of modus_operandi
modus_operandi

I've requested that this question be closed as follows:

Accepted answer: 250 points for arnold's comment #a38348953
Assisted answer: 250 points for ozo's comment #a38348937

for the following reason:

Starting auto-close process to implement the recommendations of the participating Expert(s).<br />&nbsp;<br />modus_operandi<br />EE Admin