shell脚本自动分发文件

该脚本自动分发文件,可以上传也可以下载,上传只能上传一个包,而下载可以同时下载多个文件,且把IP,账号,密码,端口写入到ipfile配置文件当中传输文件时候就不需要再输入密码了,本人用这个脚本来备份数据用。


使用方法:文章结尾有详细用法且附带截图



代码:

transport_file.sh

#!/bin/bash
#
##
#声明变量
#
SourFile="`echo $(eval echo '$'{2..9}) | cut -d: -f1`"
DesPath=`echo $* | cut -d: -f2-`
PWD=$(pwd)
File=$(pwd)/$(basename $0)
IP_File=$PWD/ipfile
F_rN() {
File_recordN=$(wc -l $IP_File | awk '{print $1}')
}

RE_HO_IN() {
Ip=$(sed -n "$1"p $IP_File | awk '{print $1}')
User=$(sed -n "$1"p $IP_File | awk '{print $2}')
Passwd=$(sed -n "$1"p $IP_File | awk '{print $3}')
Port=$(sed -n "$1"p $IP_File | awk '{print $4}')
}

### ------ 生成配置文件 ------ ###
##
#生成ipfile文件
#
if [ ! -f $IP_File ]
then
	cat > ipfile << 'EOF'
IP              user    password	port
EOF
fi

##
#生成expect.sh脚本
#
if [ ! -f remote2local_expect.sh ]
then
	cat > remote2local_expect.sh << 'EOF'
#!/usr/bin/expect -f
set user [lindex $argv 0]
set ip [lindex $argv 1]
set password [lindex $argv 2]
set des_path [lindex $argv 3]
set sou_file [lindex $argv 4]
set port [lindex $argv 5]
set timeout 10
spawn rsync -zvzP -e "ssh -p ${port}" ${user}@${ip}:${sou_file} $des_path
expect {
	"*yes/no" { send "yes\r" }
       	"password:" { send "$password\r" }
       	" " { }
}
interact

EOF

##第二个expect##

	cat  > local2remote_expect.sh << 'EOF'
#!/usr/bin/expect -f
set user [lindex $argv 0]
set ip [lindex $argv 1]
set password [lindex $argv 2]
set des_path [lindex $argv 3]
set sou_file [lindex $argv 4]
set port [lindex $argv 5]
set timeout 10
spawn rsync -zvzP -e "ssh -p ${port}" ${sou_file} ${user}@${ip}:$des_path
expect {
    	 "*yes/no" { send "yes\r" }
      	 "password:" { send "$password\r" }
       	 " " { }
}
interact
EOF
	dos2unix remote2local_expect.sh
	dos2unix  local2remote_expect.sh
	chmod 755 remote2local_expect.sh
	chmod 755 local2remote_expect.sh

fi
###生成expect完成###

### ------ 生成配置文件结束 ------ ###

#友好提示
#
use_info() {
	echo -e "\n\t This script is used to distribute files"
	printf "%b\n" "\t Used for the first time , please generate and edit \e[40;31mipfile\e[0m file\n"

	printf "%b\n" "\t \e[40;31mOption:\e[0m"
	echo -e "\t -r2l :Copy remote file to a local. -r2l equal -remote2local"
	echo -e "\t -l2r :Copy local file to a remote. -l2r equal -local2remote"

	echo -e "\n\t Usage : `basename $0` -r2l \"RemoteFile [File ...]\" : [LocalPATH]"
	echo -e "\t Usage : `basename $0` -l2r \"LocalFile [File ...]\" : [RemotePATH]"
	echo -e "\n\t Eg    : `basename $0` -r2l \"/data/file1\"  : . "
	echo -e "\t Eg    : `basename $0` -r2l \"/data/file*\"  : . "
	echo -e "\t Eg    : `basename $0` -l2r \"/root/.ssh/id_rsa.pub\"  : /root/.ssh/authorized_keys \n"
}

if [ -z $1 ];then
	use_info
	exit
fi


##
#声明File_recondN变量
F_rN
####
##
#
#该脚本的第一个参数,表示是remote2local或local2remote
mark="$1"
#
if [[ "$mark" =~ ^(-r2l|-R2L|-remote2local|-r|-R)$ ]]
then
	for i in `seq 2 $File_recordN`
	do
		RE_HO_IN $i
		$PWD/remote2local_expect.sh $User $Ip $Passwd $DesPath "$SourFile" $Port
#		echo $User $Ip $Passwd "$DesPath" "$SourFile" $Port
	done
elif [[ "$mark" =~ ^(-l2r|-local2remote|-l|-L)$ ]]
then
	for i in `seq 2 $File_recordN`
	do
		RE_HO_IN $i
		$PWD/local2remote_expect.sh $User $Ip $Passwd $DesPath $SourFile $Port
		#echo "$SourFile" $User $Ip $Passwd $DesPath
	done
else
	use_info

fi


使用方法:


1、执行脚本先生成配置文件,配置文件共3个

ipfile

local2remote_expect.sh

remote2local_expect.sh


2、配置这三个文件

配置ipfile

vim ipfile



local2remote_expect.sh 和 remote2local_expect.sh 不需要任何配置。


3、传输文件:从远程主机传输到本地



上面两条信息,因为主机10.2.4.86上的文件与70上的相同,已经由86传输到本地,故只出现一个rsync的简短描述信息。


传输文件同上,这里使用了一个通配符*




从本地上传到远程服务器



OK了,可以用它来从远程Linux服务器上备份日志数据等。我的备份环境是这样:我们需要每月备份平台日志,备份到本地的windows备份机上,我使用cygwin来执行这个脚本从远程Linux服务器每个月执行一次来备份日志。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值