Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 42 lines (34 sloc) 1.54 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")
[ -e "/etc/default/docker-mysql-backup" ] && source "/etc/default/docker-mysql-backup"
for container in $(docker ps --format '{{.Names}}\t{{.Image}}' | /bin/grep '\smysql' | awk '{print $1}')
do
BACKUP_DIR="${BACKUP_BASE}/docker_${container}"
mkdir -p "${BACKUP_DIR}"
docker exec "$container" sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' | ${GZIP} -9 > "${BACKUP_DIR}/${NOW}_complete.sql.gz"
# 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