I have to backup a set of userdata with rsync. There is just one little thing I cant figure out how to do. When a user deletes a file it is kept on the server. This is fine since it might have been deleted by mistake, but it would be nice if it did not stay there forever.

So how do I make rsync delete files on DEST when they have not been precent on SRC for a certain amount of time (say, 7 days)

I know that I could just write a script that manually uses rsync on each file in the folder and then determines if the file on DEST should be deleted, but it would be kind of dumb to do this just to learn later on that rsync has this feature already. I cant find it though.

Many thanks for you time

5 Answers 5


Rsync won't do this on its own. Something like rsnapshot will let you maintain multiple generations of rsync-based backups (hourly, daily, monthly, etc) such that you can retrieve an accidently-deleted file. By using hard links, rsnapshot lets you keep a number of snapshots laying around without using a lot more space than the "cost" of a full backup.

There are other solutions out there that do the same thing-- rsnapshot just happens to be one I have personal experience with. (I highly recommend using the "sync_first" option if you do decide to use it...)

  • Many thanks, will look into it, seems to be just the thing I need. Dec 5, 2009 at 1:43
  • Sadly, rsnapshot is no longer maintained.
    – Gary
    Aug 9, 2017 at 0:25

The --backup --backup-dir= flags might be what you are looking for, if you specify a relative path for the backup-dir a directory will be created inside your destination directory and any file deleted will be stored with it's original pathing inside that directory. If you want to use an absolute path that of course will also work.

Purging old deleted files is another problem, that my usage of --times makes more complicated , as the timestamp on the deleted files will reflect the last modified time on the file and not the time of deletion. One option I am considering is to build the backup-dir value using some sort of date string and use that to parse and purge over time. e.g. --backup-dir=/DELETED_FILES/$(date "+%Y_%m_%d-%H_%M_%S")

Play with it on some sample files, the simplist version of this would be:

rsync --delete --backup --backup-dir=DELETED_FILES/ --recursive --times src/ dest/

or a date stamped version to play with would be:

rsync --delete --backup --backup-dir=DELETED_FILES/$(date "+%Y_%m_%d-%H_%M_%S") --recursive --times src/ dest/

I would place the DELETED_FILES directory on an absolute path outside of the destination directory, you might want to do otherwise.

  • 1
    Amazing, I just started writing my own backup dir logic and came across this. There's already built-in functionality to do this! Feb 26, 2020 at 2:39

Once a week, I use the --delete option of rsync

RSYNC_OPT="--stats --delete --delete-excluded -avvzpogte  ssh --exclude-from /root/etc/exclude.txt" 

Rest of the time, I do incremental backup

RSYNC_OPT="--stats -avvzpogte ssh --exclude-from /root/etc/exclude.txt" 

Both scripts are called by cron

  • I actually considered this, but just wanted to know if there was a more "elegant" solution. But thanks none the less :) Dec 5, 2009 at 1:42

Have you done a "man rsync"? The --delete switch does what you want, but use it carefully.

If you really want to have that seven-day delay, consider using rsnapshot. It uses hard-links to conserve space for all identical files.


Look into rsnapshot. It uses rsync to do the transfer but on top of that it manages point in time snapshots using hardlinks. It will let you determine how often and how many snapshots to hold onto as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .