mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 13:39:41 +01:00
Compare commits
30 Commits
v1.3.0
...
5d5cbea60b
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d5cbea60b | |||
|
|
2cb5603b88 | ||
| 548de725dd | |||
|
|
37b2867974 | ||
| d6c01c9a4a | |||
|
|
bd92cc6844 | ||
| 3006abac3e | |||
|
|
3a16f6929c | ||
| 2f3ed7d0d8 | |||
|
|
a92bba05e4 | ||
| 7b565d54bd | |||
|
|
d10321dac6 | ||
| d151c50caa | |||
| 07ecec57b3 | |||
| bc4aab6ed0 | |||
| 12c17c18d6 | |||
| 80cd70e153 | |||
|
|
80abdb4299 | ||
| 72b21f00e6 | |||
| 3439c74257 | |||
| 4110ff4e64 | |||
| cb299a35bf | |||
| ba7c096bf3 | |||
| 6ac8dcef9e | |||
| 6bcc5d6bd4 | |||
| fd35fabf97 | |||
| 23cce10e8c | |||
| e666466d27 | |||
| 36bca254a9 | |||
| ad18d42145 |
3
.github/workflows/tests.yml
vendored
3
.github/workflows/tests.yml
vendored
@@ -182,8 +182,7 @@ jobs:
|
|||||||
-e DB_USERNAME=user \
|
-e DB_USERNAME=user \
|
||||||
-e DB_PASSWORD=password \
|
-e DB_PASSWORD=password \
|
||||||
-e GPG_PASSPHRASE=password \
|
-e GPG_PASSPHRASE=password \
|
||||||
-e DB_NAME=testdb \
|
${{ env.IMAGE_NAME }}:latest backup -d testdb --disable-compression --custom-name encrypted-bkup
|
||||||
${{ env.IMAGE_NAME }}:latest backup --disable-compression --custom-name encrypted-bkup
|
|
||||||
echo "Database encrypted backup completed"
|
echo "Database encrypted backup completed"
|
||||||
- name: Test restore encrypted backup | testdb -> testdb2
|
- name: Test restore encrypted backup | testdb -> testdb2
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
13
Dockerfile
13
Dockerfile
@@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.24.1 AS build
|
FROM golang:1.24.6 AS build
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
ARG appVersion=""
|
ARG appVersion=""
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ RUN go mod download
|
|||||||
# Build
|
# Build
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'github.com/jkaninda/mysql-bkup/utils.Version=${appVersion}'" -o /app/mysql-bkup
|
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'github.com/jkaninda/mysql-bkup/utils.Version=${appVersion}'" -o /app/mysql-bkup
|
||||||
|
|
||||||
FROM alpine:3.21.3
|
FROM alpine:3.22.1
|
||||||
ENV TZ=UTC
|
ENV TZ=UTC
|
||||||
ARG WORKDIR="/config"
|
ARG WORKDIR="/config"
|
||||||
ARG BACKUPDIR="/backup"
|
ARG BACKUPDIR="/backup"
|
||||||
@@ -18,9 +18,12 @@ ARG BACKUP_TMP_DIR="/tmp/backup"
|
|||||||
ARG TEMPLATES_DIR="/config/templates"
|
ARG TEMPLATES_DIR="/config/templates"
|
||||||
ARG appVersion=""
|
ARG appVersion=""
|
||||||
ENV VERSION=${appVersion}
|
ENV VERSION=${appVersion}
|
||||||
LABEL author="Jonas Kaninda"
|
LABEL org.opencontainers.image.title="mysql-bkup"
|
||||||
LABEL version=${appVersion}
|
LABEL org.opencontainers.image.description="A lightweight MySQL backup and restore tool"
|
||||||
LABEL github="github.com/jkaninda/mysql-bkup"
|
LABEL org.opencontainers.image.licenses="MIT"
|
||||||
|
LABEL org.opencontainers.image.authors="Jonas Kaninda <me@jonaskaninda.com>"
|
||||||
|
LABEL org.opencontainers.image.version=${appVersion}
|
||||||
|
LABEL org.opencontainers.image.source="https://github.com/jkaninda/mysql-bkup"
|
||||||
|
|
||||||
RUN apk --update add --no-cache mysql-client mariadb-connector-c tzdata ca-certificates
|
RUN apk --update add --no-cache mysql-client mariadb-connector-c tzdata ca-certificates
|
||||||
RUN mkdir -p $WORKDIR $BACKUPDIR $TEMPLATES_DIR $BACKUP_TMP_DIR && \
|
RUN mkdir -p $WORKDIR $BACKUPDIR $TEMPLATES_DIR $BACKUP_TMP_DIR && \
|
||||||
|
|||||||
254
README.md
254
README.md
@@ -3,6 +3,13 @@
|
|||||||
**MYSQL-BKUP** is a Docker container image designed to **backup, restore, and migrate MySQL databases**.
|
**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.
|
It supports a variety of storage options and ensures data security through GPG encryption.
|
||||||
|
|
||||||
|
MYSQL-BKUP is designed for seamless deployment on **Docker** and **Kubernetes**, simplifying MySQL backup, restoration, and migration across environments.
|
||||||
|
It is a lightweight, multi-architecture solution compatible with **Docker**, **Docker Swarm**, **Kubernetes**, and other container orchestration platforms.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[](https://github.com/jkaninda/mysql-bkup/actions/workflows/tests.yml)
|
[](https://github.com/jkaninda/mysql-bkup/actions/workflows/tests.yml)
|
||||||
[](https://github.com/jkaninda/mysql-bkup/actions/workflows/release.yml)
|
[](https://github.com/jkaninda/mysql-bkup/actions/workflows/release.yml)
|
||||||
[](https://goreportcard.com/report/github.com/jkaninda/mysql-bkup)
|
[](https://goreportcard.com/report/github.com/jkaninda/mysql-bkup)
|
||||||
@@ -12,9 +19,9 @@ It supports a variety of storage options and ensures data security through GPG e
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Storage Options:**
|
- **Flexible Storage Backends:**
|
||||||
- Local storage
|
- Local filesystem
|
||||||
- AWS S3 or any S3-compatible object storage
|
- Amazon S3 & S3-compatible storage (e.g., MinIO, Wasabi)
|
||||||
- FTP
|
- FTP
|
||||||
- SSH-compatible storage
|
- SSH-compatible storage
|
||||||
- Azure Blob storage
|
- Azure Blob storage
|
||||||
@@ -34,16 +41,19 @@ It supports a variety of storage options and ensures data security through GPG e
|
|||||||
- **Telegram**
|
- **Telegram**
|
||||||
- **Email**
|
- **Email**
|
||||||
|
|
||||||
## Use Cases
|
## 💡Use Cases
|
||||||
|
|
||||||
- **Automated Recurring Backups:** Schedule regular backups for MySQL databases.
|
- **Scheduled Backups**: Automate recurring backups using Docker or Kubernetes.
|
||||||
- **Cross-Environment Migration:** Easily migrate your MySQL databases across different environments using supported storage options.
|
- **Disaster Recovery:** Quickly restore backups to a clean MySQL instance.
|
||||||
- **Secure Backup Management:** Protect your data with GPG encryption.
|
- **Database Migration**: Seamlessly move data across environments using the built-in `migrate` feature.
|
||||||
|
- **Secure Archiving:** Keep backups encrypted and safely stored in the cloud or remote servers.
|
||||||
|
|
||||||
|
|
||||||
Successfully tested on:
|
## ✅ Verified Platforms:
|
||||||
|
MYSQL-BKUP has been tested and runs successfully on:
|
||||||
|
|
||||||
- Docker
|
- Docker
|
||||||
- Docker in Swarm mode
|
- Docker Swarm
|
||||||
- Kubernetes
|
- Kubernetes
|
||||||
- OpenShift
|
- OpenShift
|
||||||
|
|
||||||
@@ -59,58 +69,75 @@ Successfully tested on:
|
|||||||
|
|
||||||
- [PostgreSQL](https://github.com/jkaninda/pg-bkup)
|
- [PostgreSQL](https://github.com/jkaninda/pg-bkup)
|
||||||
|
|
||||||
## Storage:
|
|
||||||
- Local
|
|
||||||
- AWS S3 or any S3 Alternatives for Object Storage
|
|
||||||
- SSH remote storage server
|
|
||||||
- FTP remote storage server
|
|
||||||
- Azure Blob storage
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
|
|
||||||
### Simple backup using Docker CLI
|
### Simple Backup Using Docker CLI
|
||||||
|
|
||||||
To run a one time backup, bind your local volume to `/backup` in the container and run the `backup` command:
|
To perform a one-time backup, bind your local volume to `/backup` in the container and run the `backup` command:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
docker run --rm --network your_network_name \
|
docker run --rm --network your_network_name \
|
||||||
-v $PWD/backup:/backup/ \
|
-v $PWD/backup:/backup/ \
|
||||||
-e "DB_HOST=dbhost" \
|
-e "DB_HOST=dbhost" \
|
||||||
-e "DB_PORT=3306" \
|
-e "DB_PORT=3306" \
|
||||||
-e "DB_USERNAME=username" \
|
-e "DB_USERNAME=username" \
|
||||||
-e "DB_PASSWORD=password" \
|
-e "DB_PASSWORD=password" \
|
||||||
jkaninda/mysql-bkup backup -d database_name
|
jkaninda/mysql-bkup backup -d database_name
|
||||||
```
|
```
|
||||||
|
|
||||||
Alternatively, pass a `--env-file` in order to use a full config as described below.
|
Alternatively, use an environment file (`--env-file`) for configuration:
|
||||||
|
|
||||||
```yaml
|
|
||||||
docker run --rm --network your_network_name \
|
|
||||||
--env-file your-env-file \
|
|
||||||
-v $PWD/backup:/backup/ \
|
|
||||||
jkaninda/mysql-bkup backup -d database_name
|
|
||||||
```
|
|
||||||
### Simple restore using Docker CLI
|
|
||||||
|
|
||||||
To restore a database, bind your local volume to `/backup` in the container and run the `restore` command:
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
docker run --rm --network your_network_name \
|
docker run --rm --network your_network_name \
|
||||||
-v $PWD/backup:/backup/ \
|
--env-file your-env-file \
|
||||||
-e "DB_HOST=dbhost" \
|
-v $PWD/backup:/backup/ \
|
||||||
-e "DB_PORT=3306" \
|
jkaninda/mysql-bkup backup -d database_name
|
||||||
-e "DB_USERNAME=username" \
|
|
||||||
-e "DB_PASSWORD=password" \
|
|
||||||
jkaninda/mysql-bkup restore -d database_name -f backup_file.sql.gz
|
|
||||||
```
|
```
|
||||||
### Simple backup in docker compose file
|
|
||||||
|
### 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
|
||||||
|
```
|
||||||
|
|
||||||
|
> **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:
|
||||||
|
|
||||||
|
```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 restore -d database_name -f backup_file.sql.gz
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Backup with Docker Compose
|
||||||
|
|
||||||
|
Below is an example of a `docker-compose.yml` file for running a one-time backup:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
services:
|
services:
|
||||||
mysql-bkup:
|
pg-bkup:
|
||||||
# In production, it is advised to lock your image tag to a proper
|
# In production, pin your image tag to a specific release version instead of `latest`.
|
||||||
# release version instead of using `latest`.
|
# See available releases: https://github.com/jkaninda/mysql-bkup/releases
|
||||||
# Check https://github.com/jkaninda/mysql-bkup/releases
|
|
||||||
# for a list of available releases.
|
|
||||||
image: jkaninda/mysql-bkup
|
image: jkaninda/mysql-bkup
|
||||||
container_name: mysql-bkup
|
container_name: mysql-bkup
|
||||||
command: backup
|
command: backup
|
||||||
@@ -123,29 +150,39 @@ services:
|
|||||||
- DB_USERNAME=bar
|
- DB_USERNAME=bar
|
||||||
- DB_PASSWORD=password
|
- DB_PASSWORD=password
|
||||||
- TZ=Europe/Paris
|
- TZ=Europe/Paris
|
||||||
# mysql-bkup container must be connected to the same network with your database
|
|
||||||
networks:
|
networks:
|
||||||
- web
|
- web
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
web:
|
web:
|
||||||
```
|
```
|
||||||
### Docker recurring backup
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Recurring Backups with Docker
|
||||||
|
|
||||||
|
You can schedule recurring backups using the `--cron-expression` or `-e` flag:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
docker run --rm --network network_name \
|
docker run --rm --network network_name \
|
||||||
-v $PWD/backup:/backup/ \
|
-v $PWD/backup:/backup/ \
|
||||||
-e "DB_HOST=hostname" \
|
-e "DB_HOST=hostname" \
|
||||||
-e "DB_USERNAME=user" \
|
-e "DB_USERNAME=user" \
|
||||||
-e "DB_PASSWORD=password" \
|
-e "DB_PASSWORD=password" \
|
||||||
jkaninda/mysql-bkup backup -d dbName --cron-expression "@every 15m" #@midnight
|
jkaninda/mysql-bkup backup -d dbName --cron-expression "@every 15m"
|
||||||
```
|
```
|
||||||
See: https://jkaninda.github.io/mysql-bkup/reference/#predefined-schedules
|
|
||||||
|
For predefined schedules, refer to the [documentation](https://jkaninda.github.io/mysql-bkup/reference/#predefined-schedules).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Deploy on Kubernetes
|
## Deploy on Kubernetes
|
||||||
|
|
||||||
For Kubernetes, you don't need to run it in scheduled mode. You can deploy it as Job or CronJob.
|
For Kubernetes, you can deploy `mysql-bkup` as a Job or CronJob. Below are examples for both.
|
||||||
|
|
||||||
### Simple Kubernetes backup Job :
|
### Kubernetes Backup Job
|
||||||
|
|
||||||
|
This example defines a one-time backup job:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: batch/v1
|
apiVersion: batch/v1
|
||||||
@@ -158,15 +195,10 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: mysql-bkup
|
- name: mysql-bkup
|
||||||
# In production, it is advised to lock your image tag to a proper
|
# Pin the image tag to a specific release version in production.
|
||||||
# release version instead of using `latest`.
|
# See available releases: https://github.com/jkaninda/mysql-bkup/releases
|
||||||
# Check https://github.com/jkaninda/mysql-bkup/releases
|
|
||||||
# for a list of available releases.
|
|
||||||
image: jkaninda/mysql-bkup
|
image: jkaninda/mysql-bkup
|
||||||
command:
|
command: ["backup", "-d", "dbname"]
|
||||||
- /bin/sh
|
|
||||||
- -c
|
|
||||||
- backup -d dbname
|
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
memory: "128Mi"
|
memory: "128Mi"
|
||||||
@@ -184,10 +216,94 @@ spec:
|
|||||||
volumes:
|
volumes:
|
||||||
- name: backup
|
- name: backup
|
||||||
hostPath:
|
hostPath:
|
||||||
path: /home/toto/backup # directory location on host
|
path: /home/toto/backup # Directory location on the host
|
||||||
type: Directory # this field is optional
|
type: Directory # Optional field
|
||||||
restartPolicy: Never
|
restartPolicy: Never
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 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: ["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
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🚀 Why Use MYSQL-BKUP?
|
||||||
|
|
||||||
|
**MYSQL-BKUP** isn't just another MySQL backup tool, it's a robust, production-ready solution purpose-built for modern DevOps workflows.
|
||||||
|
|
||||||
|
Here’s why developers, sysadmins, and DevOps choose **MYSQL-BKUP**:
|
||||||
|
|
||||||
|
### ✅ All-in-One Backup, Restore & Migration
|
||||||
|
|
||||||
|
Whether you're backing up a single database, restoring critical data, or migrating across environments, MYSQL-BKUP handles it all with a **single, unified CLI** no scripting gymnastics required.
|
||||||
|
|
||||||
|
|
||||||
|
### 🔄 Works Everywhere You Deploy
|
||||||
|
|
||||||
|
Designed to be cloud-native:
|
||||||
|
|
||||||
|
* **Runs seamlessly on Docker, Docker Swarm, and Kubernetes**
|
||||||
|
* Supports **CronJobs** for automated scheduled backups
|
||||||
|
* Compatible with GitOps and CI/CD workflows
|
||||||
|
|
||||||
|
### ☁️ Flexible Storage Integrations
|
||||||
|
|
||||||
|
Store your backups **anywhere**:
|
||||||
|
|
||||||
|
* Local disks
|
||||||
|
* Amazon S3, MinIO, Wasabi, Azure Blob, FTP, SSH
|
||||||
|
|
||||||
|
### 🔒 Enterprise-Grade Security
|
||||||
|
|
||||||
|
* **GPG Encryption**: Protect sensitive data with optional encryption before storing backups locally or in the cloud.
|
||||||
|
* **Secure Storage** Options: Supports S3, Azure Blob, SFTP, and SSH with encrypted transfers, keeping backups safe from unauthorized access.
|
||||||
|
|
||||||
|
### 📬 Instant Notifications
|
||||||
|
|
||||||
|
Stay in the loop with real-time notifications via **Telegram** and **Email**. Know immediately when a backup succeeds—or fails.
|
||||||
|
|
||||||
|
### 🏃♂️ Lightweight and Fast
|
||||||
|
|
||||||
|
Written in **Go**, MYSQL-BKUP is fast, multi-arch compatible (`amd64`, `arm64`, `arm/v7`), and optimized for minimal memory and CPU usage. Ideal for both cloud and edge deployments.
|
||||||
|
|
||||||
|
### 🧪 Tested. Verified. Trusted.
|
||||||
|
|
||||||
|
Actively maintained with **automated testing**, **Docker image size optimizations**, and verified support across major container platforms.
|
||||||
|
|
||||||
|
---
|
||||||
## Available image registries
|
## Available image registries
|
||||||
|
|
||||||
This Docker image is published to both Docker Hub and the GitHub container registry.
|
This Docker image is published to both Docker Hub and the GitHub container registry.
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ nav_order: 12
|
|||||||
|
|
||||||
# Backup All Databases
|
# Backup All Databases
|
||||||
|
|
||||||
MySQL-Bkup supports backing up all databases on the server using the `--all-databases` (`-a`) flag. By default, this creates separate backup files for each database. If you prefer a single backup file, you can use the `--all-in-on`e (`-A`) flag.
|
MySQL-Bkup supports backing up all databases on the server using the `--all-databases` (`-a`) flag. By default, this creates separate backup files for each database. If you prefer a single backup file, you can use the `--all-in-one` (`-A`) flag.
|
||||||
|
|
||||||
Backing up all databases is useful for creating a snapshot of the entire database server, whether for disaster recovery or migration purposes.
|
Backing up all databases is useful for creating a snapshot of the entire database server, whether for disaster recovery or migration purposes.
|
||||||
## Backup Modes
|
## Backup Modes
|
||||||
@@ -21,7 +21,7 @@ Using --all-databases without --all-in-one creates individual backup files for e
|
|||||||
- Can be more manageable in cases where different databases have different retention policies.
|
- Can be more manageable in cases where different databases have different retention policies.
|
||||||
- Might take slightly longer due to multiple file operations.
|
- Might take slightly longer due to multiple file operations.
|
||||||
- It is the default behavior when using the `--all-databases` flag.
|
- It is the default behavior when using the `--all-databases` flag.
|
||||||
- It does not backup system databases (`information_schema`, `performance_schema`, `mysql`, `sys`, `innodb`).
|
- It does not backup system databases (`information_schema`, `performance_schema`, `mysql`, `sys`, `innodb`,...).
|
||||||
|
|
||||||
**Command:**
|
**Command:**
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ The configuration file can be mounted into the container at `/config/config.yaml
|
|||||||
|
|
||||||
### Key Features:
|
### Key Features:
|
||||||
- **Global Environment Variables**: Use these for databases that share the same configuration.
|
- **Global Environment Variables**: Use these for databases that share the same configuration.
|
||||||
- **Database-Specific Overrides**: Override global settings for individual databases by specifying them in the configuration file or using the database name as a suffix in the variable name (e.g., `DB_HOST_DATABASE1`).
|
- **Database-Specific Overrides**: Override global settings for individual databases by specifying them in the configuration file or using the database name as a prefix or suffix in the variable name (e.g., `DB_HOST_DATABASENAME` or `DATABASENAME_DB_HOST`).
|
||||||
- **Global Cron Expression**: Define a global `cronExpression` in the configuration file to schedule backups for all databases. If omitted, backups will run immediately.
|
- **Global Cron Expression**: Define a global `cronExpression` in the configuration file to schedule backups for all databases. If omitted, backups will run immediately.
|
||||||
- **Configuration File Path**: Specify the configuration file path using:
|
- **Configuration File Path**: Specify the configuration file path using:
|
||||||
- The `BACKUP_CONFIG_FILE` environment variable.
|
- The `BACKUP_CONFIG_FILE` environment variable.
|
||||||
@@ -89,7 +89,7 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
## Specify the path to the configuration file
|
## Specify the path to the configuration file
|
||||||
- BACKUP_CONFIG_FILE=/backup/config.yaml
|
- BACKUP_CONFIG_FILE=/backup/config.yaml
|
||||||
# Ensure the pg-bkup container is connected to the same network as your database
|
# Ensure the mysql-bkup container is connected to the same network as your database
|
||||||
networks:
|
networks:
|
||||||
- web
|
- web
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ nav_order: 1
|
|||||||
**MYSQL-BKUP** is a Docker container image designed to **backup, restore, and migrate MySQL databases**.
|
**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.
|
It supports a variety of storage options and ensures data security through GPG encryption.
|
||||||
|
|
||||||
|
**MYSQL-BKUP** is designed for seamless deployment on **Docker** and **Kubernetes**, simplifying MySQL backup, restoration, and migration across environments.
|
||||||
|
It is a lightweight, multi-architecture solution compatible with **Docker**, **Docker Swarm**, **Kubernetes**, and other container orchestration platforms.
|
||||||
---
|
---
|
||||||
|
|
||||||
## Key Features
|
## Key Features
|
||||||
@@ -39,11 +41,21 @@ It supports a variety of storage options and ensures data security through GPG e
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Use Cases
|
## 💡Use Cases
|
||||||
|
|
||||||
- **Automated Recurring Backups:** Schedule regular backups for MySQL databases.
|
- **Scheduled Backups**: Automate recurring backups using Docker or Kubernetes.
|
||||||
- **Cross-Environment Migration:** Easily migrate MySQL databases across different environments using supported storage options.
|
- **Disaster Recovery:** Quickly restore backups to a clean MySQL instance.
|
||||||
- **Secure Backup Management:** Protect your data with GPG encryption.
|
- **Database Migration**: Seamlessly move data across environments using the built-in `migrate` feature.
|
||||||
|
- **Secure Archiving:** Keep backups encrypted and safely stored in the cloud or remote servers.
|
||||||
|
|
||||||
|
|
||||||
|
## ✅ Verified Platforms:
|
||||||
|
MYSQL-BKUP has been tested and runs successfully on:
|
||||||
|
|
||||||
|
- Docker
|
||||||
|
- Docker Swarm
|
||||||
|
- Kubernetes
|
||||||
|
- OpenShift
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ This guide provides quick examples for running backups using Docker CLI, Docker
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Simple Backup Using Docker CLI
|
### Simple Backup Using Docker CLI
|
||||||
|
|
||||||
To run a one-time backup, bind your local volume to `/backup` in the container and execute the `backup` command:
|
To perform a one-time backup, bind your local volume to `/backup` in the container and run the `backup` command:
|
||||||
|
|
||||||
```bash
|
```shell
|
||||||
docker run --rm --network your_network_name \
|
docker run --rm --network your_network_name \
|
||||||
-v $PWD/backup:/backup/ \
|
-v $PWD/backup:/backup/ \
|
||||||
-e "DB_HOST=dbhost" \
|
-e "DB_HOST=dbhost" \
|
||||||
@@ -24,41 +24,58 @@ docker run --rm --network your_network_name \
|
|||||||
jkaninda/mysql-bkup backup -d database_name
|
jkaninda/mysql-bkup backup -d database_name
|
||||||
```
|
```
|
||||||
|
|
||||||
### Using an Environment File
|
Alternatively, use an environment file (`--env-file`) for configuration:
|
||||||
|
|
||||||
Alternatively, you can use an `--env-file` to pass a full configuration:
|
```shell
|
||||||
|
|
||||||
```bash
|
|
||||||
docker run --rm --network your_network_name \
|
docker run --rm --network your_network_name \
|
||||||
--env-file your-env-file \
|
--env-file your-env-file \
|
||||||
-v $PWD/backup:/backup/ \
|
-v $PWD/backup:/backup/ \
|
||||||
jkaninda/mysql-bkup backup -d database_name
|
jkaninda/mysql-bkup backup -d database_name
|
||||||
```
|
```
|
||||||
|
|
||||||
### Simple restore using Docker CLI
|
### Backup All Databases
|
||||||
|
|
||||||
To restore a database, bind your local volume to `/backup` in the container and run the `restore` command:
|
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
|
```shell
|
||||||
docker run --rm --network your_network_name \
|
docker run --rm --network your_network_name \
|
||||||
-v $PWD/backup:/backup/ \
|
-v $PWD/backup:/backup/ \
|
||||||
-e "DB_HOST=dbhost" \
|
-e "DB_HOST=dbhost" \
|
||||||
-e "DB_PORT=3306" \
|
-e "DB_PORT=3306" \
|
||||||
-e "DB_USERNAME=username" \
|
-e "DB_USERNAME=username" \
|
||||||
-e "DB_PASSWORD=password" \
|
-e "DB_PASSWORD=password" \
|
||||||
jkaninda/mysql-bkup restore -d database_name -f backup_file.sql.gz
|
jkaninda/mysql-bkup backup --all-databases --disable-compression
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> **Note:** Use the `--all-in-one` or `-A` flag to combine backups into a single file.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Simple Backup Using Docker Compose
|
### Simple Restore Using Docker CLI
|
||||||
|
|
||||||
Below is an example `docker-compose.yml` configuration for running a backup:
|
To restore a database, bind your local volume to `/backup` and run the `restore` command:
|
||||||
|
|
||||||
|
```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 restore -d database_name -f backup_file.sql.gz
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Backup with Docker Compose
|
||||||
|
|
||||||
|
Below is an example of a `docker-compose.yml` file for running a one-time backup:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
services:
|
services:
|
||||||
mysql-bkup:
|
pg-bkup:
|
||||||
# In production, lock the image tag to a specific release version.
|
# In production, pin your image tag to a specific release version instead of `latest`.
|
||||||
# Check https://github.com/jkaninda/mysql-bkup/releases for available releases.
|
# See available releases: https://github.com/jkaninda/mysql-bkup/releases
|
||||||
image: jkaninda/mysql-bkup
|
image: jkaninda/mysql-bkup
|
||||||
container_name: mysql-bkup
|
container_name: mysql-bkup
|
||||||
command: backup
|
command: backup
|
||||||
@@ -71,7 +88,6 @@ services:
|
|||||||
- DB_USERNAME=bar
|
- DB_USERNAME=bar
|
||||||
- DB_PASSWORD=password
|
- DB_PASSWORD=password
|
||||||
- TZ=Europe/Paris
|
- TZ=Europe/Paris
|
||||||
# Ensure the mysql-bkup container is connected to the same network as your database.
|
|
||||||
networks:
|
networks:
|
||||||
- web
|
- web
|
||||||
|
|
||||||
@@ -81,11 +97,11 @@ networks:
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Recurring Backup with Docker
|
### Recurring Backups with Docker
|
||||||
|
|
||||||
To schedule recurring backups, use the `--cron-expression` flag:
|
You can schedule recurring backups using the `--cron-expression` or `-e` flag:
|
||||||
|
|
||||||
```bash
|
```shell
|
||||||
docker run --rm --network network_name \
|
docker run --rm --network network_name \
|
||||||
-v $PWD/backup:/backup/ \
|
-v $PWD/backup:/backup/ \
|
||||||
-e "DB_HOST=hostname" \
|
-e "DB_HOST=hostname" \
|
||||||
@@ -98,9 +114,13 @@ For predefined schedules, refer to the [documentation](https://jkaninda.github.i
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Backup Using Kubernetes
|
## Deploy on Kubernetes
|
||||||
|
|
||||||
Below is an example Kubernetes `Job` configuration for running a backup:
|
For Kubernetes, you can deploy `mysql-bkup` as a Job or CronJob. Below are examples for both.
|
||||||
|
|
||||||
|
### Kubernetes Backup Job
|
||||||
|
|
||||||
|
This example defines a one-time backup job:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: batch/v1
|
apiVersion: batch/v1
|
||||||
@@ -113,8 +133,8 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: mysql-bkup
|
- name: mysql-bkup
|
||||||
# In production, lock the image tag to a specific release version.
|
# Pin the image tag to a specific release version in production.
|
||||||
# Check https://github.com/jkaninda/mysql-bkup/releases for available releases.
|
# See available releases: https://github.com/jkaninda/mysql-bkup/releases
|
||||||
image: jkaninda/mysql-bkup
|
image: jkaninda/mysql-bkup
|
||||||
command:
|
command:
|
||||||
- /bin/sh
|
- /bin/sh
|
||||||
@@ -128,7 +148,7 @@ spec:
|
|||||||
- name: DB_HOST
|
- name: DB_HOST
|
||||||
value: "mysql"
|
value: "mysql"
|
||||||
- name: DB_USERNAME
|
- name: DB_USERNAME
|
||||||
value: "postgres"
|
value: "user"
|
||||||
- name: DB_PASSWORD
|
- name: DB_PASSWORD
|
||||||
value: "password"
|
value: "password"
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
@@ -137,11 +157,51 @@ spec:
|
|||||||
volumes:
|
volumes:
|
||||||
- name: backup
|
- name: backup
|
||||||
hostPath:
|
hostPath:
|
||||||
path: /home/toto/backup # Directory location on the host
|
path: /home/toto/backup # Directory location on the host
|
||||||
type: Directory # Optional field
|
type: Directory # Optional field
|
||||||
restartPolicy: Never
|
restartPolicy: Never
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 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
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Key Notes
|
## Key Notes
|
||||||
|
|||||||
@@ -117,6 +117,9 @@ func createBackupTask(db *dbConfig, config *BackupConfig) {
|
|||||||
if config.all && !config.allInOne {
|
if config.all && !config.allInOne {
|
||||||
backupAll(db, config)
|
backupAll(db, config)
|
||||||
} else {
|
} else {
|
||||||
|
if db.dbName == "" && !config.all {
|
||||||
|
utils.Fatal("Database name is required, use DB_NAME environment variable or -d flag")
|
||||||
|
}
|
||||||
backupTask(db, config)
|
backupTask(db, config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -249,8 +252,10 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression, all
|
|||||||
|
|
||||||
dumpArgs := []string{fmt.Sprintf("--defaults-file=%s", mysqlClientConfig)}
|
dumpArgs := []string{fmt.Sprintf("--defaults-file=%s", mysqlClientConfig)}
|
||||||
if all && singleFile {
|
if all && singleFile {
|
||||||
|
utils.Info("Backing up all databases...")
|
||||||
dumpArgs = append(dumpArgs, "--all-databases", "--single-transaction", "--routines", "--triggers")
|
dumpArgs = append(dumpArgs, "--all-databases", "--single-transaction", "--routines", "--triggers")
|
||||||
} else {
|
} else {
|
||||||
|
utils.Info("Backing up %s database...", db.dbName)
|
||||||
dumpArgs = append(dumpArgs, db.dbName)
|
dumpArgs = append(dumpArgs, db.dbName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -255,7 +255,6 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
|
|||||||
remotePath := utils.GetEnvVariable("REMOTE_PATH", "SSH_REMOTE_PATH")
|
remotePath := utils.GetEnvVariable("REMOTE_PATH", "SSH_REMOTE_PATH")
|
||||||
storage = utils.GetEnv(cmd, "storage", "STORAGE")
|
storage = utils.GetEnv(cmd, "storage", "STORAGE")
|
||||||
prune := false
|
prune := false
|
||||||
configFile := os.Getenv("BACKUP_CONFIG_FILE")
|
|
||||||
backupRetention := utils.GetIntEnv("BACKUP_RETENTION_DAYS")
|
backupRetention := utils.GetIntEnv("BACKUP_RETENTION_DAYS")
|
||||||
if backupRetention > 0 {
|
if backupRetention > 0 {
|
||||||
prune = true
|
prune = true
|
||||||
@@ -280,10 +279,6 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
|
|||||||
encryption = true
|
encryption = true
|
||||||
usingKey = false
|
usingKey = false
|
||||||
}
|
}
|
||||||
dbName := os.Getenv("DB_NAME")
|
|
||||||
if dbName == "" && !all && configFile == "" {
|
|
||||||
utils.Fatal("Database name is required, use DB_NAME environment variable or -d flag")
|
|
||||||
}
|
|
||||||
// Initialize backup configs
|
// Initialize backup configs
|
||||||
config := BackupConfig{}
|
config := BackupConfig{}
|
||||||
config.backupRetention = backupRetention
|
config.backupRetention = backupRetention
|
||||||
|
|||||||
Reference in New Issue
Block a user