Search code examples
sshsynchronizationsftpwinscp

Continuously sync changes from web server


I'm searching for a way to get my Files synchronized (task) from a web server (Ubuntu 14) to a local server (Windows Server). The web server creates small files, which the local Server needs. The web server is in a DMZ, accessible through SSH. Only the local server is able to access folders on web server. It tried using Programs like WinSCP, but I'm not able to set a "get"-Job.

Is there a way to do this with SSH on Windows server without login every few seconds? Or is there a better solution? In the Future Web-Services are possible, but at the moment I need a quick solution.


Solution

  • Either you need to schedule a regular frequent job, that connects and downloads changes.

    Or you need to have continuously running process, that keeps the connection opened and regularly watches for changes.

    There's hardly a better solution (that's still quick and easy to implement).

    Example of continuous process implemented using WinSCP .NET assembly:

    // Setup session options
    SessionOptions sessionOptions = new SessionOptions {
        Protocol = Protocol.Sftp,
        HostName = "example.com",
        UserName = "user",
        Password = "mypassword",
        SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
    };
    
    using (Session session = new Session())
    {
        // Connect
        session.Open(sessionOptions);
    
        while (true)
        {
            // Download changes
            session.SynchronizeDirectories(
                SynchronizationMode.Local, localPath, remotePath, false).Check();
    
            // Wait 10 seconds
            Thread.Sleep(10000); 
        }
    }
    

    You will need to add a better error handling and reconnect, if connection breaks.

    If you do not want to implement this as (C#) application, you can use PowerShell script. For a complete solution, see Keep local directory up to date (download changed files from remote SFTP/FTP server).