shell 脚本、for 循环、ssh 和别名

发布于 2024-12-20 00:54:28 字数 438 浏览 0 评论 0原文

我正在尝试做这样的事情,我需要从 4 个刀片获取备份,并且 所有内容均应存储在 /home/backup/esa 位置下,其中包含 4 带有节点名称的目录(如 sc-1、sc-2、pl-1、pl-2)。每个 目录应包含相应节点的备份信息。

但我看到“我从哪个节点执行命令,只有数据正在被发送” 复制到所有 4 个目录”。知道为什么会发生这种情况吗?我的脚本是这样的:

for node in $(grep "^node" /cluster/etc/cluster.conf | awk '{print $4}');
do echo "Creating backup fornode ${node}";
ssh $node source /etc/profile.d/bkUp.sh;
asBackup -b /home/backup/esa/${node};
done

I'm trying to do something like this, I need to take backup from 4 blades, and
all should be stored under the /home/backup/esa location, which contains 4
directories with the name of the nodes (like sc-1, sc-2, pl-1, pl-2). Each
directory should contain respective node's backup information.

But I see that "from which node I execute the command, only that data is being
copied to all 4 directories". any idea why this happens? My script is like this:

for node in $(grep "^node" /cluster/etc/cluster.conf | awk '{print $4}');
do echo "Creating backup fornode ${node}";
ssh $node source /etc/profile.d/bkUp.sh;
asBackup -b /home/backup/esa/${node};
done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2024-12-27 00:54:28

您的问题是这段代码:

ssh $node source /etc/profile.d/bkUp.sh;
asBackup -b /home/backup/esa/${node};

它确实:

  1. $node 上创建远程 shell
  2. 在远程中执行命令 source /etc/profile.d/bkUp.sh shell
  3. 关闭远程 shell并忘记在该 shell 中完成的任何操作!!
  4. 在本地主机上运行 asBackup

这不是你想要的。将其更改为:

ssh "$node" "source /etc/profile.d/bkUp.sh; asBackup -b '/home/backup/esa/${node}'"

这样做:

  1. $node 上创建远程 shell
  2. 执行命令 source /etc/profile.d/bkUp.sh; 远程主机上的 asBackup -b '/home/backup/esa/${node}'

确保 /home/backup/esa/${node} code> 是 NFS 挂载(否则,文件将仅备份到远程主机上的目录中)。

请注意,/etc/profile 对于备份脚本(或其配置)来说是一个非常糟糕的地方。考虑将设置/配置移动到 /home/backup/esa ,它是(或应该)在集群的所有节点之间共享的,因此在一个地方更改它会立即在任何地方更新它。

另请注意引号的使用:单引号和双引号可确保变量 node 中的空格不会导致意外问题。当然,“$node”中不太可能有空格,但如果有,错误消息会误导您。

所以一定要正确引用。

Your problem is this piece of the code:

ssh $node source /etc/profile.d/bkUp.sh;
asBackup -b /home/backup/esa/${node};

It does:

  1. Create a remote shell on $node
  2. Execute the command source /etc/profile.d/bkUp.sh in the remote shell
  3. Close the remote shell and forget about anything done in that shell!!
  4. Run asBackup on the local host.

This is not what you want. Change it to:

ssh "$node" "source /etc/profile.d/bkUp.sh; asBackup -b '/home/backup/esa/${node}'"

This does:

  1. Create a remote shell on $node
  2. Execute the command(s) source /etc/profile.d/bkUp.sh; asBackup -b '/home/backup/esa/${node}' on the remote host

Make sure that /home/backup/esa/${node} is a NFS mount (otherwise, the files will only be backed up in a directory on the remote host).

Note that /etc/profile is a very bad place for backup scripts (or their config). Consider moving the setup/config to /home/backup/esa which is (or should be) shared between all nodes of the cluster, so changing it in one place updates it everywhere at once.

Also note the usage of quotes: The single and double quotes make sure that spaces in the variable node won't cause unexpected problems. Sure, it's very unlikely that there will be spaces in "$node" but if there are, the error message will mislead you.

So always quote properly.

尘世孤行 2024-12-27 00:54:28

你的问题的格式有点混乱,但看起来你有一个引用问题。如果这样做

ssh $node source /etc/profile.d/bkUp.sh; esaBackup -b /home/backup/esa/${node}

,则命令 source 将在 $node 上执行。命令完成后,远程连接将关闭,并且包含获取 /etc/profile.d/bkUp.sh 结果的 shell 也随之关闭。现在 esaBackup 命令已在本地计算机上运行。它不会看到你在 bkUp.sh 中保留的任何内容

你需要做的就是在你想要远程 shell 运行的所有命令周围加上引号 - 就像这样,

ssh $node "source /etc/profile.d/bkUp.sh; esaBackup -b /home/backup/esa/${node}"

这将使 ssh 运行远程命令的完整列表节点。

The formatting of your question is a bit confusing, but it looks as if you have a quoting problem. If you do

ssh $node source /etc/profile.d/bkUp.sh; esaBackup -b /home/backup/esa/${node}

then the command source is executed on $node. After the command finishes, the remote connection is closed and with it, the shell that contains the result of sourcing /etc/profile.d/bkUp.sh. Now esaBackup command is run on the local machine. It won't see anything that you keep in `bkUp.sh

What you need to do is put quotes around all the commands you want the remote shell to run -- something like

ssh $node "source /etc/profile.d/bkUp.sh; esaBackup -b /home/backup/esa/${node}"

That will make ssh run the full list of commands on the remote node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文