Using Drupal to manage your content is a great way to get the most out of a website. Especially since it has dozens of tools, plugins and features to offer. Despite all of the great things Drupal does, backup isn’t one of them. This is a real bummer for an otherwise stellar CMS software. Since there isn’t a great tool that you can use to backup a Drupal site Linux, you have to do it manually.
To backup a Drupal site on Linux, you have to create a folder to hold the exported files. In the terminal, use the mkdir command to create a backup folder.
First, log in as root with su.
Alternatively, use sudo -s to gain root if you don’t know the system password.
Next, create a backup folder in /.
mkdir -p drupal-backups
Export SQL Files
Next, you need to export all of the SQL database files. SQL files should be handled by the root account. Do not attempt to do this backup with a regular user.
In the drupal-backups folder, create an SQL sub-folder, and an installation sub-folder.
mkdir -p /drupal-backups/sql mkdir -p /drupal-backups/installation-files mkdir -p /drupal-backups/apache2-conf
Use mysqldump to export your Drupal database files from the SQL installation on the server to the backup folder.
Note: before using the command below, change “username” and “databasename” to your SQL username, and the database name in SQL that Drupal uses. On most installations, the default SQL database name is “drupal”.
cd drupal-backups/sql mysqldump -u username -p databasename > db.drupal_backup-1.sql
Back Up Installation Files
The core database files are backed up. The next step is to backup the actual installation of Drupal. To do this, you’ll need to first make a complete copy of everything from /var/www//html/. If you followed our guide to install Drupal, the core of your Drupal website on Linux is directly inside of /var/www/html, with no sub-folders. Using the cp command, make a complete copy of the html folder and place it in /drupal-backups/files.
Note: if your installation is /var/www/html/drupal, or something similar, change the cp command to suit your needs.
cp -rp /var/www/html/* /drupal-backups/installation-files/
Getting the installation of Drupal backed up is important. In those files are important site configurations, themes, and etc. Still, it’s not the only files that need to be backed up. Another key file is the Apache2 configuration. This configuration file tells the Apache web-server where your Drupal installation is, and how to load it. Without this file, the backup is useless.
To create a backup of the Apache2 configuration file, run the following command:
cp /etc/apache2/sites-available/drupal.conf /drupal-backups/apache2-conf/
Compress Backup Files
Now that all of the necessary Drupal server files have been copied and are ready for backup, it’s time to create a Tar archive. Compression will make moving the backup files much easier. To create a Tar archive of your Drupal backup files, run the following command:
tar -zcvpf drupal-website-backup.tar.gz /drupal-backups
Compressing the backups is a good idea, but it’s unsafe. If you’re going to save your Drupal website on a public cloud storage website, uploading the Tar archive, unencrypted means anyone can mess with the values inside of the SQL database files, the site files, or worse.
It’s best to encrypt this archive before doing anything else. To encrypt, make sure GnuPG is installed. Don’t have it? Look in your Linux server’s package manager for “gpg” and install it. Once it’s installed, run the following command to fully encrypt your Drupal backup archive.
gpg -c drupal-website-backup.tar.gz
Running gpg -c will tell GnuPG you want to encrypt the Tar archive and ask for a password to set for the new GPG encrypted file. Enter a secure password, and wait for the encryption to finish. When GPG finishes, feel free to copy drupal-website-backup.tar.gz.gpg and upload it to wherever you plan to keep your backup.
Download the Drupal GPG backup file to the server where you’d like to restore the backup. Then, do the following:
Create a place to hold the backup.
mkdir -p /drupal-restore/
Move the GPG file into the new backup folder.
mv /folder/where/drupal-website-backup/is/drupal-website-backup.tar.gz.gpg /drupal-restore/
Decrypt the backup, using GPG.
Extract the backup.
tar -xvpf drupal-website-backup.tar.gz
Enter the extracted backup folder.
It’s now time to start the restoration. Start by importing the Drupal SQL files into MySQL.
mysqldump -u username -p drupal < db.drupal_backup-1.sql
Now that the database is in place on the system, restore the rest of the files.
cp /drupal-restore/drupal-backups/apache2-conf/drupal.conf /etc/apache2/sites-available/
sudo ln -s /etc/apache2/sites-available/drupal.conf /etc/apache2/sites-enabled/drupal.conf cp -rp /drupal-restore/drupal-backups/installation-files/* /var/www/html/
Moving the files into place is the last critical step in the restoration process.
When everything looks good, restart your Linux server. Though it might be annoying to restart, it’s a good idea. Doing a restart will ensure that all of the services needed to run Drupal correctly come back online. When the server comes back online, everything should be as it was.