Files
mysql-bkup/README.md

298 lines
8.4 KiB
Markdown
Raw Normal View History

2024-12-06 14:23:06 +01:00
# MYSQL-BKUP
2024-10-10 21:21:52 +02:00
2024-12-06 14:23:06 +01:00
**MYSQL-BKUP** is a Docker container image designed to **backup, restore, and migrate MySQL databases**.
It supports a variety of storage options and ensures data security through GPG encryption.
2024-10-10 21:21:52 +02:00
2025-03-14 09:41:37 +01:00
[![Tests](https://github.com/jkaninda/mysql-bkup/actions/workflows/tests.yml/badge.svg)](https://github.com/jkaninda/mysql-bkup/actions/workflows/tests.yml)
[![Build](https://github.com/jkaninda/mysql-bkup/actions/workflows/release.yml/badge.svg)](https://github.com/jkaninda/mysql-bkup/actions/workflows/release.yml)
2024-01-18 15:07:01 +01:00
[![Go Report](https://goreportcard.com/badge/github.com/jkaninda/mysql-bkup)](https://goreportcard.com/report/github.com/jkaninda/mysql-bkup)
2023-12-22 10:45:45 +01:00
![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)
2024-09-05 22:42:37 +02:00
<a href="https://ko-fi.com/jkaninda"><img src="https://uploads-ssl.webflow.com/5c14e387dab576fe667689cf/5cbed8a4ae2b88347c06c923_BuyMeACoffee_blue.png" height="20" alt="buy ma a coffee"></a>
2023-12-22 10:45:45 +01:00
2024-12-06 14:23:06 +01:00
## Features
- **Storage Options:**
- Local storage
- AWS S3 or any S3-compatible object storage
- FTP
- SSH-compatible storage
2024-12-06 18:27:25 +01:00
- Azure Blob storage
2024-12-06 14:23:06 +01:00
- **Data Security:**
- Backups can be encrypted using **GPG** to ensure confidentiality.
- **Deployment Flexibility:**
- Available as the [jkaninda/mysql-bkup](https://hub.docker.com/r/jkaninda/mysql-bkup) Docker image.
- Deployable on **Docker**, **Docker Swarm**, and **Kubernetes**.
2024-12-06 22:08:51 +01:00
- Supports recurring backups of MySQL databases when deployed:
2024-12-06 14:23:06 +01:00
- On Docker for automated backup schedules.
- As a **Job** or **CronJob** on Kubernetes.
- **Notifications:**
- Get real-time updates on backup success or failure via:
- **Telegram**
- **Email**
## Use Cases
2024-12-06 22:08:51 +01:00
- **Automated Recurring Backups:** Schedule regular backups for MySQL databases.
2025-03-16 05:45:12 +01:00
- **Cross-Environment Migration:** Easily migrate MySQL databases across different environments using `migration` feature.
2024-12-06 22:08:51 +01:00
- **Secure Backup Management:** Protect your data with GPG encryption.
2024-12-06 14:23:06 +01:00
2024-08-15 06:05:39 +02:00
Successfully tested on:
2023-12-18 07:08:43 +01:00
- Docker
2024-08-15 06:05:39 +02:00
- Docker in Swarm mode
2023-12-18 07:08:43 +01:00
- Kubernetes
2024-08-21 03:52:49 +02:00
- OpenShift
2023-12-17 06:43:15 +01:00
## Documentation is found at <https://jkaninda.github.io/mysql-bkup>
2023-12-22 10:44:38 +01:00
2023-12-17 06:43:15 +01:00
## Links:
2023-12-17 06:43:15 +01:00
- [Docker Hub](https://hub.docker.com/r/jkaninda/mysql-bkup)
- [Github](https://github.com/jkaninda/mysql-bkup)
2023-12-17 19:51:37 +01:00
2024-08-03 16:08:24 +02:00
## PostgreSQL solution :
2023-12-17 19:43:20 +01:00
2024-08-03 16:08:24 +02:00
- [PostgreSQL](https://github.com/jkaninda/pg-bkup)
2023-12-19 04:07:18 +01:00
## Storage:
- Local
- AWS S3 or any S3 Alternatives for Object Storage
2024-10-23 10:31:24 +02:00
- SSH remote storage server
- FTP remote storage server
2024-12-06 18:30:38 +01:00
- Azure Blob storage
2025-03-16 11:49:36 +01:00
## Quickstart
2023-12-17 19:43:20 +01:00
2025-03-16 11:49:36 +01:00
### Simple Backup Using Docker CLI
2023-12-17 19:43:20 +01:00
2025-03-16 11:49:36 +01:00
To perform a one-time backup, bind your local volume to `/backup` in the container and run the `backup` command:
2023-12-17 19:45:28 +01:00
```shell
2025-03-16 11:49:36 +01:00
docker run --rm --network your_network_name \
-v $PWD/backup:/backup/ \
-e "DB_HOST=dbhost" \
-e "DB_PORT=3306" \
-e "DB_USERNAME=username" \
-e "DB_PASSWORD=password" \
jkaninda/mysql-bkup backup -d database_name
2023-12-17 19:45:28 +01:00
```
2023-12-19 04:07:18 +01:00
2025-03-16 11:49:36 +01:00
Alternatively, use an environment file (`--env-file`) for configuration:
2023-12-19 04:07:18 +01:00
2025-03-16 11:49:36 +01:00
```shell
docker run --rm --network your_network_name \
--env-file your-env-file \
-v $PWD/backup:/backup/ \
jkaninda/mysql-bkup backup -d database_name
```
### Backup All Databases
To back up all databases on the server, use the `--all-databases` or `-a` flag. By default, this creates individual backup files for each database.
```shell
docker run --rm --network your_network_name \
-v $PWD/backup:/backup/ \
-e "DB_HOST=dbhost" \
-e "DB_PORT=3306" \
-e "DB_USERNAME=username" \
-e "DB_PASSWORD=password" \
jkaninda/mysql-bkup backup --all-databases --disable-compression
```
2023-12-19 04:07:18 +01:00
2025-03-16 11:49:36 +01:00
> **Note:** Use the `--all-in-one` or `-A` flag to combine backups into a single file.
---
### Simple Restore Using Docker CLI
To restore a database, bind your local volume to `/backup` and run the `restore` command:
2025-01-26 12:11:29 +01:00
```shell
2025-03-16 11:49:36 +01:00
docker run --rm --network your_network_name \
-v $PWD/backup:/backup/ \
-e "DB_HOST=dbhost" \
-e "DB_PORT=3306" \
-e "DB_USERNAME=username" \
-e "DB_PASSWORD=password" \
jkaninda/mysql-bkup restore -d database_name -f backup_file.sql.gz
2025-01-26 12:11:29 +01:00
```
2025-03-16 11:49:36 +01:00
---
### Backup with Docker Compose
Below is an example of a `docker-compose.yml` file for running a one-time backup:
2023-12-19 04:07:18 +01:00
2023-12-17 06:43:15 +01:00
```yaml
services:
2025-03-16 11:49:36 +01:00
pg-bkup:
# In production, pin your image tag to a specific release version instead of `latest`.
# See available releases: https://github.com/jkaninda/mysql-bkup/releases
2023-12-22 06:04:35 +01:00
image: jkaninda/mysql-bkup
2023-12-17 06:43:15 +01:00
container_name: mysql-bkup
2024-08-10 11:29:58 +02:00
command: backup
2023-12-17 06:43:15 +01:00
volumes:
- ./backup:/backup
environment:
2024-08-21 03:49:15 +02:00
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=foo
- DB_USERNAME=bar
2023-12-17 06:43:15 +01:00
- DB_PASSWORD=password
2024-09-30 17:44:45 +02:00
- TZ=Europe/Paris
networks:
2024-12-06 14:21:55 +01:00
- web
2025-03-16 11:49:36 +01:00
networks:
web:
2023-12-17 06:43:15 +01:00
```
2025-03-16 11:49:36 +01:00
---
### Recurring Backups with Docker
You can schedule recurring backups using the `--cron-expression` or `-e` flag:
```shell
2025-03-16 11:49:36 +01:00
docker run --rm --network network_name \
-v $PWD/backup:/backup/ \
-e "DB_HOST=hostname" \
-e "DB_USERNAME=user" \
-e "DB_PASSWORD=password" \
jkaninda/mysql-bkup backup -d dbName --cron-expression "@every 15m"
```
2025-03-16 11:49:36 +01:00
For predefined schedules, refer to the [documentation](https://jkaninda.github.io/mysql-bkup/reference/#predefined-schedules).
---
## Deploy on Kubernetes
2023-12-17 19:43:20 +01:00
2025-03-16 11:49:36 +01:00
For Kubernetes, you can deploy `mysql-bkup` as a Job or CronJob. Below are examples for both.
2023-12-17 19:43:20 +01:00
2025-03-16 11:49:36 +01:00
### Kubernetes Backup Job
This example defines a one-time backup job:
2023-12-17 06:43:15 +01:00
```yaml
apiVersion: batch/v1
kind: Job
2023-12-17 06:43:15 +01:00
metadata:
name: backup-job
2023-12-17 06:43:15 +01:00
spec:
ttlSecondsAfterFinished: 100
template:
2023-12-17 06:43:15 +01:00
spec:
containers:
- name: mysql-bkup
2025-03-16 11:49:36 +01:00
# Pin the image tag to a specific release version in production.
# See available releases: https://github.com/jkaninda/mysql-bkup/releases
image: jkaninda/mysql-bkup
command:
2024-08-28 20:35:01 +02:00
- /bin/sh
- -c
- backup -d dbname
resources:
limits:
memory: "128Mi"
cpu: "500m"
env:
- name: DB_HOST
value: "mysql"
- name: DB_USERNAME
value: "user"
- name: DB_PASSWORD
value: "password"
volumeMounts:
- mountPath: /backup
name: backup
volumes:
- name: backup
hostPath:
2025-03-16 11:49:36 +01:00
path: /home/toto/backup # Directory location on the host
type: Directory # Optional field
restartPolicy: Never
2023-12-26 21:28:39 +01:00
```
2025-03-16 11:49:36 +01:00
### Kubernetes CronJob for Scheduled Backups
For scheduled backups, use a `CronJob`:
```yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: pg-bkup-cronjob
spec:
schedule: "0 2 * * *" # Runs daily at 2 AM
jobTemplate:
spec:
template:
spec:
containers:
- name: pg-bkup
image: jkaninda/mysql-bkup
command:
- /bin/sh
- -c
- backup -d dbname
env:
- name: DB_HOST
value: "mysql"
- name: DB_USERNAME
value: "user"
- name: DB_PASSWORD
value: "password"
volumeMounts:
- mountPath: /backup
name: backup
volumes:
- name: backup
hostPath:
path: /home/toto/backup
type: Directory
restartPolicy: OnFailure
```
---
## Available image registries
This Docker image is published to both Docker Hub and the GitHub container registry.
Depending on your preferences and needs, you can reference both `jkaninda/mysql-bkup` as well as `ghcr.io/jkaninda/mysql-bkup`:
```
2024-08-10 10:50:00 +02:00
docker pull jkaninda/mysql-bkup
docker pull ghcr.io/jkaninda/mysql-bkup
```
Documentation references Docker Hub, but all examples will work using ghcr.io just as well.
## References
2025-01-13 15:05:50 +01:00
We created this image as a simpler and more lightweight alternative to existing solutions. Heres why:
2023-12-26 21:28:39 +01:00
2025-01-13 15:05:50 +01:00
- **Lightweight:** Written in Go, the image is optimized for performance and minimal resource usage.
- **Multi-Architecture Support:** Supports `arm64` and `arm/v7` architectures.
- **Docker Swarm Support:** Fully compatible with Docker in Swarm mode.
- **Kubernetes Support:** Designed to work seamlessly with Kubernetes.
2023-12-26 21:28:39 +01:00
## 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]