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"