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:

# run cron
12 * * * * /usr/bin/wget --no-proxy -O - -q -t 1 > /dev/null 2>&1

# BACK UP every day, without files, and once a week full - NOTE: MAILTO set to empty
16 2 * * * bash /home/user_name/ > /dev/null 2>&1
16 3 * * 1 bash /home/user_name/ > /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/.

/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"

/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