备注
在部署集群的时候,往往需要部署多态机器, 这些机器间安装文件等 的复制 可以使用这个小脚本来完成
代码实现
#!/bin/bash
function printhelp(){
echo "============================== xssh help info ====================================="
echo "|| using e.g : ||"
echo "|| format: [xsync] [ local file path ] || [local directory path] ||"
echo "|| e.g.1: xsync /home/hadoop/hadoop-2.7.2/etc/hadoop/yarn-site.xml ||"
echo "|| e.g.2: xsync /home/hadoop/hadoop-2.7.2/etc/hadoop/ ||"
echo "|| e.g.3: xsync bin/xsync ||"
echo "|| ||"
echo "|| file path : ||"
echo "|| ||"
echo "|| ||"
echo "|| ||"
echo "|| ************************** attention please ******************************** ||"
echo "|| ||"
echo "|| The script automatically determines if the remote target folder exists, and ||"
echo "|| and if it does not, the system automatically creates the target directory ||"
echo "|| ||"
echo "|| ||"
echo "================================ xssh help info ==================================="
exit 0;
}
#1 获取输入参数个数,如果没有参数,直接退出
pcount=$#
if((pcount==0)); then
printhelp
fi
# 2. 判断是否需要新启动一个tty窗口
case $1 in
"--help"| "-h" ){
printhelp
};;
*){
echo "[DEBUG] 1 command is :$*"
echo "[DEBUG] ===============>>>>>>>>>>>>>>>>>> welcome to use this script <<<<<<<<<<<<<<<<< ========="
};;
esac
#3 获取文件名称
p1=$1
fname=`basename $p1`
echo fname=$fname
#4 获取上级目录到绝对路径
pdir=`cd -P $(dirname $p1); pwd`
echo pdir=$pdir
#5 获取当前用户名称
user=`whoami`
#6 目标主机 多个主机之间使用空格隔开
hostnames=(hadoop181 hadoop182 hadoop183)
#7 执行循环拷贝
for host in "${hostnames[@]}"
do
echo "[DEBUG] Copy local file [$pdir/$fname] to $host "
# 判断 远程父目录如果不存在, 将创建目录先
if ssh $user@$host "test -d $pdir"; then
echo "[DEBUG] Remote directories exist and need no processing "
else
echo "[DEBUG] That directory doesn't exists, Auto create That directory"
echo "[DEBUG] SSH to $host to execute commands [ mkdir -p $pdir ] "
ssh $user@$host "mkdir -p $pdir"
fi
echo "[DEBUG] execute commands [ rsync -rvl $pdir/$fname $user@$host:$pdir ] "
# rsync -rvlaz --progress $pdir/$fname $user@$host:$pdir
# scp -r $pdir/$fname $user@$host:$pdir/$fname
rsync -rvl $pdir/$fname $user@$host:$pdir
done
注意 : 当系统中没有 rsync 命令解决方案
- 方案1 : 可以使用scp代替这个命令,注释掉rsync的 开启scp代码即可
- 方案2 : 可以安装 rsync安装包
yum -y install rsync