This website stores cookies. Click here to accept them.cookie information page

File synchronisation using winscp

10th February 2012
Categories: backups, winscp

If you're responsible for maintaining a heap of files or databases and making sure you don't lose all trace of them when your server goes down, or you just want to make sure that your desktop PC is or laptop is backed up regularly, there are a few ways to go. 

If you need version control, there are applications like 'Subversion' which are designed for managing code, but can manage any type of file and give you a full version history.  You have to commit changes manually, but using "Tortoise SVN" gives you a red cross against any files you haven't yet committed.  If you don't want to set up your own Subversion server, companies like Codesion make it easy for you.

If what you really want is continuous backup, you should be looking at services like Dropbox or Mozy. These companies give you a small program to install on your computer which handles backups continuually; with dropbox it manages one folder; with other services you can specify multiple. Dropbox, however, gives you 2GB free to play with at the start.

However, if you have your own FTP or SFTP site and you don't need version control or continuous backup, WinSCP offers a little-known but free of charge solution.  

WinSCP is a file transfer program, but it comes with a powerful scripting language, including commands like synchronize (full list here).

You can run winscp on a command line and tell it to go and execute a script (which means that you can schedule said command using the Windows scheduler)

Your script can be as simple as this:

open serverBob
synchronize local C:\Backups\bob /home/bob/myfolder

(which opens a saved, named session (use the normal WinSCP window to set this up first), and copies remote files to your hard disk)

or this:

open serverBob
synchronize remote C:\Backups\bob /home/bob/myfolder

(which does the reverse).

Save your scripts as text files, and to call your scripts, just run winscp.exe /script=yourscript.txt (where yourscript.txt is the path to your script).

Happy copying!

[Edit, 14 May]

I just got caught out by the following: if you give your scheduled tasks your own user name and password, you may forget to change the scheduled tasks' login credentials when you change your own password.  Far better to give the scheduled tasks their own dedicated windows user and password!

More on jQuery

3rd February 2012
Categories: javascript, jquery

Javascript, the main language that enables dynamic websites, has been around for 15 years and, whilst powerful (it enables client-side form validation and dynamic pages, it is used for google analytics and various other trackers and is used by most modern websites for one thing or another) has generally been annoying to use. Every browser has had it's own implementation of javascript, with it's own idea's of how things should be done, this lead to lots of logical statements to handle all the different browser designs:

 

if (document.layers)

{

var container = document.layers[ID].open();

container.write(“some text”)

container.close();

}

else if (parseInt(navigator.appVersion) >=5 &&navigator.appName==”Netscape”)

{

document.getElementById(ID).innerHTML = “some text”);

}

else if (document.all)

{

document.all[ID].innerHTML = “some text”

}

 

All these codeblocks do the same thing, writing the text “some text” into a specified HTML element, the only difference is browser compatiblity. The first block works in Netscape4, the second block in Netscape 5+ and the last block works in Internet Explorer 4,5,6. If you wanted to look at this script in any other browser then you would just have to hope that it worked.

That's a lot of lines of code for something so simple, writing a more complex script would end up as a nightmare of if statements. Fortunately for the best part of the new millenium a number of javascript libraries have been released, one of the most prolific of these is JQuery. Released in 2006 it is now, according to wikipedia, used by over 52% of the 10,000 most visited sites and is the most popular library currently in use. Jquery is available from http://www.jquery.com

JQuery provides a number of useful functions that simplify interacting with the webpage and handle all the nasty cross-browser compatibility issues for you. All of Jquery's functionality is provided by the jQuery() object ( or it's alias “$()” ) and most JQuery scripts will start off with a document-ready block. This is a callback passed to the ready function of the document object that is called when the page has finished loading. So, let's rewrite our previous script using JQuery:

/blogresources/jquery-hello-world.html

 

So, this script (which also contains a method to trigger it, unlike the previous example) is only 3 lines of javascript (always nice to do more with less) and, although it can look a bit daunting at first, it is a relatively simple script. It starts off with the document-ready block that I mentioned earlier. This involves creating a JQuery object that contains the document ( $(document) ), which we then call the ready function on passing it a closure (an inline function). When this function is executed (after page load), we select the DOM element that has an id of 'click-me' and call the click method. This function allows us to bind a callback, a function that will be executed whenever the target is clicked, in this instance our function (with ONE line of code) sets the HTML contents of an element to “Hello World”. Easy.

Now I should probably take a minute to reveal the wonders of the JQuery constructor. A Jquery object can hold multiple DOM objects and there are three ways of constructing them:

  • The first (and in my experience, most common) is to use a CSS type selector ('# for id, . for class etc) passed in as a string (much like we did above with the “#click-me” and “#jquery_target” selectors.

  • The second is to pass in a DOM object (like we did with the “document” constructor above) .

  • The third is to pass in a string containing HTML (for instance $('<p class=”some-text”>some paragraph content</p>') ).

One important thing to remember is that functions that change the object(s) will generally be applied to every DOM element contained by the JQuery object, whilst functions that retrieve data will generally read the data from the first element in the JQuery object. This means that, for example, if we wished to stop any of the links in our page (or a part of our page) from working (or send an ajax notification or whatever) we could do something like the following:

 /blogresources/jquery-link-selection.html 

This script, again, is very simple. Once again we have our document-ready block, this time we are binding an on-click callback to a collection of DOM elements (every A tag within the element that has an id of “link-container”). This callback retrieves the href attribute of the clicked tag and displays it in the jquery_target box at the bottom of the page. It then returns false (this is to prevent the browser from then following the link)

So thats JQuery in a nutshell. A small, yet powerful library that enables developers to write stunning web-apps and add that little bit of shine to a website that makes visitors want to come back.  

© Salient KMS Ltd 2014

Oxford House
1600 John Smith Drive
Oxford Business Park South
Oxford OX4 2JY

01865 596 144

Oxford Web is a division of Salient KMS Ltd.