rsync: move files instead of deleting them on destination

Notice: Page may contain affiliate links for which we may earn a small commission through services like Amazon Affiliates or Skimlinks.

voodooFX

Active Member
Jan 26, 2014
247
52
28
Hi,

I have a question regarding rsync.
My NAS does a daily rsync on the backup server and this way I have a mirror copy of all the NAS files; a normal backup situation.

What I would like to implement is kind of a "trash" folder where the files are moved on the backup server when they are deleted from the NAS

So instead of --delete (delete-after | delete before) I will need a "move-to-after"
This way if I delete something I need I will be able to recover it..

Any idea? :)
 

EffrafaxOfWug

Radioactive Member
Feb 12, 2015
1,394
511
113
If filesystem snapshots aren't an option (as they're probably the most efficient method) then personally I'd advocate you switch to using rsync's --link-dest option. This'll allow you to maintain backups of directories by day using hardlinks so that you don't end up chewing up a bunch of extra space.

If you run a command line like the following on the backup server it will:
  • mirror /somepath from your host "nas" to a directory based on todays date - at the time of writing that'd be /backups/nas/2016-05-24
  • diff that directory to yesterdays backup directory /backups/nas/2016-05-23 so that only files that rsync thinks have changed between that backup dir and the current backup will get copied into /backups/nas/2016-05-24
  • create hardlinks in the /backups/nas/2016-05-24 dir to link files that haven't changed since the last backup dir on 2016-05-23 so that even though the files haven't been copied, you will still get a complete directory tree in /backups/nas/2016-05-24
Code:
rsync -aAHv backupuser@nas:/somepath /backups/nas/`date '+%Y-%m-%d'`/ --link-dest=/backups/nas/`date -d "24 hours ago" '+%Y-%m-%d'`/
Mucking around with the date command in this fashion isn't foolproof (e.g. the above command would copy everything all over again if the 2016-05-23 dir didn't exist) but you can code around that in your backup script so it works well enough for me.

If you don't want to implement it yourself then utils like rsnapshot will do the heavy lifting for you.