Skip to main content

Get rsync to generate a patch file instead of copying across files? [Resolved]

I'm copying lots of files that have changed from one server to another using rsync. I know I can use the -n option to do a dry run, so I can see what files have been changed. However is it possible to get rsync to print a diff of the file contents that's changed? I'd like to see what's happening before doing a copy? Something I can save to a file and the apply with diff(1) later?


Question Credit: Rory
Question Reference
Asked September 14, 2019
Tags: rsync, patch, diff
Posted Under: Network
30 views
7 Answers

rsync can't do this natively, but if there's a possibility of using unison you can produce diff style format from that.


credit: Philip Reynolds
Answered September 14, 2019

For create patch:

rsync -arv --only-write-batch=patch old/ new/

For apply it:

rsync -arv --read-diff=patch dir/

or use auto-generated script:

./patch.sh

Sources:


credit: FarK
Answered September 14, 2019

It's not possible natively because rsync only cares about binary differences between files.

You might be able to script it, using rsync's output. But it would be hackish.

I do believe it's natively possible with Unison though.


credit: Dan Carley
Answered September 14, 2019

Why not just use something like diff (for text files) or xdelta (for binary files) to generate the diffs? Why do you need to specifically get something out of rsync?


credit: womble
Answered September 14, 2019

To expand on Kyle's answer, this automates the process. Note that it is totally untested, probably pretty fragile, and may delete your computer and kill your dog.

#!/bin/bash

REMOTE=${1?Missing Remote Path}
LOCAL=${2?Missing Local Path}

# Trim trailing slash since we'll be adding it as a separator later
REMOTE=${REMOTE%/}
LOCAL=${LOCAL%/}

#Break it down
RHOST=${REMOTE%:*}
RPATH=${REMOTE#*:}

while read FILE; do
    diff -u ${LOCAL}/${FILE} <(ssh $RHOST "cat ${RPATH}/${FILE}")
done < <(rsync -vrn $REMOTE/ $LOCAL/ | sed '1d;/^$/q')

credit: tylerl
Answered September 14, 2019

The rsync algorithm works by comparing binary chunks of the file. Such binary diff is not meant to be printable. There is a command called rdiff that uses the rsync algorithm to generate a binary diff, but I don't think it'd be useful for what you describe, it is commonly used to implement incremental backups.


credit: sagi
Answered September 14, 2019
Your Answer