Use Winscp to sync files from Windows to Linux

Recently I upgraded my work laptop to Windows 7. At that time I didn’t want to use the previous sync methods that I have blogged about. I wanted to use something simpler (read easier to install and maintain between different machines). After doing some research I settled on using winscp. Winscp supports folder sync operations through a command line. Winscp takes a simple text file listing the commands that it is to execute. This process can be automated on Windows using batches, one to pull changes and the other to push changes.

Create a text file called ‘pull_changes.txt’ and add the following code:

#Pull changes from the remote folder to the local folder
#http://winscp.net/eng/docs/scriptcommand_synchronize



#open a connection to the server specifying the name of the server

#open scp://server.home.com:3687 -privatekey=C:\location\to\private\key.ppk



#open a connection to the server using a saved winscp session
 
open troy@server.home.com -privatekey=C:\location\to\private\key.ppk


#local folder: C:\Users\troy.williams\Documents\home sync

#remote folder: /home/troy/home sync



# Synchronize my folders, pulling changes from the remote to the local

synchronize local "C:\Users\troy.williams\Documents\home sync" "/home/troy/home sync"



#close the session

close



#exit the scripting environment

exit

Here is the push script, save the lines to a text file called ‘push_changes.txt’:

#push changes from the local folder to the remote folder
#http://winscp.net/eng/docs/scriptcommand_synchronize



#open a connection to the server specifying the name of the server

#open scp://server.home.com:10000 -privatekey=C:\location\to\private\key.ppk


#open a connection to the server using a saved session

open troy@server.home.com -privatekey=C:\location\to\private\key.ppk


#local folder: C:\Users\troy.williams\Documents\home sync

#remote folder: /home/troy/home sync



# Synchronize my folders, pushing changes from the local to the remote

synchronize remote "C:\Users\troy.williams\Documents\home sync" "/home/troy/home sync"



#close the session

close



#exit the scripting environment

exit

Here is an example of a simple batch file that can be used to execute either of the winscp command files:

@rem --------------------------------

@rem created 2011-08-08 copyright (c) 2011 Troy Williams

@rem This file will pull changes from my server at home



@ECHO OFF



SET WINSCPHOME=C:\Program Files (x86)\WinSCP

SET CWOLDPATH=%PATH%

SET PATH=%WINSCPHOME%;%PATH%



echo Pulling changes from the server



winscp.com /script=pull_changes.txt



pause

2 thoughts on “Use Winscp to sync files from Windows to Linux

  1. Mathiau says:

    great clean layout, i went through tons of pages trying to get something simple that works and so many sites just lack detail or clear instructions

  2. Thanks! I had the same problem and came up with more complicated solutions using rsync and ssh on windows. I even have a post or two about it. Using winscp is a simple, easy to explain solution that works well on windows. If I was copying files from linux to linux, ssh and rsync is the way to go.

    Troy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s