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.
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
This is where it gets fun. Before we begin make sure you have the vagrant plugin
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.
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.
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.
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
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!
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!