Here is a solution that allows synchronization (mirroring) of a sFTP folder with a local folder. The solution provided here uses the free WinSCP program. An alternative to this approach could be the paid software GoodSync which is fairly easy to use.
- Download and install WinScp and add it to the PATH variable through the custom install.
- Configure a session in WinScp
- Write the sync script used by WinScp
- Write a batch file to call WinScp
- (Optionally) add a scheduled task to run the sync script.
Just use the installer for Windows and select custom install and check the add to PATH variable in the installer when the tab shown below appears.
Configure a Session in WinScp
Start WinScp after the installation completes and create a new session. This session must be stored with all transfer settings and password.
First enter host name, user name and password. Then click the advanced button:
In the advanced dialog change the drop-down for UTF-8 encoding filenames to “On”. This is needed to make Danish characters “ÆØÅæøå” show correctly on the computer that fetched the files.
Finally, save the session by clicking Save As…”. The name entered here “firstname.lastname@example.org” is the name that will be used later in the sync script.
WinScp needs a sync script that lists the commands and settings it must use to sync the remote sFTP path to the local directory. Below is shown a script “script.bat”:
# Automatically abort script on errors option batch abort # Disable overwrite confirmations that conflict with the previous option confirm off # Connect using a password # open sftp://user:email@example.com -hostkey="ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" # Connect using stored connection open "firstname.lastname@example.org" # Force binary mode transfer option transfer binary synchronize local -delete -mirror C:\sync-tmp / # Disconnect close exit
Note that the script references two settings that must be customised upon re-use.
- The stored session name. (“email@example.com”)
- The path to which the files will be synced. (“c:\sync-tmp”)
Note that the script contains a comment which lists the case where a session is opened using a hostkey instead of a stored password.
Batch script to invoke sync
To make it easiere to run the sync script a wrapper batch file can be created “do-sync.bat” that can look like this:
"c:\Program Files (x86)\WinSCP\WinSCP.com" /console /script=sync.bat /log=sync.log
The batch script just calls WinScp (without starting a new application, change .com to exe to do so) with the sync script and also sets a bath to a log file that it uses.
Configure scheduled task
The do-sync.bat file created in the previous step can now be used in a standard Scheduled Task in Windows to setup a recurring task for fetching an update daily/weekly or …
For instructions see the guide from Microsoft.