Mac – How to automatically upload code changes on a remote VM

sftpsshsyncvirtual machine

My current works involves working on scripts running on a Ubuntu VM.

For multiple reasons, I'm editing and committing project files on a local computer running Windows, and running them on a remote server through a SSH connection (using MobaXTerm). My build process when developing is roughly as follows:

  • Write the changes in my local text editor.

  • Open the right directory in MobaXTerm's SFTP UI.

  • Click "Upload file", and select the right directory and file in the Windows file browser.

  • Run systemctl restart apache2.service or whatever I need on the VM.

(obviously the build process for production builds is much more thorough and automatized)

Steps 2 and 3 feel pretty redundant. They're repetitive, easy to forget, easy to get wrong, and I'm generally giving the computer information it should know better than me.

Is there a standardized, safe way to automatically synchronize a local folder with a remote SSH folder from Windows?

Essentially, what I want is some utility that goes File myDirectory/foo/bar/X has changed! Uploading file X to someUrl/theirDirectory/foo/bar/X with minimum configuring.

Best Answer

There is no "standardized way".

You have these options:

  • Use a tool that can watch for changes in a local directory and reflect them on a remote SFTP directory.
  • Schedule a frequent synchronization of a local folder against a remote folder.
  • There are (commercial) tools that can map a remote SFTP folder to a local drive. Using these you can directly work with/edit remote files using any local application.
  • Some editors allow direct editing of files on an SFTP server.

You can implement the first two options with WinSCP SFTP client.


Watching for changes

Use "Keep remote directory up to date" function of WinSCP.

It can be used both in command-line/console mode using the keepuptodate command, like:

winscp.com /ini=nul /log=c:\writable\path\to\synchronize.log /command ^
    "open sftp://username:password@example.com/" ^
    "keepuptodate C:\local\path /remote/path" ^
    "exit"

Or in a graphical/GUI mode. You can launch the graphical mode in WinSCP GUI (after logging in) or from a command-line using the /keepuptodate switch like:

winscp.exe sftp://username:password@example.com/ /keepuptodate C:\local\path /remote/path

Scheduling

To synchronize changes in a local directory to a remote directory, use the WinSCP synchronize script command from a batch file like:

winscp.com /ini=nul /log=c:\writable\path\to\synchronize.log /command ^
    "open sftp://username:password@example.com/" ^
    "synchronize remote C:\local\path /remote/path" ^
    "exit"

And schedule the batch file to be run frequently using Windows scheduler.

For details see also a guide to automating file transfers (or synchronization) to SFTP server.


(I'm the author of WinSCP)

Related Question