Laravel Homestead: How to Backup and Restore all Databases?

approximately 2 minutes of reading

When you use Laravel Homestead, all of your projects are stored physically on your host. You simply do not need to worry about these files when you destroy Homestead's box, as nothing will be affected. The problem comes with all of Homestead's databases. These are defined withing the box. If we destroy it, we are also going to loose all of the data. This article explains how to backup and restore all Laravel Homestead databases using a single command.

The solution is to:

  1. Backup all databases in one go
  2. Store the dump SQL file outside of the box
  3. Import it into new box (to test the restore process)

Open terminal app and ssh to your Homestead box. Assuming you have installed Laravel Homestead into ~/Homestead, then the following command should move you into the box:

cd $HOME/Homestead && vagrant ssh

A welcome message should appear:

Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-64-generic x86_64)

 _                               _                 _
| |                             | |               | |
| |__   ___  _ __ ___   ___  ___| |_ ___  __ _  __| |
| '_ \ / _ \| '_ ` _ \ / _ \/ __| __/ _ \/ _` |/ _` |
| | | | (_) | | | | | |  __/\__ \ ||  __/ (_| | (_| |
|_| |_|\___/|_| |_| |_|\___||___/\__\___|\__,_|\__,_|

* Homestead v13.2.1 | Thanks for using Homestead
* Settler v12.2.0


 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

0 packages can be updated.
0 updates are security updates.

Next move to the location where you store all of your projects (in my case it was ~/Code).

Backup Command

To backup all databases:

mysqldump -u homestead -psecret --all-databases > backup-`date '+%F-%T' | sed -e 's/:/-/g'`.sql

New dump should be located at ~/Code/backup-2018-02-27-22-43-25.sql. For convenience, current date and time will be attached as part of dump's name.

Restore Command

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

mysql -u homestead -psecret < backup-2018-02-27-22-43-25.sql

This is it. All databases will be immediately restored from the given *.sql dump file.


Words: 306
Published in: Homestead · Laravel · MySQL
Last Revision: November 07, 2022

Related Articles   📚