docker-mysql-cron-backup/backup.sh
Markus "Shorty" Uckelmann ae753e40a1 Fixes missing USE statement in SQL dump
This commit adds the additional `--databases` option to the mysqldump command.
This adds a `USE database` statement to the beginning of the SQL dump.
Without this one has to provide the database name at restore.
2017-09-07 17:10:15 +02:00

33 lines
1 KiB
Bash
Executable file

#!/bin/bash
[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
[ -z "${MYSQL_PASS}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
DATE=$(date +%Y%m%d%H%M)
echo "=> Backup started at $DATE"
databases=$(mysql -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
for db in $databases
do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]]
then
echo "Dumping database: $db"
FILENAME=/backup/$DATE.$db.sql
if mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" --databases "$db" > "$FILENAME"
then
gzip -f "$FILENAME"
else
rm -rf "$FILENAME"
fi
fi
done
if [ -n "$MAX_BACKUPS" ]
then
while [ "$(find /backup -maxdepth 1 -name "*.sql.gz" | wc -l)" -gt "$MAX_BACKUPS" ]
do
TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" | sort | head -n 1)
echo "Backup $TARGET is deleted"
rm -rf "$TARGET"
done
fi
echo "=> Backup done"