WinSCP - Synchronize host and remote
ยท 2 min read
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โ
- Save the previous script as, f.i.,
c:\temp\synchronize.txt
- Edit the script and specify:
- in case of need, replace
ftp
bysftp
USERNAME
: the FTP usernamePASSWORD
: the password associated to this accountHOST_OR_IP
: the FTP host name or his IPPORT
: the port to use (21 for a FTP connection f.i.)- The local folder where your files are stored (line
lcd "C:\Christophe"
) - The remote folder from where the files should be copied (line
cd /Christophe
), can be the ftp root or any subfolder
- in case of need, replace
- Start a DOS session
- Run
cd \temp
- 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).