shell ssh sftp 远程自动部署(上传文件)

 前提:ssh sftp 无密码登陆配置方法

1.生成密钥对:ssh-keygen -t rsa

2.将私有密钥保存本地: ~/.ssh/id_rsa,

  将公有密钥复制到远程服务器: ~/.ssh/authorized_keys

3.修改公钥文件的访问权限 chmod 644 authorized_keys

4.可能出现的异常

异常:sign_and_send_pubkey: signing failed: agent refused operation

原因: ssh-agent isn't working

命令:`eval ssh-agent -s`

`ssh-add` 或者 `ssh-add ~/.ssh/my_other_key`

5.远程部署指定目录

#!/bin/bash
sourcePath="/home/xinhuanet/workspace/out/artifacts/web_war_exploded"
targetPaht="/home/workspace/ttt"
len=${#sourcePath}
user="root"
targetIP="ip"
#创建远程目录
mkdir_remote_dir(){
ssh $user@$targetIP<<EOF
   if [ ! -d $1 ]; then
     mkdir $1
   fi
exit
EOF
}
#上传远程文件
sftp_upload_file(){
sftp $user@$targetIP<<EOF
  put -r $1 $2
  quit
EOF
}
#遍历本地目录
list_file(){
   for file in `ls  $1`
   do
       #file 文件名 $1 路径名 ; local_path本地全路径
       local_path=$1/$file
       #截取本地根路径${local_path:${len}} ;remote_path远程全路径
       remote_path=$targetPaht${local_path:${len}}
       echo $local_path"--->to---->"$remote_path
       if [ -d $local_path ]; then
            mkdir_remote_dir $remote_path
	        list_file $local_path
       elif [ -f $local_path ];  then
            sftp_upload_file $local_path $remote_path
       else
         echo 'not d and f' 
       fi
   done
}

#部署整个目录
#list_file $sourcePath

#--------一下方式---基于git------------------------------------------------
#基于git diff commit_id 查看差异文件清单进行差异化的自动化部署
#由于git中文件路径与本地编译后的文件路径存在差异,需要进一步处理.
#处理方式不统一,根据个人实际情况


git_file_list(){
    list_files=`git diff $1 $2 --name-only`
    for file in $list_files
    do
        file=${file/#"home/WebRoot/"/""}#替换。源码路径与编译路径转换
        file=${file/#"home/src"/"WEB-INF/classes"}#替换。源码路径与编译路径转换
        file=${file/%".java"/".class"}#替换。源码文件与编译文件转换
        echo $sourcePath/$file
        echo $targetPaht/$file
        #创建远程目录(不存在时)
        mkdir_remote_dir `dirname $targetPaht/$file`
        sftp_upload_file $sourcePath/$file $targetPaht/$file
    done
}
#两个commit 之间的差异文件清单
git_file_list $1 $2


 

转载于:https://my.oschina.net/shiyusen/blog/743153

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值