Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 55 lines (45 sloc) 2.34 KB
#!/bin/bash
#
# Copyright 2017 Martin Scharm
#
# This file is part of Docker MySQL-Backup.
# <https://github.com/binfalse/docker-mysql-backup>
# <https://binfalse.de/2017/02/06/docker-mysql-backup/>
#
# Docker MySQL-Backup is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Docker MySQL-Backup is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Docker MySQL-Backup. If not, see <http://www.gnu.org/licenses/>.
BACKUP_BASE="/srv/backup/mysql/"
GZIP="/bin/gzip"
NOW=$(date +"%Y-%m-%d_%H-%M")
INDIVIDUAL_DBS=1
[ -e "/etc/default/docker-mysql-backup" ] && source "/etc/default/docker-mysql-backup"
for container in $(docker ps --format '{{.Names}}\t{{.Image}}' | /bin/grep '\s\(mysql\|mariadb\)' | awk '{print $1}')
do
BACKUP_DIR="${BACKUP_BASE}/docker_${container}"
mkdir -p "${BACKUP_DIR}"
docker exec "$container" sh -c 'if [ "$MYSQL_ROOT_PASSWORD_FILE" ]; then MYSQL_ROOT_PASSWORD=`cat $MYSQL_ROOT_PASSWORD_FILE`; fi; exec mysqldump --all-databases -f -uroot -p"$MYSQL_ROOT_PASSWORD"' | ${GZIP} -9 > "${BACKUP_DIR}/${NOW}_complete.sql.gz"
if [ "$INDIVIDUAL_DBS" -eq 1 ]
then
DBS=$(docker exec "$container" sh -c 'if [ "$MYSQL_ROOT_PASSWORD_FILE" ]; then MYSQL_ROOT_PASSWORD=`cat $MYSQL_ROOT_PASSWORD_FILE`; fi; exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -Bse "show databases"')
for db in $DBS
do
[ "$db" == "information_schema" ] && continue
[ "$db" == "performance_schema" ] && continue
docker exec "$container" sh -c 'if [ "$MYSQL_ROOT_PASSWORD_FILE" ]; then MYSQL_ROOT_PASSWORD=`cat $MYSQL_ROOT_PASSWORD_FILE`; fi; exec mysqldump -f -uroot -p"$MYSQL_ROOT_PASSWORD" '"$db" | ${GZIP} -9 > "${BACKUP_DIR}/${NOW}_${db}.sql.gz"
done
fi
# delete all that are older than 30 days and the date does not end with a 2 -> keep ~3 per month
for file in $(find $BACKUP_DIR -mtime +30 | grep -v '20..-..-.2_')
do
rm $file
done
done