Automatic backup for Drupal sites using crontab and Composer

This tutoring will guide you through setting up a weekly backup script for your Drupal install that will ensure your source files, assets and database is backed up into an easy to restore .tar package.

Firstly you need to install drush which is the Drupal command line package. Historically this has been included with Ubuntu (and likely other Debian-based Linux installs) however we found that the included version (5.5) is incompatible with the version of Drupal we are using (8.0.1).
Furthermore the official version offered though the package management system (apt-get) is also not supported.

To get around this issue we can installed the package with Composer, taking advantage of its `global` argument, which installs it at the root of our current users home directory.

Lets get started. First of all grab the latest version of composer.

curl -sS https://getcomposer.org/installer | php

Then install bleeding-edge build of drush from the package list using the global argument.

composer global require drush/drush:dev-master

This will install drush under ~/.composer/vendor/drush/drush.
The actual executable will be placed in this directory.

To make things easer for us, lets create an alias to the executable, so it can be run simply by calling drush.
In our case we are logged in as root, please run ~/ to find your home directory and replace "/root/" as needed.

alias drush='/root/.composer/vendor/drush/drush/drush'

Now that we can easily run the drush command we need to setup a crontab to run out backup script.

crontab -e

If this is the first time you have opened your crontab file it will ask you to select an editor, make your selection and proceed to the crontab itself.

The first thing we are going to set in our crontab is a time variable. This variable is very useful anywhere you need to create a file over and over again, but you don't want it to be overwritten each time because it has the same name.

Place the following at the end of your crontab file, after all the commented text

DATEVAR=`%d%m%y`

We are going to be combining a few concepts here, firstly we want this script to run every week. To achieve this we can use the following schedule.

0 0 * * 1

This basically tells the cron daemon we are wanting to run this scrip at second 0, on hour 0, every day of the month, every month, on the first day of the week.
Further explanation is really outside the confines of this guide, but there are plenty of tutorials on the web explaining this in more depth if your interested.

Next we are going to make a conditional bash statement, two bash scripts can be combined that have a conditional relationship with one another. There are a few ways to do this but today we will be using the `&&` logical comparator. When we place `&&` between two bash scripts it tells bash to only run the second one if the first one succeeds.

We need to do this because for drush to be able to run the backup script, it needs to be in the Drupal root directory. So the first part of the scrip sets the directory environment, the second takes care of the backup itself.

So, our combined statement looks like this

cd /var/www/drupalappname/web && drush ard --destination=/var/www/drupalappname/backups/$($DATEVAR).tar

This command changed directory into your drupal install, and using the drush archive command, makes an archive of the files, assets and database and stores it under the backups directory with the name set to todays date.

Add that to the end of your crontab, below the DATEVAR=`%d%m%y` we added earlier and then exit the editor and your all done!