For some older projects we’re still deploying code with rsync, its not perfect but it works. Temporary files are excluded using --exclude-from=exclude.txt
, this is great until the parent folder of an excluded file needs to be deleted.
For example, say your file structure looks like this:
And you remove the data/cache directory completely:
$ rm -rf data/cache
$ rsync --exclude="*.tmp" --exclude="data/logs/*.log"
You’re going to get the error “cannot delete non-empty directory: data/cache” because data/cache/config.tmp has not been removed by rsync.
Unfortunately --delete-excluded
won’t work because you don’t want to delete the log files and other .tmp files created on the destination.
Fortunately rsync has filters (which include/exclude are shorthand for anyway), with a mode called “perishable”. Perishable excludes behave exactly as required, files are excluded from sync, and aren’t deleted on the destination unless they are in a directory that no longer exists.
The syntax for excluding files becomes:
$ rsync --filter="-,p *.tmp" --filter="-,p data/logs/"
The “-” signifies the filter is to exclude matches. The “p” makes the exclude perishable.
If, like us, you were using --exclude-from
for the patterns, you can with this syntax:
$ rsync --filter="merge,p- /home/ideal/scripts/push/excludes/lenta.txt"
This information is all available in the rsync docs under “FILTER RULES”, it just took me a while to figure out the right syntax. Hopefully these snippets help someone out.
Warning: Declaration of Social_Walker_Comment::start_lvl(&$output, $depth, $args) should be compatible with Walker_Comment::start_lvl(&$output, $depth = 0, $args = Array) in /home/customer/www/arronwoods.com/public_html/blog/wp-content/plugins/social/lib/social/walker/comment.php on line 18
Warning: Declaration of Social_Walker_Comment::end_lvl(&$output, $depth, $args) should be compatible with Walker_Comment::end_lvl(&$output, $depth = 0, $args = Array) in /home/customer/www/arronwoods.com/public_html/blog/wp-content/plugins/social/lib/social/walker/comment.php on line 42
thanks, that solved a few issuese for me that I have been struggling with. Rsync is such a powerfull tool on linux
Thank you Arron, exactly what I needed. You sure can read a man page.
I wanted to sync a WordPress staging directory to the production directory, deleting obsolete plugins and themes directories, but without overwriting .htaccess and wp-config.php and without deleting the production directory’s caches and uploads etc.
Your solution works perfectly.
thanks a lot !!!
Thank you. “–delete-excluded” is also useful.
Perfect to-the-point article solving my exact problem !
Thanks! I didn’t want “–delete-excluded” and this solved my problem. In an “.rsync-filter” file (used by the -F option), I had to remove the comma in “-,p xyz”. So my .rsync-filter file now has lines like “-p .DS_Store”.
I have checked your site and i’ve found some duplicate content,
that’s why you don’t rank high in google’s search results,
but there is a tool that can help you to create 100% unique content, search for:
Boorfe’s tips unlimited content