#关键字:shell rsync 集群文件分发
目录
一、目标:
1、实现某台机器的文件同步到其他机器的相同目录。
2、例如:在hadoop3机器的 /opt/module/hadoop-3.2.2/etc/hadoop 目录下修改了core-site.xml文件内容,现在需要把core-site.xml文件同步到集群的hadoop4,hadoop5节点。
二、环境说明:
1、三台CentOS7系统的虚拟机,主机名分别为hadoop3、hadoop4、hadoop5。
2、hadoop3、hadoop4、hadoop5 三台机器已经配置了 ssh 免密登录。
3、配置过程使用 sspu 用户。
三、配置步骤:
1、在 hadoop3 机器的/usr/local/bin 路径下创建文件 xsync
[sspu@hadoop4 bin]$ pwd
/usr/local/bin
[sspu@hadoop4 bin]$ vim xsync
2、文件 xsync 内容:
#!/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=3; host<=5; host++)); do
echo ------------------- hadoop$host --------------
rsync -rvl $pdir/$fname $user@hadoop$host:$pdir
done
3、文件 xsync 授权 chmod +x xsync
4、将脚本复制到 /usr/bin 路径中,以便全局调用 sudo cp /usr/local/bin/xsync /usr/bin/
5、让环境变量生效,执行命令 source /etc/profile
6、脚本测试:
[sspu@hadoop4 hadoop]$ xsync core-site.xml
fname=core-site.xml
pdir=/opt/module/hadoop-3.2.2/etc/hadoop
------------------- hadoop3 --------------
sending incremental file list
core-site.xml
sent 101 bytes received 47 bytes 296.00 bytes/sec
total size is 1,260 speedup is 8.51
------------------- hadoop4 --------------
sending incremental file list
sent 50 bytes received 12 bytes 41.33 bytes/sec
total size is 1,260 speedup is 20.32
------------------- hadoop5 --------------
sending incremental file list
core-site.xml
sent 101 bytes received 47 bytes 296.00 bytes/sec
total size is 1,260 speedup is 8.51