syncd - simple solution for synchronizing changes to your vagrant box

11.04.2014,

vagrant is great when it comes to pre-configuring reproducible, and portable development environments for Drupal - in particular in combination with Ansible. However, with sites of a decent size, the performance of Drupal projects running on the NFS share vagrant uses for shared folders has become more and more of an issue - at least with Virtualbox. Consequently, I've been looking into a simple, pre-configurable solution that automatically syncs changes into the vagrant box, such that the files can be served from the regular VM filesystem instead. I ended up writing a simple script that watches the project folder for file system changes via inotify and uses rsync to sync the changes to the vagrant box.

You can find the script together with some installation instructions on Github - right now it runs on Linux-based systems only due to its use of notify. More details about the script can be found in a separate blog post here.

For using the script in conjunction with vagrant, I can recommend the vagrant-triggers plugin for automatically running the script when the box is launched. For that, just place a copy of the syncd.conf file coming with the script besides your Vagrantfile and be sure to update its configuration, so it suits your environment. Then you can configure vagrant-triggers to run it by adding something like the following to your Vagrantfile:

  1. config.trigger.after [:up, :resume, :provision], :execute => "syncd run", :stdout => false, :force => true
  2. config.trigger.after [:up, :resume, :provision], :execute => "syncd start", :force => true
  3. config.trigger.before [:reload], :execute => "syncd stop", :force => true
  4. config.trigger.after [:reload], :execute => "syncd run", :force => true
  5. config.trigger.after [:reload], :execute => "syncd start", :force => true
  6. config.trigger.before [:suspend, :halt, :destroy], :execute => "syncd stop", :force => true

That's it - syncd will be launched and stopped automatically with your vagrant box now!

For the rsync approach to work out nicely in conjunction with Drupal, we are still configuring our vagrant boxes to share the files directory with the VM and exclude it from the sync operation. So the updated files can be easily inspected on the development machine, while the VM is in control of its content. Everything else, all code updates or settings.php are handled via the development machine and/or version control.

Altogether, switching to the auto-rsync approach made our Drupal installations running on vagrant boxes way faster and smoother to work with.