Skip to main content

WinSCP - Synchronize host and remote

ยท 2 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

WinSCP - Synchronize host and remote

By the use of a script, it's possible to ask WinSCP to synchronize your host and remote machine i.e. if a file is newer on the host, copy it to your remote server and the opposite.

If a file has been added to your host, copy it to your remote server and vice versa.

I'm using such script for making a full backup of some of my folders to my Synology.

The scriptโ€‹

option batch abort

option confirm off

open ftp://USERNAME:PASSWORD@HOST_OR_IP:PORT/

option transfer ascii

lcd "C:\Christophe"
cd /Christophe

synchronize remote -preservetime -transfer=automatic -filemask="|.git/"

close

exit

How to useโ€‹

  1. Save the previous script as, f.i., c:\temp\synchronize.txt
  2. Edit the script and specify:
    1. in case of need, replace ftp by sftp
    2. USERNAME: the FTP username
    3. PASSWORD: the password associated to this account
    4. HOST_OR_IP: the FTP host name or his IP
    5. PORT: the port to use (21 for a FTP connection f.i.)
    6. The local folder where your files are stored (line lcd "C:\Christophe")
    7. The remote folder from where the files should be copied (line cd /Christophe), can be the ftp root or any subfolder
  3. Start a DOS session
  4. Run cd \temp
  5. Run winscp.com from there: type "c:\program files (86)\WinSCP\WinSCP.com" /script="c:\temp\synchronize.txt"

If everything is correctly set up, WinSCP will start a session terminal and will start the synchronization.

More infoโ€‹

More info about the Synchronize verb of WinSCP: https://winscp.net/eng/docs/scriptcommand_synchronize.

  • If files/folders are already there, don't do anything.
  • If there are new files/folders, copy them.
  • If files/folders are no more on the local drive, remove them from the remote server.

So local is the master.

Local / Remote / Bothโ€‹

See the line synchronize remote.

Choose remote, local or both:

  • When the first parameter is local, changes from remote directories are applied to local directories.
  • When the first parameter is remote, changes from the local directories are applied to the remote directories.
  • When the first parameter is both, both local and remote directories can be modified (source).