From 44cdee455a70c36540e71477721d65a836c6ea09 Mon Sep 17 00:00:00 2001 From: "Fco. Javier Delgado del Hoyo" Date: Sun, 16 Apr 2017 11:29:31 +0200 Subject: [PATCH 1/3] update docker and bash syntax --- README.md | 18 +++++++++--------- backup.sh | 5 +++-- restore.sh | 11 ++++++++++- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fd4f511..a3971b5 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,15 @@ # mysql-cron-backup -This image runs mysqldump to backup your databases periodically using cron. Data is dumped to `/backup` so you can mount your backup volumes in this path. +This docker image runs mysqldump to backup your databases periodically using cron task manager. Backups are placed in `/backup` so you can mount your backup docker volume in this path. ## Usage: - docker run -d \ - --env MYSQL_USER=admin \ - --env MYSQL_PASS=password \ + docker container run -d \ + --env MYSQL_USER=root \ + --env MYSQL_PASS=my_password \ --link mysql - --volume /path/to/my/host/folder:/backup + --volume /path/to/my/backup/folder:/backup fradelg/mysql-backup ## Variables @@ -27,10 +27,10 @@ This image runs mysqldump to backup your databases periodically using cron. Data ## Restore from a backup -See the list of backups, you can run: +See the list of backups in your running docker container, just write in your favorite terminal: - docker exec backup ls /backup + docker container exec backup ls /backup -To restore database from a certain backup, simply run: +To restore a database from a certain backup, simply run: - docker exec backup /restore.sh /backup/2015.08.06.171901 + docker container exec backup /restore.sh /backup/201708060500.my_db.sql.gz diff --git a/backup.sh b/backup.sh index 62bfaa0..f0d427c 100644 --- a/backup.sh +++ b/backup.sh @@ -5,7 +5,8 @@ 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 +for db in $databases +do if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] then echo "Dumping database: $db" @@ -21,7 +22,7 @@ done if [ -n "$MAX_BACKUPS" ] then - while [ "$(find /backup -maxdepth 1 -name "*.sql.gz" | wc -l)" -gt "$MAX_BACKUPS" ]; + 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" diff --git a/restore.sh b/restore.sh index de67315..9657eba 100644 --- a/restore.sh +++ b/restore.sh @@ -1,6 +1,15 @@ #!/bin/bash +[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; } +[ -z "${MYSQL_PASS}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; } + +if [ "$#" -ne 1 ] +then + echo "You must pass the path of the backup file to restore" +fi + echo "=> Restore database from $1" -if mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" < "$1" ;then +if mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" < "$1" +then echo "=> Restore succeeded" else echo "=> Restore failed" From ae753e40a1736c32b19a0551cc36dba82f5feb30 Mon Sep 17 00:00:00 2001 From: "Markus \"Shorty\" Uckelmann" Date: Thu, 7 Sep 2017 17:10:15 +0200 Subject: [PATCH 2/3] 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. --- backup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 backup.sh diff --git a/backup.sh b/backup.sh old mode 100644 new mode 100755 index f0d427c..d2ca3dd --- a/backup.sh +++ b/backup.sh @@ -11,7 +11,7 @@ do then echo "Dumping database: $db" FILENAME=/backup/$DATE.$db.sql - if mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$db" > "$FILENAME" + if mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" --databases "$db" > "$FILENAME" then gzip -f "$FILENAME" else From 95c36b64933dc00176ab8f781ce09ac0bcb19212 Mon Sep 17 00:00:00 2001 From: "Markus \"Shorty\" Uckelmann" Date: Thu, 7 Sep 2017 17:18:55 +0200 Subject: [PATCH 3/3] Adds missing gunzip command This commit adds a gunzip command which extracts the gzip'd SQL dump and pipes it into the mysql command. Without the restore fails. --- restore.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) mode change 100644 => 100755 restore.sh diff --git a/restore.sh b/restore.sh old mode 100644 new mode 100755 index 9657eba..20561f2 --- a/restore.sh +++ b/restore.sh @@ -8,7 +8,8 @@ then fi echo "=> Restore database from $1" -if mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" < "$1" +set -o pipefail +if gunzip --stdout "$1" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" then echo "=> Restore succeeded" else