diff --git a/README.md b/README.md index 33f1ef4..16c3e3c 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,9 @@ MySQL Backup docker container image | --destination | -d | Set destination (default: local) | | --source | -s | Set source (default: local) | | --file | -f | Set file name for restoration | +| --source | -s | Set source (default: local) | +| --database | -db | Set database name | +| --port | -p | Set database port (default: 3306) | | --timeout | -t | Set timeout (default: 120s) | | --help | -h | Print this help message and exit | | --version | -V | Print version information and exit | @@ -43,7 +46,7 @@ services: command: - /bin/sh - -c - - bkup --operation backup + - bkup --operation backup -db mariadb volumes: - ./backup:/backup environment: @@ -54,6 +57,7 @@ services: - DB_PASSWORD=password ``` ## Restore database : + ```yaml version: '3' services: @@ -99,7 +103,7 @@ Simple S3 backup usage command: - /bin/sh - -c - - mysql_bkup --operation restore --source s3 -f database_20231217_115621.sql + - mysql_bkup --operation restore --source s3 -f database_20231217_115621.sql.gz volumes: - ./backup:/backup environment: @@ -149,4 +153,4 @@ spec: - name: DB_PASSWORD value: "password" restartPolicy: Never -``` +``` \ No newline at end of file diff --git a/src/mysql_bkup.sh b/src/mysql_bkup.sh index eb566eb..eda6d79 100755 --- a/src/mysql_bkup.sh +++ b/src/mysql_bkup.sh @@ -8,6 +8,7 @@ export OPERATION=backup export DESTINATION=local export DESTINATION_DIR=/backup export SOURCE=local +export FILE_COMPRESION=true usage_info() { echo "Usage: \\" @@ -41,6 +42,8 @@ help() echo " -d|--destination -- Set destination (default: local)" echo " -s|--source -- Set source (default: local)" echo " -s|--file -- Set file name " + echo " -db|--database -- Set database name " + echo " -p|--port -- Set database port (default: 3306)" echo " -t|--timeout -- Set timeout (default: 120s)" echo " -h|--help -- Print this help message and exit" echo " -v|--version -- Print version information and exit" @@ -74,6 +77,11 @@ flags() [ $# = 0 ] && error "No file specified - file to restore" export FILE_NAME="$1" shift;; + (-db|--database) + shift + [ $# = 0 ] && error "No database name specified" + export DB_DATABASE="$1" + shift;; (-t|--timeout) shift [ $# = 0 ] && error "No timeout specified" @@ -96,7 +104,7 @@ backup() echo "Please make sure all required options are set " else ## Backup database - mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} > ${DESTINATION_DIR}/${DB_DATABASE}_${TIME}.sql + mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} | gzip > ${DESTINATION_DIR}/${DB_DATABASE}_${TIME}.sql.gz echo "Database has been saved" fi exit @@ -109,7 +117,11 @@ if [ -z "${DB_HOST}" ] || [ -z "${DB_DATABASE}" ] || [ -z "${DB_USERNAME}" ] | else ## Restore database if [ -f "${DESTINATION_DIR}/$FILE_NAME" ]; then - cat ${DESTINATION_DIR}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} + if gzip -t ${DESTINATION_DIR}/$FILE_NAME; then + zcat ${DESTINATION_DIR}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} + else + cat ${DESTINATION_DIR}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} + fi echo "Database has been restored" else echo "Error, file not found in /backup folder" @@ -139,11 +151,15 @@ 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 -echo "$ACCESS_KEY:$SECRET_KEY" | tee /etc/passwd-s3fs -chmod 600 /etc/passwd-s3fs -echo "Mounting Object storage in /s3mnt .... " -s3fs $BUCKETNAME /s3mnt -o passwd_file=/etc/passwd-s3fs -o use_cache=/tmp/s3cache -o allow_other -o url=$S3_ENDPOINT -o use_path_request_style -ls /s3mnt | wc -l + echo "$ACCESS_KEY:$SECRET_KEY" | tee /etc/passwd-s3fs + chmod 600 /etc/passwd-s3fs + echo "Mounting Object storage in /s3mnt .... " + if [ -z "$(ls -A /s3mnt)" ]; then + s3fs $BUCKETNAME /s3mnt -o passwd_file=/etc/passwd-s3fs -o use_cache=/tmp/s3cache -o allow_other -o url=$S3_ENDPOINT -o use_path_request_style + ls /s3mnt | wc -l + else + echo "Object storage already mounted in /s3mnt" + fi export DESTINATION_DIR=/s3mnt fi }