xsync 集群同步脚本「建议收藏」

2022-09-05 10:32:36 浏览数 (1)

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

scp 和rsync均是远程拷贝,

rsync 远程分发

xsync 集群分发脚本

循环复制文件到所有节点相同的目录下

新建一个脚本

代码语言:javascript复制
touch xsync.sh
vim xsync.sh
代码语言:javascript复制
#!/bin/bash
# $#:表示传递给脚本或函数的参数个数。
#1 获取输入参数个数,如果没有参数,直接退出
pcount=$#
if((pcount==0)); then
echo no args;
exit;
fi
 
#2 获取文件名称
p1=$1
fname=`basename $p1`
echo fname=$fname
 
#3 获取上级目录到绝对路径
pdir=`cd -P $(dirname $p1); pwd`
echo pdir=$pdir
 
#4 获取当前用户名称
user=`whoami`
 
#5 循环
#for((host=0; host<5; host  )); do
        #echo $pdir/$fname $user@192.168.3.15$host:$pdir
 #       echo --------------- 192.168.3.15$host ----------------
 #       rsync -rvl $pdir/$fname $user@192.168.3.15$host:$pdir
#done
 
#5 循环
for host in { 
   master,slaver1,slaver2,slaver3,slaver4,slaver5}; do
        #echo $pdir/$fname $user@$host:$pdir
        echo --------------- $host ----------------
        rsync -rvl $pdir/$fname $user@$host:$pdir
done

赋予执行权限

代码语言:javascript复制
chmod 777 xsync.sh

测试 同步一个文件夹

代码语言:javascript复制
 cd /usr/local/
 xsync.sh hive

执行结果

代码语言:javascript复制
[root@master local]# xsync.sh hive
fname=hive
pdir=/usr/local
--------------- master ----------------
sending incremental file list

sent 53 bytes  received 17 bytes  28.00 bytes/sec
total size is 0  speedup is 0.00
--------------- slaver1 ----------------
sending incremental file list

sent 49 bytes  received 17 bytes  44.00 bytes/sec
total size is 0  speedup is 0.00
--------------- slaver2 ----------------
sending incremental file list
hive/

sent 52 bytes  received 20 bytes  48.00 bytes/sec
total size is 0  speedup is 0.00
--------------- slaver3 ----------------
sending incremental file list
hive/

sent 52 bytes  received 20 bytes  48.00 bytes/sec
total size is 0  speedup is 0.00
--------------- slaver4 ----------------
sending incremental file list
hive/

sent 56 bytes  received 20 bytes  50.67 bytes/sec
total size is 0  speedup is 0.00
--------------- slaver5 ----------------
sending incremental file list
hive/

sent 52 bytes  received 20 bytes  48.00 bytes/sec
total size is 0  speedup is 0.00
[root@master local]# 

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137335.html原文链接:https://javaforall.cn

0 人点赞