Releases: jkaninda/mysql-bkup
v0.6
v0.5
v0.4
v0.3
Scheduled backup with docker
To run in scheduled mode you need to add --mode scheduled end specify the period by adding --period "*/30 * * * *"
For Kubernetes, you don't need to run it in scheduled mode.
Run it as CronJob without flag mode or with the flag by adding --mode default
Run in Scheduled mode
This tool can be run as CronJob in Kubernetes for a regular backup which makes deployment on Kubernetes easy as Kubernetes has CronJob resources.
For Docker, you need to run it in scheduled mode by adding --mode scheduled
flag and specify the periodical backup time by adding --period "0 1 * * *"
flag.
Make an automated backup on Docker
Syntax of crontab (field description)
The syntax is:
- 1: Minute (0-59)
- 2: Hours (0-23)
- 3: Day (0-31)
- 4: Month (0-12 [12 == December])
- 5: Day of the week(0-7 [7 or 0 == sunday])
Easy to remember format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
At every 30th minute
*/30 * * * *
“At minute 0.” every hour
0 * * * *
“At 01:00.” every day
0 1 * * *
Example of scheduled mode
Docker run :
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 bkup --operation backup --dbname $DB_NAME --mode scheduled --period "0 1 * * *"
With Docker compose
version: "3"
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
privileged: true
devices:
- "/dev/fuse"
command:
- /bin/sh
- -c
- bkup --operation backup --storage s3 --path /mys3_custome_path --dbname database_name --mode scheduled --period "*/30 * * * *"
environment:
- DB_PORT=3306
- DB_HOST=mysqlhost
- DB_USERNAME=userName
- DB_PASSWORD=${DB_PASSWORD}
- ACCESS_KEY=${ACCESS_KEY}
- SECRET_KEY=${SECRET_KEY}
- BUCKETNAME=${BUCKETNAME}
- S3_ENDPOINT=${S3_ENDPOINT}
v0.2
MySQL Backup
MySQL Backup tool, backup database to S3 or Object Storage
Run on:
- Docker
- Kubernetes
Links:
Postgres solution :
Storage:
- local
- s3
- Object storage
Usage
Options | Shorts | Usage |
---|---|---|
mysql_bkup | bkup | CLI utility |
--operation | -o | Set operation. backup or restore (default: backup) |
--storage | -s | Set storage. local or s3 (default: local) |
--file | -f | Set file name for restoration |
--path | Set s3 path without file name. eg: /custom_path | |
--dbname | -d | Set database name |
--port | -p | Set database port (default: 3306) |
--timeout | -t | Set timeout (default: 60s) |
--help | -h | Print this help message and exit |
--version | -V | Print version information and exit |
Backup database :
Simple backup usage
bkup --operation backup --dbname database_name
bkup -o backup -d database_name
S3
bkup --operation backup --storage s3 --dbname database_name
Docker run:
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:
version: '3'
services:
mariadb:
container_name: mariadb
image: mariadb
environment:
MYSQL_DATABASE: mariadb
MYSQL_USER: mariadb
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command:
- /bin/sh
- -c
- bkup --operation backup -d database_name
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
Restore database :
Simple database restore operation usage
bkup --operation restore --dbname database_name --file database_20231217_115621.sql
bkup -o restore -f database_20231217_115621.sql
S3
bkup --operation restore --storage s3 --file database_20231217_115621.sql
Docker run:
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 mydb_20231219_022941.sql.gz
Docker compose file:
version: '3'
services:
mariadb:
container_name: mariadb
image: mariadb:latest
environment:
MYSQL_DATABASE: mariadb
MYSQL_USER: mariadb
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command:
- /bin/sh
- -c
- bkup --operation restore --file database_20231217_115621.sql --dbname database_name
volumes:
- ./backup:/backup
environment:
#- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command
- DB_PORT=3306
- DB_HOST=mariadb
- DB_NAME=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
Run
docker-compose up -d
Backup to S3
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 bkup -o backup -s s3 -d database_name
To change s3 backup path add this flag : --path myPath . default path is /mysql_bkup
Simple S3 backup usage
bkup --operation backup --storage s3 --dbname mydatabase
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
tty: true
privileged: true
devices:
- "/dev/fuse"
command:
- /bin/sh
- -c
- mysql_bkup --operation restore --storage s3 -f database_20231217_115621.sql.gz
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
- ACCESS_KEY=${ACCESS_KEY}
- SECRET_KEY=${SECRET_KEY}
- BUCKETNAME=${BUCKETNAME}
- S3_ENDPOINT=${S3_ENDPOINT}
Run "docker run" from crontab
Make an automated backup (every night at 1).
backup_script.sh
#!/bin/sh
DB_USERNAME='db_username'
DB_PASSWORD='password'
DB_HOST='db_hostname'
DB_NAME='db_name'
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 -d $DB_NAME
chmod +x backup_script.sh
Your crontab looks like this:
0 1 * * * /path/to/backup_script.sh
Kubernetes CronJob
Simple Kubernetes CronJob usage:
apiVersion: batch/v1
kind: CronJob
metadata:
name: mysql-bkup-job
spec:
schedule: "0 1 * * *"
jobTemplate:
spec:
template:
spec:
backoffLimit: 2
containers:
- name: mysql-bkup
image: jkaninda/mysql-bkup
securityContext:
privileged: true
command:
- /bin/sh
- -c
- bkup -o backup -s s3 --path /custom_path
env:
- name: DB_PORT
value: "3306"
- name: DB_HOST
value: ""
- name: DB_NAME
value: ""
- name: DB_USERNAME
value: ""
# Please use secret!
- name: DB_PASSWORD
value: "password"
- name: ACCESS_KEY
value: ""
- name: SECRET_KEY
value: ""
- name: BUCKETNAME
value: ""
- name: S3_ENDPOINT
value: "https://s3.amazonaws.com"
restartPolicy: Never
v0.1
MySQL Backup
MySQL Backup tool, backup database to S3 or Object Storage
- Docker
- Kubernetes
Storage:
- local
- s3
- Object storage
Usage
Options | Shorts | Usage |
---|---|---|
mysql_bkup | bkup | CLI utility |
--operation | -o | Set operation. backup or restore (default: backup) |
--storage | -s | Set storage. local or s3 (default: local) |
--file | -f | Set file name for restoration |
--path | Set s3 path without file name. eg: /custom_path | |
--database | -db | Set database name |
--port | -p | Set database port (default: 3306) |
--timeout | -t | Set timeout (default: 60s) |
--help | -h | Print this help message and exit |
--version | -V | Print version information and exit |
Backup database :
Simple backup usage
bkup --operation backup
bkup -o backup
S3
bkup --operation backup --storage s3
Docker run:
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 compose file:
version: '3'
services:
mariadb:
container_name: mariadb
image: mariadb:latest
environment:
MYSQL_DATABASE: mariadb
MYSQL_USER: mariadb
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
mysql-bkup:
image: jkaninda/mysql-bkup:latest
container_name: mysql-bkup
command:
- /bin/sh
- -c
- bkup --operation backup -db mariadb
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mariadb
- DB_DATABASE=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
Restore database :
Simple database restore operation usage
bkup --operation restore --file database_20231217_115621.sql
bkup -o restore -f database_20231217_115621.sql
S3
bkup --operation restore --storage s3 --file database_20231217_115621.sql
Docker run:
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 compose file:
version: '3'
services:
mariadb:
container_name: mariadb
image: mariadb:latest
environment:
MYSQL_DATABASE: mariadb
MYSQL_USER: mariadb
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
mysql-bkup:
image: jkaninda/mysql-bkup:latest
container_name: mysql-bkup
command:
- /bin/sh
- -c
- bkup --operation restore --file database_20231217_115621.sql
volumes:
- ./backup:/backup
environment:
#- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command
- DB_PORT=3306
- DB_HOST=mariadb
- DB_DATABASE=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
Run
docker-compose up -d
Backup to S3
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
To change s3 backup path add this flag : --path myPath . default path is /mysql_bkup
Simple S3 backup usage
bkup --operation backup --storage s3 --database mydatabase
mysql-bkup:
image: jkaninda/mysql-bkup:latest
container_name: mysql-bkup
tty: true
privileged: true
devices:
- "/dev/fuse"
command:
- /bin/sh
- -c
- mysql_bkup --operation restore --source s3 -f database_20231217_115621.sql.gz
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_DATABASE=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
- ACCESS_KEY=${ACCESS_KEY}
- SECRET_KEY=${SECRET_KEY}
- BUCKETNAME=${BUCKETNAME}
- S3_ENDPOINT=${S3_ENDPOINT}
Run "docker run" from crontab
Make an automated backup (every night at 1).
backup_script.sh
#!/bin/sh
DB_USERNAME='db_username'
DB_PASSWORD='password'
DB_HOST='db_hostname'
DB_NAME='db_name'
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
chmod +x backup_script.sh
Your crontab looks like this:
0 1 * * * /path/to/backup_script.sh
Kubernetes CronJob
Simple Kubernetes CronJob usage:
apiVersion: batch/v1
kind: CronJob
metadata:
name: mysql-bkup-job
spec:
schedule: "0 0 * * *"
jobTemplate:
spec:
template:
spec:
backoffLimit: 4
containers:
- name: mysql-bkup
image: jkaninda/mysql-bkup:latest
command:
- /bin/sh
- -c
- bkup --operation backup
env:
- name: DB_PORT
value: "3306"
- name: DB_HOST
value: "mysql-svc"
- name: DB_DATABASE
value: "mariadb"
- name: DB_USERNAME
value: "mariadb"
# Please use secret instead!
- name: DB_PASSWORD
value: "password"
restartPolicy: Never