0

我的文件夹中有 100 个文件,文件夹中有 100/data01/primary个不同的文件。所有这 200 个文件都来自,如果文件不存在,那么它肯定存在。/data02/secondarymachineXmachineAmachineBmachineAmachineB

因此,我们将文件从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

因此,/data01/primary 文件夹中将有 100 个文件,而 machineX 中的 /data02/secondary 文件夹中将有 100 个不同的文件,这些文件来自 machineA 和 machineB。

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

#!/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 一切,我可以从我的源服务器作为abc用户在这些目标服务器上执行 ssh。

ssh abc@${DESTINATION_SERVER[0]}
4

1 回答 1

1

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

$ ssh user@hostname "/usr/bin/md5sum filename"
a40bd6fe1ae2c03addba2473e0bdc63b  filename

如果您想自动执行任务,请将其分配给这样的变量。

remote_md5sum=`ssh user@hostname  "/usr/bin/md5sum filename"`

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

顺便说一句,我在这种情况下使用私钥身份验证,所以我不需要密码。#!/bin/bash

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 user@$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
于 2014-09-22T19:16:51.670 回答