Save-as-you-go and make backups in a jiffy with DDEV

November 20, 2018 3 min read

Edit this page
Pale photo of a snowy mountainscape
Photo by Will Echols on Unsplash.

To us, the ideal local development environment should be fast and easy to use and give you what you need to get your job done. DDEV’s ddev snapshot command helps you recover quickly, and the new `export-db` command makes you portable backups in a jiffy.

Here’s how to easily save and restore databases with DDEV, so you can recover if something goes wrong or you change your mind about the development direction you’re going.

Download DDEV

Using ddev snapshot to save-as-you-go

Software and web development is about continuously solving problems and trying new things. You want to be able to take risks, but calculated risks. You can’t eliminate mistakes, but you can make it easier to recover and learn from them. Use DDEV’s snapshot command to capture where you are now with your project. You’ll have the peace of mind you need to keep going. There’s no risk of losing work, you can come back to this point at any time.

To save a version of your DDEV database use one quick command:

ddev snapshot

The snapshot is automatically named with the project name and timestamp. Make it even clearer by naming the snapshot so you can identify it easily:

ddev snapshot --name=before-mucking

Then, down the line when you’ve done something you wish you hadn’t, restore it with:

ddev restore-snapshot before-mucking

And you’re back to the working version of the database you had before you mucked with it.

Here’s a video showing you how to use it.

DDEV snapshotting uses mariabackup inside the database container. This is super, super fast in both directions. It’s not a copy of the database directory, the state of the database is properly preserved. This means when you come back to work, everything is as you left it.

Important: DDEV snapshot isn’t backward-compatible. Snapshots from previous versions of DDEV cannot be restored with v1.3 or higher because the mariabackup included with MariaDB 10.2 is not compatible with earlier backups. There’s an easy workaround to fix this explained in the DDEV documentation.

Backing up with ddev export-db and with mysqldump

The ddev snapshot command is a great way to make a quick dump of your database, but it’s not as portable as a text-based database dump. With the recent release of DDEV v1.4, we introduced the ddev export-db command.

On the command line, DDEV has always had [ddev import-db](https://ddev.readthedocs.io/en/stable/users/cli-usage/#importing-a-database) but until now we didn’t have the correlating export option. Now you can!

Run this command to create a text-based database dump:

ddev export-db

Check out the documentation for examples of how you can use options to output to a file, for example:

ddev export-db -f /tmp/db.sql.gz

Another way you can backup your data with DDEV is by using ddev ssh or ddev ssh -s db doing a database dump using the MySQL client. Here’s an example of how you can do that:

ddev ssh
mkdir /var/www/html/.tarballs
mysqldump db | gzip >/var/www/html/.tarballs/db.YYYYMMDD.sql.gz

To restore an SQL dump like that on the host you would use this command:

ddev import-db --src=.tarballs/db.YYYYMMDD.sql.gz

A text-format database dump created with either this technique or ddev export-db can typically be restored on any MySQL/MariaDB server without trouble. It’s not as fast as ddev snapshot, and the restore is much slower. But it’s portable.

Posted In