Skip to content

Latest commit

 

History

History
73 lines (60 loc) · 1.66 KB

集群同步脚本.md

File metadata and controls

73 lines (60 loc) · 1.66 KB

一、创建 xsync.sh 文件

[root@hadoop1 bin]# cd /usr/local/bin
[root@hadoop1 bin]# vim xsync.sh
#!/bin/bash

# 获取输出参数,如果没有参数则直接返回
pcount=$#
if [ $pcount -eq 0 ]
then
        echo "no parameter find !";
        exit;
fi

# 获取传输文件名
p1=$1
filename=`basename $p1`
echo "load file $p1 success !"

# 获取文件的绝对路径
pdir=`cd -P $(dirname $p1); pwd`
echo "file path is $pdir"

# 获取当前用户(如果想使用root用户权限拷贝文件,在命令后加入-root参数即可)
user=$2
case "$user" in
"-root")
        user="root";;
"")
        user=`whoami`;;
*)
        echo "illegal parameter $user"

esac

echo $user
# 拷贝文件到从机(这里注意主机的host需要根据你的实际情况配置,要与你具体的主机名对应)
for (( host=1;host<=3;host++ ))
do
        echo "================current host is hadoop$host================="
        rsync -rvl $pdir/$filename $user@hadoop$host:$pdir
done

echo "complate !"

二、测试

[root@hadoop1 bin]# xsync.sh xsync.sh 
load file xsync.sh success !
file path is /root/bin
root
================current host is hadoop1=================
sending incremental file list

sent 45 bytes  received 12 bytes  114.00 bytes/sec
total size is 858  speedup is 15.05
================current host is hadoop2=================
sending incremental file list
xsync.sh

sent 950 bytes  received 35 bytes  656.67 bytes/sec
total size is 858  speedup is 0.87
================current host is hadoop3=================
sending incremental file list
xsync.sh

sent 950 bytes  received 35 bytes  1,970.00 bytes/sec
total size is 858  speedup is 0.87
complate !