Ubuntu – How to make rsync delete files that have been deleted from the source folder

backupdeletersyncsync

I recently set up a machine with Ubuntu Server to host game servers. I installed a backup plugin for each game server that creates frequent backups of game world files in a particular folder on the machine. I also established a cron task to automatically copy those backups to my Dropbox folder every night using rsync with the -a option.

After a few months my Dropbox account reached its storage limit and I realized I would not be able to keep so many backups, so I configured the game server backup plugin to not retain so many backups, then waited a few days to see if it would delete the older backups as it is scheduled to do on a weekly basis. The backup plugin eventually did its job and deleted the older backups, so I was expecting the rsync cron task to subsequently delete the older backups from my Dropbox folder to match the source folder, but it has not done so. So I have a couple of questions:

  • By default, does rsync only add files to the destination folder
    that have been added to the source folder and change files that
    have been changed in the source folder but NOT delete files
    that were deleted from the source folder?

  • If that is the case, what is the best way to make rsync do this? I
    want the destination folder to perfectly reflect the source folder,
    and that means deleting any files that have been deleted from the source
    folder.

I see some options listed in the manual page for rsync that might do the trick, but since I'm not familiar with.

Best Answer

To delete files in the target, add the --delete option to your command. For example:

rsync -avh source/ dest/ --delete
Related Question