Backup Drupal Sites using Backup and Migrate Module

Backup is an essential aspect for every site but often overlooked. Backup seems time-consuming and unnecessary, but when things happen, it can be a life saver freeing you from unexpected damage. It is a question of how backups can be made quickly, preferably automatically, without taking too much time. In addition, it is also essential to make sure when backups are restored, it works reliably as expected without surprises.

In this tutorial, we introduce a module that helps to provide such a solution.

The Backup and Migrate module can backup the database and files of a Drupal site. The module also provides a restore operation of the backups when needed. It can be easily installed in a Drupal site, and it is free. With this module, the authorized user can perform backup manually or automatically. Backups can flexibly include only the database or user files, or both.

When operated manually, backups can be downloaded immediately in compressed file format, or stored in a safe location in the server. When automatic operation is preferred, it can be scheduled, and the backed up files in compressed format will be stored in the server. The site can be taken offline with a notification message during the backup procedure, and return to normal after it’s completed.

Remember you should never rely entirely on a single backup solution. Things can still go wrong. The backup and restoration process may fail for many different reasons. It’s good to have a second backup system, such as at the server hosting level.

Getting Started

There are no dependencies for Backup and Migrate. Users can follow normal module installation.

To install using Composer, run the following:

composer require 'drupal/backup_migrate:^5.0'

Simply enable the module after installation.

Setup of Private Path

When backups are stored in the server, they should be stored in a safe location which cannot be accessed from the internet by other users as a security measure. To do this we need the private files directory.

By default, Drupal stores user files in a public files directory. There is an option for a private files directory but by default it is not configured. It requires to be manually configured before it is available.

To check the private files directory setting, go to:

Administration > Configuration > Media > File System (/admin/config/media/file-system)

If there is no change to the default configuration, it will look like this:

Drupal file system

To configure this private files directory, we need to enable the private file system. We need to make changes to the settings.php file under the sites/default directory in the Drupal site.

Find and edit this file on the server and search for the following line:

$settings['file_private_path'] = '';

Note that there is a ‘#’ sign meaning the line is commented out (not effective). Uncomment it, and insert the directory path as follow:

$settings['file_private_path'] = $app_root . '/../private-files;

Save the settings.php file and exit from the editor.

In this setting, if we follow the same file structure mentioned above, it will be located outside of the docroot directory, like this:

/public_html
|-------------/sites/default
|----------------------------/setttings.php
/private-files
|-------------/backup_migrate
|--------------------------------/backup-yyyy-mm-ddThh-mm-ss.???

Go back to our Drupal site, and make a ‘Clear all caches’ to make the changes effective by going to:

Administration > Configuration > Development > Performance (/admin/config/media/file-system)

Now the private file system is configured. To check it again, go to:

Administration > Configuration > Media > File System (/admin/config/media/file-system)

and the new settings should be shown as follows:

After this is configured, now both public and private file systems are available. By default, original content types will be configured to the public file directory, unless otherwise re-configured. But for the Backup and Migrate module, the private files directory is where the backups will be stored.

Quick Backup Operation

Now the Backup and Migrate module is ready for use. We can go to the module configuration at:

Administration > Configuration > Development > Backup and Migrate  (/admin/config/development/backup_migrate)

The following screen will be shown:

Backup and Migrate - backup

Take note of the message on the page:

“It is recommended to not use the “Entire site” backup as it has a tendency of falling on anything but the tiniest of sites. Hopefully this will be fixed in a future release.”

The ‘entire site’ option is available, but not recommended at this time for this version of the module.

This first screen is a Quick Backup operation. It provides an easy user interface to perform a backup manually and immediately. The picture above shows options available which should be self-explanatory. Note that there are only 2 options for the backup destination, either immediate download, or store to the ‘Private Files Directory’. This is why we need to configure the private file system above.

Apart from this Quick Backup option, there are other options available.

Advanced Backup

The advanced backup option allows additional settings for immediate backup. In particular, it provides an option to take the site offline and show a maintenance message during the backup process. Once completed, the site will be taken back online. This is recommended as a good practice.

There is a timestamp automatically attached to the backup files. Here in this advanced backup operation, the timestamp format can be changed accordingly.

Note that by default, cache data in the database tables are excluded. Files of certain types in the public files directory are also excluded by default.

In the backup destination, an ‘upload’ option is available, but should not be used. It is reserved for uploading the backup data to another backup server.

Backup and Migrate - advanced backup

Scheduled Backup

Another important option is to have scheduled backup. In this operation, backup can be configured to automatically run at the specified time. This is probably the most frequently used option. To schedule a backup, go to the ‘Schedules’ tab, and press ‘Add Schedule’:

Follow the self-explanatory options to schedule a backup. Schedule can be made in weeks, days, hours or minutes.

Note that Private Files Directory is the only option for the backup destination as it is a scheduled unattended process. There is an option of ‘Number to keep’, which allows users to determine how many different backup versions to be kept. Versions exceeding this number will be automatically deleted. For example, if it is a daily backup and 14 versions are kept, the system will delete the earliest version when it is performing the 15th backup.

After configuring the scheduled backup, remember to enable it by checking the ‘Schedule enabled’ option.

Saved Backups

A list of all saved backups in the Private Files Directory can be found by at the ‘Saved Backups’ tab:

A list of all available saved backups can be found here. Options are available for:

  • Restore – restore the specific backup files
  • Download – download the file accordingly and store elsewhere
  • Delete – remove the backup files permanently

Saved backup files can be restored from here. Furthermore, backups can also be restored from an external backup file which could be downloaded earlier. To restore from external file, press the ‘Restore’ tab and follow the settings accordingly:

Summary

This is an excellent backup option, but you should never rely entirely on a single solution. It is recommended to have alternative solutions in parallel. An obvious choice is to have another hosting level backup solution made at the server level. Many hosting service providers offer this service. Another alternative solution is to make use of cron jobs to schedule backups at the system level. It is not uncommon to have multiple backup arrangements to secure the content and the site to avoid any losses or damages.

This Backup and Migrate module is a good choice for a free backup solution, easy to set up and easy to manage. Scheduled backups provide automatic backup with a predefined number of backup versions. This can free us from the repetitive but unavoidable backup job, and save us a lot of time.

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.