Refactoring

This commit is contained in:
2023-12-22 06:04:35 +01:00
parent a644fe2bd7
commit c8ffec2dc9
4 changed files with 32 additions and 38 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -36,4 +36,4 @@ jobs:
platforms: linux/amd64,linux/arm64 platforms: linux/amd64,linux/arm64
tags: | tags: |
"${{env.BUILDKIT_IMAGE}}:latest" "${{env.BUILDKIT_IMAGE}}:latest"
"${{env.BUILDKIT_IMAGE}}:v1.0" "${{env.BUILDKIT_IMAGE}}:v0.2"

View File

@@ -24,7 +24,7 @@ MySQL Backup tool, backup database to S3 or Object Storage
| --storage | -s | Set storage. local or s3 (default: local) | | --storage | -s | Set storage. local or s3 (default: local) |
| --file | -f | Set file name for restoration | | --file | -f | Set file name for restoration |
| --path | | Set s3 path without file name. eg: /custom_path | | --path | | Set s3 path without file name. eg: /custom_path |
| --database | -db | Set database name | | --dbname | -d | Set database name |
| --port | -p | Set database port (default: 3306) | | --port | -p | Set database port (default: 3306) |
| --timeout | -t | Set timeout (default: 60s) | | --timeout | -t | Set timeout (default: 60s) |
| --help | -h | Print this help message and exit | | --help | -h | Print this help message and exit |
@@ -35,20 +35,20 @@ MySQL Backup tool, backup database to S3 or Object Storage
Simple backup usage Simple backup usage
```sh ```sh
bkup --operation backup bkup --operation backup --dbname databas_name
``` ```
```sh ```sh
bkup -o backup bkup -o backup -d databas_name
``` ```
### S3 ### S3
```sh ```sh
bkup --operation backup --storage s3 bkup --operation backup --storage s3 --dbname databas_name
``` ```
## Docker run: ## Docker run:
```sh ```sh
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup:latest bkup -o backup -db database_name docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup:latest bkup -o backup -d database_name
``` ```
## Docker compose file: ## Docker compose file:
@@ -57,25 +57,24 @@ version: '3'
services: services:
mariadb: mariadb:
container_name: mariadb container_name: mariadb
image: mariadb:latest image: mariadb
environment: environment:
MYSQL_DATABASE: mariadb MYSQL_DATABASE: mariadb
MYSQL_USER: mariadb MYSQL_USER: mariadb
MYSQL_PASSWORD: password MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password MYSQL_ROOT_PASSWORD: password
mysql-bkup: mysql-bkup:
image: jkaninda/mysql-bkup:latest image: jkaninda/mysql-bkup
container_name: mysql-bkup container_name: mysql-bkup
command: command:
- /bin/sh - /bin/sh
- -c - -c
- bkup --operation backup -db mariadb - bkup --operation backup -d databas_name
volumes: volumes:
- ./backup:/backup - ./backup:/backup
environment: environment:
- DB_PORT=3306 - DB_PORT=3306
- DB_HOST=mariadb - DB_HOST=mariadb
- DB_DATABASE=mariadb
- DB_USERNAME=mariadb - DB_USERNAME=mariadb
- DB_PASSWORD=password - DB_PASSWORD=password
``` ```
@@ -84,7 +83,7 @@ services:
Simple database restore operation usage Simple database restore operation usage
```sh ```sh
bkup --operation restore --file database_20231217_115621.sql bkup --operation restore --file database_20231217_115621.sql --dbname databas_name
``` ```
```sh ```sh
@@ -99,7 +98,7 @@ bkup --operation restore --storage s3 --file database_20231217_115621.sql
## Docker run: ## Docker run:
```sh ```sh
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup:latest bkup -o backup -db database_name -f napata_20231219_022941.sql.gz docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup bkup -o backup -d database_name -f napata_20231219_022941.sql.gz
``` ```
## Docker compose file: ## Docker compose file:
@@ -116,19 +115,19 @@ services:
MYSQL_PASSWORD: password MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password MYSQL_ROOT_PASSWORD: password
mysql-bkup: mysql-bkup:
image: jkaninda/mysql-bkup:latest image: jkaninda/mysql-bkup
container_name: mysql-bkup container_name: mysql-bkup
command: command:
- /bin/sh - /bin/sh
- -c - -c
- bkup --operation restore --file database_20231217_115621.sql - bkup --operation restore --file database_20231217_115621.sql --dbname databas_name
volumes: volumes:
- ./backup:/backup - ./backup:/backup
environment: environment:
#- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command #- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command
- DB_PORT=3306 - DB_PORT=3306
- DB_HOST=mariadb - DB_HOST=mariadb
- DB_DATABASE=mariadb - DB_NAME=mariadb
- DB_USERNAME=mariadb - DB_USERNAME=mariadb
- DB_PASSWORD=password - DB_PASSWORD=password
``` ```
@@ -140,17 +139,18 @@ docker-compose up -d
## Backup to S3 ## Backup to S3
```sh ```sh
docker run --rm --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=db_hostname" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" -e "ACCESS_KEY=your_access_key" -e "SECRET_KEY=your_secret_key" -e "BUCKETNAME=your_bucket_name" -e "S3_ENDPOINT=https://eu2.contabostorage.com" jkaninda/mysql-bkup:latest bkup -o backup -s s3 -db invoice docker run --rm --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=db_hostname" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" -e "ACCESS_KEY=your_access_key" -e "SECRET_KEY=your_secret_key" -e "BUCKETNAME=your_bucket_name" -e "S3_ENDPOINT=https://eu2.contabostorage.com" jkaninda/mysql-bkup:latest bkup -o backup -s s3 -d invoice
``` ```
> To change s3 backup path add this flag : --path myPath . default path is /mysql_bkup > To change s3 backup path add this flag : --path myPath . default path is /mysql_bkup
Simple S3 backup usage Simple S3 backup usage
```sh ```sh
bkup --operation backup --storage s3 --database mydatabase bkup --operation backup --storage s3 --dbname mydatabase
``` ```
```yaml ```yaml
mysql-bkup: mysql-bkup:
image: jkaninda/mysql-bkup:latest image: jkaninda/mysql-bkup
container_name: mysql-bkup container_name: mysql-bkup
tty: true tty: true
privileged: true privileged: true
@@ -163,7 +163,7 @@ bkup --operation backup --storage s3 --database mydatabase
environment: environment:
- DB_PORT=3306 - DB_PORT=3306
- DB_HOST=mysql - DB_HOST=mysql
- DB_DATABASE=mariadb - DB_NAME=mariadb
- DB_USERNAME=mariadb - DB_USERNAME=mariadb
- DB_PASSWORD=password - DB_PASSWORD=password
- ACCESS_KEY=${ACCESS_KEY} - ACCESS_KEY=${ACCESS_KEY}
@@ -186,7 +186,7 @@ DB_HOST='db_hostname'
DB_NAME='db_name' DB_NAME='db_name'
BACKUP_DIR='/some/path/backup/' BACKUP_DIR='/some/path/backup/'
docker run --rm --name mysql-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/mysql-bkup:latest bkup -o backup -db $DB_NAME docker run --rm --name mysql-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/mysql-bkup:latest bkup -o backup -d $DB_NAME
``` ```
```sh ```sh
@@ -227,7 +227,7 @@ spec:
value: "3306" value: "3306"
- name: DB_HOST - name: DB_HOST
value: "mysql-svc" value: "mysql-svc"
- name: DB_DATABASE - name: DB_NAME
value: "mariadb" value: "mariadb"
- name: DB_USERNAME - name: DB_USERNAME
value: "mariadb" value: "mariadb"

View File

@@ -5,10 +5,8 @@ MY_SQL_DUMP=/usr/bin/mysqldump
arg0=$(basename "$0" .sh) arg0=$(basename "$0" .sh)
blnk=$(echo "$arg0" | sed 's/./ /g') blnk=$(echo "$arg0" | sed 's/./ /g')
export OPERATION=backup export OPERATION=backup
export DESTINATION=local
export STORAGE=local export STORAGE=local
export STORAGE_PATH=/backup export STORAGE_PATH=/backup
export SOURCE=local
export S3_PATH=/mysql-bkup export S3_PATH=/mysql-bkup
export TIMEOUT=60 export TIMEOUT=60
export FILE_COMPRESION=true export FILE_COMPRESION=true
@@ -45,7 +43,7 @@ help()
echo " -s |--storage -- Set storage (default: local)" echo " -s |--storage -- Set storage (default: local)"
echo " -f |--file -- Set file name " echo " -f |--file -- Set file name "
echo " |--path -- Set s3 path, without file name" echo " |--path -- Set s3 path, without file name"
echo " -db|--database -- Set database name " echo " -d |--dbname -- Set database name "
echo " -p |--port -- Set database port (default: 3306)" 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"
@@ -63,18 +61,14 @@ flags()
[ $# = 0 ] && error "No operation specified - restore or backup" [ $# = 0 ] && error "No operation specified - restore or backup"
export OPERATION="$1" export OPERATION="$1"
shift;; shift;;
(-d|--destination) (-d|--dbname)
shift shift
[ $# = 0 ] && error "No destination specified - local or s3 | default local" [ $# = 0 ] && error "No database name specified"
export DESTINATION="$1" export DB_NAME="$1"
export SOURCE="$1"
export STORAGE="$1"
shift;; shift;;
(-s|--storage) (-s|--storage)
shift shift
[ $# = 0 ] && error "No storage specified - local or s3 | default local" [ $# = 0 ] && error "No storage specified - local or s3 | default local"
export SOURCE="$1"
export DESTINATION="$1"
export STORAGE="$1" export STORAGE="$1"
shift;; shift;;
(-f|--file) (-f|--file)
@@ -90,7 +84,7 @@ flags()
(-db|--database) (-db|--database)
shift shift
[ $# = 0 ] && error "No database name specified" [ $# = 0 ] && error "No database name specified"
export DB_DATABASE="$1" export DB_NAME="$1"
shift;; shift;;
(-p|--port) (-p|--port)
shift shift
@@ -115,14 +109,14 @@ flags()
backup() backup()
{ {
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then if [ -z "${DB_HOST}"] || [ -z "${DB_NAME}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
echo "Please make sure all required options are set " echo "Please make sure all required options are set "
else else
## Test database connection ## Test database connection
mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} -e"quit" mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} -e"quit"
## Backup database ## Backup database
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} | gzip > ${STORAGE_PATH}/${DB_DATABASE}_${TIME}.sql.gz mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} | gzip > ${STORAGE_PATH}/${DB_NAME}_${TIME}.sql.gz
echo "Database has been saved" echo "Database has been saved"
fi fi
exit 0 exit 0
@@ -130,15 +124,15 @@ exit 0
restore() restore()
{ {
if [ -z "${DB_HOST}" ] || [ -z "${DB_DATABASE}" ] || [ -z "${DB_USERNAME}" ] || [ -z "${DB_PASSWORD}" ]; then if [ -z "${DB_HOST}" ] || [ -z "${DB_NAME}" ] || [ -z "${DB_USERNAME}" ] || [ -z "${DB_PASSWORD}" ]; then
echo "Please make sure all required options are set " echo "Please make sure all required options are set "
else else
## Restore database ## Restore database
if [ -f "${STORAGE_PATH}/$FILE_NAME" ]; then if [ -f "${STORAGE_PATH}/$FILE_NAME" ]; then
if gzip -t ${STORAGE_PATH}/$FILE_NAME; then if gzip -t ${STORAGE_PATH}/$FILE_NAME; then
zcat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} zcat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME}
else else
cat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} cat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME}
fi fi
echo "Database has been restored" echo "Database has been restored"
else else