WinSCP 实现批处理同步文件

起因

在项目开发过程中,前后分离,各自开发减少耦合度,但是在服务器后端与前端在同个Tomcat服务下面,因此前端在开发好或是修改了Bug就打包给后端开发部署到服务器,但是由于前端部署的频率太高,导致后端频繁的给前端部署,这样影响了后端开发,为此提供工具让前端自行部署,但考虑到前端对服务器这块的学习成本与必要性,所以考虑提供一键部署工具,因此看到了WinSCP。

什么是 WinSCP

官方地址: https://winscp.net/eng/docs/lang:chs
WinSCP是一款开源的SFTP客户端,运行于Windows系统下,遵照GPL发布。WinSCP除了SFTP,还支持SSH、SCP。它的主要功能就是在本地与远程计算机间安全的复制文件,并且可以直接编辑文件。

下载和安装 WinSCP

下载地址 https://winscp.net/eng/download.php
安装 : 一路单击“下一步”即可。

主要功能

  • 图形用户界面
  • 多语言
  • 与 Windows 完美集成(拖拽, URL, 快捷方式)
  • 支持所有常用文件操作
  • 支持基于 SSH-1、SSH-2 的 SFTP 和 SCP 协议
  • 支持批处理脚本和命令行方式
  • 多种半自动、自动的目录同步方式
  • 内置文本编辑器
  • 支持 SSH 密码、键盘交互、公钥和 Kerberos(GSS) 验证
  • 通过与 Pageant(PuTTY Agent)集成支持各种类型公钥验证
  • 提供 Windows Explorer 与 Norton Commander 界面
  • 可选地存储会话信息
  • 可将设置存在配置文件中而非注册表中,适合在移动介质上操作

注意支持批处理脚本和命令行方式,这是我们所需要的

文件操作

WinSCP 可以执行所有基本的文件操作,例如下载和上传。同时允许为文件和目录重命名、改变属性、建立符号链接和快捷方式。

连接到远程计算机

使用 WinSCP 可以连接到一台提供 SFTP (SSH File Transfer Protocol)或 SCP (Secure Copy Protocol)服务的 SSH (Secure Shell)服务器,通常是 UNIX 服务器。SFTP 包含于 SSH-2 包中,SCP 在 SSH-1 包中。两种协议都能运行在以后的 SSH 版本之上。WinSCP 同时支持 SSH-1 和 SSH-2。 但WinSCP不支持编码选择,也就是说,你在Windows下使用WinSCP连接一个Linux机器,因为Linux和Windows的默认编码不同,因此是无法访问上面的中文文件或者文件夹的(将看到乱码)。一种解决方法就是在打开winscp时登录中的 Advanced Options–Environment中将 “UTF-8 encoding for filenames”设为on.

使用批处理脚本解决我们的问题

官方文档 https://winscp.net/eng/docs/scripting

编写脚本文件example.bat

option confirm off
# connent
open root:[email protected]:2018 -privatekey=E:\online_test_mp_rsa.ppk -passphrase=soqiadmin1234

# 切换目录至multiple
cd /usr/local/tomcat/mp/webapps/ROOT/multiple
# 同步本地目录e:\temp 到远程 multiple
synchronize remote -delete e:\temp

# Disconnect
close
# Exit WinSCP
exit
pause

执行脚本文件example.bat

"C:\Program Files (x86)\WinSCP\WinSCP.com" /script=e:\example.bat /log=log.log

根据需求参考命令列表实现功能

命令    描述
Command Description
call    Executes arbitrary remote shell command
cd  Changes remote working directory
checksum    Calculates checksum of remote file
chmod   Changes permissions of remote file
close   Closes session
cp  Duplicates remote file
echo    Prints message onto script output
exit    Closes all sessions and terminates the program
get Downloads file from remote directory to local directory
help    Displays help
keepuptodate    Continuously reflects changes in local directory on remote one
lcd Changes local working directory
lls Lists the contents of local directory
ln  Creates remote symbolic link
lpwd    Prints local working directory
ls  Lists the contents of remote directory
mkdir   Creates remote directory
mv  Moves or renames remote file
open    Connects to server
option  Sets or shows value of script options
put Uploads file from local directory to remote directory
pwd Prints remote working directory
rm  Removes remote file
rmdir   Removes remote directory
session Lists connected sessions or selects active session
stat    Retrieves attributes of remote file
synchronize Synchronizes remote directory with local one

你可能感兴趣的:(WinSCP 实现批处理同步文件)