fix issue #24 counting backups per DB

This commit is contained in:
Fco. Javier Delgado del Hoyo 2019-07-28 15:58:55 +02:00
parent b2a78edcdc
commit 7f01602f0f
4 changed files with 18 additions and 15 deletions

View file

@ -1,10 +1,7 @@
FROM alpine:3.7
LABEL maintainer "Fco. Javier Delgado del Hoyo <frandelhoyo@gmail.com>"
COPY ["run.sh", "backup.sh", "restore.sh", "/"]
RUN apk add --update bash mysql-client gzip openssl && rm -rf /var/cache/apk/* && mkdir /backup &&\
chmod u+x /backup.sh /restore.sh
RUN apk add --update bash mysql-client gzip openssl && rm -rf /var/cache/apk/*
ENV DOCKERIZE_VERSION v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
@ -16,6 +13,8 @@ ENV CRON_TIME="0 3 * * sun" \
MYSQL_PORT="3306" \
TIMEOUT="10s"
COPY ["run.sh", "backup.sh", "restore.sh", "/"]
RUN mkdir /backup && chmod u+x /backup.sh /restore.sh
VOLUME ["/backup"]
CMD dockerize -wait tcp://${MYSQL_HOST}:${MYSQL_PORT} -timeout ${TIMEOUT} /run.sh

View file

@ -40,19 +40,18 @@ services:
expose:
- 3306
volumes:
- data:/var/lib/mysql
# If there is not scheme, restore the last created backup (if exists)
- ${VOLUME_PATH}/backup/latest.${DATABASE_NAME}.sql.gz:/docker-entrypoint-initdb.d/database.sql.gz
environment:
- MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
- MYSQL_DATABASE=${DATABASE_NAME}
- MYSQL_USER=${WORDPRESS_DB_USER}
- MYSQL_PASSWORD=${WORDPRESS_DB_PASSWORD}
restart: unless-stopped
mysql-cron-backup:
image: fradelg/mysql-cron-backup
depends_on:
- my_mariadb
- mariadb
volumes:
- ${VOLUME_PATH}/backup:/backup
environment:
@ -65,6 +64,8 @@ services:
- CRON_TIME=0 3 * * *
restart: unless-stopped
volumes:
data:
```
## Restore from a backup

View file

@ -4,8 +4,9 @@
DATE=$(date +%Y%m%d%H%M)
echo "=> Backup started at $(date "+%Y-%m-%d %H:%M:%S")"
databases=${MYSQL_DATABASE:-${MYSQL_DB:-$(mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)}}
for db in $databases
DATABASES=${MYSQL_DATABASE:-${MYSQL_DB:-$(mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)}}
DB_COUNTER=0
for db in ${DATABASES}
do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]]
then
@ -18,6 +19,7 @@ do
echo "==> Creating symlink to latest backup: $(basename "$FILENAME".gz)"
rm "$LATEST" 2> /dev/null
cd /backup && ln -s $(basename "$FILENAME".gz) $(basename "$LATEST") && cd -
DB_COUNTER=$(( DB_COUNTER + 1 ))
else
rm -rf "$FILENAME"
fi
@ -26,13 +28,14 @@ done
if [ -n "$MAX_BACKUPS" ]
then
echo "=> Max number of backups ("$MAX_BACKUPS") reached. Deleting oldest backups"
while [ "$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | wc -l)" -gt "$MAX_BACKUPS" ]
MAX_FILES=$(( MAX_BACKUPS * DB_COUNTER ))
while [ "$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | wc -l)" -gt "$MAX_FILES" ]
do
TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | sort | head -n 1)
rm -rf "$TARGET"
echo "==> Backup $TARGET has been deleted"
echo "==> Max number of backups ($MAX_BACKUPS) reached. Deleting ${TARGET} ..."
rm -rf "${TARGET}"
echo "==> Backup ${TARGET} deleted"
done
fi
echo "=> Backup process finished at echo $(date "+%Y-%m-%d %H:%M:%S")"
echo "=> Backup process finished at $(date "+%Y-%m-%d %H:%M:%S")"

2
run.sh
View file

@ -2,7 +2,7 @@
touch /mysql_backup.log
tail -F /mysql_backup.log &
if [ -n "${INIT_BACKUP}" ]; then
if [ "${INIT_BACKUP}" -gt "0" ]; then
echo "=> Create a backup on the startup"
/backup.sh
elif [ -n "${INIT_RESTORE_LATEST}" ]; then