大家好,又见面了,我是你们的朋友全栈君。
在 /bin/目录下创建 xsync
首先 :yum install -y rsync
脚本如下:
#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器
for host in master node1 node2
do
echo ==================== $host ====================
#3. 遍历所有目录,挨个发送
for file in $@
do
#4. 判断文件是否存在
if [ -e $file ]
then
#5. 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
#6. 获取当前文件的名称
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exists!
fi
done
done
给xsync文件赋予执行权限:
chmod 777 xsync
然后执行 : sudo xsync 目录
如:将 master机器/opt下的scp复制到 node1 node2
[root@master bin]# xsync /opt/scp
-bash: /usr/local/bin/xsync: /bin/bash^M: 坏的解释器: 没有那个文件或目录
[root@master bin]# sudo xsync /opt/scp
==================== master ====================
sending incremental file list
sent 134 bytes received 18 bytes 304.00 bytes/sec
total size is 49 speedup is 0.32
==================== node1 ====================
sending incremental file list
sent 134 bytes received 18 bytes 304.00 bytes/sec
total size is 49 speedup is 0.32
==================== node2 ====================
sending incremental file list
sent 130 bytes received 18 bytes 296.00 bytes/sec
total size is 49 speedup is 0.33
[root@master bin]# cd
[root@master ~]# xsync /opt/scp
-bash: /usr/local/bin/xsync: /bin/bash^M: 坏的解释器: 没有那个文件或目录
[root@master ~]# sudo xsync /opt/scp
==================== master ====================
sending incremental file list
sent 130 bytes received 18 bytes 296.00 bytes/sec
total size is 49 speedup is 0.33
==================== node1 ====================
sending incremental file list
sent 134 bytes received 18 bytes 304.00 bytes/sec
total size is 49 speedup is 0.32
==================== node2 ====================
sending incremental file list
sent 134 bytes received 18 bytes 304.00 bytes/sec
total size is 49 speedup is 0.32
[root@master ~]# cd /opt/
[root@master opt]# cd scp/
[root@master scp]# ll
总用量 4
-rw-r--r-- 1 root root 45 12月 11 21:06 1.html
drwxr-xr-x 2 root root 22 12月 11 21:38 abc
[root@master scp]# mkdir aaaaa
[root@master scp]# cd aaaaa/
[root@master aaaaa]# vi aa.text
[root@master aaaaa]# cd
[root@master ~]# sudo xsync /opt/scp
==================== master ====================
sending incremental file list
sent 183 bytes received 19 bytes 404.00 bytes/sec
total size is 62 speedup is 0.31
==================== node1 ====================
sending incremental file list
scp/
scp/aaaaa/
scp/aaaaa/aa.text
sent 249 bytes received 48 bytes 594.00 bytes/sec
total size is 62 speedup is 0.21
==================== node2 ====================
sending incremental file list
scp/
scp/aaaaa/
scp/aaaaa/aa.text
sent 249 bytes received 48 bytes 594.00 bytes/sec
total size is 62 speedup is 0.21
[root@master ~]# cd
[root@master ~]# cd /bin/
[root@master bin]# vi xsync
[root@master bin]#
附:
zk.sh 脚本
#!/bin/bash
case $1 in
"start"){
for i in master node1 node2
do
echo ---------- zookeeper $i 启动 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh start"
done
};;
"stop"){
for i in master node1 node2
do
echo ---------- zookeeper $i 停止 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh stop"
done
};;
"status"){
for i in master node1 node2
do
echo ---------- zookeeper $i 状态 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh status"
done
};;
esac
附:
jpsall.sh 脚本:
#!/bin/bash
# 执行jps命令查询每台服务器上的节点状态
echo ======================集群节点状态====================
for i in master node1 node2
do
echo ====================== $i ====================
ssh $i "/opt/jdk/jdk1.8.0_202/bin/jps"
done
echo ======================执行完毕====================
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137280.html原文链接:https://javaforall.cn