将文件从两台机器移动到shell脚本中的四台机器中

时间:2013-12-16 23:27:42

标签: linux bash shell unix scp

我正在开展一个项目,我需要将文件从一台机器移动到另一台机器。所有机器都是Ubuntu机器,它运行的是Ubuntu 12.04。所以我想,我可以直接scp文件。

现在,下面是我的文本文件,它将由另一个程序生成,其中包含有关哪台机器具有哪些文件的详细信息。

{ dbx45.dc1.host.com=[0, 1024, 4, 1028],
  dbx46.dc1.host.com=[1, 1025, 5, 1029],
  dbx47.dc1.host.com=[2, 1026, 6, 1030],
  dbx48.dc1.host.com=[3, 1027, 7, 1031] }

在上面的文本文件中,我有一组key = value对组合,其中每个机器负责key is the client machine namevalue is the name of files。例如,在上面的文件中,

dbx45.dc1.host.com is the client machine responsible for 0 file name, 1024 file name, 4 file name, 1028 file name.
dbx46.dc1.host.com is the client machine responsible for 1 file name, 1025 file name, 5 file name, 1029 file name.
dbx47.dc1.host.com is the client machine responsible for 2 file name, 1026 file name, 6 file name, 1030 file name.
dbx48.dc1.host.com is the client machine responsible for 3 file name, 1027 file name, 7 file name, 1031 file name.

现在所有文件都只驻留在这两台机器上,如下所述。这意味着,文件随机分布在这两台机器中,因此不像只有一台机器包含所有文件。

slc4b03.dc1.host.com
chd1b02.dc2.host.com

上述两台机器中的所有文件都位于此文件夹中 -

/bat/data/snapshot/

,文件名就像这样 -

`t1_weekly_0_200003_5.data` (this is for file 0)
`t1_weekly_1024_200003_5.data` (this is for file number 1024)
`t1_weekly_4_200003_5.data` (this is for file number 4)

and similarly for others as well.

现在的问题是

  1. 阅读txt文件,找出哪个客户端机器负责哪些文件。
  2. 然后相应地从这两台机器中提取这些文件(如果它不在一台机器上,那么它将在第二台机器中),并将其(scp)放在此路径中的相应客户机中(/export/home/username/primary)
  3. 我可以从任何机器运行这个shell脚本,因为我们只是scp(-ing)它。

    我不确定我们如何在shell脚本中执行此操作?有什么想法吗?

    更新: -

    文件编号位于文件名中。

    此文件 - t1_weekly_0_200003_5.data用于文件0.同样,此文件为 - t1_weekly_1024_200003_5.data用于文件1024.

1 个答案:

答案 0 :(得分:1)

部分答案:数据结构可以通过微小的修改读入Perl(Perl非常能够处理)。此代码获取显示的数据文件并将其转换为此代码打印的结构:

#!/usr/bin/env perl
use strict;
use warnings;

# Slurp input into $data
my $data;
{
local $/;
$data = <>;
}
print "data = <<$data>>\n";

# Convert to eval-able format
$data =~ s/{/ \$x = {/m;
$data =~ s/}/};/m;
$data =~ s/([^ ]+)=/"$1" => /gm;
print "data = <<$data>>\n";

# Eval it
my $x;
eval $data;

# Dump the data structure
foreach my $key (keys %{$x})
{
    foreach my $val (@{$x->{$key}})
    {
        print "$key: $val\n";
    }
}

示例运行:

data = <<{ dbx45.dc1.host.com=[0, 1024, 4, 1028],
  dbx46.dc1.host.com=[1, 1025, 5, 1029],
  dbx47.dc1.host.com=[2, 1026, 6, 1030],
  dbx48.dc1.host.com=[3, 1027, 7, 1031] }
>>
data = << $x = { "dbx45.dc1.host.com" => [0, 1024, 4, 1028],
  "dbx46.dc1.host.com" => [1, 1025, 5, 1029],
  "dbx47.dc1.host.com" => [2, 1026, 6, 1030],
  "dbx48.dc1.host.com" => [3, 1027, 7, 1031] };
>>
dbx46.dc1.host.com: 1
dbx46.dc1.host.com: 1025
dbx46.dc1.host.com: 5
dbx46.dc1.host.com: 1029
dbx47.dc1.host.com: 2
dbx47.dc1.host.com: 1026
dbx47.dc1.host.com: 6
dbx47.dc1.host.com: 1030
dbx48.dc1.host.com: 3
dbx48.dc1.host.com: 1027
dbx48.dc1.host.com: 7
dbx48.dc1.host.com: 1031
dbx45.dc1.host.com: 0
dbx45.dc1.host.com: 1024
dbx45.dc1.host.com: 4
dbx45.dc1.host.com: 1028

您需要考虑从那里处理数据,但至少您有一个结构,您可以从中确定在给定计算机上保存哪些文件。您可以轻松创建反向索引列表,每个文件保存在哪台机器上。

您可以(并且可能应该)在修改和评估数据之前添加一些数据验证,以最大程度地降低恶意用户将危险信息输入数据文件的可能性。 (许多程序从磁盘读取可执行代码(在一些办公产品中考虑宏)很容易被恶意代码欺骗。不要让你的程序成为其中之一!

相关问题