I use rsync to do backups of files and folders:

 rsync -avqz --stats --delete /media/server/ /media/Server2/

This deletes files which are not present on source and makes sure that the destination is an exact copy of source

But my boss wants that I should delete files only which are not present on source and are 1 week old. Something like:

rsync -avqz --stats --delete(1 week old) /media/server/ /media/Server2/

Any ideas?


5 Answers 5


Rsync does not support this behavior out of the box. You might find another tool such as rsnapshot such as Jaylen suggested to be a good move.

If you really do want to do exactly as you describe, here is what you can do.

  • First, run your rsync WITHOUT a delete. This will just copy everything over from your live data to your backup.
  • Next run the rsync again with the delete, but this time in TEST mode (-n). This will generate a list of files to BE deleted based on what has been deleted in your live data but not yet in your backups. Save this output to a file with the date.
  • Now keep those list of things to delete, but only process them a week later. Keep a queue and only process the deletion suggestions that have been in the queue for at least a week.

Alternatively, use the output of the rsync --delete -n but instead of queuing, use some other check such as the last modified date in the backup to make sure you only delete things that have not otherwise been edited inside of a week. This is not quite the behavior you asked for but would be easier to program and might suffice.


i would recommend to you and your boss to set up rsnapshot instead. it will give you snapshots as often and going back as far as you like.

  • +1 - I use a similar method (using rsync with a couple of scripts of my own based on the technique described in mikerubel.org/computers/rsync_snapshots) to maintain my backups and it works well. I keep weekly snapshots going back almost a year (and daily ones for a month) which take up surprisingly little space. I'd probably use rsnapshot but my scripts were started, some years ago, before that existed in a stable form. Oct 19, 2010 at 10:47

An alternative tool is rdiff-backup: http://www.nongnu.org/rdiff-backup/, which keeps both a mirror and a configurable by date, incremental backup.


rsync will do this with a little tweak...

on the machine you aim to backup run the command:

rsync -abvh --backup-dir=../oldcopysfilesdir/`date +%Y-%m-%d_%H.%M` --delete /path/to/source/ pi@

this will backup files and folders and create a folder "oldcopysfilesdir" on the target machine whith all old copys of changed files and folders, all in specific time stamped folders.

to delete old copys in the folder "oldcopysfilesdir" on the target machine run:

find /path/to/oldcopysfilesdir/* -mtime +7 -delete

this command will delete old files and folders older then 7 days, but only old copys of changed files. In "/path/to/target" there always will be a up to date mirror of the "/path/to/source/" directory. To autamate, make anacron or cron jobs for the commands.


Simply You can write a small bash script to execute rsync and delete 7 days old files .then you can run that script using crontab.


    rsync -avqz --stats --delete /media/server/ /media/Server2/
    find /media/Server2/* -mtime +7 -exec rm {} \;
#-------------------End of the script-----------------------
  • 3
    I think the aim was to keep files that have been deleted for a week, as a backup where deleted files are removed immediately isn't that much use. This seems to still remove any deleted files immediately, and then go and delete anything that hasn't been used for 7 days on top which is even worse.
    – USD Matt
    Dec 7, 2017 at 9:01
  • This script is hella dangerous. If rsync fails in your example, it'll delete things anyway.
    – user884475
    Oct 21, 2021 at 19:56

Your Answer

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