How to Backup and Restore all Databases in Laravel Homestead

2018-02-27 Laravel

When you use Homestead, all files you work with are stored physically on your local directory that is mapped through Homestead.yaml file. You simply don’t need to worry about these file when you have to destroy the box itself.

The problem s with corresponding databases. All databases live within the box, so destroying it means that you will loose all your databases. The solution is to backup all Homestead databases at one go, store the dumped SQL file outside of the box and then import it into new box. Open Terminal and ssh to your Homestead box. Next cd to the folder you mapped in the YAML file. In my case it was a directory Code, it lives in my home directory.

Now run:

mysqldump -u homestead -psecret --all-databases > 2018_02_27_all_db_backup.sql

The backup file will land on your local drive obviously.

To restore all databases in new Homestead box, simply ssh into it and type:

mysql -u homestead -psecret < 2018_02_27_all_db_backup.sql