File transfer using SFTP
In most of the organization SMB port
(445) which is used for file sharing is disable for security reasons. But file
sharing over the network is one of the important factor withing infrastructure.
(Ex: DR Sync, Remote file backup Etc.)
In this blog we will see how to
transfer files from Local path to remote server over SFTP.
Secure File Transfer
Protocol (SFTP) is a secure version of File Transfer Protocol (FTP),
which facilitates data access and data transfer over a Secure Shell (SSH) data
stream.
Ensure “WinSCP” is
installed on system from where file will get copied.
Please find below script which
established connection to destination file server.
#Script
Name : script.txt
#
Connect to SFTP server using a password
open
sftp://<Username>:<Passowrd>@<IPAddress>/
-hostkey="<SSL Thumbprint>"
#
Ex: open sftp://Devendra:Techni@123@10.256.85.45/-hostkey="ssh-ed4587
256ejfh5874HRJFO542545dgdgjsdkkd45588RALE555FHKA="
#
Upload file
synchronize
remote <SourcePath>\ /<SFTP_Path>/
#
Ex: synchronize remote D:\SQL_Logs\ /SFTP/SQL/Daily/DB1/
#
Exit WinSCP
Exit
Note: In
above script we have used “synchronize remote” option instead of “put”
Which will be synced local data with remote server.
“put” option copy all data from source
to destination. Whereas, “synchronize” option only copy new / modified
data.
Ex:
Consider there are 5 files on local
server which already have been copied on remote server. If new file is created
or any exiting file is updated on local server, only those files will be
replaced on remote server.
Above script need to be called from
“*.bat” file which need to be placed on path where “winscp.com” utility is
available.
Ex: C:\Program Files
(x86)\WinSCP\SFTP_Copy.bat
Code:
--
SFTP_Copy.bat
winscp.com
/ini=nul /script= D:\Copy_Utility\script.txt /log="
D:\Copy_Utility\SFTP_logs\SFTPLog_!D-!M-!Y_!@.log" /loglevel=0
/logsize=5*10M
Above code will call “script.txt” file
and logged the output in “SFTP_logs” folder.
Logs have been configured in such a way
that it will limit log file size to 10 MB and will allow up
to 5 archived logs.
At the end, we need to schedule windows
job to execute “SFTP_Copy.bat”.
Ref :
https://winscp.net/eng/docs/commandline#logging
https://winscp.net/eng/docs/guide_automation
https://winscp.net/eng/docs/scriptcommand_put
https://winscp.net/eng/docs/scripting#commands
Comments
Post a Comment