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
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 SchedulesConfiguring 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 SourceAre 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/
Backups are not just essential when you need to migrate your website. Taking regular backups is the best thing you can do to protect yourself from data loss. And it doesn’t have to be a tedious job when Drupal 8 (and 9) offers you with modules that can help automate backups. The Backup and Migrate module and the CLI method we discussed here, are my two most recommended methods to automate your backups. Curious to know how we can implement such amazing Drupal modules and features for your next project? Talk to our Drupal experts today.Drupal Drupal 8 Drupal 9 Drupal Development Drupal Planet Drupal Tutorial Shefali ShettyApr 05, 2017 Subscribe For Our Newsletter And Stay Updated Subscribe
Leave us a CommentShefali ShettyApr 05, 2017 Recent Posts Image How to perform Automated Backups on a Drupal 8 (or 9) Website Image How to Export/Import Menus, Custom Blocks & Taxonomies with the Drupal 9 (& 8) Structure Sync Module Image Drupal Project Management: What No One Is Talking About Want to extract the maximum out of Drupal? TALK TO US Featured Success Stories
A Drupal powered multi-site, multi-lingual platform to enable a unified user experience at SEMI.link
Discover how our technology enabled UX Magazine to cater to their massive audience and launch outreach programs.link
Discover how a Drupal powered internal portal encouraged the sellers at Flipkart to obtain the latest insights with respect to a particular domain.