Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexandroPerez/c6122b1f8648273f83b894e5c4436e6a to your computer and use it in GitHub Desktop.
Save AlexandroPerez/c6122b1f8648273f83b894e5c4436e6a to your computer and use it in GitHub Desktop.
Sync from windows host to vagrant box using cygwin, rsync and inotifywait

Requirements

You need to have cygwin and its following packages installed

  • make (Devel group -> make)
  • rsync (Net group -> rsync)
  • ssh (Net group -> openSSH)

NOTE: You may already have ssh installed due to git shell, or other shell installation, but make sure you install the cygwin version of ssh (openSSH)

If you already have cygwin installed, but not the packages, all you have to do is run the installation again, and select the packages when asked. If it's the same version of cygwin, ti will only install the packages.

Setup Cygwin to ssh to Vagrant

Once you have met the requirements, you need to setup Cygwin so that it can ssh into your vagrant machine using the command ssh. We usually access our machine using vagrant ssh, but that won't let us use rsync which relies on ssh to connect to a remote server, in this case being our Vagrant machine.

Get your Vagrant ssh-config

In cygwin use the command vagrant ssh-config in the root folder of your vagrant machine (where the Vagrantfile is located) to get the necessary ssh configuration:

$ vagrant ssh-config

#OUTPUT:
Host default
  HostName 127.0.0.1
  User ubuntu
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/Workspace/udacity/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Copy the output and paste it in your ~/.ssh/config file as follows:

Host udacity
  HostName 127.0.0.1
  User ubuntu
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/Workspace/udacity/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

In this example I changed Host default to Host udacity to match the name of the vagrant machine, and to allow me to ssh into the vagrant machine using the command:

$ ssh ubuntu@udacity

If everything goes as expected, you should be able to login into your machine using the command ssh from cygwin.

Install inotify-win

repository: https://github.com/thekid/inotify-win

inotify-win is a port of the inotifywait tool for Windows.

clone and compile

In cygwin, clone the inotofy-win repo, cd into it and then run make to compile it:

$ git clone https://github.com/thekid/inotify-win.git

$ cd inotify-win

$ make

#OUTCOME:
inotifywait.exe

rename and move to a folder found in $PATH

I renamed inotifywait.exe to inotifywait and then moved it to the /usr/loacl/bin/ folder in cygwin (this is the first path in the $PATH variable). You can place the executable anywhere, as long as it's available in $PATH. The shell script bellow needs access to inotifywait

Get the Shell Script for Cygwin

I've created a shell script intended for Cygwin called autosync.sh.

Get it here: https://gist.github.com/AlexandroPerez/b749ed7c1255961794fb1f0d804fbe32

Inspect the code, download it and test it after this.

Important to .syncignore!

You'll need to make sure to add paths to .syncignore that you don't want deleted from your remote vagrant machine. rsync will delete in remote anything that's not in the source. To avoid this you'll have to add individual files and folders you want excluded to .syncignore.

If you wan't to help providing .syncignore examples for whatever work you perform, post a comment.

@AlexandroPerez
Copy link
Author

My .syncignore when I work with node.js and gulp

node_modules
build
npm-debug.log
.git
.syncignore
package-lock.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment