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

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

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

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

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

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

4.可能出现的异常ssh

异常:sign_and_send_pubkey: signing failed: agent refused operationui

缘由: ssh-agent isn't workingspa

命令:`eval ssh-agent -s`code

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

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
相关文章
相关标签/搜索