After Vagrant Crash How To Recover Database

It becomes more common for businesses to use vagrant as single virtual machine solution in house that can be shared among developers that way ensuring every developer has the same version of services, frameworks and libraries running. It also saves developer time for upgrades and maintaining their development sandbox.

In short, vagrant manages the configurations defining what ports forwarded, memory size and others configuration including which virtual machine to load. The actual virtual machine is provided by Virtual Box technology and is available as separate entity.

Sometime it happens that vagrant crashes and hangs at the load process. For me, i got the following error message:

[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.

The logs are available in your Virtual Box folder or by default these images are in your home directory ‘VirtualBox VMs’ folder

Which Virtual Box image corresponds which Vagrant config?

In case you have more than one vagrant configurations or multiple virtual box images on your machine. To find out the particular Vagrant configuration pointing to which virtual image, go to the vagrant configuration folder and look at ‘.vagrant’ file. You will see something like:

{"active":{"default":"79a80fbd-2804-4dde-abd8-c23bb18a9b9a"}}% 

That is the UUID of the Virtual Box image to load from this vagrant configuration.
Next, how do you know what is the UUID for particular Virtual Box image. Go to the folder of your VirtualBox images and run:

VBoxManage list vms

This will display each VirtualBox name with its UUID

If your Vagrant crashes, perhaps, instead of spending time troubleshooting you would just load new instance. if so, you may still need the data from database of the old virtual image.

Step 1: Access VirtualBox image directly
With ‘VirtualBox’ software, you can run any Virtual box image you like. Once you start, the default login:

username:vagrant
password:vagrant

Step 2: Add Shared folder to access the mysql dump file
To add shared folder, go to ‘Settings’->Shared Folder and select folder(i.e.share) on your machine itself
Next mount that folder as following

sudo mount -t vboxsf share ~/host

Where the ‘share’ is the folder you specified in share folder settings above and the ‘~/host’ is the folder on the virtual image
Note: make sure your permissions set on virtual machine and your original machine accordingly
Step 3: Take mysql dump file
To take the back up, got the new mounted folder and run mysqldump command as follows:

mysqldump -u root -p nameOfDatabase>backupFile.sql

This will create the back up file and save into folder outside the virtual image that you have direct access. Afterwards, you can share this folder with another virtual image to import the database.

Useful links