backup-drupal-db 1.59 KB
Newer Older
1
#!/bin/bash
2
#############################################################################
3 4 5 6 7 8 9 10 11 12 13
# Purpose:
#   Back up a Drupal site's database, and delete old backups.
#
# Parameters:
#   <drush-alias>
#     The Drush alias of the Drupal site to be backed up.
#
# Prerequisites:
#   * Drush 6+
#   * Globally available Drush aliases
#
14 15 16 17
# Author: Colan Schwartz
# Licence: GPLv3
#############################################################################

18 19 20 21 22 23 24 25 26 27 28
# Configuration options.
BACKUP_DIR=/var/local/backups/mysql
GROUP_OWNER=adm
KEEP_DAYS=60

# Command paths.
DRUSH=/usr/local/bin/drush
CHMOD=/bin/chmod
CHGRP=/bin/chgrp
LN=/bin/ln
FIND=/usr/bin/find
29 30 31 32 33 34 35 36 37
MKDIR=/bin/mkdir
ECHO=\echo

# Make sure that the parameters are specified.
SCRIPTNAME=$(basename $0)
if [[ -z $1 ]]; then
  $ECHO "Usage: $SCRIPTNAME <drush-alias>"
  exit 1
fi
38

39 40 41
# Prevent accessing unset variables
set -u

42 43
# Create the backup directory if it doesn't exist yet.
$MKDIR -p $BACKUP_DIR
44

45
# Set the current timestamp and file targets.
46 47
DATE=$(date "+%FT%T")
DST_BASE="$BACKUP_DIR/$1.$DATE.sql"
48
DST_FILE="$DST_BASE.gz"
49
LINK_TARGET="$BACKUP_DIR/$1.LATEST.sql.gz"
50 51 52

# Run the Drush command to dump all of the tables.
# Cache tables are to maintain their structure, but will not contain any data.
53
$DRUSH $1 sql-dump --quiet --gzip --result-file=$DST_BASE --structure-tables-list=cache*
54 55 56 57 58 59 60

# Make the files readable by the user and group, no other permissions.
$CHMOD 440 $DST_FILE
$CHGRP $GROUP_OWNER $DST_FILE

# Set a pointer to the latest backup file.
$LN -sf $DST_FILE $LINK_TARGET
61

62
# Delete old backups.
63
$FIND $BACKUP_DIR -maxdepth 1 -type f -mtime +$KEEP_DAYS -delete