diff --git a/.gitignore b/.gitignore index 2a50d4c..855d1ac 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ backup data compose.yaml -.env \ No newline at end of file +.env +test.md +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 0887a35..bc45391 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ MySQL Backup tool, backup database to S3 or Object Storage ![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/jkaninda/mysql-bkup?style=flat-square) ![Docker Pulls](https://img.shields.io/docker/pulls/jkaninda/mysql-bkup?style=flat-square) -> Run on: +> Runs on: - Docker - Kubernetes diff --git a/build.sh b/build.sh index cdf7218..d43c9e9 100755 --- a/build.sh +++ b/build.sh @@ -8,4 +8,4 @@ fi docker build -f src/docker/Dockerfile -t jkaninda/mysql-bkup:$tag . -docker-compose up -d \ No newline at end of file +docker compose up -d \ No newline at end of file diff --git a/src/mysql_bkup.sh b/src/mysql_bkup.sh index 1e1097b..e9ad9c7 100755 --- a/src/mysql_bkup.sh +++ b/src/mysql_bkup.sh @@ -1,4 +1,5 @@ -#!/bin/sh +#!/usr/bin/env bash + set -e TIME=$(date +%Y%m%d_%H%M%S) MY_SQL_DUMP=/usr/bin/mysqldump @@ -109,14 +110,15 @@ flags() backup() { - if [ -z "${DB_HOST}"] || [ -z "${DB_NAME}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then - echo "Please make sure all required options are set " + if [[ -z $DB_HOST ]] || [[ -z $DB_NAME ]] || [[ -z $DB_USERNAME ]] || [[ -z $DB_PASSWORD ]]; then + echo "Please make sure all required environment variables are set " else ## Test database connection mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} -e"quit" ## Backup database mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} | gzip > ${STORAGE_PATH}/${DB_NAME}_${TIME}.sql.gz + echo "$TIME: ${DB_NAME}_${TIME}.sql.gz" | tee -a "${STORAGE_PATH}/history.txt" echo "Database has been saved" fi exit 0 @@ -124,8 +126,8 @@ exit 0 restore() { -if [ -z "${DB_HOST}" ] || [ -z "${DB_NAME}" ] || [ -z "${DB_USERNAME}" ] || [ -z "${DB_PASSWORD}" ]; then - echo "Please make sure all required options are set " +if [[ -z $DB_HOST ]] || [[ -z $DB_NAME ]] || [[ -z $DB_USERNAME ]] || [[ -z $DB_PASSWORD ]]; then + echo "Please make sure all required environment variables are set " else ## Restore database if [ -f "${STORAGE_PATH}/$FILE_NAME" ]; then @@ -157,7 +159,7 @@ s3_restore() mount_s3() { -if [ -z "${ACCESS_KEY}"] || [ -z "${SECRET_KEY}"]; then +if [[ -z $ACCESS_KEY ]] || [[ -z $SECRET_KEY ]]; then echo "Please make sure all environment variables are set " echo "BUCKETNAME=$BUCKETNAME \nACCESS_KEY=$nACCESS_KEY \nSECRET_KEY=$SECRET_KEY" else