Linux – rsync cannot delete non-empty directory

linuxrsync

When syncing my remote backup I get the error rsync cannot delete non-empty directory.

I know the cause: I have excluded some files and when rsync tries to delete a remote directory because it doesn't exists locally, it can't since it is not empty. Without the exclusion the directory would be emptied.

I also know I could get rid of the problem by using –delete-excluded, but that is not want I want.

I would like to skip uploading certain files (because they are too big for my upload speed), but I want to keep those I have already uploaded some time ago if they exist locally. If they do not exist locally, they should be deleted. –delete-excluded would delete them all on the remote side.

That also means that if a directory doesn't exists anymore locally, it should be deleted even if it contains excluded files.

Is it possible to setup rsync to accomplish this?

Best Answer

Use the p modifier:

A p indicates that a rule is perishable, meaning that it is ignored in directories that are being deleted. For instance, the -C option’s default rules that exclude things like "CVS" and "*.o" are marked as perishable, and will not prevent a directory that was removed on the source from being deleted on the destination.

For example, I use the -F option, which "tells rsync to look for per-directory .rsync-filter files that have been sprinkled through the hierarchy and use their rules to filter the files in the transfer."

My .rsync-filter contains lines like these to filter out these "droppings" that Macs leave behind them:

-p .DS_Store
-p ._*

If using exclude instead of just -, add a comma. For example :

exclude,p .DS_Store
exclude,p Thumbs.db
Related Question