WinSCP – Script to Synchronize from Linux Server to Local Machine

Last updated on July 24th, 2019 at 01:21 pm

Synchronize from SFTP server to local directory with a script

1. Install WinSCP on a local machine you want to sync files

2. Connect first time only to the server via SFTP GUI to copy server key:

3. Go to WinSCP folder location “C:\Program Files (x86)\WinSCP”

4. Create a BATCH file with the following commands:

cd C:\Program Files (x86)\WinSCP
winscp.com /script=SyncToLocal.txt
pause

5. Create a TXT file called SyncToLocal.txt with the following commands:

Option 1a – Local (HostKey)

option batch abort
option confirm off
open sftp://username:[email protected] -hostkey="ssh-rsa 2048 7c:5c:f4:ab:s4:12:55:fb:14:6h"
synchronize local C:\LocalFolder /server/outgoing
exit

 

Option 1b – Remote (HostKey)

option batch abort
option confirm off
open sftp://username:[email protected] -hostkey="ssh-rsa 2048 7c:5c:f4:ab:s4:12:55:fb:14:6h"
synchronize remote C:\LocalFolder /server/outgoing
exit

 

Option 1c – Both (HostKey)

option batch abort
option confirm off
open sftp://username:[email protected] -hostkey="ssh-rsa 2048 7c:5c:f4:ab:s4:12:55:fb:14:6h"
synchronize both C:\LocalFolder /server/outgoing
"exit"

 

Option 2a – Local (Password)

"C:\Program Files (x86)\WinSCP\winscp.com" /command "open sftp://username:[email protected]/" ^
"synchronize local C:\LocalFolder/outgoing /server/outgoing/" ^
"exit"

 

Option 2b – Remote (Password)

"C:\Program Files (x86)\WinSCP\winscp.com" /command "open sftp://username:[email protected]/" ^
"synchronize remote C:\LocalFolder/outgoing /server/outgoing/" ^
"exit"

 

Option 2c – Both (Password) – [optional: filemask for sync only .csv files]

"C:\Program Files (x86)\WinSCP\winscp.com" /command "open sftp://username:[email protected]/" ^
"synchronize both -filemask="*.csv" C:\LocalFolder/outgoing /server/outgoing/" ^
"exit"

 

6. Now we created 2 files on WinSCP folder and we can run the batch file to sync from Server to Local Machine

Enjoy 🙂

Leave a comment

Your email address will not be published. Required fields are marked *