2014-09-22 86 views
0

我有/data01/primary文件夹中的100个文件和machineX中的/data02/secondary文件夹中的100个不同文件。所有这200个文件来自machineAmachineB,如果文件不在machineA中,那么肯定应该在machineB中。如何将源服务器中的文件的md5checksum与shell脚本中的目标服务器进行比较?

因此,我们正在将文件machineAmachineB(它们是源服务器)复制到machineX(它是目标服务器)。我们从machineA和machineB复制的文件位于这个目录中/checkbat/data/snapshot/20140918,所以我们在这两个源服务器中都有这个目录。

现在我试图通过与machineA和machineB进行比较来对machineX中的200个文件执行md5校验和。

文件路径是这样的,因为您可以看到除了1,2,3,4号码之外,所有内容都是相同的。

t1_monthly_1980_1_200003_5.data 
t1_monthly_1980_2_200003_5.data 
t1_monthly_1980_3_200003_5.data 
t1_monthly_1980_4_200003_5.data 

所以会有100个文件像上面/ DATA01 /主文件夹,并在machineX/DATA02 /次级文件夹中,其来自machineA的和到machineB 100个不同的文件。

现在我需要做的是将/data01/primary文件夹中的100个文件的md5checksum与machineAmachineB中的文件进行比较。如果源服务器中的任何文件校验和与目标服务器的校验和不同,请在源服务器和目标服务器上打印文件名及其校验和。

#!/bin/bash 

export PRIMARY=/data01/primary 
export SECONDARY=/data02/secondary 

readonly DESTINATION_SERVER=(machineA machineB) 
export DESTINATION_SERVER_1=${DESTINATION_SERVER[0]} 
export DESTINATION_SERVER_2=${DESTINATION_SERVER[1]} 

export FILES_LOCATION_ON_DESTINATION=/checkbat/data/snapshot/20140918 

readonly SOURCE_SERVER=machineX 

export dir3=$FILES_LOCATION_ON_DESTINATION 

# compare the checksum and find the files whose checksum are different 

for entry in "$PRIMARY"/* 
do 
    echo "$entry" 
    # now how to compare the file checksum of this file with same file in machineA or machineB 
done 

我知道该怎么做了md5checksum上的一个文件,但不知道如何将文件校验通过网络比较?这可能吗?

md5sum filename 

我安装我的SSH的一切,我可以从我的源服务器做的那些目标服务器上的SSH为abc用户。

ssh [email protected]${DESTINATION_SERVER[0]} 

回答

1

我会用ssh来执行这个任务。

$ ssh [email protected] "/usr/bin/md5sum filename" 
a40bd6fe1ae2c03addba2473e0bdc63b filename 

如果你想自动化任务,然后将它分配给这样的变量。

remote_md5sum=`ssh [email protected] "/usr/bin/md5sum filename"` 

然后,您可以使用$ remote_md5sum中的值来验证它是否有效。

顺便说一下,我在这种情况下使用私钥认证,这使得我不需要密码。 #!/斌/庆典

export PRIMARY=/data01/primary 
export SECONDARY=/data02/secondary 

readonly DESTINATION_SERVERS=(machineA machineB) 
export DESTINATION_SERVER_1=${DESTINATION_SERVERS[0]} 
export DESTINATION_SERVER_2=${DESTINATION_SERVERS[1]} 

export FILES_LOCATION_ON_DESTINATION=/checkbat/data/snapshot/20140918 

readonly SOURCE_SERVER=machineX 

export dir3=$FILES_LOCATION_ON_DESTINATION 

# compare the checksum and find the files whose checksum are different 

for entry in "$PRIMARY"/* 
do 
    local_md5sum=`/usr/bin/md5sum "$entry" | awk '{print $1}'` 

     for DESTINATION_SERVER in $DESTINATION_SERVERS 
     do 
       remote_md5sum=`ssh [email protected]$DESTINATION_SERVER /usr/bin/md5sum "$entry" | awk '{print $1}'` 

       # now how to compare the file checksum of this file with same file in machineA or machineB 
       if [ "$local_md5sum" -eq "$remote_md5sum" ] 
       then 
         echo "match"; 
       else 
         echo "not match" 
       fi 
     done 
done 
+0

我已经安装SSH一切为用户'abc'但不知道我该怎么做在一个shell脚本的一切。 – john 2014-09-22 19:24:55

+0

谢谢你的例子。我正在尝试你的建议,并添加了'echo local_md5sum'并运行它,但它没有在控制台上打印出任何东西。你知道可能是什么原因吗? – john 2014-09-22 19:49:26

+0

尝试执行/ usr/bin/md5sum文件名| awk'{print $ 1}',如果这不起作用,请确认/ usr/bin/md5sum存在。或者可能是我刚刚更新的脚本中的拼写错误。 DESTINATION_SERVER vs destination_server – jbrahy 2014-09-22 20:43:18