diff --git a/Dockerfile b/Dockerfile index 2e141e2..02961b2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,7 @@ -FROM alpine:3.7 +FROM alpine:3.10 LABEL maintainer "Fco. Javier Delgado del Hoyo " -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 diff --git a/README.md b/README.md index 1ce5e5a..4ffcfed 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/backup.sh b/backup.sh index b5be2ba..30314da 100755 --- a/backup.sh +++ b/backup.sh @@ -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")" diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..d0b693f --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,33 @@ +version: "2" +services: + mariadb: + image: mariadb:10 + container_name: my_mariadb + expose: + - 3306 + volumes: + - data:/var/lib/mysql + - ${VOLUME_PATH}/backup:/backup + environment: + - MYSQL_DATABASE=${DATABASE_NAME} + - MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD} + restart: unless-stopped + + backup: + build: . + image: fradelg/mysql-cron-backup + depends_on: + - mariadb + volumes: + - ${VOLUME_PATH}/backup:/backup + environment: + - MYSQL_HOST=my_mariadb + - MYSQL_USER=root + - MYSQL_PASS=${MARIADB_ROOT_PASSWORD} + - MAX_BACKUPS=1 + - INIT_BACKUP=1 + - CRON_TIME=0 0 * * * + restart: unless-stopped + +volumes: + data: \ No newline at end of file diff --git a/run.sh b/run.sh index f821cd6..999d3ef 100755 --- a/run.sh +++ b/run.sh @@ -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