mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2025-01-18 16:22:33 +01:00
Merge pull request #47 from Stelage/master
Allow restoring without specifying DATABASE_NAME
This commit is contained in:
commit
b9c13f99a0
2 changed files with 24 additions and 4 deletions
15
README.md
15
README.md
|
@ -74,13 +74,17 @@ volumes:
|
||||||
|
|
||||||
## Restore from a backup
|
## Restore from a backup
|
||||||
|
|
||||||
|
### List all available backups :
|
||||||
|
|
||||||
See the list of backups in your running docker container, just write in your favorite terminal:
|
See the list of backups in your running docker container, just write in your favorite terminal:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker container exec <your_mysql_backuo_container_name> ls /backup
|
docker container exec <your_mysql_backup_container_name> ls /backup
|
||||||
```
|
```
|
||||||
|
|
||||||
To restore a database from a certain backup you have to specify the database name in the variable MYSQL_DATABASE:
|
### Restore using a compose file
|
||||||
|
|
||||||
|
To restore a database from a certain backup you may have to specify the database name in the variable MYSQL_DATABASE:
|
||||||
|
|
||||||
```YAML
|
```YAML
|
||||||
mysql-cron-backup:
|
mysql-cron-backup:
|
||||||
|
@ -96,3 +100,10 @@ mysql-cron-backup:
|
||||||
- MYSQL_PASS=${MARIADB_ROOT_PASSWORD}
|
- MYSQL_PASS=${MARIADB_ROOT_PASSWORD}
|
||||||
- MYSQL_DATABASE=${DATABASE_NAME}
|
- MYSQL_DATABASE=${DATABASE_NAME}
|
||||||
```
|
```
|
||||||
|
### Restore using a docker command
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker container exec <your_mysql_backup_container_name> /restore.sh backup/<your_sql_backup_gz_file>
|
||||||
|
```
|
||||||
|
|
||||||
|
if no database name is specified, `restore.sh` will try to find the database name from the backup file.
|
13
restore.sh
13
restore.sh
|
@ -7,10 +7,19 @@ then
|
||||||
echo "You must pass the path of the backup file to restore"
|
echo "You must pass the path of the backup file to restore"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "=> Restore database from $1"
|
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
||||||
|
SQL=$(gunzip -c "$1")
|
||||||
DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}}
|
DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}}
|
||||||
if gunzip --stdout "$1" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME"
|
if [ -z "${DB_NAME}"]
|
||||||
|
then
|
||||||
|
DB_NAME=$(echo "$SQL" | grep -oE '(Database: (\w*))' | cut -d ' ' -f 2)
|
||||||
|
fi
|
||||||
|
[ -z "${DB_NAME}" ] && { echo "=> database name cannot be found" && exit 1; }
|
||||||
|
|
||||||
|
echo "=> Restore database $DB_NAME from $1"
|
||||||
|
|
||||||
|
if echo "$SQL" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$DB_NAME"
|
||||||
then
|
then
|
||||||
echo "=> Restore succeeded"
|
echo "=> Restore succeeded"
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in a new issue