16

I have two servers that have should have the same setup except for known differences.

By running:

find / \( -path /proc -o -path /sys -o -path /dev \) -prune -o -print | sort > allfiles.txt

I can find a list of all the files on one server and compare it against the list of files on the the other server. This will show me the differences in the names of the files that reside on the servers.

What I really want to do is run a checksum on all the files on both of the servers and compare them to also find where the contents are different. e.g

find / \( -path /proc -o -path /sys -o -path /dev \) -prune -o -print | xargs /usr/bin/sha1sum

Is this a sensible way to do this? I was thinking that rysnc already has most of this functionality but can it be used to provide the list of differences?

Stuart Woodward
  • 1,343
  • 4
  • 14
  • 29
  • 3
    Next time you should use `-print0` instead of `-print` and `xargs -0` instead of `xargs`. You'll avoid any issues caused by "odd" file names. – Cristian Ciupitu Jun 01 '09 at 15:37

3 Answers3

23

You're right, rsync is perfect for this. Use --itemize-changes (aka -i). Make sure you can run this as root on both sides (or some other user with full access to the machine):

rsync -ani --delete / root@remotehost:/
  • -a is for archive, and basically makes rsync make an exact duplicate (apart from some cases involving links)
  • -n is for dry-run, and means nothing will actually be changed (This one is IMPORTANT! :))
  • -i is for itemize-changes, and outputs a simple-to-understand-once-you-get-it format showing every file that needs to be updated (the syntax is explained fully in the man page under the detailed help for that trigger).
  • --delete makes rsync delete files that exist on the destination but not the source.

If you want to exclude certain paths, use commands like --exclude /var. The exclude patterns are relative to the source directory (which in this case is /, so they are effectively absolute).

Alex J
  • 2,844
  • 2
  • 23
  • 24
  • If you exclude /var, you will also exclude where crontabs are stored, which probably *DO* need to be checked. However, you do want to exclude things like /var/log and /var/cache – Sean Reifschneider Nov 26 '11 at 06:44
7

You may want to investigate rsync's -c flag. From man rsync:

    -c, --checksum              skip based on checksum, not mod-time & size

I'd leave a comment to Alex Jurkiewicz's answer, but I don't have enough rep :'( yet...

Mike Mazur
  • 6,133
  • 2
  • 20
  • 13
  • 1
    Some scenarios exist which are where this flag is useful. The most common I've seen is when you have data stored in some sort of 'weird' (usually binary) format which is a constant size. If the file is updated by an automated process on both sides of the sync at exactly the same time (say log rotation or similar) rsync can miss it when syncing on just size/m-time. Definitely possible, but rather rare. – Alex J Jun 01 '09 at 10:13
  • I would have expected it to just checksum the *content* of the files, so that you could compare without paying attention to mod-time and so forth, but it appears that Alex is right... is there a good way to ignore all meta-data and only pay attention to the content, the way git does? – iconoclast Aug 24 '12 at 21:54
4

One useful tool you should consider is rpm -Va. This will print out a list of all packaged files that differ from when they were packaged. This ignores any non-packaged files, but it's a very good way to get an idea of files that were changed since the install, that are part of the base system. They also include a flag that tells you if they are considered configuration files.

For example:

S.5....T  c /root/.bashrc
S.5....T  c /etc/yum/yum-updatesd.conf
.M......    /usr/bin/rdate
..5....T  c /etc/dbbackup.conf
S.5....T  c /etc/webalizer.conf
SM5....T  c /etc/sysconfig/iptables-config

So .bashrc and yum-updatesd.conf are "configuration" files that have changed in size, time, and MD5 checksum. rdate has had it's mode change...

The RPM database is a very useful thing.

guntbert
  • 631
  • 9
  • 21
Sean Reifschneider
  • 10,720
  • 3
  • 25
  • 28