There’s always a probability of a software application crashing or encountering a technical fault. To reduce this probability, certain strategies with specific steps have to be implemented to troubleshoot or at the very least, recover from it.
In the worst-case scenario, if nothing else works, then restoring from a backup is the best possible solution. Thus, timely backup is a critical step in ensuring that data integrity remains intact.
Having a backup also ensures that you can keep on working without worrying about the possibility of losing your data.
Let us take a look at how to create backups of the database for Drupal 8 applications using Drush and MySQL command.
Why Drush and MySQL command?
Drush is a powerhouse of features. Keeping usability, terseness, and efficiency of the command-line in mind, Drush is designed to meet any challenge. It is fast, easy to customize, and integrates seamlessly.
MySQL is the best cost-effective and open-source database solution because its database server provides scalability, high performance, and supports the ability to handle deeply embedded applications. Moreover, it ensures strong data protection of data assets through SSH and SSL mechanisms.
Now, let us focus on the steps required to backup the Drupal 8 application database.
Database backup using the MySQL command
- MySQL -u root -p
Connection with MySQL
- show databases
Show all databases from MySQL
- show tables from database name
Show tables from database name
- mysqldump db_name > db_file.sql
Backup local database without a password
- mysqldump -u USERNAME -p dico db_name > db_file.sql
Backup / Local / Remote Database with Password
- mysqldump -u USERNAME -p db_name table_name > table_file.sql
Backup a single table
- mysqldump -u USERNAME -p db_name | gzip > db_file.sql.gz
Backup and compress DB using gzip
- MySQL -u USERNAME -p db_name < db_name.sql
Restore the database
- gunzip < db_file.sql.gz |MySQL| -u USERNAME -p PASSWORD db_name
Restore a gzip compressed file using gunzip
Database backup using the Drush Command
- Drush Archive-Dump
Backup both files and database in a single directory
Drush Archive-Dump backups code file and database in the same file. Database dump saved to
Archive saved to
- Drush SQL-dump
Take SQL dump of the database using Drush
Drush extracts the database details from settings.php and takes backup as a drupal8.sql file in a Drush-backups folder .
You can give any custom path or custom_name.sql file for database backup .
Note – ~ denotes the root path of the server.
- Drush SQL-CLI
Restore the SQL dump database using Drush
/var/www/html/mariadb#drush sql-cli <~/drush-backups/drupal8.sql
Using this command Drush extracts the database details from settings.php file and automatically restores the database from drupal8.sql backup.
Code backup using the Tar Command
The syntax for the TAR command
tar -zcvf archive-name.tar.gz directory-name
-z : Compress archive using gzip program
-c: Create an archive
-v: Verbose i.e display progress while creating an archive
-f: Archive Filename
E.g – Suppose I want to take a backup of abc[dot]com files and folder on /var/www/prod location following command will be
www-user@web10:/var/www/prod$ tar -zcvf xyz_824022018.tar.gz abc[dot]com
This will take backup on this location
/var/www/prod/grazitti_824022018.tar.gz of the abc[dot]com folder.
Kudos! You’ve learned how to create backups of the database for Drupal 8 applications using the Drush and MySQL commands. Periodic backups can mitigate unforeseen circumstances that might occur.
Want More Information About Drupal Services? Contact Us!
Popular Blog Posts