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.

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

rsync -avh source/ dest/ --delete
  • 1
    Perhaps using -aqr instead would also be an idea :) – TheBicentennialMan Apr 28 '16 at 9:05
  • 5
    Make sure the source is a directory. Using source/* dest/ won't work. – Tom Saleeba Sep 27 '16 at 1:13
  • 2
    I found that event with --delete or --delete-after it won't delete because of some errors: "IO error encountered -- skipping file deletion". To resolve this add --ignore-errors option and debug the errors separately – MHT Dec 18 '16 at 13:25
  • 7
    Works perfectly! Even though it is working, I would recommend to ALWAYS use -n, --dry-run option, before running rsync, specially when it comes to options like these (--delete). It will avoid any possible headache :). – ivanleoncz May 31 '17 at 16:36
  • @MHT : No answer seems to be complete without your comment. – Lonnie Best Aug 31 at 21:12

The rsync command wont delete any file while you use some of its options delete in that command. So if any file or folder added in source, it'll be synced to target without any deletion.

I suggest you to use rsync for make backup from source files and use find ... rm for deletion files for period of time or size of files:

rsync [options] SOURCE TARGET
find TARGET -maxdepth 1 -type f -mtime +60 -exec rm -f {} \;

The above code block, make a backup from source and then delete every files which last modified time are more than 2 month.

UPDATE

As I find that the delete options are just for TARGET that if some files are removed from source, rsync --delete remove them from TARGET. And the delete option by after and before, as mentioned in its man page:

--delete-before         receiver deletes before transfer, not during

Means that:

  1. rsync delete the file from TARGET which are removed from SOURCE.
  2. rsync start syncing files.

--delete-after receiver deletes after transfer, not during

Means that:

  1. rsync start syncing files.
  2. rsync delete the file from TARGET which are removed from SOURCE after syncing.

NOTE: the --delete-{before/after} implement just in TARGET.

  • 1
    Thanks for the reply!! Are you referring to a "delete" option for rsync? Why can't I just use the "delete" option for rsync? – user254251 Jun 3 '14 at 6:03
  • @user254251, If you use delete, the rsync command immediately delete files. But in this case there is more time for any errors. – shgnInc Jun 3 '14 at 6:44
  • If I understand correctly, you're saying I should separate the deletion task from the rsync task to avoid errors. I have a question, though. The manual page for rsync lists some options that appear to serve the purpose of separating the deletion task by running it before or after the copying task. For example, I see two options called --delete-before and --delete-after. Would rsync with these options have the same effect as the method you described? I read the full description of each option on the manual page but there is some information in the descriptions that I don't understand. – user254251 Jun 3 '14 at 8:02
  • @user254251, Answer was updated. I don't know if there is a way to delete files from TARGET by time limitation in rsync. – shgnInc Jun 3 '14 at 9:48
  • Thanks! So do you think I am safe from errors if I simply use the --delete-before option? I am not specifying a timeout so I shouldn't have to worry if the rsync deletion stage delays the transfer stage. rsync runs once per day and that is plenty of time to sync the backup files before the next sync, so I shouldn't need to time-limit the sync either. – user254251 Jun 4 '14 at 5:13

This command will copy increment data and keep it in sync with remote server.

  1. It will copy only incremental data.
  2. It will delete if any data deleted from source.
  3. It will copy again from source if any data deleted at destination.
  4. basically this command will keep the both environment in sync.

rsync -avWe ssh --delete-before (source) root@localhost:(destination) rsync -avW --delete-before -e ssh (source) root@localhost:(destination)

Example:

rsync -avWe ssh --delete-before /data root@192.168.254.254:/backup
rsync -avW --delete-before -e ssh /data root@192.168.254.254:/backup
  • 1
    I think -W makes it not to copy only incremental data but to always copy whole files. – Tulains Córdova Nov 25 '15 at 19:27
  • I got this error by executing this command: rsync: Failed to exec --delete-before: No such file or directory (2) – Jeff Tian Dec 10 '15 at 2:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.