前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux xsync命令脚本

Linux xsync命令脚本

作者头像
全栈程序员站长
发布2022-08-22 13:59:23
1.8K0
发布2022-08-22 13:59:23
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

在 /bin/目录下创建 xsync

首先 :yum install -y rsync

脚本如下:

代码语言:javascript
复制
#!/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

代码语言:javascript
复制
[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 脚本

代码语言:javascript
复制
#!/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 脚本:

代码语言:javascript
复制
#!/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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月5,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档