How to delete files older than X on backup during sync?

Matt McCutchen matt at
Sun Nov 9 19:06:53 GMT 2008

On Sun, 2008-11-09 at 19:03 +0100, Thomas von Eyben wrote:
> On Sun, Nov 9, 2008 at 4:34 PM, Matt McCutchen <matt at> wrote:
> > I think it would make more sense to count from the time the backup file
> > is saved.  To do that, use a separate --backup-dir each day (e.g.,
> > --backup-dir=../backups/$(date +%Y-%m-%d) ) and simply delete each
> > backup dir as a whole when it becomes two weeks old.
> >
> > The only disadvantage is that with multiple backup dirs, it becomes less
> > trivial to find backups of a particular path, but it would be easy to
> > write a script to help with this.
> Actually, I think I need to be a little more specific (now I think
> about it): I only want to keep the files that are deleted on the
> source for the additional period of two weeks.
> So I want to keep the back up identical to the source, just not
> deleting files in the backup that are deleted in the source (should be
> fairly easy with rsync ;-) BUT I  only want to keep these
> deleted-on-source files for an additional period of two weeks…

I understand.  However, I can't think of a better way to do what you
want than what I already suggested.  The issue is how to record when a
file was deleted from the source so that it can be deleted from the
destination two weeks later.

It would be possible to use --delete --max-delete=0, save the itemize
output to a file, and write a script to parse it two weeks later and
actually carry out the deletions.  However, it seems easier to me to
keep the destination identical to the source and use -b --backup-dir to
move the deleted files to a separate "backup dir" on the destination.
Then you just delete each backup dir two weeks after it is created.

If you want a view of the source including the recently deleted files,
you can easily generate one from the identical copy and the backup dir
series using another rsync command, something like this:

sources=(src-ident/ $(ls -rd backup-*/))
rsync -a --delete "${sources[@]/#/--link-dest=../}" \
	"${sources[@]}" src-all/

Here src-ident/ contains the identical copy, backup-YYYY-MM-DD/ is the
backup dir for that date, we assume backup dirs older than two weeks
have already been deleted, and the combined view goes to src-all/ .


More information about the rsync mailing list