Commit 7677171f authored by Colan Schwartz's avatar Colan Schwartz

Added MySQL (or equivalent) backup script for databases.

parent 16379c1e
#!/bin/sh
#############################################################################
# Filename: mysql-backup-databases
# Purpose: To dump all of the mysql databases, compress them, and keep
# a few dumps for some time.
# Author: Colan Schwartz
# Licence: GPLv3
#############################################################################
# Prevent accessing unset variables
set -u
# Credential file
CRED_FILE=/etc/mysql/debian.cnf
# mysqldump opts
OPTS="--single-transaction --skip-lock-tables"
CACHETABLES="cache cache_admin_menu cache_apachesolr cache_block cache_bootstrap cache_content cache_ds_panels cache_field cache_filter cache_form cache_htmlpurifier cache_location cache_luceneapi cache_menu cache_metatag cache_page cache_path cache_rules cache_token cache_update cache_variable cache_views cache_views_data ctools_css_cache ctools_object_cache views_data_export_object_cache views_object_cache"
SCRIPTNAME=$(basename $0)
# Set the backup directory.
BACKUPDIR=/var/local/backups/mysql
if [ ! -d "$BACKUPDIR" ]; then
logger "$SCRIPTNAME: Destination directory doesn't exist."
mkdir -p $BACKUPDIR
fi
# Set the group owner of the dumps
owner_group=staff
# Avoid taking all the CPU
#renice +19 -p $$ > /dev/null
# Get the password to connect to MySQL
if [ -r "$CRED_FILE" ]; then
OPTS="--defaults-file=$CRED_FILE $OPTS"
fi
# Make sure that another instance of this script isn't running.
pgrep $SCRIPTNAME
RET=$?
if [ "$RET" -eq 0 ]; then
logger "$SCRIPTNAME: Another instance of the process $SCRIPTNAME is running, exitting
to avoid multiples instances"
exit 1
fi
# Some DB don't need to be backup
SKIP_BACKUP_LIST="test|temp"
# Get the database names to backup.
DATABASES=`\ls -F /var/lib/mysql | grep '\/$' | sed 's/\///g' | grep -vwE $SKIP_BACKUP_LIST`
# Dump each database and compress it.
for DATABASE in $DATABASES
do
IGNORETABLES=$(for TABLE in $(echo $CACHETABLES); do echo -n " --ignore-table=$DATABASE.$TABLE"; done; echo -n " ")
DATE=$(date "+%y%m%d%H%M")
DST_FILE="$BACKUPDIR/$DATABASE-$DATE.sql.gz"
LINK_TARGET="$BACKUPDIR/$DATABASE-LATEST.sql.gz"
mysqldump $OPTS $IGNORETABLES $DATABASE | gzip > $DST_FILE
chmod 440 $DST_FILE
chgrp $owner_group $DST_FILE
ln -sf $DST_FILE $LINK_TARGET
done
# Delete old backups
KEEP_DAYS=60
find $BACKUPDIR -maxdepth 1 -type f -mtime +$KEEP_DAYS -delete
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment