mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2025-01-18 16:22:33 +01:00
fix issue #24 counting backups per DB
This commit is contained in:
parent
b2a78edcdc
commit
7f01602f0f
4 changed files with 18 additions and 15 deletions
|
@ -1,10 +1,7 @@
|
||||||
FROM alpine:3.7
|
FROM alpine:3.7
|
||||||
LABEL maintainer "Fco. Javier Delgado del Hoyo <frandelhoyo@gmail.com>"
|
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/*
|
||||||
|
|
||||||
RUN apk add --update bash mysql-client gzip openssl && rm -rf /var/cache/apk/* && mkdir /backup &&\
|
|
||||||
chmod u+x /backup.sh /restore.sh
|
|
||||||
|
|
||||||
ENV DOCKERIZE_VERSION v0.6.1
|
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 \
|
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" \
|
MYSQL_PORT="3306" \
|
||||||
TIMEOUT="10s"
|
TIMEOUT="10s"
|
||||||
|
|
||||||
|
COPY ["run.sh", "backup.sh", "restore.sh", "/"]
|
||||||
|
RUN mkdir /backup && chmod u+x /backup.sh /restore.sh
|
||||||
VOLUME ["/backup"]
|
VOLUME ["/backup"]
|
||||||
|
|
||||||
CMD dockerize -wait tcp://${MYSQL_HOST}:${MYSQL_PORT} -timeout ${TIMEOUT} /run.sh
|
CMD dockerize -wait tcp://${MYSQL_HOST}:${MYSQL_PORT} -timeout ${TIMEOUT} /run.sh
|
||||||
|
|
|
@ -40,19 +40,18 @@ services:
|
||||||
expose:
|
expose:
|
||||||
- 3306
|
- 3306
|
||||||
volumes:
|
volumes:
|
||||||
|
- data:/var/lib/mysql
|
||||||
# If there is not scheme, restore the last created backup (if exists)
|
# 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
|
- ${VOLUME_PATH}/backup/latest.${DATABASE_NAME}.sql.gz:/docker-entrypoint-initdb.d/database.sql.gz
|
||||||
environment:
|
environment:
|
||||||
- MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
|
- MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
|
||||||
- MYSQL_DATABASE=${DATABASE_NAME}
|
- MYSQL_DATABASE=${DATABASE_NAME}
|
||||||
- MYSQL_USER=${WORDPRESS_DB_USER}
|
|
||||||
- MYSQL_PASSWORD=${WORDPRESS_DB_PASSWORD}
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
mysql-cron-backup:
|
mysql-cron-backup:
|
||||||
image: fradelg/mysql-cron-backup
|
image: fradelg/mysql-cron-backup
|
||||||
depends_on:
|
depends_on:
|
||||||
- my_mariadb
|
- mariadb
|
||||||
volumes:
|
volumes:
|
||||||
- ${VOLUME_PATH}/backup:/backup
|
- ${VOLUME_PATH}/backup:/backup
|
||||||
environment:
|
environment:
|
||||||
|
@ -65,6 +64,8 @@ services:
|
||||||
- CRON_TIME=0 3 * * *
|
- CRON_TIME=0 3 * * *
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
data:
|
||||||
```
|
```
|
||||||
|
|
||||||
## Restore from a backup
|
## Restore from a backup
|
||||||
|
|
17
backup.sh
17
backup.sh
|
@ -4,8 +4,9 @@
|
||||||
|
|
||||||
DATE=$(date +%Y%m%d%H%M)
|
DATE=$(date +%Y%m%d%H%M)
|
||||||
echo "=> Backup started at $(date "+%Y-%m-%d %H:%M:%S")"
|
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)}}
|
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
|
DB_COUNTER=0
|
||||||
|
for db in ${DATABASES}
|
||||||
do
|
do
|
||||||
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]]
|
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]]
|
||||||
then
|
then
|
||||||
|
@ -18,6 +19,7 @@ do
|
||||||
echo "==> Creating symlink to latest backup: $(basename "$FILENAME".gz)"
|
echo "==> Creating symlink to latest backup: $(basename "$FILENAME".gz)"
|
||||||
rm "$LATEST" 2> /dev/null
|
rm "$LATEST" 2> /dev/null
|
||||||
cd /backup && ln -s $(basename "$FILENAME".gz) $(basename "$LATEST") && cd -
|
cd /backup && ln -s $(basename "$FILENAME".gz) $(basename "$LATEST") && cd -
|
||||||
|
DB_COUNTER=$(( DB_COUNTER + 1 ))
|
||||||
else
|
else
|
||||||
rm -rf "$FILENAME"
|
rm -rf "$FILENAME"
|
||||||
fi
|
fi
|
||||||
|
@ -26,13 +28,14 @@ done
|
||||||
|
|
||||||
if [ -n "$MAX_BACKUPS" ]
|
if [ -n "$MAX_BACKUPS" ]
|
||||||
then
|
then
|
||||||
echo "=> Max number of backups ("$MAX_BACKUPS") reached. Deleting oldest backups"
|
MAX_FILES=$(( MAX_BACKUPS * DB_COUNTER ))
|
||||||
while [ "$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | wc -l)" -gt "$MAX_BACKUPS" ]
|
while [ "$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | wc -l)" -gt "$MAX_FILES" ]
|
||||||
do
|
do
|
||||||
TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | sort | head -n 1)
|
TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | sort | head -n 1)
|
||||||
rm -rf "$TARGET"
|
echo "==> Max number of backups ($MAX_BACKUPS) reached. Deleting ${TARGET} ..."
|
||||||
echo "==> Backup $TARGET has been deleted"
|
rm -rf "${TARGET}"
|
||||||
|
echo "==> Backup ${TARGET} deleted"
|
||||||
done
|
done
|
||||||
fi
|
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
2
run.sh
|
@ -2,7 +2,7 @@
|
||||||
touch /mysql_backup.log
|
touch /mysql_backup.log
|
||||||
tail -F /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"
|
echo "=> Create a backup on the startup"
|
||||||
/backup.sh
|
/backup.sh
|
||||||
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
|
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
|
||||||
|
|
Loading…
Reference in a new issue