HowTo: Auto synchronise files upon network connection (Windows)

TestLab.grid-6x2There are times where we need to work away from the office network and the safety of backup infrastructure.  What would be nice is to automatically synchronise files on my laptop whenever I connect to the office network.

Working locally on laptops, it should go without saying, carries a danger of loosing data either through “Doh!” moments or through hardware failure (travelling can indeed be hard on the little grey rectangular friends of ours) so we need a solution that provides some safety to our data – preferably without us having to remember to do anything.  Holding back the urge to write something bespoke for once I looked at what I already have my dispersal –  GIT, RoboCopy and Task Scheduler to the rescue – Let’s start auto synchronising our files upon connecting to a given network

Bring me synchronisation solutions

So I want a solution that gives me the security of having my data in a location other than my laptop – preferably with that location having backups of it’s own.  Carrying and using USB keys for backups are not an option – keys get lost and do not provide the file security I require.  I needed a solution that synchronises data on my laptop with an file store on the office network .  More over, i want this to happen automatically – I want something to detect when I am on the office network (either physically attached or over VPN).

Enter stage left : Task Scheduler

Turns out you can create a task (in task scheduler) that gets triggered when a specific event is raised by windows. There are a whole host of events to which you can attach but the one I am interested in lives under Microsoft-Windows-NetworkProfile/Operational > Network Profile with the Event ID of 10000.  So let’s create a simple task that gets triggered by this event. To test this event out we will just launch a command prompt window.

  1. Open Task Scheduler (open Start Menu, Type Task)
  2. Right click on the Task Scheduler Library and select Create Basic Task
    1
  3. Create a Basic Task that
    1. Specify a name for your task (click Next)
    2. Specify the trigger for your task as “When an specific Event is Logged” (click Next)
    3. Select Microsoft-Windows-NetworkProfile/Operational from the Log menu
    4. Select Network Profile from the source
    5. Type 10000 in the event ID field (click Next)
    6. Select “Start a program” from the Action options list
    7. Type cmd into the “Program/Script” field (click Next
    8. Check the “Open the Properties dialog …” checkbox and click FinishCropperCapture[6]
  4. Switch to the Conditions tab and select from the network list the network which, upon connecting, you want the application to run.
    3
  5. That’s it !

 

Disconnect from the network in question then reconnect – if you have configured everything correctly a command prompt window should launch automatically.

 

Synchronise with RoboCopy

Robocopy offers us the easiest way to synchronise two folders.  We can change the task we created earlier to invoke the following command

robocopy \\SourceServer\Share \\DestinationServer\Share /MIR /ZB /XA:H /W:5

Here is an explaination of the options I chose:

  • /MIR specifies that robocopy should mirror the source directory and the destination directory. Beware that this may delete files at the destination.
  • /ZB ensures robocopy can resume the transfer of a large file in mid-file instead of restarting.  If that fails, try  using the backup method instead
  • /XA:H makes robocopy ignore hidden files, usually these will be system files that we’re not interested in.
  • /W:5 reduces the wait time between failures to 5 seconds instead of the 30 second default.

Synchronise with GIT

I want to bring some change mangement / version control to the files I work upon on my laptop – the obvious answer was to use a distributed version control system such as GIT.

Using GIT for version control

  1. Initialise a ‘bare’ repository within your office network share folder (in my case H:\Projects).  It is easiest in the long run if the folder is empty to begin with.
  2. Clone your office network share repository to your local drive (in my case C:\_Work)
  3. Commit any files you have locally in your working directory

Now we have a cloned repository we can commit changes locally as often as we desire, view file change history and even restore to any saved version we like.  But those changes are only local – in order to get to the desired state of having a version existing on the office network share we need to push all changes to the server.  We will modify our scheduled task to execute a “GIT PUSH” whenever we connect to the network.

Create a BAT file with contents along the lines of

@echo off
c:
cd \_Work
git add -A && git commit -am “AutoSync commit”
git push

Change the scheduled task you created earlier to execute this BAT file.  Now every time you connect to the network the task will execute a GIT PUSH command that synchronises all your changes and change history to the server.  Bosh!