Compare checksum of files between two servers and report mismatch Compare checksum of files between two servers and report mismatch linux linux

Compare checksum of files between two servers and report mismatch


If your primary goal is not to count the checksums but list differences, perhaps faster (and easier) way would be to run rsync with --dry-run option. If any files listed, they differs, for example:

MBP:~ jhartman$ rsync -avr --dry-run rsync-test 192.168.1.100:/tmp/; echo $?building file list ... donersync-test/file1.txtsent 172 bytes  received 26 bytes  396.00 bytes/sectotal size is 90  speedup is 0.45

Of course, because of --dry-run no files changed on the target.

I hope it will help,Jarek


If the files are in the directory /primary and /secondary instead of under these directories, lose the find.You may also wish to parallelize the md5-calculation. So that would make it:

#!/bin/bashcd /primarymd5sum * > /tmp/file-p &cd /secondarymd5sum * > /tmp/file-s &waitcat  /tmp/file-p /tmp/file-s | ssh machineB '(cd /bat/snap/ && md5sum -c)'

With a relatively small set of files:

$ time find . -exec md5sum {} \;7e74a9f865a91c5b56b5cab9709f1f36  ./file631f01c98ff2016971fb1ea22be3c2cf  ./hostsd41d8cd98f00b204e9800998ecf8427e  ./fortune854749d05af711e2d473f12375d720fb0a92  ./vboxdrv-Module.symversbf4b1d740f7151dea0f42f5e9e2b0c34  ./tmpavG1pBa9b0d3af1b80a46b92dfe1ce56b2e85c  ./in.clean.4524real    0m0.046suser    0m0.035ssys 0m0.006s$ time md5sum *7e74a9f865a91c5b56b5cab9709f1f36  filed41d8cd98f00b204e9800998ecf8427e  fortune8547631f01c98ff2016971fb1ea22be3c2cf  hostsa9b0d3af1b80a46b92dfe1ce56b2e85c  in.clean.4524bf4b1d740f7151dea0f42f5e9e2b0c34  tmpavG1pB49d05af711e2d473f12375d720fb0a92  vboxdrv-Module.symversreal    0m0.005suser    0m0.003ssys 0m0.002s

(just to prove that find is not always the quickest).


Using md5sum you can ask it to check files against an input md5sum file.

man md5sum: the following two options are useful:

  • -c, --check: read MD5 sums from the FILEs and check them
  • --quiet : don't print OK for each successfully verified file

So all we need to do is build such a file and pass it on. The easiest is the following (from machineA) :

$ cd /primary; md5sum * | ssh machineB '(cd /bat/snap; md5sum -c - --quiet 2>/dev/null)`$ cd /secondary; md5sum * | ssh machineB '(cd /bat/snap; md5sum -c - --quiet 2>/dev/null)`

This will report things as :

file1: FAILEDfile2: FAILED open or read

This will give you all the failed files per directory. You can do any post processing later on with any flavour of awk.