Back up system for a Drupal site with Drush

Drush back up system for Drupal site

Here is an example of how you can set up a back up system for a Drupal web site with Drush. Daily backups are kept for a week, after which they are deleted, and the weekly, full backups are deleted after 30 days. This script comes with no warranties of any kind, as always, test thoroughly before using it.

Add cron jobs

Open cron jobs from the terminal:
$ crontab -e

Insert this:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
# run cron
12 * * * * /usr/bin/wget --no-proxy -O - -q -t 1 http://example.org/cron.php?cron_key=r1tliiXzM...6M2Mdk > /dev/null 2>&1

# BACK UP every day, without files, and once a week full - NOTE: MAILTO set to empty
# MAILTO=""
16 2 * * * bash /home/user_name/drupal-backup.sh > /dev/null 2>&1
16 3 * * 1 bash /home/user_name/drupal-backup-full.sh > /dev/null 2>&1

# delete daily backups older than seven days #TEST-file creation: touch -d 20170903 old_file
16 4 * * * find /home/user_name/backup/daily/ -mtime +7 -type f -delete

# delete weekly full backups older than thirty days
16 4 * * * find /home/user_name/backup/weekly/ -mtime +30 -type f -delete

Create back up folders

This command will create the folders /home/user_name/backup/daily and /home/user_name/backup/weekly
$ mkdir -p /home/user_name/backup/{daily,weekly}

Back up scripts

Note: This assumes that Drush is located at /home/user_name/.local/bin/drush and web site at /home/user_name/public_html/.
drupal-backup.sh:

#!/bin/bash
/home/user_name/.local/bin/drush --root=/home/user_name/public_html/ archive-dump --destination=/home/user_name/backup/daily/website_backup_`date +"%Y_%m_%d_%H%M"`.tar --tar-options="--exclude=sites/default/files"

drupal-backup-full.sh:

#!/bin/bash
/home/user_name/.local/bin/drush --root=/home/user_name/public_html/ archive-dump --destination=/home/user_name/backup/weekly/website_backup_`date +"%Y_%m_%d_%H%M"`.tar