-
Notifications
You must be signed in to change notification settings - Fork 453
Description
In backup tasks this is very important to keep N files in destination when using --delete* options to rsync.
Usually we backup some periodic files in local folder SRC/ to backup_server:/DST/
Usually we have limited space in backup_server, so we need to remove old files there (often without possibility to use bash scripts on destination).
One of strategy to achieve this - use --delete or --delete-after as option to rsync call. So we need to keep N files in source directory, removing old ones, then call rsync -av --delete-after SRC/ DST/ - thus we got fresh list of backupped files without very old ones.
But some time very bad conditions occur that make possible to emptify source folders and then we got big problems.
Imagine, we make backups from locally mounted LVM-partition to remote server. One day we loose LVM, then cron job starts, then it sees empty src-folder for rsync - and in seconds we loose backup copies at remote server thanks to --delete-after option... When source server crashes, we loose ALL files, origin and backupped.
My proposition is to implement a kind of --retain-last N option to rsync in addition to --delete* options.
With this option (and one of --delete-* option also), on remote side rsync should filter out files that should not be deleted due to retain policy "destination should keep last N files".
As with bash script, we need to implement ls -1t | tail -n+$((N+1)) | xargs -r rm
- get list of files in DST/, sorted by its time of (modification|creation), newer are first|top
- cut off newer files, tailing list from N+1 line (if any)
- remove only that old files.
Thus we'll never remove N files at destination backup folder, keeping them in bad cases.