Zach Adams Web Developer and Programmer

Keep Varying Vagrant Vagrants (VVV) in Sync across multiple computers

July 15, 2015

Varying Vagrant Vagrants (VVV) is a vagrant box designed for WordPress developers to setup a fully-functioning and fully-equipped local development server that can easily manage multiple WordPress sites. VVV is an awesome addition to any WP developers workflow, however one of the problems I had with it was its inability to sync files and databases across multiple computers. This meant that VVV was stuck on the computer it was installed on, and I needed to be able to work with VVV on my work computer and my laptop.

Syncing Files

It turns out this was the easy part. All you need to do is symlink your VVV directory to whatever syncing program you happen to be using (Dropbox, Google Drive, etc.). You can also move your entire VVV directory into Dropbox, but I prefer a symlink to keep the separation. You can symlink the folder with the following command:

ln -s ~/vagrant-local ~/Dropbox/vvv

Syncing Databases

This is where it gets fun. Before we begin make sure you have the vagrant plugin vagrant-triggers installed.

So VVV automatically runs a script called db_backup on halt, which backs up all the databases into the folder database/backups. We’ll create a script to copy these backups to a sync folder, and since Dropbox/Google Drive/etc. syncs your folders it will automatically detect and sync these .sql files for us. First we’ll need to edit the Vagrantfile.

Vagrantfile

We want to tell VVV that on vagrant up we want to import all backed up databases back into VVV. Since we have the vagrant-triggers plugin installed we can tell VVV to run a script on up. Open your Vagrantfile and near the bottom there’s a section for Vagrant Triggers that looks like this:

  if defined? VagrantPlugins::Triggers
    config.trigger.before :halt, :stdout => true do
      run "vagrant ssh -c 'vagrant_halt'"
    end
    config.trigger.before :suspend, :stdout => true do
      run "vagrant ssh -c 'vagrant_suspend'"
    end
    config.trigger.before :destroy, :stdout => true do
      run "vagrant ssh -c 'vagrant_destroy'"
    end
  end

Add the following snippet directly below the if statement:

 config.trigger.after :up, :stdout => true do
   info "Importing databases..."
   run_remote "bash /srv/database/sync-sql.sh"
 end

This tells VVV to run the sync-sql script on up, which we’ll create next. This section should now look like this.

sync-sql.sh

Now we’ll create a script based on the import-sql.sh file located in the database folder. Go ahead and create a file called sync-sql.sh in the database folder and copy/paste the the script located here. The script is commented so you can see what it’s doing.

vagrant_halt_custom

Now we want to tell vagrant that during the halt command we want to copy the backups it’s made into the sync folder. Copy the script here and create a file called vagrant_halt_custom in the config/homebin folder.

Next you have to run vagrant provision to make sure all the files get copied to the correct locations.

The last thing we want to do is ssh into the vagrant box with vagrant ssh and run the following commands so that vagrant can run the script:

sudo chmod 755 ~/bin/vagrant_halt_custom
sudo chmod +x ~/bin/vagrant_halt_custom

Give it a try!

Run vagrant halt on computer 1 and make sure that the database/sync folder is created and that the backup .sql files are copied. Wait for your sync program to copy over the .sql files and run vagrant up on computer 2 and make sure the sync-sql script runs and refreshes the databases!

If you’ve done everything right you should now be able to sync your files and your databases across multiple computers! Let me know if you encounter any problems!

Comments

Tony says:
August 11th 2015 at 12:00am
Hi Zach,This looks great. Have you had any issues so far? I've read a couple articles about people using Dropbox to sync to computers and there were occassional issues with the database syncing.Thanks for sharing this!
Dave van Hoorn says:
November 29th 2015 at 12:00am
Hi Zach,Thanks for the tutorial. Please keep in mind that Google Drive doesn't support symlinks the way you're describing it in the first step. It does work the other way around: put all the files in your Drive directory and symlink to another folder.Cheers
Brett says:
February 6th 2016 at 12:00am
Instead of using dropbox or google drive. Is it possible to have it sync to a repository on Bitbucket?The problem my co-worker and I are running into is trying to keep the wordpress database in sync while we both work on a wordpress site.
Justin Peacock says:
August 19th 2016 at 12:00am
Hey Zach,This has been the best solution I've found so far but an issue I'm running into right now is.When you halt machine 1 then start machine 2 I'm getting the following. ```A VirtualBox machine with the name 'vagrant-local' already exists. Please use another name or delete the machine with the existing name, and try again.```Is there a vagrant config that needs to be specific to each machine? Any help is appreciated. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *