How to backup Directus
Updating Directus is a simple process and can be completed in just a few commands but sometimes changes can happen that will break your project, especially if you have custom extensions. This article will cover how you can backup your Directus project so you can restore after a failed update. This will require command-line access to your Directus project folder and storage locations.
To backup Directus, make a copy of your project files, .env file and perform a data dump of your database.
Backing-up a Project
It is important to make a backup of your project in case everything goes very badly. Some updates will make changes to your database which will making rolling back to previous versions impossible.
- Make a copy of the files within each storage adapter, and store them in a safe place. This can be a zip/tar file somewhere on the same server so you can easily extract them again if needed. If space is an issue, the backup can be stored elsewhere once you are happy the update has succeeded.
- Make a copy of the Env file (
/project_folder/.env
), and store it in a safe place. This contains all your configurations. - Create a database dump (a sql backup of your database)
MySQL Backup
From the terminal, run this command and replace the [database_name] with the Directus database.
// MySQL
mysqldump -u [user name] –p [password] [database_name] > /path/dumpfilename.sql
To restore the database, make sure to drop the existing database and create and empty one. Run the following in the mysql console.
> DROP DATABASE database_name;
> CREATE DATABASE database_name;
> GRANT ALL PRIVILEGES TO database_name.* FOR username IDENTIFIED BY 'password';
Quit the mysql console back to shell. Then restore the database from the sql dump.
mysql -u [user] -p [database_name] < /path/dumpfilename.sql
PostgreSQL Backup
From the terminal, run this command and replace the [database_name] with the Directus database.
pg_dump [database_name] > /path/dumpfilename.sql
To Restore the backup, you need to drop the database first.
psql template1 -c 'drop database [database_name];'
Now recreate the database.
psql template1 -c 'create database [database_name] with owner [your_user_name];
Lastly, restore the SQL dump to the empty database.
psql [database_name] < /path/dumpfilename.sql
MSSQL Backup
Use the SQL Server Management Studio (SSMS) software to make a backup. Right-click on the database, then go to Tasks and click Backup. Follow the prompts to complete the process.
Now that you have a backup of your project, you can update to the latest version.
Conclusion
Now you know how you backup Directus and update it to the latest version. It's worth storing your backups offline for a few weeks to ensure everything is working as expected.
To stay informed of new releases, subscribe to this website below.