How can I check if rsync made any changes in bash?
Based on the comments to my original question, make rsync output to stdout with the -i flag and use a non string check condition to see if anything actually changed within the error code check. Wrapping the rsync command in a variable allows the check to be done.
RSYNC_COMMAND=$(rsync -aEim --delete /path/to/remote/ /path/to/local/)
if [ $? -eq 0 ]; then
# Success do some more work!
if [ -n "${RSYNC_COMMAND}" ]; then
# Stuff to run, because rsync has changes
else
# No changes were made by rsync
fi
else
# Something went wrong!
exit 1
fi
Potential downside, you have to lose the verbose output, but you can always log it to a file instead.
I wanted a more strict solution. I don't want to grep for Number of created files:
(the message could be in another language) or remove all lines but two in -v
output (who knows what summary rsync
will print in the next version?).
I found that you can set the format of a rsync
's log, but not the format of its stdout (see man rsyncd.conf
).
For example, add a "File changed!" to each line with an actually changed file, and then grep
for it:
rsync -a \
--log-file=/tmp/rsync.log \
--log-file-format="File changed! %f %i" \
source-dir target-dir
if fgrep "File changed!" /tmp/rsync.log > /dev/null; then
echo "rsync did something!"
fi