Last active
July 4, 2019 12:48
-
-
Save Eihen/53be043c217009d5705da28e07a1b1f4 to your computer and use it in GitHub Desktop.
Script to backup mysql databases from multiple connections defined in the .my.cnf file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Based on Gabriel O'Brien's mariadb-backup.sh (https://github.com/gmobrien/mariadb-backup.sh) | |
# The main difference is that this script use the mysql config groups to allow backups from multiple connections simultaneously | |
# Check the original license bellow (https://gist.github.com/Eihen/53be043c217009d5705da28e07a1b1f4#file-license) | |
# If not in violation of that license you can do whatever you want with this | |
set -ef -o pipefail | |
# Set the default values | |
SUFFIXES=() | |
TO_FLUSH=() | |
ROOT='/var/lib/mysql/dumps' | |
FLUSHS='--flush-logs --flush-privileges' | |
DUMP_OPTS='--events --routines --single-transaction' | |
KEEP=60 | |
HASH=sha256 | |
FORMAT='+%Y-%m-%d_%H-%M-%S' | |
while getopts dg:r:no:k:h:f: opt; do | |
case $opt in | |
\?) echo '-d: Use the defaults set for mysqldump/mysqlshow as one of the connections' | |
echo '-g <group>: Defaults group suffix to use (can be used multiple times)' | |
echo "-r <path>: Database backups root (Default: $ROOT)" | |
echo '-n: Do not flush things (use after the groups that should not be flushed)' | |
echo "-o <opts>: Dump options (Default: $DUMP_OPTS)" | |
echo "-k <amount>: Amount of backups to keep (Default: $KEEP_AMOUNT)" | |
echo "-h <alg>: Hash algorithm for the checksums (Default: $HASH)" | |
echo "-f <format>: Format for the timestamps (Default: $STAMP_FORMAT)" | |
exit 1;; | |
d) SUFFIXES+=('') | |
TO_FLUSH+=(true) | |
;; | |
g) SUFFIXES+=("${OPTARG}") | |
TO_FLUSH+=(true) | |
;; | |
r) ROOT="${OPTARG}";; | |
n) TO_FLUSH[$((${#TO_FLUSH[@]} - 1))]=false;; | |
o) DUMP_OPTS="${OPTARG}";; | |
k) KEEP="${OPTARG}";; | |
h) HASH="${OPTARG}";; | |
f) FORMAT="${OPTARG}";; | |
esac | |
done | |
# Set a secure mask for the backups | |
umask 0027 | |
# Create backup dir | |
STAMP="$(date $FORMAT)" | |
DIR="${ROOT}/${STAMP}" | |
mkdir -p "${DIR}" | |
echo "Backup location: $DIR" | |
function getDatabases() { | |
mysqlshow $1 \ | |
| sed -r '/Databases|information_schema|performance_schema|mysql/d' \ | |
| awk '{ print $2 }' \ | |
| sed -r '/^$/d' | |
} | |
function dumpDatabase() { | |
nice -n 19 mysqldump $2 $1 | nice -n 19 gzip | |
} | |
function checksum() { | |
CHECKSUM="$(openssl $1 $2 | cut -d' ' -f2)" | |
echo "$(basename $2) $CHECKSUM" | |
} | |
function getBackups() { | |
find "${ROOT}" -maxdepth 1 -type d -exec basename {} \; | |
} | |
# Dump databases | |
for SUFFIX in "${SUFFIXES[@]}"; do | |
if [ $SUFFIX ]; then | |
DEFAULTS="--defaults-group-suffix=_$SUFFIX" | |
# Avoid conflicts between database names in different connections | |
AT="@${SUFFIX}" | |
fi | |
# Get database list for this group | |
DATABASES=("$(getDatabases $DEFAULTS)") | |
COUNTER=0 | |
for DATABASE in ${DATABASES[@]}; do | |
printf 'Backing up %s%s...' $DATABASE $AT | |
ARGS="${DEFAULTS} ${DUMP_OPTS}" | |
if [ "${TO_FLUSH[$COUNTER]}" = true ]; then | |
ARGS+=" ${FLUSHES}" | |
fi | |
dumpDatabase $DATABASE $ARGS > "${DIR}/${DATABASE}${AT}.sql.gz" | |
checksum "${HASH}" "${DIR}/${DATABASE}${AT}.sql.gz" >> "${DIR}/${HASH^^}SUMS" | |
echo "Done!" | |
COUNTER=$((COUNTER + 1)) | |
done | |
done | |
# Create a symlink for the latest backup | |
echo "Creating latest symlink..." | |
rm -f "${ROOT}/latest" | |
ln -s "${DIR}" "${ROOT}/latest" | |
# Delete old backups if the amount exceed $KEEP | |
count="$(getBackups | wc -l)" | |
diff="$(($count - $KEEP))" | |
if [ "${diff}" -gt "0" ]; then | |
echo "Removing ${diff} old backup(s):" | |
for backup in "$(getBackups | sort | head -n ${diff})"; do | |
echo " - $backup" | |
rm -rf "${ROOT}/${backup}" | |
done | |
else | |
echo "No old backups to remove (Existing: ${count} of ${KEEP})" | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright (c) 2015 Gabriel M. O'Brien | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment