Many sysadmins face the challenge of managing growing backup archives. While logrotate is traditionally used for log files, its powerful rotation capabilities can be effectively applied to backup files as well.
Logrotate's core functionality - file rotation based on time/size and retention policies - perfectly matches backup management needs. The key configuration parameters that make it suitable:
rotate 5 # Keep 5 backups
daily # Rotate daily
compress # Though files are already compressed
missingok # Don't error if no files exist
notifempty # Don't rotate empty files
Create a new config file in /etc/logrotate.d/backups:
/root/backup_*.tar.gz {
daily
rotate 5
missingok
notifempty
nocreate
dateext
dateformat -%Y-%m-%d
extension .tar.gz
sharedscripts
postrotate
# Optional: Any post-rotation commands
endscript
}
For more complex scenarios, consider these additional parameters:
size 100M # Rotate when file exceeds 100MB
maxage 30 # Delete files older than 30 days
olddir /backups/archive # Move old files to different directory
copytruncate # Useful for applications that keep files open
Always test before deploying:
# Dry run to test configuration
logrotate -d /etc/logrotate.d/backups
# Force rotation (even if not due)
logrotate -vf /etc/logrotate.d/backups
For regular execution, add to root's crontab:
0 0 * * * /usr/sbin/logrotate /etc/logrotate.d/backups
While logrotate works well, alternatives exist:
# Simple find command alternative
find /root -name "backup_*.tar.gz" -mtime +30 -exec rm {} \;
# Using tmpwatch (RHEL/CentOS)
tmpwatch 30d /root
While logrotate is traditionally used for log files, its powerful rotation capabilities make it perfectly suitable for managing backup archives like your .tar.gz
files. Here's how to configure it:
/root/backup_*.tar.gz {
daily
rotate 5
compress
missingok
notifempty
nocreate
}
The configuration above tells logrotate to:
- Process all
.tar.gz
files in /root starting with "backup_" - Check files daily (though rotation only happens when run)
- Keep 5 rotated versions (your requirement)
compress
is already set though your files are compressed - harmless in this casemissingok
prevents errors if no files match the patternnocreate
prevents logrotate from creating new empty files
You have two main approaches to implement this:
1. System-wide configuration:
sudo vim /etc/logrotate.d/backups
Add your configuration and save. Logrotate will automatically include this in its daily runs.
2. Custom configuration file with manual execution:
logrotate -vf /path/to/your/config_file
The -v
flag enables verbose output, -f
forces rotation.
Always test new configurations before deployment:
sudo logrotate --debug /etc/logrotate.d/backups
If you prefer not to use logrotate, consider these bash alternatives:
# Simple keep-last-5 implementation
ls -t /root/backup_*.tar.gz | tail -n +6 | xargs rm -f
# More robust version with checks
find /root -name "backup_*.tar.gz" -type f | \\
sort -r | \\
awk 'NR>5' | \\
xargs -I {} rm -v {}
For automated cleanup, add to root's crontab:
0 3 * * * /usr/sbin/logrotate /etc/logrotate.d/backups