When creating websites, you often need to quickly backup the data that isn’t in your code repository. In the case of Drupal, it’s possible to generate copies via the “Backup and Migrate” module.
This module offers options for both beginners and advanced users. In this article, I’ll present its functionalities in detail.
The module was released at the end of 2007 for Drupal 5. It’s been extensively updated since then. The 8.x-3.x branch appeared in 2015, along with Drupal 8, however it didn’t have a stable release. It took another three years for the stable 8.x-4.0 version to see the light of day.
The popularity of the module is confirmed by the number of installations – according to the official statistics, it’s used by over 220 thousand pages, including approx. 40 thousand based on Drupal 8.
Four developers are responsible for maintaining the module:
- Damien McKenna from the Mediacurrent
- Alexandru Andrascu from the Intellix
- Ronan Dowling from the Pantheon
- Drew Gorton from the Pantheon
There are 822 commits in the code repository, created by over 70 people.
What is the module used for?
Backup and Migrate is an advanced tool for creating backup copies. It operates on the database, as well as public and private files. It integrates with Drupal CRON, thus ensuring the possibility of making regular backups to various destinations.
The module is available at https://www.drupal.org/project/backup_migrate. You can install it from a .zip file or run the command composer require drupal/backup_migrate in the console.
You can find the settings administration panel in the Configuration → Development → Backup and Migrate menu.
The module was designed with both beginners and advanced users in mind. The former don’t have to delve into the configuration of data export, they just need to use the quick backup option:
Clicking “Backup now” will download the database (or public/private files as selected in the “Backup Source” field). What’s more, the database backup will be devoid of cache and log entries, so it’ll take up much less space.
Some more settings are available in the “Advanced” tab. I’ll describe these later in the article.
By clicking Settings → Sources you’ll access the list of available backup sources.
By default, the sources can be:
- public files,
- private files,
- whole page code along with a database.
This set can be extended slightly with external MySQL databases and any directories on the server. It can be done using the “Add Backup Source” option.
It’s possible to define own sources via the “Backup and Migrate” module’s API.
A backup destination is the place where it is to be put. This can be, for example, a server directory or a web browser. The available destinations are listed in Settings → Destinations.
As with sources, you can define your own destinations via the “Add Backup Destination” button and a corresponding hook. At the end of the article, I present some destination-adding modules like SFTP Server and S3 Cloud.
Remember that backups contain sensitive data and access to them should be strictly limited. Drupal is one of the most secure CMSs available, so it’s worth using its private file system. In addition, the possibility of encrypting files with backups comes in handy. To use it, add the Defuse library to the project using the command:
composer require defuse/php-encryption
After clearing the cache, the “Encrypt File” option will appear in the module configuration panel with the field for entering a password. It’ll be used to encrypt the generated archives.
I’ve already covered how to create a quick backup. It’s time for its slightly more advanced version, which you can find in the “Backup” → “Advanced Backup” tab.
Here’s a quick overview of the options available:
- Source – used to select a predefined source.
- Backup file – allows you to configure the name and format of the backup file.
- Backup encryption – enables archive encryption.
- Take site offline – turns off the website while creating its backup.
- Exclude database tables – allows you to exclude selected tables in the database (or only their contents) from the backup.
- Exclude files – used to exclude selected files when creating a directory copy.
- Destination – allows you to select the backup destination.
If you want to save such a set of advanced options for the future, take a look at the profiles in the “Settings” → “Settings profiles” tab.
All backups on the server are listed in the “Saved Backups” list.
Interestingly, this list also works when saving backups to SFTP and S3 servers.
Less often needed, but still an extremely interesting option, is to restore the website from a backup. For this purpose, you can use either one of the copies from the list presented above or upload any file via the form.
The last important functionality of the Backup and Migrate module is the ability to create backup schedules.
Here you have everything you need to build an efficient backup mechanism. These actions are performed using Drupal CRON functionalities. Therefore, no additional server configuration is needed.
Hooks and integrations
Backup and Migrate provides one basic hook hook_backup_migrate_service_object_alter(), allowing you to change practically the entire engine of the module. This is where additional plugins and filters are defined. You can define the following elements:
- Backup sources – by default it is the database, public/private files and the entire website along with the source code.
- Backup destinations – such as cloud storage or local directory storage.
- Filters modifying the archives being generated – e.g. encryption.
On the basis of the above mechanism, many auxiliary modules that offer integration with external providers of disk space were created. Unfortunately, most of them run only under Drupal 7 control. These include:
- Backup and Migrate AWS S3 – adds the Amazon S3 servers support.
- Backup and Migrate Flysystem – adds Flysystem support.
- Backup and Migrate Yandex Disk – adds free Yandex Disk support.
- Backup and Migrate Prune (Drupal 7 only) – deletes old backups in accordance with the rules specified by the user.
- Backup and Migrate SFTP (Drupal 7 only) – allows you to save backup files into the SFTP server.
- Backup and Migrate Dropbox (Drupal 7 only) – adds Dropbox support.
- Backup and Migrate OpenCloud (Drupal 7 only) – adds OpenCloud support.
- HPCloud (Drupal 7 only) – adds HP Cloud support.
In the face of new technologies, the Backup and Migrate module is sometimes considered redundant. However, we use it in our Drupal agency as it offers the easiest way to download a database and files from a Drupal-based website. It’s easy to set up and works almost immediately.
In the case of smaller websites, the module can be useful as a supplement to a standard backup. Especially when the code is on a shared server where you don’t have full control of the system.