Different Drupal developers have different techniques to backup their Drupal websites. In this article, we will be talking about two of my go-to methods to backup a Drupal 8 or Drupal 9 website. One with the help of the Backup and migrate Drupal module. And the other way is with Cron jobs using CLI.
Data loss can be brutal and devastating, no matter who or what caused it. The good news is that it can be prevented by taking regular backups of your site. Sometimes, backups are done every day and sometimes, occasionally. The regularity depends on various factors like the size of your Drupal site or how often you update the website. Luckily, Drupal 8 and 9 provides you with easier options to backup and restore your website with the help of the Backup and migrate Drupal module and via CLI.
Using the Backup and Migrate Module for Drupal 8 and 9
The backup and migrate module can backup both database as well as files. It can also backup the entire Drupal application folder. The module also has an encryption feature which requires you to download an additional library called the Defuse PHP encryption library-
composer require defuse/php-encryption
Steps for usage:
- Install the Backup and Migrate module.
- Create a private directory and assign with writable permissions (similar to what we’d give to the files folder).
- Set the path of private directory in settings.php file so that the module can use it for storing the backups.
UI for the Backup and Migrate Module
Once you install the module, head to Administration -> Configuration -> Development -> Backup and Migrate. You will see 5 tabs:
Backup – where you can do quick backups. Once you click on Backup now, you will be able to see it in the Saved Backups tab.
Restore – where you can restore the earlier backups
Saved Backups – will have the list of all the backups you have taken so far
Schedules – where you can schedule your backups
Settings – contains general settings and options for this module
Schedules:
This is one of the most important features of the module. The module provides an automated scheduler where you can schedule your backup. This runs automatically during every cron run. You can add as many schedules as you want, to take backups of the database, public or private files directories or the entire site.
Viewing all the Schedules
Configuring the Schedule
Here is where you can add or edit the schedules. You can give a name for the schedule. There’s an option to enable or disable the scheduler. Select one of the various backup sources like the database, public or private files, directories, or the entire site. Or add your own in the Settings tab. If you are backing up both the database and files, you can have different backup destinations configured for each of the backup type. This means you can have a different destination for the database backups, a different destination for the file backups, and such. The module also provides configurations for adding more destinations of your choice in the Settings tab. The Frequency option lets you specify how often you want a backup to take place as a cron job. You can select options from hours, minutes, days, and weeks. The Numbers to keep option allows us to specify the number of backups we want to keep. It automatically deletes the older backups once it reaches the limit that you set, thereby solving memory and storage problems.
Configuring the Schedules
Adding your Backup Source
Are there any limitations?
Sadly, yes. As per my personal experience, I’ve noticed that when the database size grows, I do run into Time-out errors. And this is a problem because on certain servers, we cannot increase the maximum execution time for the database to execute. The other issue that I have faced is memory issue. It may run out of memory in some cases depending on the configuration of the server or the database size or the size of the backup. When we run into such issues, we can use the CLI method.
The CLI Method to Backup
In the command-line interface method, we can perform backups using these two methods:
- Using Drush in combination with the Crontab
- Using Native commands and Crontab depending on what we need to backup.
Using Drush and Crontab
Scenario: Scheduling a database backup at 3 a.m. every morning –
Format:
0 3 * * * -r sql:dump --result-file=.sql --gzip
Example:
0 3 * * * /var/www/html/drupal_demo/vendor/drush/drush/drush -r
/var/www/html/drupal_demo sql:dump --result-file=/var/www/html/drupal_demo/db-dump-`date +%d%m%Y%H%M%S`.sql --gzip
Using Native commands and Crontab
Scenario: The same one - scheduling a database backup at 3 a.m. every morning -
Format:
0 3 * * * mysqldump -u -p | gzip -c > .sql.gz
Example:
0 3 * * * mysqldump -u drupal -p drupal_demo | gzip -c >
/var/www/html/drupal_demo/db-dump-`date +%d%m%Y%H%M%S`.sql.gz
Scenario: Scheduling a Files backup at 5 a.m. every morning -
Format:
0 5 * * * tar -czvf .tgz
Example:
0 5 * * * tar -czvf /home/ubuntu/backups/drupal_demo-files-`date +%d%m%Y%H%M%S`.tgz
/var/www/html/drupal_demo/web/sites/default/files/