| 1 | #!/bin/bash |
| 2 | |
| 3 | verbose=0 |
| 4 | |
| 5 | if [ "$1" = "-v" ]; then |
| 6 | verbose=1 |
| 7 | shift |
| 8 | fi |
| 9 | |
| 10 | if [ $# != 3 ]; then |
| 11 | echo "Usage: $0 [-v] host1[@host1] host2[@host2] basedir" >&2 |
| 12 | exit 1 |
| 13 | fi |
| 14 | |
| 15 | left1="${1%@*}" |
| 16 | left2="${1#*@}" |
| 17 | |
| 18 | right1="${2%@*}" |
| 19 | right2="${2#*@}" |
| 20 | |
| 21 | basedir="$3" |
| 22 | |
| 23 | left_cmd="ssh $left1 'csync2 -or $basedir -P $right2 | sort | xargs md5sum'" |
| 24 | right_cmd="ssh $right1 'csync2 -or $basedir -P $left2 | sort | xargs md5sum'" |
| 25 | |
| 26 | if [ $verbose -eq 1 ]; then |
| 27 | echo |
| 28 | echo "L: $left_cmd" |
| 29 | echo "R: $right_cmd" |
| 30 | echo |
| 31 | fi |
| 32 | |
| 33 | my_md5sum='perl -w -e '\'' |
| 34 | use strict; |
| 35 | use Digest::MD5; |
| 36 | |
| 37 | foreach my $f (@ARGV) { |
| 38 | if (-l $f) { |
| 39 | print "LINK:", Digest::MD5->new->add(readlink($f))->hexdigest, " $f\n"; |
| 40 | next; |
| 41 | } |
| 42 | if (-f $f) { |
| 43 | open(FILE, $f) or die "Can not open >>$f<<: $!"; |
| 44 | binmode(FILE); |
| 45 | print "DATA:", Digest::MD5->new->addfile(*FILE)->hexdigest, " $f\n"; |
| 46 | close(FILE); |
| 47 | next; |
| 48 | } |
| 49 | print "SPECIALFILE:0 $f\n"; |
| 50 | } |
| 51 | '\' |
| 52 | |
| 53 | tic="'" |
| 54 | my_md5sum="${my_md5sum//$tic/$tic\\$tic$tic}" |
| 55 | |
| 56 | left_cmd="${left_cmd/md5sum/$my_md5sum}" |
| 57 | right_cmd="${right_cmd/md5sum/$my_md5sum}" |
| 58 | |
| 59 | diff -u <( eval "$left_cmd" ) <( eval "$right_cmd" ) | awk ' |
| 60 | |
| 61 | function isort(A, n, |
| 62 | i, j, hold) |
| 63 | { |
| 64 | for (i=1; i<n; i++) |
| 65 | { |
| 66 | hold = A[j = i]; |
| 67 | while (A[j-1] > hold) |
| 68 | { j--; A[j+1] = A[j]; } |
| 69 | A[j] = hold; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /^-[a-zA-Z0-9]/ { gotsomething=1; if ('$verbose') print; sub(/^./, ""); all[$2] = 1; left[$2] = $1; } |
| 74 | /^\+[a-zA-Z0-9]/ { gotsomething=1; if ('$verbose') print; sub(/^./, ""); all[$2] = 1; right[$2] = $1; } |
| 75 | |
| 76 | END { |
| 77 | outcount = 0; |
| 78 | |
| 79 | for (filename in all) { |
| 80 | outlines[filename] = sprintf("%s %s %s", |
| 81 | (left[filename] == "" ? "-" : "X"), |
| 82 | (right[filename] == "" ? "-" : "X"), |
| 83 | filename); |
| 84 | sortindex[outcount] = filename; |
| 85 | outcount++; |
| 86 | } |
| 87 | |
| 88 | if ('$verbose' && gotsomething) |
| 89 | printf "\n"; |
| 90 | |
| 91 | isort(sortindex, outcount); |
| 92 | for (i=0; i<outcount; i++) |
| 93 | print outlines[sortindex[i]]; |
| 94 | |
| 95 | if ('$verbose') |
| 96 | printf "Found %d differences.\n", outcount; |
| 97 | } |
| 98 | ' |
| 99 | |
| 100 | if [ $verbose -eq 1 ]; then |
| 101 | echo |
| 102 | echo "X - ... Found this file on left host ($1) only." |
| 103 | echo "- X ... Found this file on right host ($2) only." |
| 104 | echo "X X ... Found file on both hosts but content is different." |
| 105 | echo |
| 106 | fi |
| 107 |