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.

I have a backup script that compresses various files and directories and creates .tgz archives. Files are named, e.g.

...
backup_2010-10-28.tar.gz
backup_2010-10-29.tar.gz
backup_2010-10-30.tar.gz
backup_2010-10-31.tar.gz
backup_2010-11-01.tar.gz

I want to manage these files so only the last 5 backups are kept, with older files being deleted.

Can I use logrotate to do this? They aren't log files and are already compressed. They're in /root and not in /var/log - can I still use it?

Thanks

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Logrotate rotates files, so the answer is yes - probably, and if no sufficient permissions then place them in /backup or something. Check what group and user the rotated logs have :-).

There's options for compression in logrotate, så if "compress" is NOT configured - well then it won't try. Also in your case, "rotate 5" option.

Take a look in /etc/logrotate.d (or where ever it's stored in your system)

share|improve this answer

Without a change to your process, logrotate on its own will not do what you're looking for here. The key problem here is that, while logrotate can take wildcards, it will not treat the files as one if you do so and will instead attempt to rotate all of them individually, which is definitely NOT what you want.

You can, however, make it work the way you describe as long as the most recent backup is created without a date stamp. If you backup process creates /root/backup.tar.gz for instance, you could use the following logrotate configuration:

/root/backup.tar.gz {
    rotate 5
    nocompress
    dateext
    dateformat _%Y-%m-%d
    extension .tar.gz
    missingok
}

The quick rundown of the options here:

  • rotate 5 -- keep 5 rotations before deleting
  • nocompress -- do not compress the files after rotating
  • dateext -- use the date as the rotation extension instead of incrementing numbers
  • dateformat _%Y-%m-%d -- set the date extension format you want to use
  • extension .tar.gz -- make the .tar.gz come after the rotation extension
  • missingok -- if the file we want to rotate isn't there, don't worry about it and move on (the default is to throw an error)

Hope this helps!

share|improve this answer
1  
Nice work. I learned a couple of things by reading this. I do want to add though, that some distributions, notably RedHat EL, tend to strip out options that aren't "Enterprise Ready," so YMMV. –  zerolagtime Nov 1 '10 at 18:09

You don't have to use logrotate to do it. Just use a command like this:

ls -1 /root/backup_* | sort -r | tail -n +6 | xargs rm > /dev/null 2>&1

This command will leave the most recent 5 files and remove the remaining (if any). You can use it in a cron job.

share|improve this answer

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.