Files
mysql-bkup/README.md

193 lines
4.7 KiB
Markdown
Raw Normal View History

2023-12-17 06:43:15 +01:00
# MySQL Backup
2023-12-18 07:08:43 +01:00
MySQL Backup tool, backup database to S3 or Object Storage
- Docker
- Kubernetes
2023-12-17 06:43:15 +01:00
[![Build](https://github.com/jkaninda/mysql-bkup/actions/workflows/build.yml/badge.svg)](https://github.com/jkaninda/mysql-bkup/actions/workflows/build.yml)
![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/jkaninda/mysql-bkup?style=flat-square)
![Docker Pulls](https://img.shields.io/docker/pulls/jkaninda/mysql-bkup?style=flat-square)
- [Docker Hub](https://hub.docker.com/r/jkaninda/mysql-bkup)
- [Github](https://github.com/jkaninda/mysql-bkup)
## Storage:
- local
- s3
2023-12-17 13:30:07 +01:00
- Object storage
## Usage
| Options | Shorts | Usage |
|---------------|--------|------------------------------------|
2023-12-17 13:35:23 +01:00
| mysql_bkup | bkup | Command utility |
2023-12-18 07:10:03 +01:00
| --operation | -o | Set operation. backup or restore (default: backup) |
| --destination | -d | Set destination. local or s3 (default: local) |
| --source | -s | Set source. local or s3 (default: local) |
2023-12-17 13:30:07 +01:00
| --file | -f | Set file name for restoration |
2023-12-17 19:22:09 +01:00
| --database | -db | Set database name |
| --port | -p | Set database port (default: 3306) |
2023-12-17 19:26:42 +01:00
| --timeout | -t | Set timeout (default: 60s) |
2023-12-17 13:30:07 +01:00
| --help | -h | Print this help message and exit |
| --version | -V | Print version information and exit |
2023-12-17 06:43:15 +01:00
## Backup database :
2023-12-17 19:43:20 +01:00
Simple backup usage
```sh
bkup --operation backup
```
```sh
bkup -o backup
```
2023-12-17 19:51:37 +01:00
### S3
```sh
bkup --operation backup --destination s3
```
2023-12-17 19:43:20 +01:00
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:
image: jkaninda/mysql-bkup:latest
container_name: mysql-bkup
command:
2023-12-17 13:30:07 +01:00
- /bin/sh
- -c
2023-12-17 19:22:09 +01:00
- bkup --operation backup -db mariadb
2023-12-17 06:43:15 +01:00
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mariadb
- DB_DATABASE=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
bkup --operation restore --file database_20231217_115621.sql
```
```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
bkup --operation restore --source s3 --file database_20231217_115621.sql
```
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:
image: jkaninda/mysql-bkup:latest
container_name: mysql-bkup
2023-12-17 13:30:07 +01:00
command:
- /bin/sh
- -c
2023-12-17 13:35:23 +01:00
- bkup --operation restore --file database_20231217_115621.sql
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
- DB_DATABASE=mariadb
- 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-17 13:35:23 +01:00
Simple S3 backup usage
2023-12-17 19:43:20 +01:00
```sh
bkup --operation backup --destination s3 -database mydatabase
```
2023-12-17 13:35:23 +01:00
```yaml
mysql-bkup:
image: jkaninda/mysql-bkup:latest
container_name: mysql-bkup
tty: true
privileged: true
devices:
- "/dev/fuse"
command:
- /bin/sh
- -c
2023-12-17 19:22:09 +01:00
- mysql_bkup --operation restore --source s3 -f database_20231217_115621.sql.gz
2023-12-17 13:35:23 +01:00
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}
```
2023-12-17 19:43:20 +01:00
## Kubernetes CronJob
Simple Kubernetes CronJob usage:
2023-12-17 06:43:15 +01:00
```yaml
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
2023-12-17 13:30:07 +01:00
- bkup --operation backup
2023-12-17 06:43:15 +01:00
env:
- name: DB_PORT
value: "3306"
- name: DB_HOST
value: "mysql-svc"
- name: DB_DATABASE
value: "mariadb"
- name: DB_USERNAME
value: "mariadb"
2023-12-17 13:38:05 +01:00
# Please use secret instead!
2023-12-17 06:43:15 +01:00
- name: DB_PASSWORD
value: "password"
restartPolicy: Never
2023-12-17 19:22:09 +01:00
```