大家好,又见面了,我是你们的朋友全栈君。
scp 和rsync均是远程拷贝,
rsync 远程分发
xsync 集群分发脚本
循环复制文件到所有节点相同的目录下
新建一个脚本
touch xsync.sh
vim xsync.sh
#!/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
赋予执行权限
chmod 777 xsync.sh
测试
同步一个文件夹
cd /usr/local/
xsync.sh hive
执行结果
[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]#
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/137335.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...