dix (Database Import eXport)

dix is a simple console application that will make your life easier if you are working with MySQL databases. Let's say that you are working with some PHP framework or CMS and that you want to test a new module/plugin/add-on, or maybe you want to upgrade to the latest version. The very first thing you should do is to make a backup of your database. Doing this will save you a lot of headaches if something goes wrong.

Usually, this means that you have to call MySQL dump and export database somewhere on your disc. Although not a difficult task, it's error-prone. Where to export a database file, how to name it, and how will you remember why you exported a database? These are just some questions to think about and consider when exporting a database. This is where dix will help you to save time.

When you install dix, you should first configure a directory where you will store your exported databases.

dix config --dir=/home/goran/backup/db

After you have your directory set up, you can start exporting databases. But, you first have to decide whether you want to type database username and password every time you are making an export, or do you want to save credentials and avoid typing them in the future. If you want to configure database credentials once, you can use the config command.

dix config --dbname=dev_goran_cloud --user=dev_goran_cloud --pass=mystrongpassword

Now, you are ready to export your first database. Since you have the database credentials stored in the application configuration, you can use the following command to export database:

dix export --dbname=dev_goran_cloud --msg="My first database export"

Database file will be exported to the directory you specified before. Very useful option is that you can add message to your export. This way you have a description associated with the database, so you know why you exported it. Message is an optional argument, so you can omit it. You can see all your exports if you use the log command.

dix log

Each export has a unique ID. This is used later, when you want to import a specific database. You can also search and filter log to find exactly what you want. Now lets say that your experiment failed, and that your database needs to be restored. You can do this just as easy. Use the following command:

dix import --dbname=dev_goran_cloud

This will import the last exported version of the "dev_goran_cloud" database. If you want to import some other version of this database, you can specify its export log ID:

dix import --id=541265bfb223259ee62535c7e62c01f08ef14830

You don't have to enter all those characters. The first five characters are enough:

dix import --id=54126

After executing this command you will be asked for confirmation. Importing will overwrite the existing database, so be careful what are you doing. If you need the current database, then you should export it.

This article gives a general overview of dix. To see a detailed explanation of all commands and arguments go to the Github project page where you can find all information you need about usage and installation.

About the Author

Goran Nikolovski is a senior web developer who loves working with Drupal, React, and React Native. He is the founder of this website and he enjoys sharing his knowledge. Need help with anything? Get in touch.