-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup-drupal-db
executable file
·63 lines (53 loc) · 1.59 KB
/
backup-drupal-db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
#############################################################################
# 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
#
# Author: Colan Schwartz
# Licence: GPLv3
#############################################################################
# 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
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
# Prevent accessing unset variables
set -u
# Create the backup directory if it doesn't exist yet.
$MKDIR -p $BACKUP_DIR
# Set the current timestamp and file targets.
DATE=$(date "+%FT%T")
DST_BASE="$BACKUP_DIR/$1.$DATE.sql"
DST_FILE="$DST_BASE.gz"
LINK_TARGET="$BACKUP_DIR/$1.LATEST.sql.gz"
# Run the Drush command to dump all of the tables.
# Cache tables are to maintain their structure, but will not contain any data.
$DRUSH $1 sql-dump --quiet --gzip --result-file=$DST_BASE --structure-tables-list=cache*
# 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
# Delete old backups.
$FIND $BACKUP_DIR -maxdepth 1 -type f -mtime +$KEEP_DAYS -delete