On my debian 9.4 machine I have a script that automatically compresses & backups up all my files in a srv/ with the date in the name xx-xx-xxxx.tar. It is moved to my backups/ directory.

I would like to limit the amount of .tar files (backups) to 10 in the folder, and remove the oldest .tar file every time a new tar is created.

What would be the best way of going about this? Here is my script:

#Purpose = Backup of Important Data
#Created on 17-1-2012
#Author = Hafiz Haider
#Version 1.0
TIME=`date +%b-%d-%y`            # This Command will add date in Backup File Na$
FILENAME=backup-$TIME.tar.gz    # Here i define Backup file name format.
SRCDIR=/srv/daemon-data                    # Location of Important Data Directo$
DESDIR=/backups           # Destination of backup file.

Add the following in the script towards the end:

find "$DESDIR/$FILENAME" -type f -mtime +10 -delete

This will find your backup file (tar.gz) in the directory and delete any backup that is more than 10 days older.

| improve this answer | |
  • 1
    This is good when the script is run daily, but I'd point out that this specifically removes files that are older than 10 days, and not specifically "the oldest beyond 10". – Jeff Schaller Mar 15 '19 at 18:09
  • 1
    I think you'd also want to adjust this to something like find "$DESDIR" -name "backup-*-tar.gz" ... so that you're not accidentally searching for today's backup filename, which will never be more than 10 days old. – Jeff Schaller Mar 15 '19 at 18:10

If you have zsh available, you could use it to supplement your bash script:

# ...
tar -cpzf "$DESDIR"/"$FILENAME" "$SRCDIR" 
zsh -c 'rm -- "$DESDIR"/backup-*-tar.gz(om[11,-1])'

The meaning here is to ask zsh to call rm with a list of files; that list of files is generated from the wildcard $DESDIR/backup-*-tar.gz (which I gathered from your script -- and added quoting to), followed by a zsh "glob qualifier", in parenthesis. That glob qualifier says to "order" (sort) the matching files by modification time (newest first); that list is then sliced to select only the 11th and onwards files -- which are then removed, leaving the 10 newest files. The -1 syntax means "count backwards from the end of the list" and effectively refers to the last element of the list.

| improve this answer | |
  • Absolutely beautiful. Kudos for the excellent explanation. – AndreasT Nov 8 '19 at 9:32

You want the same logic that is used by the system utility logrotate(8), which can be configured to keep up to a maximum number of files. In user space, there are a number of logrotate-like utilities, as described in this answer: https://superuser.com/a/868519

That being said, a simple configuration file like

"/home/coolman/backups" {
    rotate 10

should work with the command line

/usr/sbin/logrotate -s $HOME/backups.state -f $HOME/backups.config
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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