shell 脚本、for 循环、ssh 和别名
我正在尝试做这样的事情,我需要从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是这段代码:
它确实:
$node
上创建远程 shellsource /etc/profile.d/bkUp.sh
shellasBackup
。这不是你想要的。将其更改为:
这样做:
$node
上创建远程 shellsource /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:
It does:
$node
source /etc/profile.d/bkUp.sh
in the remote shellasBackup
on the local host.This is not what you want. Change it to:
This does:
$node
source /etc/profile.d/bkUp.sh; asBackup -b '/home/backup/esa/${node}'
on the remote hostMake 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.
你的问题的格式有点混乱,但看起来你有一个引用问题。如果这样做
,则命令
source
将在$node
上执行。命令完成后,远程连接将关闭,并且包含获取/etc/profile.d/bkUp.sh
结果的 shell 也随之关闭。现在 esaBackup 命令已在本地计算机上运行。它不会看到你在 bkUp.sh 中保留的任何内容你需要做的就是在你想要远程 shell 运行的所有命令周围加上引号 - 就像这样,
这将使 ssh 运行远程命令的完整列表节点。
The formatting of your question is a bit confusing, but it looks as if you have a quoting problem. If you do
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
. NowesaBackup
command is run on the local machine. It won't see anything that you keep in `bkUp.shWhat you need to do is put quotes around all the commands you want the remote shell to run -- something like
That will make ssh run the full list of commands on the remote node.