Shell脚本:分发文件到各个集群节点

找一个全局目录/root/bin 写脚本

javascript 复制代码
touch xsync
chmod 777 xsync
javascript 复制代码
#!/bin/bash

#作者:ldj
#时间:2024-10-15
#描述:拷贝文件

#1. 判断参数个数
if [ $# -lt 1 ]
    then
       echo "Error: Not Enough Argument!"
       exit
fi

#2.遍历集群所有机器
space=">>>>>>>  "
slash="/"
host_arr=(hadoop10 hadoop11 hadoop12)

for host in ${host_arr[@]}
do	
	#3.获取参数列表(文件)
	for file in $@
	do
	    #4.文件是否存在
	    if [ -e $file ]
		    then
		        #5.当前文件父se目录
		        pdir=$(cd -P $(dirname $file); pwd)
			    #6.当前文件名称
			    fname=$(basename $file)
			    echo $pdir$slash$fname$space$host
				
			    ssh $host "mkdir -p $pdir"
		        rsync -av $pdir/$fname $host:$pdir
		    else
		        echo $file does not exists!
	    fi
	done
done

1.从Window 直接拷贝到Linux可能会报错:/bin/bash^M: bad interpreter: No such file or directory

执行脚本前先 执行

javascript 复制代码
sed -i 's/\r$//' xsync

2.还有一个注意配置域名解析 (重启生效)

相关推荐
少妇的美梦11 小时前
logstash教程
运维
chen94511 小时前
k8s集群部署vector日志采集器
运维
chen94511 小时前
aws ec2部署harbor,使用s3存储
运维
轻松Ai享生活16 小时前
5 节课深入学习Linux Cgroups
linux
christine-rr16 小时前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神55516 小时前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆16 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_2642208917 小时前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
乌萨奇也要立志学C++17 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
雨落Liy17 小时前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡