1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash

echo "######################################"
echo "检测多台服务器指定目录下的文件一致性"
echo "#####################################"

#通过对比两台服务器上文件的md5值,达到检测一致性的目的

if [ $# -lt 1 ]
then
echo "请输入需要比较的文件目录或者文件"
exit
fi

dir=$1

# 清空之前的文件夹
n_match=/tmp/xmatch/
rm -rf /tmp/xmatch/*
mkdir -p $n_match

# 遍历集群所有机器
hosts=(hadoop001 hadoop002 hadoop003 hadoop004)
for host in ${hosts[*]}
do
echo ===================== $host ===================
tmp_f="/tmp/md5_${host}.txt"
ssh $host "if [ -f $tmp_f ]; then rm -f $tmp_f > /dev/null; fi"

# 遍历所有目录,计算文件MD5
for file in $@
do
# 判断文件是否存在
if [ -e $file ];
then
# 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
# 获取当前文件名
fname=$(basename $file)
full_file=$pdir/$fname

ssh $host "if [ -f $full_file ];
then
echo $(md5sum $full_file) >> $tmp_f;
else
find $pdir -type f|xargs md5sum >> $tmp_f;
fi"
else
echo ${file}文件不存在!
fi
done
# 将生成的文件复制到当前服务器
scp $host:$tmp_f $n_match
chmod 777 ${n_match}md5_${host}.txt
done

for((i=0;i<${#hosts[*]}-1;i++))
do
for((j=1+$i;j<${#hosts[@]};j++))
do
echo ====================== 比较 ${hosts[i]} 和 ${hosts[j]} 的文件差异 =====================
#diff ${n_match}md5_${hosts[i]}.txt ${n_match}md5_${hosts[j]}.txt -y -W 50
for f in `awk '{print $2}' ${n_match}md5_${hosts[i]}.txt`
do
if grep -qw "$f" ${n_match}md5_${hosts[j]}.txt;
then
md5_a=`grep -w "$f" ${n_match}md5_${hosts[i]}.txt | awk '{print $1}'`
md5_b=`grep -w "$f" ${n_match}md5_${hosts[j]}.txt | awk '{print $1}'`
if [[ "$md5_a" != "$md5_b" ]];
then
echo "${f} is changed!"
fi
else
echo "${f} is deleted."
fi
done
done
done