寒假学习13-Linux xsync命令脚本

Posted on 2022-01-13 19:05  ***Pepsi***  阅读(163)  评论(0编辑  收藏  举报

使用xsync脚本循环同步文件到各个目标节点的相同目录下(需要用户具有root权限或者直接使用root用户创建xsync)
1.sudo创建xsync文件(root用户可直接创建)

xsync脚本源码:

#!/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

yum install -y rsync

给xsync文件赋予执行权限:

chmod  777 xsync

  • 注意:
  • 1.确保各个节点都安装了rsync工具并启动

如:将 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 ======================执行完毕====================

 

Copyright © 2024 ***Pepsi***
Powered by .NET 8.0 on Kubernetes