How to use rsync to delete files older than 1 week
Solution 1:
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.
Solution 2:
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.
Solution 3:
An alternative tool is rdiff-backup: http://www.nongnu.org/rdiff-backup/, which keeps both a mirror and a configurable by date, incremental backup.