Files
mysql-bkup/docs/quickstart/index.md

212 lines
5.4 KiB
Markdown
Raw Normal View History

2025-01-13 14:40:46 +01:00
---
title: Quickstart
layout: home
nav_order: 2
---
# Quickstart
This guide provides quick examples for running backups using Docker CLI, Docker Compose, and Kubernetes.
---
2025-03-16 11:49:36 +01:00
### Simple Backup Using Docker CLI
2025-01-13 14:40:46 +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:
2025-01-13 14:40:46 +01:00
2025-03-16 11:49:36 +01:00
```shell
2025-01-13 14:40:46 +01:00
docker run --rm --network your_network_name \
-v $PWD/backup:/backup/ \
-e "DB_HOST=dbhost" \
2025-01-26 12:11:29 +01:00
-e "DB_PORT=3306" \
2025-01-13 14:40:46 +01:00
-e "DB_USERNAME=username" \
-e "DB_PASSWORD=password" \
jkaninda/mysql-bkup backup -d database_name
```
2025-03-16 11:49:36 +01:00
Alternatively, use an environment file (`--env-file`) for configuration:
2025-01-13 14:40:46 +01:00
2025-03-16 11:49:36 +01:00
```shell
2025-01-13 14:40:46 +01:00
docker run --rm --network your_network_name \
--env-file your-env-file \
-v $PWD/backup:/backup/ \
jkaninda/mysql-bkup backup -d database_name
```
2025-03-16 11:49:36 +01:00
### 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.
2025-01-26 12:11:29 +01:00
2025-03-16 11:49:36 +01:00
---
### 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
2025-01-13 14:40:46 +01:00
---
2025-03-16 11:49:36 +01:00
### Backup with Docker Compose
2025-01-13 14:40:46 +01:00
2025-03-16 11:49:36 +01:00
Below is an example of a `docker-compose.yml` file for running a one-time backup:
2025-01-13 14:40:46 +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
2025-01-13 14:40:46 +01:00
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: backup
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=foo
- DB_USERNAME=bar
- DB_PASSWORD=password
- TZ=Europe/Paris
networks:
- web
networks:
web:
```
---
2025-03-16 11:49:36 +01:00
### Recurring Backups with Docker
2025-01-13 14:40:46 +01:00
2025-03-16 11:49:36 +01:00
You can schedule recurring backups using the `--cron-expression` or `-e` flag:
2025-01-13 14:40:46 +01:00
2025-03-16 11:49:36 +01:00
```shell
2025-01-13 14:40:46 +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"
```
For predefined schedules, refer to the [documentation](https://jkaninda.github.io/mysql-bkup/reference/#predefined-schedules).
---
2025-03-16 11:49:36 +01:00
## Deploy on Kubernetes
For Kubernetes, you can deploy `mysql-bkup` as a Job or CronJob. Below are examples for both.
### Kubernetes Backup Job
2025-01-13 14:40:46 +01:00
2025-03-16 11:49:36 +01:00
This example defines a one-time backup job:
2025-01-13 14:40:46 +01:00
```yaml
apiVersion: batch/v1
kind: Job
metadata:
name: backup-job
spec:
ttlSecondsAfterFinished: 100
template:
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
2025-01-13 14:40:46 +01:00
image: jkaninda/mysql-bkup
command:
- /bin/sh
- -c
- backup -d dbname
resources:
limits:
memory: "128Mi"
cpu: "500m"
env:
- name: DB_HOST
value: "mysql"
- name: DB_USERNAME
2025-03-16 11:49:36 +01:00
value: "user"
2025-01-13 14:40:46 +01:00
- 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
2025-01-13 14:40:46 +01:00
restartPolicy: Never
```
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
```
2025-01-13 14:40:46 +01:00
---
## Key Notes
- **Volume Binding**: Ensure the `/backup` directory is mounted to persist backup files.
- **Environment Variables**: Use environment variables or an `--env-file` to pass database credentials and other configurations.
- **Cron Expressions**: Use standard cron expressions or predefined schedules for recurring backups.
- **Kubernetes Jobs**: Use Kubernetes `Job` or `CronJob` for running backups in a Kubernetes cluster.