集群间文件拷贝脚本

备注

在部署集群的时候,往往需要部署多态机器, 这些机器间安装文件等 的复制 可以使用这个小脚本来完成

代码实现

#!/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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值