2023-12-17 06:43:15 +01:00
# MySQL Backup
2024-01-19 05:45:07 +01:00
MySQL Backup and Restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.
2023-12-22 10:45:45 +01:00
[](https://github.com/jkaninda/mysql-bkup/actions/workflows/build.yml)
2024-01-18 15:07:01 +01:00
[](https://goreportcard.com/report/github.com/jkaninda/mysql-bkup)
2023-12-22 10:45:45 +01:00


2023-12-26 21:28:39 +01:00
< p align = "center" >
< a href = "https://github.com/jkaninda/mysql-bkup" >
< img src = "https://www.mysql.com/common/logos/logo-mysql-170x115.png" alt = "Logo" >
< / a >
< / p >
2023-12-23 22:21:15 +01:00
> Runs on:
2023-12-18 07:08:43 +01:00
- Docker
- Kubernetes
2023-12-17 06:43:15 +01:00
2023-12-22 10:46:13 +01:00
> Links:
- [Docker Hub ](https://hub.docker.com/r/jkaninda/mysql-bkup )
- [Github ](https://github.com/jkaninda/mysql-bkup )
2023-12-25 02:31:44 +01:00
## PostgreSQL solution :
2023-12-24 19:25:54 +01:00
2023-12-25 02:31:44 +01:00
- [PostgreSQL ](https://github.com/jkaninda/pg-bkup )
2023-12-22 10:44:38 +01:00
2023-12-17 06:43:15 +01:00
## Storage:
- local
- s3
2023-12-17 13:30:07 +01:00
- Object storage
2023-12-26 21:28:39 +01:00
## Volumes:
- /s3mnt => S3 mounting path
- /backup => local storage mounting path
2023-12-17 13:30:07 +01:00
## Usage
2024-01-20 04:02:39 +01:00
| 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) |
| --mode | -m | Set execution mode. default or scheduled (default: default) |
| --disable-compression | | Disable database backup compression |
| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") |
| --timeout | -t | Set timeout (default: 60s) |
| --help | -h | Print this help message and exit |
| --version | -V | Print version information and exit |
2023-12-17 06:43:15 +01:00
2024-01-14 12:33:44 +01:00
## Note:
Creating a user for backup tasks who has read-only access is recommended!
> create read-only user
```sh
mysql -u root -p
```
```sql
CREATE USER read_only_user IDENTIFIED BY 'your_strong_password';
```
```sql
GRANT SELECT, SHOW VIEW ON *.* TO read_only_user;
```
```sql
FLUSH PRIVILEGES;
```
2023-12-17 06:43:15 +01:00
## Backup database :
2023-12-17 19:43:20 +01:00
Simple backup usage
```sh
2023-12-22 10:28:49 +01:00
bkup --operation backup --dbname database_name
2023-12-17 19:43:20 +01:00
```
```sh
2023-12-22 10:28:49 +01:00
bkup -o backup -d database_name
2023-12-17 19:43:20 +01:00
```
2023-12-17 19:51:37 +01:00
### S3
```sh
2023-12-22 10:28:49 +01:00
bkup --operation backup --storage s3 --dbname database_name
2023-12-17 19:51:37 +01:00
```
2023-12-19 04:07:18 +01:00
## Docker run:
2023-12-17 19:43:20 +01:00
2023-12-19 04:07:18 +01:00
```sh
2023-12-22 06:04:35 +01:00
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
2023-12-19 04:07:18 +01:00
```
## Docker compose file:
2023-12-17 06:43:15 +01:00
```yaml
version: '3'
services:
mariadb:
container_name: mariadb
2023-12-22 06:04:35 +01:00
image: mariadb
2023-12-17 06:43:15 +01:00
environment:
MYSQL_DATABASE: mariadb
MYSQL_USER: mariadb
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
mysql-bkup:
2023-12-22 06:04:35 +01:00
image: jkaninda/mysql-bkup
2023-12-17 06:43:15 +01:00
container_name: mysql-bkup
command:
2023-12-17 13:30:07 +01:00
- /bin/sh
- -c
2023-12-22 10:26:40 +01:00
- bkup --operation backup -d database_name
2023-12-17 06:43:15 +01:00
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
```
## Restore database :
2023-12-17 19:22:09 +01:00
2023-12-17 19:43:20 +01:00
Simple database restore operation usage
```sh
2023-12-22 10:28:49 +01:00
bkup --operation restore --dbname database_name --file database_20231217_115621.sql
2023-12-17 19:43:20 +01:00
```
```sh
2023-12-18 07:10:03 +01:00
bkup -o restore -f database_20231217_115621.sql
2023-12-17 19:43:20 +01:00
```
2023-12-17 19:45:28 +01:00
### S3
```sh
2023-12-19 06:37:25 +01:00
bkup --operation restore --storage s3 --file database_20231217_115621.sql
2023-12-17 19:45:28 +01:00
```
2023-12-19 04:07:18 +01:00
## Docker run:
```sh
2023-12-22 10:26:40 +01:00
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
2023-12-19 04:07:18 +01:00
```
## Docker compose file:
2023-12-17 06:43:15 +01:00
```yaml
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:
2023-12-22 06:04:35 +01:00
image: jkaninda/mysql-bkup
2023-12-17 06:43:15 +01:00
container_name: mysql-bkup
2023-12-17 13:30:07 +01:00
command:
- /bin/sh
- -c
2023-12-22 10:26:40 +01:00
- bkup --operation restore --file database_20231217_115621.sql --dbname database_name
2023-12-17 06:43:15 +01:00
volumes:
- ./backup:/backup
environment:
2023-12-17 13:30:07 +01:00
#- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command
2023-12-17 06:43:15 +01:00
- DB_PORT=3306
- DB_HOST=mariadb
2023-12-22 06:04:35 +01:00
- DB_NAME=mariadb
2023-12-17 06:43:15 +01:00
- DB_USERNAME=mariadb
- DB_PASSWORD=password
```
## Run
2023-12-17 19:43:20 +01:00
2023-12-17 06:43:15 +01:00
```sh
docker-compose up -d
```
2023-12-17 13:35:23 +01:00
## Backup to S3
2023-12-17 19:43:20 +01:00
2023-12-19 06:37:25 +01:00
```sh
2023-12-25 03:41:28 +01:00
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://s3.us-west-2.amazonaws.com" jkaninda/mysql-bkup bkup -o backup -s s3 -d database_name
2023-12-19 06:37:25 +01:00
```
2023-12-24 19:25:54 +01:00
> To change s3 backup path add this flag : --path /myPath . default path is /mysql_bkup
2023-12-22 06:04:35 +01:00
2023-12-17 13:35:23 +01:00
Simple S3 backup usage
2023-12-17 19:43:20 +01:00
```sh
2023-12-22 06:04:35 +01:00
bkup --operation backup --storage s3 --dbname mydatabase
2023-12-17 19:43:20 +01:00
```
2023-12-17 13:35:23 +01:00
```yaml
2023-12-24 11:52:11 +01:00
version: '3'
services:
2023-12-17 13:35:23 +01:00
mysql-bkup:
2023-12-22 06:04:35 +01:00
image: jkaninda/mysql-bkup
2023-12-17 13:35:23 +01:00
container_name: mysql-bkup
privileged: true
devices:
- "/dev/fuse"
command:
- /bin/sh
- -c
2024-01-20 04:02:39 +01:00
- mysql-bkup --operation restore --storage s3 -f database_20231217_115621.sql.gz
2023-12-17 13:35:23 +01:00
environment:
- DB_PORT=3306
- DB_HOST=mysql
2023-12-22 06:04:35 +01:00
- DB_NAME=mariadb
2023-12-17 13:35:23 +01:00
- DB_USERNAME=mariadb
- DB_PASSWORD=password
- ACCESS_KEY=${ACCESS_KEY}
- SECRET_KEY=${SECRET_KEY}
- BUCKETNAME=${BUCKETNAME}
- S3_ENDPOINT=${S3_ENDPOINT}
```
2023-12-24 11:52:11 +01:00
## 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.
2023-12-24 14:59:05 +01:00
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.
2023-12-24 11:52:11 +01:00
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:
2023-12-24 12:01:31 +01:00
```conf
2023-12-24 11:52:11 +01:00
* * * * * command to be executed
2023-12-24 12:01:31 +01:00
```
2023-12-24 12:00:50 +01:00
```conf
2023-12-24 11:52:11 +01:00
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
2023-12-24 12:00:50 +01:00
```
2023-12-24 11:52:11 +01:00
2023-12-24 14:59:05 +01:00
> At every 30th minute
```conf
*/30 * * * *
```
> “At minute 0.” every hour
```conf
0 * * * *
```
> “At 01:00.” every day
```conf
0 1 * * *
```
2023-12-24 12:02:02 +01:00
## Example of scheduled mode
2023-12-24 11:52:11 +01:00
> Docker run :
```sh
2023-12-25 02:31:44 +01:00
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 * * *"
2023-12-24 11:52:11 +01:00
```
2023-12-19 04:07:18 +01:00
2023-12-24 11:52:11 +01:00
> With Docker compose
```yaml
2023-12-24 12:19:12 +01:00
version: "3"
2023-12-24 11:52:11 +01:00
services:
mysql-bkup:
2023-12-25 02:31:44 +01:00
image: jkaninda/mysql-bkup
2023-12-24 11:52:11 +01:00
container_name: mysql-bkup
2023-12-24 12:28:54 +01:00
privileged: true
devices:
- "/dev/fuse"
2023-12-24 11:52:11 +01:00
command:
- /bin/sh
- -c
2023-12-24 12:19:12 +01:00
- bkup --operation backup --storage s3 --path /mys3_custome_path --dbname database_name --mode scheduled --period "*/30 * * * * "
2023-12-24 11:52:11 +01:00
environment:
- DB_PORT=3306
2023-12-24 12:19:12 +01:00
- DB_HOST=mysqlhost
- DB_USERNAME=userName
- DB_PASSWORD=${DB_PASSWORD}
- ACCESS_KEY=${ACCESS_KEY}
- SECRET_KEY=${SECRET_KEY}
- BUCKETNAME=${BUCKETNAME}
- S3_ENDPOINT=${S3_ENDPOINT}
2023-12-24 11:52:11 +01:00
```
2023-12-19 04:07:18 +01:00
2023-12-17 13:35:23 +01:00
2023-12-17 19:43:20 +01:00
## Kubernetes CronJob
2023-12-24 11:52:11 +01:00
For Kubernetes you don't need to run it in scheduled mode.
2023-12-17 19:43:20 +01:00
Simple Kubernetes CronJob usage:
2023-12-17 06:43:15 +01:00
```yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: mysql-bkup-job
spec:
2023-12-22 10:26:40 +01:00
schedule: "0 1 * * *"
2023-12-17 06:43:15 +01:00
jobTemplate:
spec:
template:
spec:
containers:
- name: mysql-bkup
2023-12-22 10:26:40 +01:00
image: jkaninda/mysql-bkup
securityContext:
privileged: true
2023-12-17 06:43:15 +01:00
command:
- /bin/sh
- -c
2023-12-22 10:26:40 +01:00
- bkup -o backup -s s3 --path /custom_path
2023-12-17 06:43:15 +01:00
env:
- name: DB_PORT
2023-12-22 10:26:40 +01:00
value: "3306"
2023-12-17 06:43:15 +01:00
- name: DB_HOST
2023-12-22 10:26:40 +01:00
value: ""
2023-12-22 06:04:35 +01:00
- name: DB_NAME
2023-12-22 10:26:40 +01:00
value: ""
2023-12-17 06:43:15 +01:00
- name: DB_USERNAME
2023-12-22 10:26:40 +01:00
value: ""
# Please use secret!
2023-12-17 06:43:15 +01:00
- name: DB_PASSWORD
value: "password"
2023-12-22 10:26:40 +01:00
- name: ACCESS_KEY
value: ""
- name: SECRET_KEY
value: ""
- name: BUCKETNAME
value: ""
- name: S3_ENDPOINT
2023-12-25 03:41:28 +01:00
value: "https://s3.us-west-2.amazonaws.com"
2023-12-17 06:43:15 +01:00
restartPolicy: Never
2023-12-26 21:28:39 +01:00
```
## Contributing
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please create an issue or submit a pull request.
Make sure to follow the existing coding style and provide tests for your changes.
## License
This project is licensed under the MIT License. See the LICENSE file for details.
## Authors
**Jonas Kaninda**
- < https: // github . com / jkaninda >
## Copyright
Copyright (c) [2023] [Jonas Kaninda]