Skip to content

Instantly share code, notes, and snippets.

@DirkR
Created November 19, 2012 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DirkR/4110221 to your computer and use it in GitHub Desktop.
Save DirkR/4110221 to your computer and use it in GitHub Desktop.
Script tu run Drupal's "drush bb" to scheduled backup location and remove old Backups
#!/bin/sh
#
# backup_migrate is a powerful module to create backups of a drupal database.
# It also has a tight drush integration and supports cron-based scheduled
# backups as well as removal out outdated backups. But you cannot define a
# eliable time frame when to run the backup. So it's possible that the
# scheduled backup runs at highnoon and sets your website into maintenance
# mode for a few seconds. That's where this script jumps in.
#
# This script runs drush on a set of drupal installations (defined in
# $SITEALIASES) and
#
# 1. creates backup into the scheduled backups folder using the $BAM_PROFILE
# backup profile
# 2. Removes all backups older than $GRACE_PERIOD days.
#
# Run this script via cron at the time you like and disable the
# scheduled backup_migrate task inside Drupal
SITEALIASES="live beta" # which site aliases should be included
WWWUSER=wwwrun # which user runs the the apache process
BAM_PROFILE=default # get the backup profile name with
# "drush @alias bam-profiles"
GRACE_PERIOD=4 # After how many days will old backups be removed
# This code comes from http://stackoverflow.com/a/3951175 -
# Thanks to jilles (http://stackoverflow.com/users/298656/jilles)
# for this solution
case $GRACE_PERIOD in
''|*[!0-9]*) echo "The variable GRACE_PERIOD contains an invalid value: '$GRACE_PERIOD'"; exit 1;;
*) true ;;
esac
for a in $SITEALIASES
do
if [ "X$WWWUSER" == "X" -o "X$WWWUSER" == "X$USER" ]
then
drush @${a} bam-backup db scheduled $BAM_PROFILE 2> /dev/null
else
sudo -u $WWWUSER drush @${a} bam-backup db scheduled $BAM_PROFILE 2> /dev/null
fi
DRUPAL_MAJOR_VERSION=$(drush @${a} st "Drupal version" 2>&1 | grep 'Drupal version' | sed -e 's/^[^0-9]*//' -e 's/\..*//')
ROOT_FOLDER=$(drush @${a} dd | egrep -v '^(X-Powered-By:|Content-type: text/html)' | egrep -v '^\s+$')
if [ $DRUPAL_MAJOR_VERSION -lt 7 ]
then
PRIVATE_FILES_FOLDER=$(drush @${a} vget file_directory_path | awk -F\" '{ if ($1 ~ /file_directory_path/) print $2 }')
else
PRIVATE_FILES_FOLDER=$(drush @${a} vget file_private_path | awk -F\" '{ if ($1 ~ /file_private_path/) print $2 }')
fi
find $ROOT_FOLDER/$PRIVATE_FILES_FOLDER/backup_migrate/scheduled/ \
-name \*.mysql* \
-ctime +${GRACE_PERIOD} \
-exec rm "{}" \;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment