Synchronizing files from a computer to a remote server in real time in essence means that anytime files are created, updated or deleted, they are instantly uploaded to the remote server or deleted from it. This can prove to be beneficial in a number of cases including storing remote copies of your photos and videos, deploying application changes as part of continuous integration and deployment processes, and sharing files with 3rd parties automatically.

Dropbox, Box, Egnyte are just some of the many products that handle these processes end-to-end. They provide a client that tracks changes on your file system, the remote storage required, and the means to access and share files on the remote storage.

Syncing files to an SFTP server tends to be a favorable solution for many enterprises since it’s generic and is easy to automate, and because files can be easily and securely accessed with simple GUI or command line clients as well. Keep reading on if you would like to follow through with just that and learn how to synchronize your files on a Windows computer or server with a remote SFTP server.

Syncplify.me AFT!

We stumbled upon Syncplify.me AFT at a time when a mutual customer was synchronizing files on their server to SFTP To Go and encountered an issue. We admired Syncplify’s approach to resolving the problem and so we decided to give it a try ourselves!

AFT stands for automated file transfer and its sole purpose is to give the user complete control over automating file transfer, using the aftJS language, an extended version of JavaScript, that permits the user to write simple scripts to monitor file system changes, connect to remote servers, upload, download and delete files. It is equipped with pre-packaged scripts to help you get started quickly. The scripting language is also powerful enough to give you full control over the processes. For instance, if you would like to create a new file on the remote server each time a file changes or move files on the remote server when they're deleted locally, instead of overwriting the previous version, it's all possible.

Another one of its advantages is its ability to be used cross platform, on both Windows and Linux.

An example sync script that keeps deleted files intact on the remote server goes like this:

{
  // Let's enable console feedback, in case we're running this script via the shell
  ConsoleFeedback = true;

  // First, let's create the file-system watcher
  watcher = new FsWatcher();
  // Then we elect to delay notification by *at least* 300 seconds (5 minutes)
  // (useful to allow the file system to finish whatever operation is ongoing)
  watcher.DelayBySeconds = 300;
  // We may choose *not* to be notified of certain events
  watcher.NotifyRename = false;
  watcher.NotifyRemove = false;
  watcher.NotifyChmod = false;
  // We can specify inclusion and exclusion filters (optional, not mandatory)
  watcher.InclusionFilter = ['*.*'];
  watcher.ExclusionFilter = ['notes.txt', 'budget.xlsx'];
  // Almost ready, let's tell the object what folder we want to monitor
  watcher.WatchDir('C:\\TestFolder', false);
  // And then start the watcher
  watcher.Start();

  // We need to keep checking events indefinitely, an endless cycle is what we need
  while (true) {
    // Let's sleep for 500 milliseconds at each cycle, to keep CPU usage low
    Sleep(500);
    // When inside an endless cycle, it's always safe to check if we received a Halt signal at every cycle
    if (HaltSignalReceived()) {
      break;
    }
    // No Halt signal? Good, then let's acquire the list of pending event that we need to process
    evt = watcher.Events()
    // Do we have at least 1 event to process?
    if (evt.length > 0) {
      // We only connect to the server IF there are events to be processes
      var scli = new SftpClient();
      scli.Host = 'sftphost.com:22';
      scli.User = 'your_username';
      scli.Pass = 'your_password';
      scli.Options.UploadPolicy = AlwaysOverwrite;
      if (scli.Connect()) {
        // Cycle over all pending events...
        for (var i = 0; i < evt.length; i++) {
          if (evt[i].Event == 'WRITE') {
            // If it is a WRITE event (new or modified file) let's upload it to the server
            scli.UploadWithPath(evt[i].Object, '/DestinationPath', 1);
          }
        }
        // Do not forget to close the connection
        scli.Close();
      }
      // Set the client object to null to save memory
      scli = null;
    }
  }

See original script here

You can download and check out Syncplify.me AFT’s pricing plans here.

Cloud FTP with maximum security and reliability
SFTP To Go offers managed cloud storage service - highly available, reliable and secure. Great for companies of any size, any scale.
Try SFTP To Go for free!

WinSCP

WinSCP is a well known free SFTP client for Windows. It can also be used to automate processes with its many command line interface options and its .Net Assembly library that can be used in PowerShell and various other .NET languages.

As opposed to Syncplify.me AFT, WinSCP is not platform agnostic and only works on Windows, probably due to it originally being a port of scp - the Linux secure copy command. It also doesn’t allow you to have complete control over the sync process , but the command line argument keepuptodate will most likely keep you covered in most use cases with its various options.

An example command line used to open an SFTP session and sync a directory to it would look like this:

C:\path_to_winscp\WinSCP.exe /console /ini=C:\path_to_config\WinSCP.ini /command "option batch continue" "open sftp://your_username:your_password@sftphost.com:22" "keepuptodate C:\Test_Folder /DestinationPath' -filemask=|notes.txt;*.tmp"

Download WinSCP here or read more about the keepuptodate option here.

To automate syncing using WinSCP, use the task scheduler:

  1. Open Task Scheduler: Press Win + R, type taskschd.msc, and press Enter.
  2. Create a task: click on "Create Basic Task..." in the actions pane on the right and name it.
  3. Set the trigger: Choose "Daily", set the "Recur every" field to 1 day and click Next, then click "Repeat task every" and select 5 minutes for the dropdown. Set the "For a duration of" field to 1 day.
  4. Set the action: Choose "Start a Program", in the "Program/script" field, riwse to the folder where WinSCP is installed and select WinSCP.exe. In the "Add arguments (optional)" field, add the the following:
    /console /ini=C:\path_to_config\WinSCP.ini /command "option batch continue" "open sftp://your_username:your_password@sftphost.com:22" "keepuptodate C:\Test_Folder /DestinationPath' -filemask=|notes.txt;*.tmp" (make sure to change the paths to the configuration file and target folder, and the SFTP endpoint and credentials you use).
  5. Click "Next" and "Finish to create the task initially.
  6. Set the task not to run concurrently: Find the newly created task in the Task Scheduler Library, right-click it and select "Properties". Go to the "Settings" tab and check the option "Do not start a new instance" under the "If the task is already running, the the following rule applies" section. Click OK to save your changes.

Cloud FTP with maximum security and reliability
SFTP To Go offers managed cloud storage service - highly available, reliable and secure. Great for companies of any size, any scale.
Try SFTP To Go for free!

Post photo by Vincent Branciforti on Unsplash