Enable database backup compression

This commit is contained in:
2023-12-17 19:22:09 +01:00
parent fb941f24bb
commit 54cb493448
2 changed files with 30 additions and 10 deletions

View File

@@ -21,6 +21,9 @@ MySQL Backup docker container image
| --destination | -d | Set destination (default: local) | | --destination | -d | Set destination (default: local) |
| --source | -s | Set source (default: local) | | --source | -s | Set source (default: local) |
| --file | -f | Set file name for restoration | | --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) | | --timeout | -t | Set timeout (default: 120s) |
| --help | -h | Print this help message and exit | | --help | -h | Print this help message and exit |
| --version | -V | Print version information and exit | | --version | -V | Print version information and exit |
@@ -43,7 +46,7 @@ services:
command: command:
- /bin/sh - /bin/sh
- -c - -c
- bkup --operation backup - bkup --operation backup -db mariadb
volumes: volumes:
- ./backup:/backup - ./backup:/backup
environment: environment:
@@ -54,6 +57,7 @@ services:
- DB_PASSWORD=password - DB_PASSWORD=password
``` ```
## Restore database : ## Restore database :
```yaml ```yaml
version: '3' version: '3'
services: services:
@@ -99,7 +103,7 @@ Simple S3 backup usage
command: command:
- /bin/sh - /bin/sh
- -c - -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: volumes:
- ./backup:/backup - ./backup:/backup
environment: environment:
@@ -149,4 +153,4 @@ spec:
- name: DB_PASSWORD - name: DB_PASSWORD
value: "password" value: "password"
restartPolicy: Never restartPolicy: Never
``` ```

View File

@@ -8,6 +8,7 @@ export OPERATION=backup
export DESTINATION=local export DESTINATION=local
export DESTINATION_DIR=/backup export DESTINATION_DIR=/backup
export SOURCE=local export SOURCE=local
export FILE_COMPRESION=true
usage_info() usage_info()
{ {
echo "Usage: \\" echo "Usage: \\"
@@ -41,6 +42,8 @@ help()
echo " -d|--destination -- Set destination (default: local)" echo " -d|--destination -- Set destination (default: local)"
echo " -s|--source -- Set source (default: local)" echo " -s|--source -- Set source (default: local)"
echo " -s|--file -- Set file name " 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 " -t|--timeout -- Set timeout (default: 120s)"
echo " -h|--help -- Print this help message and exit" echo " -h|--help -- Print this help message and exit"
echo " -v|--version -- Print version information and exit" echo " -v|--version -- Print version information and exit"
@@ -74,6 +77,11 @@ flags()
[ $# = 0 ] && error "No file specified - file to restore" [ $# = 0 ] && error "No file specified - file to restore"
export FILE_NAME="$1" export FILE_NAME="$1"
shift;; shift;;
(-db|--database)
shift
[ $# = 0 ] && error "No database name specified"
export DB_DATABASE="$1"
shift;;
(-t|--timeout) (-t|--timeout)
shift shift
[ $# = 0 ] && error "No timeout specified" [ $# = 0 ] && error "No timeout specified"
@@ -96,7 +104,7 @@ backup()
echo "Please make sure all required options are set " echo "Please make sure all required options are set "
else else
## Backup database ## 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" echo "Database has been saved"
fi fi
exit exit
@@ -109,7 +117,11 @@ if [ -z "${DB_HOST}" ] || [ -z "${DB_DATABASE}" ] || [ -z "${DB_USERNAME}" ] |
else else
## Restore database ## Restore database
if [ -f "${DESTINATION_DIR}/$FILE_NAME" ]; then 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" echo "Database has been restored"
else else
echo "Error, file not found in /backup folder" 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 "Please make sure all environment variables are set "
echo "BUCKETNAME=$BUCKETNAME \nACCESS_KEY=$nACCESS_KEY \nSECRET_KEY=$SECRET_KEY" echo "BUCKETNAME=$BUCKETNAME \nACCESS_KEY=$nACCESS_KEY \nSECRET_KEY=$SECRET_KEY"
else else
echo "$ACCESS_KEY:$SECRET_KEY" | tee /etc/passwd-s3fs echo "$ACCESS_KEY:$SECRET_KEY" | tee /etc/passwd-s3fs
chmod 600 /etc/passwd-s3fs chmod 600 /etc/passwd-s3fs
echo "Mounting Object storage in /s3mnt .... " 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 if [ -z "$(ls -A /s3mnt)" ]; then
ls /s3mnt | wc -l 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 export DESTINATION_DIR=/s3mnt
fi fi
} }