Skip to content

Instantly share code, notes, and snippets.

@jmfernandez
Last active November 7, 2018 19:47
Show Gist options
  • Save jmfernandez/083b3ba6d76d9a682031a26cdf385444 to your computer and use it in GitHub Desktop.
Save jmfernandez/083b3ba6d76d9a682031a26cdf385444 to your computer and use it in GitHub Desktop.
Script to do backups of a Drupal deployment
# Every day backup, keeping the 7 newest
42 3 * * * /root/doDrupalBackup.sh ; rm -r $(ls -d1 /gpfs/drupal_backups/*-*-* | head -n -7)
# Every week backup, keeping the 4 newest
42 4 * * 1 mkdir -p /gpfs/drupal_backups/weekly && cp -dpr /gpfs/drupal_backups/$(date -I) /gpfs/drupal_backups/weekly ; rm -r $(ls -d1 /gpfs/drupal_backups/weekly/*-*-* | head -n -4)
# Every month backup, keeping the 12 newest
42 5 1 * * mkdir -p /gpfs/drupal_backups/monthly && cp -dpr /gpfs/drupal_backups/$(date -I) /gpfs/drupal_backups/monthly ; rm -r $(ls -d1 /gpfs/drupal_backups/monthly/*-*-* | head -n -12)
# Every year backup
42 6 1 1 * mkdir -p /gpfs/drupal_backups/yearly && cp -dpr /gpfs/drupal_backups/$(date -I) /gpfs/drupal_backups/yearly
#!/bin/bash
BACKUPDIR=/gpfs/drupal_backups/$(date -I)
# Mimmicking drupal with these variables
DRUPAL_ROOT=/var/www/drupal
site=default
app_root="${DRUPALDIR}"
site_path="sites/${site}"
umask 077
# Create the backup dir
mkdir -p "${BACKUPDIR}"
# Create the log file
exec >& "${BACKUPDIR}/backup.log"
# Copy this backup script
cp "$0" "${BACKUPDIR}"
# Backup the sensible directories, as https://www.drupal.org/docs/user_guide/en/prevent-backups.html
tar -c -p -f - -C "${DRUPAL_ROOT}" sites modules themes | bzip2 -9c > "${BACKUPDIR}"/drupal_backup.tar.bz2
# Read the mysql database configuration data
eval "$(php -r "define('DRUPAL_ROOT','${DRUPAL_ROOT}');include '${DRUPAL_ROOT}/core/lib/Drupal/Component/Assertion/Handle.php'; \$app_root='${app_root}';\$site_path='${site_path}';include '${DRUPAL_ROOT}/sites/default/settings.php'; foreach(\$databases['$site']['default'] as \$key => &\$val) { print 'D_' . \$key . '=' . \$val . ';' ; }")"
# And do the backup
mysqldump --host="${D_host}" --user="${D_username}" --password="${D_password}" --port="${D_port}" "${D_database}" | bzip2 -9c > "${BACKUPDIR}"/mysql_backup.sql.bz2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment