update docker and bash syntax

This commit is contained in:
Fco. Javier Delgado del Hoyo 2017-04-16 11:29:31 +02:00
parent b3bb91adf7
commit 44cdee455a
3 changed files with 22 additions and 12 deletions

View file

@ -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

View file

@ -5,7 +5,8 @@
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"
@ -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"

View file

@ -1,6 +1,15 @@
#!/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 if mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" < "$1"
then
echo "=> Restore succeeded" echo "=> Restore succeeded"
else else
echo "=> Restore failed" echo "=> Restore failed"