Files
mysql-bkup/README.md

248 lines
6.5 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
2023-12-22 06:16:15 +01:00
> Run on:
2023-12-18 07:08:43 +01:00
- 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-18 07:11:02 +01:00
| mysql_bkup | bkup | CLI utility |
2023-12-18 07:10:03 +01:00
| --operation | -o | Set operation. backup or restore (default: backup) |
2023-12-19 06:37:25 +01:00
| --storage | -s | Set storage. local or s3 (default: local) |
2023-12-17 13:30:07 +01:00
| --file | -f | Set file name for restoration |
2023-12-19 06:43:48 +01:00
| --path | | Set s3 path without file name. eg: /custom_path |
2023-12-22 06:04:35 +01:00
| --dbname | -d | Set database name |
2023-12-17 19:22:09 +01:00
| --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
2023-12-22 06:04:35 +01:00
bkup --operation backup --dbname databas_name
2023-12-17 19:43:20 +01:00
```
```sh
2023-12-22 06:04:35 +01:00
bkup -o backup -d databas_name
2023-12-17 19:43:20 +01:00
```
2023-12-17 19:51:37 +01:00
### S3
```sh
2023-12-22 06:04:35 +01:00
bkup --operation backup --storage s3 --dbname databas_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:26:40 +01:00
bkup --operation restore --file database_20231217_115621.sql --dbname database_name
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-22 10:26:40 +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://eu2.contabostorage.com" jkaninda/mysql-bkup bkup -o backup -s s3 -d database_name
2023-12-19 06:37:25 +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
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
tty: true
privileged: true
devices:
- "/dev/fuse"
command:
- /bin/sh
- -c
2023-12-22 10:26:40 +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-19 04:07:18 +01:00
## Run "docker run" from crontab
Make an automated backup (every night at 1).
> backup_script.sh
```sh
#!/bin/sh
DB_USERNAME='db_username'
DB_PASSWORD='password'
DB_HOST='db_hostname'
DB_NAME='db_name'
BACKUP_DIR='/some/path/backup/'
2023-12-22 06:04:35 +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:latest bkup -o backup -d $DB_NAME
2023-12-19 04:07:18 +01:00
```
```sh
chmod +x backup_script.sh
```
Your crontab looks like this:
```conf
0 1 * * * /path/to/backup_script.sh
```
2023-12-17 13:35:23 +01:00
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:
2023-12-22 10:26:40 +01:00
schedule: "0 1 * * *"
2023-12-17 06:43:15 +01:00
jobTemplate:
spec:
template:
spec:
2023-12-22 10:26:40 +01:00
backoffLimit: 2
2023-12-17 06:43:15 +01:00
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
value: "https://s3.amazonaws.com"
2023-12-17 06:43:15 +01:00
restartPolicy: Never
2023-12-17 19:22:09 +01:00
```