Synchronize Files in Realtime from a Windows PC to a Linux Box
A little while ago I set up a local test Linux VM that is nearly identical to my production web server. Because I like to develop on a Windows machine using PhpEd as my IDE of choice, it can get quite cumbersome to keep SSH’ing files over to the Linux box to test in my true environment.
I searched around the web for various syncing utilities that would keep my local files synced to my Linux box in realtime. The one I finally landed on is WinSCP. WinSCP has a feature that allows you to keep a remote folder synced to one on your Windows PC in realtime called “Keep Folder Up To Date” – and even better yet it can be automated from the command line or batch script.
Here’s how I managed to create a simple batch script that automatically synchronizes my folders when I run it, then continually monitors it for changes.
If you don’t already have it, grab yourself the latest copy of WinSCP from here.
Create A Saved Session
After you’ve installed WinSCP, fire it up and create yourself a new stored session. Since this is only a local test box and nobody can get at it because I’m behind a router, I have no problem storing the password in the saved session (it’s obviously not the same password as my production server). When I saved my session, I called mine
ubuntu (remember this for later).
Once you have that saved, go ahead and click the Login button once so WinSCP stores your encryption key. Next go ahead and close WinSCP.
Create A Batch File For Easy Syncing
While we can use the program’s “Keep Folder Up To Date” feature from the GUI, it’s much easier to automate this task using a simple batch script. The script will perform an initial sync, after which it will continuously monitor our folder for changes and upload them.
Here’s what the simple one line batch script looks like (let’s call it
START /B C:\Path\To\WinSCP.exe "ubuntu" /console /command "synchronize remote C:\Local\Folder\To\Sync /var/www/remotefolder -delete -nopermissions -criteria=time -filemask=|.svn/;tmp/;cache/" "keepuptodate C:\Local\Folder\To\Sync /var/www/remotefolder -delete -nopermissions -filemask=|.svn/;tmp/;cache/"
What’s going on here? Let’s break it down:
START /B– This is telling the command line to launch our executable and not wait for it to exit. If we didn’t include the
/Bswitch the batch script would stay open until we closed the executable.
C:\Path\To\WinSCP.exe– This is simply the path to the WinSCP executable.
"ubuntu"– Here we specify the name of the saved session to use. Because I saved my session earlier with this name, it’s the value I pass here.
/console– Tell WinSCP to launch in console mode.
/command– The command switch is used to tell WinSCP which commands to execute. If you notice in our script, we’re actually executing two separate commands. Each command is enclosed in double quotes.
"synchronize remote C:\Local\Folder\To\Sync /var/www/remotefolder -delete -nopermissions -criteria=time -filemask=|.svn/;tmp/;cache/". Let’s break this down further:
synchronize– First we want to perform a sync so our remote folder is up to date.
remote– This can be one of three parameters. When the parameter is
local, changes from remote directory are applied to local directory. When the parameter is
remote, changes from the local directory are applied to the remote directory. When the parameter is
both, both local and remote directories can be modified. Because we want our local changes to be reflected on the remote sever, we use
C:\Local\Folder\To\Sync– The folder that contains our local files to be uploaded to the remote server.
/var/www/remotefolder– This is the folder that will be kept up to date with our local copy.
-delete– This will delete remote files that we have deleted from our local copy. If you don’t want this to happen then you may remove this option.
-nopermissions– Use the default remote file permissions.
critera=time– The criteria that will be used for file comparison. We will use the last modification time.
-filemask=|.svn/;tmp/;cache/– WinSCP allows you to specify a filemask to include or exclude specific types of files/directories. Here I’ve chosen to ignore
cachedirectories. You can modify this to your liking and read more about the filemask here.
"keepuptodate C:\Local\Folder\To\Sync /var/www/remotefolder -delete -nopermissions -filemask=|.svn/;tmp/;cache/"– This is almost identical to our last command, all the parameters are the same except the
keepuptodatecommand. This will start the monitoring process and automatically upload any changed files to our remote server.
Syncing Multiple Folders
You basically have two options for syncing more than one “root” folder at a time. You could duplicate the above line and include it in the same batch script so both are folders are kept up to date at the same time – or – create separate batch scripts for each. I have chose to create separate batch scripts for each of my websites since I’m usually only working on one at a time – but the choice is up to you!