Losing data in Vagrant after restart or destroy

In my opinion, the answers to your questions depend on how you set up the project. On a project that I work on our set up is the following (simplified):

  • database server (cloud hosted)
  • origin server (cloud hosted)
  • vagrant box for local development
  • git repository
  • ...

On vagrant init each developer gets a copy of the environment which is a ubuntu server. All application code (website) is mounted into the virtual machine from your local file system. We keep this application in the git repository. Then we keep a few scripts inside the vagrant machine that synchronize the database from the production (or demo server) - basically it downloads the database dump file and places it into your virtual machine.

In that case, if you do vagrant destroy and then vagrant init you need to run the database sync script to get the databse back. Our developers (mostly) don't change the content of the database so we don't need to keep the local changes.

I hope that this helps a bit.


Homestead already added DB Backup feature, If you have Vagrant 2.1.0 or above than all you have to do is add this to Homestead.yaml

backup: true

Once configured, Homestead will export your databases to mysql_backup and postgres_backup directories when the vagrant destroy command is executed. Readmore


The top answer is pretty good, I just wanted to add to your third question.

To keep your changes, you should keep the database on your host machine, and have vagrant connect to it to use it. This way you won't lose the database on vagrant destroy.

You're probably already using NFS to sync the folders with the code, so this is just a step in a similar direction.