mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2025-01-18 16:22:33 +01:00
commit
071e0ca61e
3 changed files with 24 additions and 13 deletions
18
README.md
18
README.md
|
@ -2,15 +2,15 @@
|
||||||
|
|
||||||
# mysql-cron-backup
|
# 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:
|
## Usage:
|
||||||
|
|
||||||
docker run -d \
|
docker container run -d \
|
||||||
--env MYSQL_USER=admin \
|
--env MYSQL_USER=root \
|
||||||
--env MYSQL_PASS=password \
|
--env MYSQL_PASS=my_password \
|
||||||
--link mysql
|
--link mysql
|
||||||
--volume /path/to/my/host/folder:/backup
|
--volume /path/to/my/backup/folder:/backup
|
||||||
fradelg/mysql-backup
|
fradelg/mysql-backup
|
||||||
|
|
||||||
## Variables
|
## Variables
|
||||||
|
@ -27,10 +27,10 @@ This image runs mysqldump to backup your databases periodically using cron. Data
|
||||||
|
|
||||||
## Restore from a backup
|
## 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
|
||||||
|
|
7
backup.sh
Normal file → Executable file
7
backup.sh
Normal file → Executable file
|
@ -5,12 +5,13 @@
|
||||||
DATE=$(date +%Y%m%d%H%M)
|
DATE=$(date +%Y%m%d%H%M)
|
||||||
echo "=> Backup started at $DATE"
|
echo "=> Backup started at $DATE"
|
||||||
databases=$(mysql -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
|
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" != _* ]]
|
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]]
|
||||||
then
|
then
|
||||||
echo "Dumping database: $db"
|
echo "Dumping database: $db"
|
||||||
FILENAME=/backup/$DATE.$db.sql
|
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
|
then
|
||||||
gzip -f "$FILENAME"
|
gzip -f "$FILENAME"
|
||||||
else
|
else
|
||||||
|
@ -21,7 +22,7 @@ done
|
||||||
|
|
||||||
if [ -n "$MAX_BACKUPS" ]
|
if [ -n "$MAX_BACKUPS" ]
|
||||||
then
|
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
|
do
|
||||||
TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" | sort | head -n 1)
|
TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" | sort | head -n 1)
|
||||||
echo "Backup $TARGET is deleted"
|
echo "Backup $TARGET is deleted"
|
||||||
|
|
12
restore.sh
Normal file → Executable file
12
restore.sh
Normal file → Executable file
|
@ -1,6 +1,16 @@
|
||||||
#!/bin/bash
|
#!/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"
|
echo "=> Restore database from $1"
|
||||||
if mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" < "$1" ;then
|
set -o pipefail
|
||||||
|
if gunzip --stdout "$1" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS"
|
||||||
|
then
|
||||||
echo "=> Restore succeeded"
|
echo "=> Restore succeeded"
|
||||||
else
|
else
|
||||||
echo "=> Restore failed"
|
echo "=> Restore failed"
|
||||||
|
|
Loading…
Reference in a new issue