diff --git a/Makefile b/Makefile index 6516d7c..2433f0a 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ compile: docker-build: docker build -f docker/Dockerfile -t jkaninda/mysql-bkup:latest . -docker-run: #docker-build +docker-run: docker-build docker run --rm --network web --name mysql-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} backup --prune --keep-last 2 docker-restore: docker-build docker run --rm --network web --name mysql-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} restore -f ${FILE_NAME} diff --git a/README.md b/README.md index 70fae0d..ce67294 100644 --- a/README.md +++ b/README.md @@ -85,57 +85,45 @@ networks: ``` ## Deploy on Kubernetes -For Kubernetes, you don't need to run it in scheduled mode. You can deploy it as CronJob. +For Kubernetes, you don't need to run it in scheduled mode. You can deploy it as Job or CronJob. -### Simple Kubernetes CronJob usage: +### Simple Kubernetes backup Job : ```yaml apiVersion: batch/v1 -kind: CronJob +kind: Job metadata: - name: bkup-job + name: backup spec: - schedule: "0 1 * * *" - jobTemplate: + template: spec: - template: - spec: - containers: - - name: mysql-bkup - # In production, it is advised to lock your image tag to a proper - # release version instead of using `latest`. - # Check https://github.com/jkaninda/mysql-bkup/releases - # for a list of available releases. - image: jkaninda/mysql-bkup - command: - - /bin/sh - - -c - - mysql-bkup backup -s s3 --path /custom_path - env: - - name: DB_PORT - value: "5432" - - name: DB_HOST - value: "" - - name: DB_NAME - value: "" - - name: DB_USERNAME - value: "" - # Please use secret! - - name: DB_PASSWORD - value: "" - - name: AWS_S3_ENDPOINT - value: "https://s3.amazonaws.com" - - name: AWS_S3_BUCKET_NAME - value: "xxx" - - name: AWS_REGION - value: "us-west-2" - - name: AWS_ACCESS_KEY - value: "xxxx" - - name: AWS_SECRET_KEY - value: "xxxx" - - name: AWS_DISABLE_SSL - value: "false" - restartPolicy: Never + containers: + - name: mysql-bkup + # In production, it is advised to lock your image tag to a proper + # release version instead of using `latest`. + # Check https://github.com/jkaninda/mysql-bkup/releases + # for a list of available releases. + image: jkaninda/mysql-bkup + command: + - bkup + - backup + resources: + limits: + memory: "128Mi" + cpu: "500m" + env: + - name: DB_PORT + value: "3306" + - name: DB_HOST + value: "" + - name: DB_NAME + value: "dbname" + - name: DB_USERNAME + value: "username" + # Please use secret! + - name: DB_PASSWORD + value: "" + restartPolicy: Never ``` ## Available image registries diff --git a/docker/Dockerfile b/docker/Dockerfile index 65f894c..5ac701a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -31,7 +31,9 @@ ENV SSH_HOST_NAME="" ENV SSH_IDENTIFY_FILE="" ENV SSH_PORT="22" ARG DEBIAN_FRONTEND=noninteractive -ENV VERSION="v1.2.1" +ENV VERSION="v1.2.2" +ENV BACKUP_CRON_EXPRESSION="" +ENV GNUPGHOME="/tmp/gnupg" ARG WORKDIR="/app" ARG BACKUPDIR="/backup" ARG BACKUP_TMP_DIR="/tmp/backup" @@ -40,7 +42,6 @@ ARG BACKUP_CRON_SCRIPT="/usr/local/bin/backup_cron.sh" LABEL author="Jonas Kaninda" RUN apt-get update -qq -#RUN apt-get install build-essential libcurl4-openssl-dev libxml2-dev mime-support -y RUN apt install mysql-client supervisor cron gnupg -y # Clear cache @@ -48,14 +49,16 @@ RUN apt-get clean && rm -rf /var/lib/apt/lists/* RUN mkdir $WORKDIR RUN mkdir $BACKUPDIR -RUN mkdir -p $BACKUP_TMP_DIR +RUN mkdir -p $BACKUP_TMP_DIR && \ + mkdir -p $GNUPGHOME RUN chmod 777 $WORKDIR RUN chmod 777 $BACKUPDIR RUN chmod 777 $BACKUP_TMP_DIR RUN touch $BACKUP_CRON && \ touch $BACKUP_CRON_SCRIPT && \ chmod 777 $BACKUP_CRON && \ - chmod 777 $BACKUP_CRON_SCRIPT + chmod 777 $BACKUP_CRON_SCRIPT && \ + chmod 777 $GNUPGHOME COPY --from=build /app/mysql-bkup /usr/local/bin/mysql-bkup RUN chmod +x /usr/local/bin/mysql-bkup @@ -65,4 +68,18 @@ RUN ln -s /usr/local/bin/mysql-bkup /usr/local/bin/bkup ADD docker/supervisord.conf /etc/supervisor/supervisord.conf WORKDIR $WORKDIR -ENTRYPOINT ["/usr/local/bin/mysql-bkup"] +# Create backup shell script +COPY <> %s" -`, os.Getenv("SCHEDULE_PERIOD"), cronLogFile) +`, os.Getenv("BACKUP_CRON_EXPRESSION"), cronLogFile) if err := utils.WriteToFile(cronJob, cronContent); err != nil { utils.Fatal("Error writing to %s: %v\n", cronJob, err)