mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 13:39:41 +01:00
Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d9d44c2798 | |||
| 300a592508 | |||
| be82e841e7 | |||
| a73a365ebf | |||
| 75e965c0c5 | |||
| fc60ddb308 | |||
| 573ef15ef3 | |||
| b1776d3689 | |||
| 376d47f738 | |||
| eb6268f8ec | |||
| 731e2d789d | |||
| 6300a8f2dd | |||
| cd827a9277 | |||
| 71cf3fae85 | |||
| 528282bbd4 | |||
| 002c93a796 | |||
| b6192f4c42 | |||
| d5061453b0 | |||
| 0bc7497512 | |||
| 489dfdf842 | |||
|
|
907e70d552 | ||
| 696477fe5c | |||
|
|
56a8b51660 | ||
| c76a00139c | |||
| 0f43871765 | |||
| 9ba6abe3f4 | |||
|
|
764583d88f | ||
|
|
dbf4dc596a | ||
|
|
06c89a9b78 | ||
| ec8bdd806c | |||
|
|
828b11c6dd | ||
| 1d01e13909 | |||
| bd65db2418 | |||
| 75b809511e | |||
| fc028a2c55 | |||
| 7fa0c6a118 | |||
| 661702a97e |
6
.github/workflows/deploy-docs.yml
vendored
6
.github/workflows/deploy-docs.yml
vendored
@@ -32,14 +32,14 @@ jobs:
|
|||||||
working-directory: docs
|
working-directory: docs
|
||||||
- name: Setup Pages
|
- name: Setup Pages
|
||||||
id: pages
|
id: pages
|
||||||
uses: actions/configure-pages@v2
|
uses: actions/configure-pages@v5
|
||||||
- name: Build with Jekyll
|
- name: Build with Jekyll
|
||||||
working-directory: docs
|
working-directory: docs
|
||||||
run: bundle exec jekyll build --baseurl "${{ steps.pages.outputs.base_path }}"
|
run: bundle exec jekyll build --baseurl "${{ steps.pages.outputs.base_path }}"
|
||||||
env:
|
env:
|
||||||
JEKYLL_ENV: production
|
JEKYLL_ENV: production
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-pages-artifact@v1
|
uses: actions/upload-pages-artifact@v3
|
||||||
with:
|
with:
|
||||||
path: 'docs/_site/'
|
path: 'docs/_site/'
|
||||||
|
|
||||||
@@ -52,4 +52,4 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Deploy to GitHub Pages
|
- name: Deploy to GitHub Pages
|
||||||
id: deployment
|
id: deployment
|
||||||
uses: actions/deploy-pages@v1
|
uses: actions/deploy-pages@v4
|
||||||
23
.github/workflows/lint.yml
vendored
Normal file
23
.github/workflows/lint.yml
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
name: Lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
name: Run on Ubuntu
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Clone the code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: '~1.23'
|
||||||
|
|
||||||
|
- name: Run linter
|
||||||
|
uses: golangci/golangci-lint-action@v6
|
||||||
|
with:
|
||||||
|
version: v1.61
|
||||||
290
.github/workflows/tests.yml
vendored
Normal file
290
.github/workflows/tests.yml
vendored
Normal file
@@ -0,0 +1,290 @@
|
|||||||
|
name: Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- nightly
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
env:
|
||||||
|
IMAGE_NAME: mysql-bkup
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
services:
|
||||||
|
mysql:
|
||||||
|
image: mysql:9
|
||||||
|
env:
|
||||||
|
MYSQL_ROOT_PASSWORD: password
|
||||||
|
MYSQL_DATABASE: testdb
|
||||||
|
MYSQL_USER: user
|
||||||
|
MYSQL_PASSWORD: password
|
||||||
|
ports:
|
||||||
|
- 3306:3306
|
||||||
|
options: >-
|
||||||
|
--health-cmd="mysqladmin ping -h 127.0.0.1 -uuser -ppassword"
|
||||||
|
--health-interval=10s
|
||||||
|
--health-timeout=5s
|
||||||
|
--health-retries=5
|
||||||
|
mysql8:
|
||||||
|
image: mysql:8
|
||||||
|
env:
|
||||||
|
MYSQL_ROOT_PASSWORD: password
|
||||||
|
MYSQL_DATABASE: testdb
|
||||||
|
MYSQL_USER: user
|
||||||
|
MYSQL_PASSWORD: password
|
||||||
|
ports:
|
||||||
|
- 3308:3306
|
||||||
|
options: >-
|
||||||
|
--health-cmd="mysqladmin ping -h 127.0.0.1 -uuser -ppassword"
|
||||||
|
--health-interval=10s
|
||||||
|
--health-timeout=5s
|
||||||
|
--health-retries=5
|
||||||
|
mysql5:
|
||||||
|
image: mysql:5
|
||||||
|
env:
|
||||||
|
MYSQL_ROOT_PASSWORD: password
|
||||||
|
MYSQL_DATABASE: testdb
|
||||||
|
MYSQL_USER: user
|
||||||
|
MYSQL_PASSWORD: password
|
||||||
|
ports:
|
||||||
|
- 3305:3306
|
||||||
|
options: >-
|
||||||
|
--health-cmd="mysqladmin ping -h 127.0.0.1 -uuser -ppassword"
|
||||||
|
--health-interval=10s
|
||||||
|
--health-timeout=5s
|
||||||
|
--health-retries=5
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
- name: Create Minio container
|
||||||
|
run: |
|
||||||
|
docker run -d --rm --name minio \
|
||||||
|
--network host \
|
||||||
|
-p 9000:9000 \
|
||||||
|
-e MINIO_ACCESS_KEY=minioadmin \
|
||||||
|
-e MINIO_SECRET_KEY=minioadmin \
|
||||||
|
-e MINIO_REGION_NAME="eu" \
|
||||||
|
minio/minio server /data
|
||||||
|
echo "Create Minio container completed"
|
||||||
|
- name: Install MinIO Client (mc)
|
||||||
|
run: |
|
||||||
|
curl -O https://dl.min.io/client/mc/release/linux-amd64/mc
|
||||||
|
chmod +x mc
|
||||||
|
sudo mv mc /usr/local/bin/
|
||||||
|
|
||||||
|
- name: Wait for MinIO to be ready
|
||||||
|
run: sleep 5
|
||||||
|
|
||||||
|
- name: Configure MinIO Client
|
||||||
|
run: |
|
||||||
|
mc alias set local http://localhost:9000 minioadmin minioadmin
|
||||||
|
mc alias list
|
||||||
|
|
||||||
|
- name: Create MinIO Bucket
|
||||||
|
run: |
|
||||||
|
mc mb local/backups
|
||||||
|
echo "Bucket backups created successfully."
|
||||||
|
# Build the Docker image
|
||||||
|
- name: Build Docker Image
|
||||||
|
run: |
|
||||||
|
docker buildx build --build-arg appVersion=test -t ${{ env.IMAGE_NAME }}:latest --load .
|
||||||
|
|
||||||
|
- name: Verify Docker images
|
||||||
|
run: |
|
||||||
|
docker images
|
||||||
|
|
||||||
|
- name: Wait for MySQL to be ready
|
||||||
|
run: |
|
||||||
|
docker run --rm --network host mysql:9 mysqladmin ping -h 127.0.0.1 -uuser -ppassword --wait
|
||||||
|
- name: Test restore
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=root \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest restore -f init.sql
|
||||||
|
echo "Database restore completed"
|
||||||
|
- name: Test restore Mysql8
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_PORT=3308 \
|
||||||
|
-e DB_USERNAME=root \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest restore -f init.sql
|
||||||
|
echo "Test restore Mysql8 completed"
|
||||||
|
- name: Test restore Mysql5
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_PORT=3305 \
|
||||||
|
-e DB_USERNAME=root \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest restore -f init.sql
|
||||||
|
echo "Test restore Mysql5 completed"
|
||||||
|
- name: Test backup
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=user \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest backup
|
||||||
|
echo "Database backup completed"
|
||||||
|
- name: Test backup Mysql8
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_PORT=3308 \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=user \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest backup
|
||||||
|
echo "Test backup Mysql8 completed"
|
||||||
|
- name: Test backup Mysql5
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_PORT=3305 \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=user \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest backup
|
||||||
|
echo "Test backup Mysql5 completed"
|
||||||
|
- name: Test encrypted backup
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=user \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e GPG_PASSPHRASE=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest backup --disable-compression --custom-name encrypted-bkup
|
||||||
|
echo "Database encrypted backup completed"
|
||||||
|
- name: Test restore encrypted backup | testdb -> testdb2
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=root \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e GPG_PASSPHRASE=password \
|
||||||
|
-e DB_NAME=testdb2 \
|
||||||
|
${{ env.IMAGE_NAME }}:latest restore -f /backup/encrypted-bkup.sql.gpg
|
||||||
|
echo "Test restore encrypted backup completed"
|
||||||
|
- name: Test migrate database testdb -> testdb3
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=root \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e GPG_PASSPHRASE=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
-e TARGET_DB_HOST=127.0.0.1 \
|
||||||
|
-e TARGET_DB_PORT=3306 \
|
||||||
|
-e TARGET_DB_NAME=testdb3 \
|
||||||
|
-e TARGET_DB_USERNAME=root \
|
||||||
|
-e TARGET_DB_PASSWORD=password \
|
||||||
|
${{ env.IMAGE_NAME }}:latest migrate
|
||||||
|
echo "Test migrate database testdb -> testdb3 completed"
|
||||||
|
- name: Test backup all databases
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=root \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest backup --all-databases
|
||||||
|
echo "Database backup completed"
|
||||||
|
- name: Test multiple backup
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e TESTDB2_DB_USERNAME=root \
|
||||||
|
-e TESTDB2_DB_PASSWORD=password \
|
||||||
|
-e TESTDB2_DB_HOST=127.0.0.1 \
|
||||||
|
${{ env.IMAGE_NAME }}:latest backup -c /backup/test_config.yaml
|
||||||
|
echo "Database backup completed"
|
||||||
|
- name: Test backup Minio (s3)
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=user \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
-e AWS_S3_ENDPOINT="http://127.0.0.1:9000" \
|
||||||
|
-e AWS_S3_BUCKET_NAME=backups \
|
||||||
|
-e AWS_ACCESS_KEY=minioadmin \
|
||||||
|
-e AWS_SECRET_KEY=minioadmin \
|
||||||
|
-e AWS_DISABLE_SSL="true" \
|
||||||
|
-e AWS_REGION="eu" \
|
||||||
|
-e AWS_FORCE_PATH_STYLE="true" ${{ env.IMAGE_NAME }}:latest backup -s s3 --custom-name minio-backup
|
||||||
|
echo "Test backup Minio (s3) completed"
|
||||||
|
- name: Test restore Minio (s3)
|
||||||
|
run: |
|
||||||
|
docker run --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=user \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
-e AWS_S3_ENDPOINT="http://127.0.0.1:9000" \
|
||||||
|
-e AWS_S3_BUCKET_NAME=backups \
|
||||||
|
-e AWS_ACCESS_KEY=minioadmin \
|
||||||
|
-e AWS_SECRET_KEY=minioadmin \
|
||||||
|
-e AWS_DISABLE_SSL="true" \
|
||||||
|
-e AWS_REGION="eu" \
|
||||||
|
-e AWS_FORCE_PATH_STYLE="true" ${{ env.IMAGE_NAME }}:latest restore -s s3 -f minio-backup.sql.gz
|
||||||
|
echo "Test backup Minio (s3) completed"
|
||||||
|
- name: Test scheduled backup
|
||||||
|
run: |
|
||||||
|
docker run -d --rm --name ${{ env.IMAGE_NAME }} \
|
||||||
|
-v ./migrations:/backup/ \
|
||||||
|
--network host \
|
||||||
|
-e DB_HOST=127.0.0.1 \
|
||||||
|
-e DB_USERNAME=user \
|
||||||
|
-e DB_PASSWORD=password \
|
||||||
|
-e DB_NAME=testdb \
|
||||||
|
${{ env.IMAGE_NAME }}:latest backup -e "@every 10s"
|
||||||
|
|
||||||
|
echo "Waiting for backup to be done..."
|
||||||
|
sleep 25
|
||||||
|
docker logs ${{ env.IMAGE_NAME }}
|
||||||
|
echo "Test scheduled backup completed"
|
||||||
|
# Cleanup: Stop and remove containers
|
||||||
|
- name: Clean up
|
||||||
|
run: |
|
||||||
|
docker stop ${{ env.IMAGE_NAME }} || true
|
||||||
|
docker rm ${{ env.IMAGE_NAME }} || true
|
||||||
@@ -27,6 +27,7 @@ linters:
|
|||||||
- gosimple
|
- gosimple
|
||||||
- govet
|
- govet
|
||||||
- ineffassign
|
- ineffassign
|
||||||
|
# - lll
|
||||||
- misspell
|
- misspell
|
||||||
- nakedret
|
- nakedret
|
||||||
- prealloc
|
- prealloc
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.23.5 AS build
|
FROM golang:1.24.1 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.2
|
FROM alpine:3.21.3
|
||||||
ENV TZ=UTC
|
ENV TZ=UTC
|
||||||
ARG WORKDIR="/config"
|
ARG WORKDIR="/config"
|
||||||
ARG BACKUPDIR="/backup"
|
ARG BACKUPDIR="/backup"
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -3,6 +3,7 @@
|
|||||||
**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.
|
||||||
|
|
||||||
|
[](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)
|
||||||

|

|
||||||
@@ -74,6 +75,7 @@ To run a one time backup, bind your local volume to `/backup` in the container a
|
|||||||
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_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
|
||||||
@@ -87,7 +89,19 @@ Alternatively, pass a `--env-file` in order to use a full config as described be
|
|||||||
-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
|
||||||
|
|
||||||
|
To restore a database, bind your local volume to `/backup` in the container 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
|
||||||
|
```
|
||||||
### Simple backup in docker compose file
|
### Simple backup in docker compose file
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@@ -44,11 +44,14 @@ var BackupCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
//Backup
|
// Backup
|
||||||
BackupCmd.PersistentFlags().StringP("storage", "s", "local", "Define storage: local, s3, ssh, ftp, azure")
|
BackupCmd.PersistentFlags().StringP("storage", "s", "local", "Define storage: local, s3, ssh, ftp, azure")
|
||||||
BackupCmd.PersistentFlags().StringP("path", "P", "", "Storage path without file name. e.g: /custom_path or ssh remote path `/home/foo/backup`")
|
BackupCmd.PersistentFlags().StringP("path", "P", "", "Storage path without file name. e.g: /custom_path or ssh remote path `/home/foo/backup`")
|
||||||
BackupCmd.PersistentFlags().StringP("cron-expression", "e", "", "Backup cron expression (e.g., `0 0 * * *` or `@daily`)")
|
BackupCmd.PersistentFlags().StringP("cron-expression", "e", "", "Backup cron expression (e.g., `0 0 * * *` or `@daily`)")
|
||||||
BackupCmd.PersistentFlags().StringP("config", "c", "", "Configuration file for multi database backup. (e.g: `/backup/config.yaml`)")
|
BackupCmd.PersistentFlags().StringP("config", "c", "", "Configuration file for multi database backup. (e.g: `/backup/config.yaml`)")
|
||||||
BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
|
BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
|
||||||
|
BackupCmd.PersistentFlags().BoolP("all-databases", "a", false, "Backup all databases")
|
||||||
|
BackupCmd.PersistentFlags().BoolP("all-in-one", "A", false, "Backup all databases in a single file")
|
||||||
|
BackupCmd.PersistentFlags().StringP("custom-name", "", "", "Custom backup name")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ var RestoreCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
//Restore
|
// Restore
|
||||||
RestoreCmd.PersistentFlags().StringP("file", "f", "", "File name of database")
|
RestoreCmd.PersistentFlags().StringP("file", "f", "", "File name of database")
|
||||||
RestoreCmd.PersistentFlags().StringP("storage", "s", "local", "Define storage: local, s3, ssh, ftp")
|
RestoreCmd.PersistentFlags().StringP("storage", "s", "local", "Define storage: local, s3, ssh, ftp")
|
||||||
RestoreCmd.PersistentFlags().StringP("path", "P", "", "AWS S3 path without file name. eg: /custom_path or ssh remote path `/home/foo/backup`")
|
RestoreCmd.PersistentFlags().StringP("path", "P", "", "AWS S3 path without file name. eg: /custom_path or ssh remote path `/home/foo/backup`")
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ var rootCmd = &cobra.Command{
|
|||||||
Example: utils.MainExample,
|
Example: utils.MainExample,
|
||||||
Version: appVersion,
|
Version: appVersion,
|
||||||
}
|
}
|
||||||
var operation = ""
|
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
|
|||||||
61
docs/how-tos/backup-all.md
Normal file
61
docs/how-tos/backup-all.md
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
---
|
||||||
|
title: Backup all databases in the server
|
||||||
|
layout: default
|
||||||
|
parent: How Tos
|
||||||
|
nav_order: 12
|
||||||
|
---
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
Backing up all databases is useful for creating a snapshot of the entire database server, whether for disaster recovery or migration purposes.
|
||||||
|
## Backup Modes
|
||||||
|
|
||||||
|
### Separate Backup Files (Default)
|
||||||
|
|
||||||
|
Using --all-databases without --all-in-one creates individual backup files for each database.
|
||||||
|
|
||||||
|
- Creates separate backup files for each database.
|
||||||
|
- Provides more flexibility in restoring individual databases or tables.
|
||||||
|
- Can be more manageable in cases where different databases have different retention policies.
|
||||||
|
- Might take slightly longer due to multiple file operations.
|
||||||
|
- It is the default behavior when using the `--all-databases` flag.
|
||||||
|
- It does not backup system databases (`information_schema`, `performance_schema`, `mysql`, `sys`, `innodb`).
|
||||||
|
|
||||||
|
**Command:**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
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
|
||||||
|
```
|
||||||
|
### Single Backup File
|
||||||
|
|
||||||
|
Using --all-in-one (-A) creates a single backup file containing all databases.
|
||||||
|
|
||||||
|
- Creates a single backup file containing all databases.
|
||||||
|
- Easier to manage if you need to restore everything at once.
|
||||||
|
- Faster to back up and restore in bulk.
|
||||||
|
- Can be problematic if you only need to restore a specific database or table.
|
||||||
|
- It is recommended to use this option for disaster recovery purposes.
|
||||||
|
- It backups system databases as well.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
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-in-one
|
||||||
|
```
|
||||||
|
|
||||||
|
### When to Use Which?
|
||||||
|
|
||||||
|
- Use `--all-in-one` if you want a quick, simple backup for disaster recovery where you'll restore everything at once.
|
||||||
|
- Use `--all-databases` if you need granularity in restoring specific databases or tables without affecting others.
|
||||||
@@ -34,8 +34,8 @@ Below is an example configuration file (`config.yaml`) that defines multiple dat
|
|||||||
```yaml
|
```yaml
|
||||||
# Optional: Define a global cron expression for scheduled backups.
|
# Optional: Define a global cron expression for scheduled backups.
|
||||||
# Example: "@every 20m" (runs every 20 minutes). If omitted, backups run immediately.
|
# Example: "@every 20m" (runs every 20 minutes). If omitted, backups run immediately.
|
||||||
cronExpression: ""
|
cronExpression: "" # Optional: Define a global cron expression for scheduled backups.
|
||||||
|
backupRescueMode: false # Optional: Set to true to enable rescue mode for backups.
|
||||||
databases:
|
databases:
|
||||||
- host: mysql1 # Optional: Overrides DB_HOST or uses DB_HOST_DATABASE1.
|
- host: mysql1 # Optional: Overrides DB_HOST or uses DB_HOST_DATABASE1.
|
||||||
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_DATABASE1.
|
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_DATABASE1.
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
title: Receive notifications
|
title: Receive notifications
|
||||||
layout: default
|
layout: default
|
||||||
parent: How Tos
|
parent: How Tos
|
||||||
nav_order: 12
|
nav_order: 13
|
||||||
---
|
---
|
||||||
|
|
||||||
# Receive Notifications
|
# Receive Notifications
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ To run a one-time backup, bind your local volume to `/backup` in the container a
|
|||||||
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_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
|
||||||
@@ -34,6 +35,19 @@ docker run --rm --network your_network_name \
|
|||||||
jkaninda/mysql-bkup backup -d database_name
|
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
|
||||||
|
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
|
||||||
|
```
|
||||||
---
|
---
|
||||||
|
|
||||||
## Simple Backup Using Docker Compose
|
## Simple Backup Using Docker Compose
|
||||||
|
|||||||
@@ -6,28 +6,31 @@ nav_order: 3
|
|||||||
|
|
||||||
# Configuration Reference
|
# Configuration Reference
|
||||||
|
|
||||||
Backup, restore, and migration targets, schedules, and retention policies are configured using **environment variables** or **CLI flags**.
|
MySQL backup, restore, and migration processes can be configured using **environment variables** or **CLI flags**.
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## CLI Utility Usage
|
## CLI Utility Usage
|
||||||
|
|
||||||
| Option | Short Flag | Description |
|
The `mysql-bkup` CLI provides commands and options to manage MySQL backups efficiently.
|
||||||
|-------------------------|------------|-------------------------------------------------------------------------------|
|
|
||||||
| `pg-bkup` | `bkup` | CLI utility for managing PostgreSQL backups. |
|
| Option | Short Flag | Description |
|
||||||
| `backup` | | Perform a backup operation. |
|
|-------------------------|------------|-----------------------------------------------------------------------------------------|
|
||||||
| `restore` | | Perform a restore operation. |
|
| `mysql-bkup` | `bkup` | CLI tool for managing MySQL backups, restoration, and migration. |
|
||||||
| `migrate` | | Migrate a database from one instance to another. |
|
| `backup` | | Executes a backup operation. |
|
||||||
| `--storage` | `-s` | Storage type (`local`, `s3`, `ssh`, etc.). Default: `local`. |
|
| `restore` | | Restores a database from a backup file. |
|
||||||
| `--file` | `-f` | File name for restoration. |
|
| `migrate` | | Migrates a database from one instance to another. |
|
||||||
| `--path` | | Path for storage (e.g., `/custom_path` for S3 or `/home/foo/backup` for SSH). |
|
| `--storage` | `-s` | Specifies the storage type (`local`, `s3`, `ssh`, etc.). Default: `local`. |
|
||||||
| `--config` | `-c` | Configuration file for multi database backup. (e.g: `/backup/config.yaml`). |
|
| `--file` | `-f` | Defines the backup file name for restoration. |
|
||||||
| `--dbname` | `-d` | Database name. |
|
| `--path` | | Sets the storage path (e.g., `/custom_path` for S3 or `/home/foo/backup` for SSH). |
|
||||||
| `--port` | `-p` | Database port. Default: `3306`. |
|
| `--config` | `-c` | Provides a configuration file for multi-database backups (e.g., `/backup/config.yaml`). |
|
||||||
| `--disable-compression` | | Disable compression for database backups. |
|
| `--dbname` | `-d` | Specifies the database name to back up or restore. |
|
||||||
| `--cron-expression` | `-e` | Cron expression for scheduled backups (e.g., `0 0 * * *` or `@daily`). |
|
| `--port` | `-p` | Defines the database port. Default: `3306`. |
|
||||||
| `--help` | `-h` | Display help message and exit. |
|
| `--disable-compression` | | Disables compression for database backups. |
|
||||||
| `--version` | `-V` | Display version information and exit. |
|
| `--cron-expression` | `-e` | Schedules backups using a cron expression (e.g., `0 0 * * *` or `@daily`). |
|
||||||
|
| `--all-databases` | `-a` | Backs up all databases separately (e.g., `backup --all-databases`). |
|
||||||
|
| `--all-in-one` | `-A` | Backs up all databases in a single file (e.g., `backup --all-databases --single-file`). |
|
||||||
|
| `--custom-name` | `` | Sets custom backup name for one time backup |
|
||||||
|
| `--help` | `-h` | Displays the help message and exits. |
|
||||||
|
| `--version` | `-V` | Shows version information and exits. |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -40,6 +43,8 @@ Backup, restore, and migration targets, schedules, and retention policies are co
|
|||||||
| `DB_NAME` | Optional (if provided via `-d` flag) | Database name. |
|
| `DB_NAME` | Optional (if provided via `-d` flag) | Database name. |
|
||||||
| `DB_USERNAME` | Required | Database username. |
|
| `DB_USERNAME` | Required | Database username. |
|
||||||
| `DB_PASSWORD` | Required | Database password. |
|
| `DB_PASSWORD` | Required | Database password. |
|
||||||
|
| `DB_SSL_CA` | Optional | Database client CA certificate file |
|
||||||
|
| `DB_SSL_MODE` | Optional(`0 or 1`) default: `0` | Database client Enable CA validation |
|
||||||
| `AWS_ACCESS_KEY` | Required for S3 storage | AWS S3 Access Key. |
|
| `AWS_ACCESS_KEY` | Required for S3 storage | AWS S3 Access Key. |
|
||||||
| `AWS_SECRET_KEY` | Required for S3 storage | AWS S3 Secret Key. |
|
| `AWS_SECRET_KEY` | Required for S3 storage | AWS S3 Secret Key. |
|
||||||
| `AWS_BUCKET_NAME` | Required for S3 storage | AWS S3 Bucket Name. |
|
| `AWS_BUCKET_NAME` | Required for S3 storage | AWS S3 Bucket Name. |
|
||||||
|
|||||||
6
go.mod
6
go.mod
@@ -2,14 +2,15 @@ module github.com/jkaninda/mysql-bkup
|
|||||||
|
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
require github.com/spf13/pflag v1.0.5 // indirect
|
require github.com/spf13/pflag v1.0.6 // indirect
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-mail/mail v2.3.1+incompatible
|
github.com/go-mail/mail v2.3.1+incompatible
|
||||||
github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e
|
github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e
|
||||||
github.com/jkaninda/go-storage v0.1.3
|
github.com/jkaninda/go-storage v0.1.3
|
||||||
|
github.com/jkaninda/go-utils v0.1.1
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.9.1
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,7 +27,6 @@ require (
|
|||||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a // indirect
|
|
||||||
github.com/jlaffaye/ftp v0.2.0 // indirect
|
github.com/jlaffaye/ftp v0.2.0 // indirect
|
||||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
|||||||
16
go.sum
16
go.sum
@@ -22,7 +22,7 @@ github.com/bramvdbogaerde/go-scp v1.5.0 h1:a9BinAjTfQh273eh7vd3qUgmBC+bx+3TRDtkZ
|
|||||||
github.com/bramvdbogaerde/go-scp v1.5.0/go.mod h1:on2aH5AxaFb2G0N5Vsdy6B0Ml7k9HuHSwfo1y0QzAbQ=
|
github.com/bramvdbogaerde/go-scp v1.5.0/go.mod h1:on2aH5AxaFb2G0N5Vsdy6B0Ml7k9HuHSwfo1y0QzAbQ=
|
||||||
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
|
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
|
||||||
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
|
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
@@ -43,10 +43,8 @@ github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e h1:jtFKZHt/PLGQ
|
|||||||
github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e/go.mod h1:Y1EXpPWQ9PNd7y7E6ez3xgnzZc8fuDWXwX/1/dXNCE4=
|
github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e/go.mod h1:Y1EXpPWQ9PNd7y7E6ez3xgnzZc8fuDWXwX/1/dXNCE4=
|
||||||
github.com/jkaninda/go-storage v0.1.3 h1:lEpHVgFLKSvjsi/6tAek96Y07za3vxmsXF2/+jiCMZU=
|
github.com/jkaninda/go-storage v0.1.3 h1:lEpHVgFLKSvjsi/6tAek96Y07za3vxmsXF2/+jiCMZU=
|
||||||
github.com/jkaninda/go-storage v0.1.3/go.mod h1:zVRnLprBk/9AUz2+za6Y03MgoNYrqKLy3edVtjqMaps=
|
github.com/jkaninda/go-storage v0.1.3/go.mod h1:zVRnLprBk/9AUz2+za6Y03MgoNYrqKLy3edVtjqMaps=
|
||||||
github.com/jkaninda/go-utils v0.0.0-20250122054739-d330fecee150 h1:AgcKk58P/Z+u4DBE2MTyZ6kCweA89YUpX7TuttHS3oQ=
|
github.com/jkaninda/go-utils v0.1.1 h1:PMrtXR9d51YzHo85y9Z6YVL0YyBURbRTPemHVbFDqZg=
|
||||||
github.com/jkaninda/go-utils v0.0.0-20250122054739-d330fecee150/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
|
github.com/jkaninda/go-utils v0.1.1/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
|
||||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a h1:ZTpKujQGhEF266RWkD2cXnCsafk3R2+sGtfbzCQSs1s=
|
|
||||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
|
|
||||||
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
|
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
|
||||||
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
|
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
|
||||||
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
||||||
@@ -70,10 +68,10 @@ github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzG
|
|||||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
|
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||||
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
|
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
|||||||
35
migrations/init.sql
Normal file
35
migrations/init.sql
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
-- Create the database testdb2 and testdb3
|
||||||
|
CREATE DATABASE IF NOT EXISTS testdb2;
|
||||||
|
CREATE DATABASE IF NOT EXISTS testdb3;
|
||||||
|
CREATE DATABASE IF NOT EXISTS fakedb;
|
||||||
|
USE testdb;
|
||||||
|
|
||||||
|
-- Create the 'users' table
|
||||||
|
CREATE TABLE users (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
name VARCHAR(100) NOT NULL,
|
||||||
|
email VARCHAR(100) NOT NULL UNIQUE,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create the 'orders' table
|
||||||
|
CREATE TABLE orders (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
user_id INT NOT NULL,
|
||||||
|
amount DECIMAL(10,2) NOT NULL,
|
||||||
|
status ENUM('pending', 'completed', 'canceled') NOT NULL DEFAULT 'pending',
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Insert fake users
|
||||||
|
INSERT INTO users (name, email) VALUES
|
||||||
|
('Alice Smith', 'alice@example.com'),
|
||||||
|
('Bob Johnson', 'bob@example.com'),
|
||||||
|
('Charlie Brown', 'charlie@example.com');
|
||||||
|
|
||||||
|
-- Insert fake orders
|
||||||
|
INSERT INTO orders (user_id, amount, status) VALUES
|
||||||
|
(1, 100.50, 'completed'),
|
||||||
|
(2, 200.75, 'pending'),
|
||||||
|
(3, 50.00, 'canceled');
|
||||||
13
migrations/test_config.yaml
Normal file
13
migrations/test_config.yaml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#cronExpression: "@every 20s"
|
||||||
|
#backupRescueMode: false
|
||||||
|
databases:
|
||||||
|
- host: 127.0.0.1
|
||||||
|
port: 3306
|
||||||
|
name: testdb
|
||||||
|
user: user
|
||||||
|
password: password
|
||||||
|
- name: testdb2
|
||||||
|
# database credentials from environment variables
|
||||||
|
#TESTDB2_DB_USERNAME
|
||||||
|
#TESTDB2_DB_PASSWORD
|
||||||
|
#TESTDB2_DB_HOST
|
||||||
@@ -39,7 +39,11 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
|
|||||||
utils.Info("Backup database to Azure Blob Storage")
|
utils.Info("Backup database to Azure Blob Storage")
|
||||||
|
|
||||||
// Backup database
|
// Backup database
|
||||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
|
||||||
|
if err != nil {
|
||||||
|
recoverMode(err, "Error backing up database")
|
||||||
|
return
|
||||||
|
}
|
||||||
finalFileName := config.backupFileName
|
finalFileName := config.backupFileName
|
||||||
if config.encryption {
|
if config.encryption {
|
||||||
encryptBackup(config)
|
encryptBackup(config)
|
||||||
@@ -100,7 +104,7 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
|
|||||||
})
|
})
|
||||||
// Delete temp
|
// Delete temp
|
||||||
deleteTemp()
|
deleteTemp()
|
||||||
utils.Info("Backup successfully completed in %s", duration)
|
utils.Info("The backup of the %s database has been completed in %s", db.dbName, duration)
|
||||||
}
|
}
|
||||||
func azureRestore(db *dbConfig, conf *RestoreConfig) {
|
func azureRestore(db *dbConfig, conf *RestoreConfig) {
|
||||||
utils.Info("Restore database from Azure Blob storage")
|
utils.Info("Restore database from Azure Blob storage")
|
||||||
|
|||||||
254
pkg/backup.go
254
pkg/backup.go
@@ -26,6 +26,8 @@ SOFTWARE.
|
|||||||
package pkg
|
package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jkaninda/encryptor"
|
"github.com/jkaninda/encryptor"
|
||||||
"github.com/jkaninda/go-storage/pkg/local"
|
"github.com/jkaninda/go-storage/pkg/local"
|
||||||
@@ -33,10 +35,10 @@ import (
|
|||||||
"github.com/jkaninda/mysql-bkup/utils"
|
"github.com/jkaninda/mysql-bkup/utils"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,7 +51,8 @@ func StartBackup(cmd *cobra.Command) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
dbConf = initDbConfig(cmd)
|
dbConf = initDbConfig(cmd)
|
||||||
if config.cronExpression == "" {
|
if config.cronExpression == "" {
|
||||||
BackupTask(dbConf, config)
|
config.allowCustomName = true
|
||||||
|
createBackupTask(dbConf, config)
|
||||||
} else {
|
} else {
|
||||||
if utils.IsValidCronExpression(config.cronExpression) {
|
if utils.IsValidCronExpression(config.cronExpression) {
|
||||||
scheduledMode(dbConf, config)
|
scheduledMode(dbConf, config)
|
||||||
@@ -72,14 +75,18 @@ func scheduledMode(db *dbConfig, config *BackupConfig) {
|
|||||||
|
|
||||||
// Test backup
|
// Test backup
|
||||||
utils.Info("Testing backup configurations...")
|
utils.Info("Testing backup configurations...")
|
||||||
testDatabaseConnection(db)
|
err := testDatabaseConnection(db)
|
||||||
|
if err != nil {
|
||||||
|
utils.Error("Error connecting to database: %s", db.dbName)
|
||||||
|
utils.Fatal("Error: %s", err)
|
||||||
|
}
|
||||||
utils.Info("Testing backup configurations...done")
|
utils.Info("Testing backup configurations...done")
|
||||||
utils.Info("Creating backup job...")
|
utils.Info("Creating backup job...")
|
||||||
// Create a new cron instance
|
// Create a new cron instance
|
||||||
c := cron.New()
|
c := cron.New()
|
||||||
|
|
||||||
_, err := c.AddFunc(config.cronExpression, func() {
|
_, err = c.AddFunc(config.cronExpression, func() {
|
||||||
BackupTask(db, config)
|
createBackupTask(db, config)
|
||||||
utils.Info("Next backup time is: %v", utils.CronNextTime(config.cronExpression).Format(timeFormat))
|
utils.Info("Next backup time is: %v", utils.CronNextTime(config.cronExpression).Format(timeFormat))
|
||||||
|
|
||||||
})
|
})
|
||||||
@@ -101,28 +108,66 @@ func multiBackupTask(databases []Database, bkConfig *BackupConfig) {
|
|||||||
if db.Path != "" {
|
if db.Path != "" {
|
||||||
bkConfig.remotePath = db.Path
|
bkConfig.remotePath = db.Path
|
||||||
}
|
}
|
||||||
BackupTask(getDatabase(db), bkConfig)
|
createBackupTask(getDatabase(db), bkConfig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackupTask backups database
|
// createBackupTask backup task
|
||||||
func BackupTask(db *dbConfig, config *BackupConfig) {
|
func createBackupTask(db *dbConfig, config *BackupConfig) {
|
||||||
|
if config.all && !config.allInOne {
|
||||||
|
backupAll(db, config)
|
||||||
|
} else {
|
||||||
|
backupTask(db, config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// backupAll backup all databases
|
||||||
|
func backupAll(db *dbConfig, config *BackupConfig) {
|
||||||
|
databases, err := listDatabases(*db)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatal("Error listing databases: %s", err)
|
||||||
|
}
|
||||||
|
for _, dbName := range databases {
|
||||||
|
if dbName == "information_schema" || dbName == "performance_schema" || dbName == "mysql" || dbName == "sys" || dbName == "innodb" || dbName == "Database" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
db.dbName = dbName
|
||||||
|
config.backupFileName = fmt.Sprintf("%s_%s.sql.gz", dbName, time.Now().Format("20060102_150405"))
|
||||||
|
backupTask(db, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// backupTask backup task
|
||||||
|
func backupTask(db *dbConfig, config *BackupConfig) {
|
||||||
utils.Info("Starting backup task...")
|
utils.Info("Starting backup task...")
|
||||||
startTime = time.Now()
|
startTime = time.Now()
|
||||||
|
prefix := db.dbName
|
||||||
|
if config.all && config.allInOne {
|
||||||
|
prefix = "all_databases"
|
||||||
|
}
|
||||||
|
|
||||||
// Generate file name
|
// Generate file name
|
||||||
backupFileName := fmt.Sprintf("%s_%s.sql.gz", db.dbName, time.Now().Format("20060102_150405"))
|
backupFileName := fmt.Sprintf("%s_%s.sql.gz", prefix, time.Now().Format("20060102_150405"))
|
||||||
if config.disableCompression {
|
if config.disableCompression {
|
||||||
backupFileName = fmt.Sprintf("%s_%s.sql", db.dbName, time.Now().Format("20060102_150405"))
|
backupFileName = fmt.Sprintf("%s_%s.sql", prefix, time.Now().Format("20060102_150405"))
|
||||||
|
}
|
||||||
|
if config.customName != "" && config.allowCustomName && !config.all {
|
||||||
|
backupFileName = fmt.Sprintf("%s.sql.gz", config.customName)
|
||||||
|
if config.disableCompression {
|
||||||
|
backupFileName = fmt.Sprintf("%s.sql", config.customName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
config.backupFileName = backupFileName
|
config.backupFileName = backupFileName
|
||||||
switch config.storage {
|
s := strings.ToLower(config.storage)
|
||||||
|
switch s {
|
||||||
case "local":
|
case "local":
|
||||||
localBackup(db, config)
|
localBackup(db, config)
|
||||||
case "s3", "S3":
|
case "s3":
|
||||||
s3Backup(db, config)
|
s3Backup(db, config)
|
||||||
case "ssh", "SSH", "remote", "sftp":
|
case "ssh", "remote", "sftp":
|
||||||
sshBackup(db, config)
|
sshBackup(db, config)
|
||||||
case "ftp", "FTP":
|
case "ftp":
|
||||||
ftpBackup(db, config)
|
ftpBackup(db, config)
|
||||||
case "azure":
|
case "azure":
|
||||||
azureBackup(db, config)
|
azureBackup(db, config)
|
||||||
@@ -130,6 +175,8 @@ func BackupTask(db *dbConfig, config *BackupConfig) {
|
|||||||
localBackup(db, config)
|
localBackup(db, config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// startMultiBackup start multi backup
|
||||||
func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
||||||
utils.Info("Starting Multi backup task...")
|
utils.Info("Starting Multi backup task...")
|
||||||
conf, err := readConf(configFile)
|
conf, err := readConf(configFile)
|
||||||
@@ -147,6 +194,7 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
|||||||
if bkConfig.cronExpression == "" {
|
if bkConfig.cronExpression == "" {
|
||||||
multiBackupTask(conf.Databases, bkConfig)
|
multiBackupTask(conf.Databases, bkConfig)
|
||||||
} else {
|
} else {
|
||||||
|
backupRescueMode = conf.BackupRescueMode
|
||||||
// Check if cronExpression is valid
|
// Check if cronExpression is valid
|
||||||
if utils.IsValidCronExpression(bkConfig.cronExpression) {
|
if utils.IsValidCronExpression(bkConfig.cronExpression) {
|
||||||
utils.Info("Running backup in Scheduled mode")
|
utils.Info("Running backup in Scheduled mode")
|
||||||
@@ -157,7 +205,11 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
|||||||
// Test backup
|
// Test backup
|
||||||
utils.Info("Testing backup configurations...")
|
utils.Info("Testing backup configurations...")
|
||||||
for _, db := range conf.Databases {
|
for _, db := range conf.Databases {
|
||||||
testDatabaseConnection(getDatabase(db))
|
err = testDatabaseConnection(getDatabase(db))
|
||||||
|
if err != nil {
|
||||||
|
recoverMode(err, fmt.Sprintf("Error connecting to database: %s", db.Name))
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
utils.Info("Testing backup configurations...done")
|
utils.Info("Testing backup configurations...done")
|
||||||
utils.Info("Creating backup job...")
|
utils.Info("Creating backup job...")
|
||||||
@@ -187,78 +239,83 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BackupDatabase backup database
|
// BackupDatabase backup database
|
||||||
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool) {
|
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression, all, singleFile bool) error {
|
||||||
storagePath = os.Getenv("STORAGE_PATH")
|
storagePath = os.Getenv("STORAGE_PATH")
|
||||||
|
|
||||||
utils.Info("Starting database backup...")
|
utils.Info("Starting database backup...")
|
||||||
|
|
||||||
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
if err := testDatabaseConnection(db); err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("database connection failed: %w", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
testDatabaseConnection(db)
|
|
||||||
// Backup Database database
|
|
||||||
utils.Info("Backing up database...")
|
|
||||||
|
|
||||||
// Verify is compression is disabled
|
|
||||||
if disableCompression {
|
|
||||||
// Execute mysqldump
|
|
||||||
cmd := exec.Command("mysqldump",
|
|
||||||
"-h", db.dbHost,
|
|
||||||
"-P", db.dbPort,
|
|
||||||
"-u", db.dbUserName,
|
|
||||||
db.dbName,
|
|
||||||
)
|
|
||||||
output, err := cmd.Output()
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// save output
|
|
||||||
file, err := os.Create(filepath.Join(tmpPath, backupFileName))
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
defer func(file *os.File) {
|
|
||||||
err := file.Close()
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
}(file)
|
|
||||||
|
|
||||||
_, err = file.Write(output)
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
utils.Info("Database has been backed up")
|
|
||||||
|
|
||||||
|
dumpArgs := []string{fmt.Sprintf("--defaults-file=%s", mysqlClientConfig)}
|
||||||
|
if all && singleFile {
|
||||||
|
dumpArgs = append(dumpArgs, "--all-databases", "--single-transaction", "--routines", "--triggers")
|
||||||
} else {
|
} else {
|
||||||
// Execute mysqldump
|
dumpArgs = append(dumpArgs, db.dbName)
|
||||||
cmd := exec.Command("mysqldump", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName)
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
gzipCmd := exec.Command("gzip")
|
|
||||||
gzipCmd.Stdin = stdout
|
|
||||||
gzipCmd.Stdout, err = os.Create(filepath.Join(tmpPath, backupFileName))
|
|
||||||
err = gzipCmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := gzipCmd.Wait(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
utils.Info("Database has been backed up")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
backupPath := filepath.Join(tmpPath, backupFileName)
|
||||||
|
if disableCompression {
|
||||||
|
return runCommandAndSaveOutput("mysqldump", dumpArgs, backupPath)
|
||||||
|
}
|
||||||
|
return runCommandWithCompression("mysqldump", dumpArgs, backupPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// runCommandAndSaveOutput runs a command and saves the output to a file
|
||||||
|
func runCommandAndSaveOutput(command string, args []string, outputPath string) error {
|
||||||
|
cmd := exec.Command(command, args...)
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to execute %s: %v, output: %s", command, err, string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.WriteFile(outputPath, output, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
// runCommandWithCompression runs a command and compresses the output
|
||||||
|
func runCommandWithCompression(command string, args []string, outputPath string) error {
|
||||||
|
cmd := exec.Command(command, args...)
|
||||||
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create stdout pipe: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gzipCmd := exec.Command("gzip")
|
||||||
|
gzipCmd.Stdin = stdout
|
||||||
|
gzipFile, err := os.Create(outputPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create gzip file: %w", err)
|
||||||
|
}
|
||||||
|
defer func(gzipFile *os.File) {
|
||||||
|
err := gzipFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
utils.Error("Error closing gzip file: %v", err)
|
||||||
|
}
|
||||||
|
}(gzipFile)
|
||||||
|
gzipCmd.Stdout = gzipFile
|
||||||
|
|
||||||
|
if err := gzipCmd.Start(); err != nil {
|
||||||
|
return fmt.Errorf("failed to start gzip: %w", err)
|
||||||
|
}
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return fmt.Errorf("failed to execute %s: %w", command, err)
|
||||||
|
}
|
||||||
|
if err := gzipCmd.Wait(); err != nil {
|
||||||
|
return fmt.Errorf("failed to wait for gzip completion: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.Info("Database has been backed up")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// localBackup backup database to local storage
|
||||||
func localBackup(db *dbConfig, config *BackupConfig) {
|
func localBackup(db *dbConfig, config *BackupConfig) {
|
||||||
utils.Info("Backup database to local storage")
|
utils.Info("Backup database to local storage")
|
||||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
|
||||||
|
if err != nil {
|
||||||
|
recoverMode(err, "Error backing up database")
|
||||||
|
return
|
||||||
|
}
|
||||||
finalFileName := config.backupFileName
|
finalFileName := config.backupFileName
|
||||||
if config.encryption {
|
if config.encryption {
|
||||||
encryptBackup(config)
|
encryptBackup(config)
|
||||||
@@ -301,9 +358,10 @@ func localBackup(db *dbConfig, config *BackupConfig) {
|
|||||||
}
|
}
|
||||||
// Delete temp
|
// Delete temp
|
||||||
deleteTemp()
|
deleteTemp()
|
||||||
utils.Info("Backup successfully completed in %s", duration)
|
utils.Info("The backup of the %s database has been completed in %s", db.dbName, duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// encryptBackup encrypt backup
|
||||||
func encryptBackup(config *BackupConfig) {
|
func encryptBackup(config *BackupConfig) {
|
||||||
backupFile, err := os.ReadFile(filepath.Join(tmpPath, config.backupFileName))
|
backupFile, err := os.ReadFile(filepath.Join(tmpPath, config.backupFileName))
|
||||||
outputFile := fmt.Sprintf("%s.%s", filepath.Join(tmpPath, config.backupFileName), gpgExtension)
|
outputFile := fmt.Sprintf("%s.%s", filepath.Join(tmpPath, config.backupFileName), gpgExtension)
|
||||||
@@ -333,3 +391,43 @@ func encryptBackup(config *BackupConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// listDatabases list all databases
|
||||||
|
func listDatabases(db dbConfig) ([]string, error) {
|
||||||
|
databases := []string{}
|
||||||
|
// Create the mysql client config file
|
||||||
|
if err := createMysqlClientConfigFile(db); err != nil {
|
||||||
|
return databases, errors.New(err.Error())
|
||||||
|
}
|
||||||
|
utils.Info("Listing databases...")
|
||||||
|
// Step 1: List all databases
|
||||||
|
cmd := exec.Command("mariadb", fmt.Sprintf("--defaults-file=%s", mysqlClientConfig), "-e", "SHOW DATABASES;")
|
||||||
|
var out bytes.Buffer
|
||||||
|
cmd.Stdout = &out
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return databases, fmt.Errorf("failed to list databases: %s", err)
|
||||||
|
}
|
||||||
|
// Step 2: Parse the output
|
||||||
|
for _, _db := range strings.Split(out.String(), "\n") {
|
||||||
|
if _db != "" {
|
||||||
|
databases = append(databases, _db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return databases, nil
|
||||||
|
}
|
||||||
|
func recoverMode(err error, msg string) {
|
||||||
|
if err != nil {
|
||||||
|
if backupRescueMode {
|
||||||
|
utils.NotifyError(fmt.Sprintf("%s : %v", msg, err))
|
||||||
|
utils.Error("Error: %s", msg)
|
||||||
|
utils.Error("Backup rescue mode is enabled")
|
||||||
|
utils.Error("Backup will continue")
|
||||||
|
} else {
|
||||||
|
utils.Error("Error: %s", msg)
|
||||||
|
utils.Fatal("Error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,8 +42,9 @@ type Database struct {
|
|||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
}
|
}
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Databases []Database `yaml:"databases"`
|
CronExpression string `yaml:"cronExpression"`
|
||||||
CronExpression string `yaml:"cronExpression"`
|
BackupRescueMode bool `yaml:"backupRescueMode"`
|
||||||
|
Databases []Database `yaml:"databases"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type dbConfig struct {
|
type dbConfig struct {
|
||||||
@@ -76,6 +77,10 @@ type BackupConfig struct {
|
|||||||
publicKey string
|
publicKey string
|
||||||
storage string
|
storage string
|
||||||
cronExpression string
|
cronExpression string
|
||||||
|
all bool
|
||||||
|
allInOne bool
|
||||||
|
customName string
|
||||||
|
allowCustomName bool
|
||||||
}
|
}
|
||||||
type FTPConfig struct {
|
type FTPConfig struct {
|
||||||
host string
|
host string
|
||||||
@@ -114,7 +119,7 @@ func initDbConfig(cmd *cobra.Command) *dbConfig {
|
|||||||
utils.GetEnv(cmd, "dbname", "DB_NAME")
|
utils.GetEnv(cmd, "dbname", "DB_NAME")
|
||||||
dConf := dbConfig{}
|
dConf := dbConfig{}
|
||||||
dConf.dbHost = os.Getenv("DB_HOST")
|
dConf.dbHost = os.Getenv("DB_HOST")
|
||||||
dConf.dbPort = os.Getenv("DB_PORT")
|
dConf.dbPort = utils.EnvWithDefault("DB_PORT", "3306")
|
||||||
dConf.dbName = os.Getenv("DB_NAME")
|
dConf.dbName = os.Getenv("DB_NAME")
|
||||||
dConf.dbUserName = os.Getenv("DB_USERNAME")
|
dConf.dbUserName = os.Getenv("DB_USERNAME")
|
||||||
dConf.dbPassword = os.Getenv("DB_PASSWORD")
|
dConf.dbPassword = os.Getenv("DB_PASSWORD")
|
||||||
@@ -144,15 +149,26 @@ func getDatabase(database Database) *dbConfig {
|
|||||||
|
|
||||||
// Helper function to get environment variable or use a default value
|
// Helper function to get environment variable or use a default value
|
||||||
func getEnvOrDefault(currentValue, envKey, suffix, defaultValue string) string {
|
func getEnvOrDefault(currentValue, envKey, suffix, defaultValue string) string {
|
||||||
|
// Return the current value if it's already set
|
||||||
if currentValue != "" {
|
if currentValue != "" {
|
||||||
return currentValue
|
return currentValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for suffixed or prefixed environment variables if a suffix is provided
|
||||||
if suffix != "" {
|
if suffix != "" {
|
||||||
envSuffix := os.Getenv(fmt.Sprintf("%s_%s", envKey, strings.ToUpper(suffix)))
|
suffixUpper := strings.ToUpper(suffix)
|
||||||
|
envSuffix := os.Getenv(fmt.Sprintf("%s_%s", envKey, suffixUpper))
|
||||||
if envSuffix != "" {
|
if envSuffix != "" {
|
||||||
return envSuffix
|
return envSuffix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
envPrefix := os.Getenv(fmt.Sprintf("%s_%s", suffixUpper, envKey))
|
||||||
|
if envPrefix != "" {
|
||||||
|
return envPrefix
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fall back to the default value using a helper function
|
||||||
return utils.EnvWithDefault(envKey, defaultValue)
|
return utils.EnvWithDefault(envKey, defaultValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,11 +255,18 @@ 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
|
||||||
}
|
}
|
||||||
disableCompression, _ = cmd.Flags().GetBool("disable-compression")
|
disableCompression, _ = cmd.Flags().GetBool("disable-compression")
|
||||||
|
customName, _ := cmd.Flags().GetString("custom-name")
|
||||||
|
all, _ := cmd.Flags().GetBool("all-databases")
|
||||||
|
allInOne, _ := cmd.Flags().GetBool("all-in-one")
|
||||||
|
if allInOne {
|
||||||
|
all = true
|
||||||
|
}
|
||||||
_, _ = cmd.Flags().GetString("mode")
|
_, _ = cmd.Flags().GetString("mode")
|
||||||
passphrase := os.Getenv("GPG_PASSPHRASE")
|
passphrase := os.Getenv("GPG_PASSPHRASE")
|
||||||
_ = utils.GetEnv(cmd, "path", "AWS_S3_PATH")
|
_ = utils.GetEnv(cmd, "path", "AWS_S3_PATH")
|
||||||
@@ -257,6 +280,10 @@ 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
|
||||||
@@ -269,6 +296,9 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
|
|||||||
config.publicKey = publicKeyFile
|
config.publicKey = publicKeyFile
|
||||||
config.usingKey = usingKey
|
config.usingKey = usingKey
|
||||||
config.cronExpression = cronExpression
|
config.cronExpression = cronExpression
|
||||||
|
config.all = all
|
||||||
|
config.allInOne = allInOne
|
||||||
|
config.customName = customName
|
||||||
return &config
|
return &config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ package pkg
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
goutils "github.com/jkaninda/go-utils"
|
||||||
"github.com/jkaninda/mysql-bkup/utils"
|
"github.com/jkaninda/mysql-bkup/utils"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"os"
|
"os"
|
||||||
@@ -36,7 +38,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func intro() {
|
func intro() {
|
||||||
fmt.Println("Starting MySQL Backup...")
|
fmt.Println("Starting MYSQL-BKUP...")
|
||||||
fmt.Printf("Version: %s\n", utils.Version)
|
fmt.Printf("Version: %s\n", utils.Version)
|
||||||
fmt.Println("Copyright (c) 2024 Jonas Kaninda")
|
fmt.Println("Copyright (c) 2024 Jonas Kaninda")
|
||||||
}
|
}
|
||||||
@@ -65,27 +67,30 @@ func deleteTemp() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestDatabaseConnection tests the database connection
|
// TestDatabaseConnection tests the database connection
|
||||||
func testDatabaseConnection(db *dbConfig) {
|
func testDatabaseConnection(db *dbConfig) error {
|
||||||
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
// Create the mysql client config file
|
||||||
if err != nil {
|
if err := createMysqlClientConfigFile(*db); err != nil {
|
||||||
return
|
return errors.New(err.Error())
|
||||||
}
|
}
|
||||||
utils.Info("Connecting to %s database ...", db.dbName)
|
utils.Info("Connecting to %s database ...", db.dbName)
|
||||||
// Set database name for notification error
|
// Set database name for notification error
|
||||||
utils.DatabaseName = db.dbName
|
utils.DatabaseName = db.dbName
|
||||||
cmd := exec.Command("mariadb", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName, "-e", "quit")
|
|
||||||
|
// Prepare the command to test the database connection
|
||||||
|
cmd := exec.Command("mariadb", fmt.Sprintf("--defaults-file=%s", mysqlClientConfig), db.dbName, "-e", "quit")
|
||||||
// Capture the output
|
// Capture the output
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
cmd.Stdout = &out
|
cmd.Stdout = &out
|
||||||
cmd.Stderr = &out
|
cmd.Stderr = &out
|
||||||
err = cmd.Run()
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal("Error testing database connection: %v\nOutput: %s", err, out.String())
|
|
||||||
|
|
||||||
|
// Run the command
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return fmt.Errorf("failed to connect to database %s: %v, output: %s", db.dbName, err, out.String())
|
||||||
}
|
}
|
||||||
utils.Info("Successfully connected to %s database", db.dbName)
|
|
||||||
|
|
||||||
|
utils.Info("Successfully connected to %s database", db.dbName)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkPubKeyFile checks gpg public key
|
// checkPubKeyFile checks gpg public key
|
||||||
@@ -183,3 +188,16 @@ func RemoveLastExtension(filename string) string {
|
|||||||
}
|
}
|
||||||
return filename
|
return filename
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create mysql client config file
|
||||||
|
func createMysqlClientConfigFile(db dbConfig) error {
|
||||||
|
caCertPath := goutils.GetStringEnvWithDefault("DB_SSL_CA", "/etc/ssl/certs/ca-certificates.crt")
|
||||||
|
sslMode := goutils.GetStringEnvWithDefault("DB_SSL_MODE", "0")
|
||||||
|
// Create the mysql client config file
|
||||||
|
mysqlClientConfigFile := filepath.Join(tmpPath, "my.cnf")
|
||||||
|
mysqlCl := fmt.Sprintf("[client]\nhost=%s\nport=%s\nuser=%s\npassword=%s\nssl-ca=%s\nssl=%s\n", db.dbHost, db.dbPort, db.dbUserName, db.dbPassword, caCertPath, sslMode)
|
||||||
|
if err := os.WriteFile(mysqlClientConfigFile, []byte(mysqlCl), 0644); err != nil {
|
||||||
|
return fmt.Errorf("failed to create mysql client config file: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -51,7 +51,10 @@ func StartMigration(cmd *cobra.Command) {
|
|||||||
conf := &RestoreConfig{}
|
conf := &RestoreConfig{}
|
||||||
conf.file = backupFileName
|
conf.file = backupFileName
|
||||||
// Backup source Database
|
// Backup source Database
|
||||||
BackupDatabase(dbConf, backupFileName, true)
|
err := BackupDatabase(dbConf, backupFileName, true, false, false)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatal("Error backing up database: %s", err)
|
||||||
|
}
|
||||||
// Restore source database into target database
|
// Restore source database into target database
|
||||||
utils.Info("Restoring [%s] database into [%s] database...", dbConf.dbName, targetDbConf.targetDbName)
|
utils.Info("Restoring [%s] database into [%s] database...", dbConf.dbName, targetDbConf.targetDbName)
|
||||||
RestoreDatabase(&newDbConfig, conf)
|
RestoreDatabase(&newDbConfig, conf)
|
||||||
|
|||||||
@@ -39,7 +39,11 @@ import (
|
|||||||
func sshBackup(db *dbConfig, config *BackupConfig) {
|
func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||||
utils.Info("Backup database to Remote server")
|
utils.Info("Backup database to Remote server")
|
||||||
// Backup database
|
// Backup database
|
||||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
|
||||||
|
if err != nil {
|
||||||
|
recoverMode(err, "Error backing up database")
|
||||||
|
return
|
||||||
|
}
|
||||||
finalFileName := config.backupFileName
|
finalFileName := config.backupFileName
|
||||||
if config.encryption {
|
if config.encryption {
|
||||||
encryptBackup(config)
|
encryptBackup(config)
|
||||||
@@ -104,7 +108,7 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
|
|||||||
})
|
})
|
||||||
// Delete temp
|
// Delete temp
|
||||||
deleteTemp()
|
deleteTemp()
|
||||||
utils.Info("Backup successfully completed in %s", duration)
|
utils.Info("The backup of the %s database has been completed in %s", db.dbName, duration)
|
||||||
|
|
||||||
}
|
}
|
||||||
func remoteRestore(db *dbConfig, conf *RestoreConfig) {
|
func remoteRestore(db *dbConfig, conf *RestoreConfig) {
|
||||||
@@ -156,7 +160,11 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
|
|||||||
utils.Info("Backup database to the remote FTP server")
|
utils.Info("Backup database to the remote FTP server")
|
||||||
|
|
||||||
// Backup database
|
// Backup database
|
||||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
|
||||||
|
if err != nil {
|
||||||
|
recoverMode(err, "Error backing up database")
|
||||||
|
return
|
||||||
|
}
|
||||||
finalFileName := config.backupFileName
|
finalFileName := config.backupFileName
|
||||||
if config.encryption {
|
if config.encryption {
|
||||||
encryptBackup(config)
|
encryptBackup(config)
|
||||||
@@ -216,5 +224,5 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
|
|||||||
})
|
})
|
||||||
// Delete temp
|
// Delete temp
|
||||||
deleteTemp()
|
deleteTemp()
|
||||||
utils.Info("Backup successfully completed in %s", duration)
|
utils.Info("The backup of the %s database has been completed in %s", db.dbName, duration)
|
||||||
}
|
}
|
||||||
|
|||||||
140
pkg/restore.go
140
pkg/restore.go
@@ -25,6 +25,7 @@ SOFTWARE.
|
|||||||
package pkg
|
package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/jkaninda/encryptor"
|
"github.com/jkaninda/encryptor"
|
||||||
"github.com/jkaninda/go-storage/pkg/local"
|
"github.com/jkaninda/go-storage/pkg/local"
|
||||||
"github.com/jkaninda/mysql-bkup/utils"
|
"github.com/jkaninda/mysql-bkup/utils"
|
||||||
@@ -56,11 +57,17 @@ func StartRestore(cmd *cobra.Command) {
|
|||||||
}
|
}
|
||||||
func localRestore(dbConf *dbConfig, restoreConf *RestoreConfig) {
|
func localRestore(dbConf *dbConfig, restoreConf *RestoreConfig) {
|
||||||
utils.Info("Restore database from local")
|
utils.Info("Restore database from local")
|
||||||
|
basePath := filepath.Dir(restoreConf.file)
|
||||||
|
fileName := filepath.Base(restoreConf.file)
|
||||||
|
restoreConf.file = fileName
|
||||||
|
if basePath == "" || basePath == "." {
|
||||||
|
basePath = storagePath
|
||||||
|
}
|
||||||
localStorage := local.NewStorage(local.Config{
|
localStorage := local.NewStorage(local.Config{
|
||||||
RemotePath: storagePath,
|
RemotePath: basePath,
|
||||||
LocalPath: tmpPath,
|
LocalPath: tmpPath,
|
||||||
})
|
})
|
||||||
err := localStorage.CopyFrom(restoreConf.file)
|
err := localStorage.CopyFrom(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error copying backup file: %s", err)
|
utils.Fatal("Error copying backup file: %s", err)
|
||||||
}
|
}
|
||||||
@@ -68,88 +75,79 @@ func localRestore(dbConf *dbConfig, restoreConf *RestoreConfig) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RestoreDatabase restore database
|
// RestoreDatabase restores the database from a backup file
|
||||||
func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
|
func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
|
||||||
if conf.file == "" {
|
if conf.file == "" {
|
||||||
utils.Fatal("Error, file required")
|
utils.Fatal("Error, file required")
|
||||||
}
|
}
|
||||||
extension := filepath.Ext(filepath.Join(tmpPath, conf.file))
|
|
||||||
rFile, err := os.ReadFile(filepath.Join(tmpPath, conf.file))
|
filePath := filepath.Join(tmpPath, conf.file)
|
||||||
outputFile := RemoveLastExtension(filepath.Join(tmpPath, conf.file))
|
rFile, err := os.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error reading backup file: %s ", err)
|
utils.Fatal("Error reading backup file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension := filepath.Ext(filePath)
|
||||||
|
outputFile := RemoveLastExtension(filePath)
|
||||||
|
|
||||||
if extension == ".gpg" {
|
if extension == ".gpg" {
|
||||||
|
decryptBackup(conf, rFile, outputFile)
|
||||||
if conf.usingKey {
|
|
||||||
utils.Info("Decrypting backup using private key...")
|
|
||||||
utils.Warn("Backup decryption using a private key is not fully supported")
|
|
||||||
prKey, err := os.ReadFile(conf.privateKey)
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal("Error reading public key: %s ", err)
|
|
||||||
}
|
|
||||||
err = encryptor.DecryptWithPrivateKey(rFile, outputFile, prKey, conf.passphrase)
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal("error during decrypting backup %v", err)
|
|
||||||
}
|
|
||||||
utils.Info("Decrypting backup using private key...done")
|
|
||||||
} else {
|
|
||||||
if conf.passphrase == "" {
|
|
||||||
utils.Error("Error, passphrase or private key required")
|
|
||||||
utils.Fatal("Your file seems to be a GPG file.\nYou need to provide GPG keys. GPG_PASSPHRASE or GPG_PRIVATE_KEY environment variable is required.")
|
|
||||||
} else {
|
|
||||||
utils.Info("Decrypting backup using passphrase...")
|
|
||||||
// decryptWithGPG file
|
|
||||||
err := encryptor.Decrypt(rFile, outputFile, conf.passphrase)
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal("Error decrypting file %s %v", file, err)
|
|
||||||
}
|
|
||||||
utils.Info("Decrypting backup using passphrase...done")
|
|
||||||
// Update file name
|
|
||||||
conf.file = RemoveLastExtension(file)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if utils.FileExists(filepath.Join(tmpPath, conf.file)) {
|
restorationFile := filepath.Join(tmpPath, conf.file)
|
||||||
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
if !utils.FileExists(restorationFile) {
|
||||||
|
utils.Fatal("File not found: %s", restorationFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := testDatabaseConnection(db); err != nil {
|
||||||
|
utils.Fatal("Error connecting to the database: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.Info("Restoring database...")
|
||||||
|
restoreDatabaseFile(db, restorationFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decryptBackup(conf *RestoreConfig, rFile []byte, outputFile string) {
|
||||||
|
if conf.usingKey {
|
||||||
|
utils.Info("Decrypting backup using private key...")
|
||||||
|
prKey, err := os.ReadFile(conf.privateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
utils.Fatal("Error reading private key: %v", err)
|
||||||
}
|
}
|
||||||
testDatabaseConnection(db)
|
if err := encryptor.DecryptWithPrivateKey(rFile, outputFile, prKey, conf.passphrase); err != nil {
|
||||||
utils.Info("Restoring database...")
|
utils.Fatal("Error decrypting backup: %v", err)
|
||||||
|
|
||||||
extension := filepath.Ext(filepath.Join(tmpPath, conf.file))
|
|
||||||
// Restore from compressed file / .sql.gz
|
|
||||||
if extension == ".gz" {
|
|
||||||
str := "zcat " + filepath.Join(tmpPath, conf.file) + " | mariadb -h " + db.dbHost + " -P " + db.dbPort + " -u " + db.dbUserName + " " + db.dbName
|
|
||||||
_, err := exec.Command("sh", "-c", str).Output()
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal("Error, in restoring the database %v", err)
|
|
||||||
}
|
|
||||||
utils.Info("Restoring database... done")
|
|
||||||
utils.Info("Database has been restored")
|
|
||||||
// Delete temp
|
|
||||||
deleteTemp()
|
|
||||||
|
|
||||||
} else if extension == ".sql" {
|
|
||||||
// Restore from sql file
|
|
||||||
str := "cat " + filepath.Join(tmpPath, conf.file) + " | mariadb -h " + db.dbHost + " -P " + db.dbPort + " -u " + db.dbUserName + " " + db.dbName
|
|
||||||
_, err := exec.Command("sh", "-c", str).Output()
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal("Error in restoring the database %v", err)
|
|
||||||
}
|
|
||||||
utils.Info("Restoring database... done")
|
|
||||||
utils.Info("Database has been restored")
|
|
||||||
// Delete temp
|
|
||||||
deleteTemp()
|
|
||||||
} else {
|
|
||||||
utils.Fatal("Unknown file extension %s", extension)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
utils.Fatal("File not found in %s", filepath.Join(tmpPath, conf.file))
|
if conf.passphrase == "" {
|
||||||
|
utils.Fatal("Passphrase or private key required for GPG file.")
|
||||||
|
}
|
||||||
|
utils.Info("Decrypting backup using passphrase...")
|
||||||
|
if err := encryptor.Decrypt(rFile, outputFile, conf.passphrase); err != nil {
|
||||||
|
utils.Fatal("Error decrypting file: %v", err)
|
||||||
|
}
|
||||||
|
conf.file = RemoveLastExtension(conf.file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func restoreDatabaseFile(db *dbConfig, restorationFile string) {
|
||||||
|
extension := filepath.Ext(restorationFile)
|
||||||
|
var cmdStr string
|
||||||
|
|
||||||
|
switch extension {
|
||||||
|
case ".gz":
|
||||||
|
cmdStr = fmt.Sprintf("zcat %s | mariadb --defaults-file=%s %s", restorationFile, mysqlClientConfig, db.dbName)
|
||||||
|
case ".sql":
|
||||||
|
cmdStr = fmt.Sprintf("cat %s | mariadb --defaults-file=%s %s", restorationFile, mysqlClientConfig, db.dbName)
|
||||||
|
default:
|
||||||
|
utils.Fatal("Unknown file extension: %s", extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command("sh", "-c", cmdStr)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatal("Error restoring database: %v\nOutput: %s", err, string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.Info("Database has been restored successfully.")
|
||||||
|
deleteTemp()
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,7 +39,11 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
|
|||||||
|
|
||||||
utils.Info("Backup database to s3 storage")
|
utils.Info("Backup database to s3 storage")
|
||||||
// Backup database
|
// Backup database
|
||||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
|
||||||
|
if err != nil {
|
||||||
|
recoverMode(err, "Error backing up database")
|
||||||
|
return
|
||||||
|
}
|
||||||
finalFileName := config.backupFileName
|
finalFileName := config.backupFileName
|
||||||
if config.encryption {
|
if config.encryption {
|
||||||
encryptBackup(config)
|
encryptBackup(config)
|
||||||
@@ -103,7 +107,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
|
|||||||
})
|
})
|
||||||
// Delete temp
|
// Delete temp
|
||||||
deleteTemp()
|
deleteTemp()
|
||||||
utils.Info("Backup successfully completed in %s", duration)
|
utils.Info("The backup of the %s database has been completed in %s", db.dbName, duration)
|
||||||
|
|
||||||
}
|
}
|
||||||
func s3Restore(db *dbConfig, conf *RestoreConfig) {
|
func s3Restore(db *dbConfig, conf *RestoreConfig) {
|
||||||
|
|||||||
15
pkg/var.go
15
pkg/var.go
@@ -24,7 +24,10 @@ SOFTWARE.
|
|||||||
|
|
||||||
package pkg
|
package pkg
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
const tmpPath = "/tmp/backup"
|
const tmpPath = "/tmp/backup"
|
||||||
const gpgHome = "/config/gnupg"
|
const gpgHome = "/config/gnupg"
|
||||||
@@ -42,6 +45,8 @@ var (
|
|||||||
usingKey = false
|
usingKey = false
|
||||||
backupSize int64 = 0
|
backupSize int64 = 0
|
||||||
startTime = time.Now()
|
startTime = time.Now()
|
||||||
|
backupRescueMode = false
|
||||||
|
mysqlClientConfig = filepath.Join(tmpPath, "my.cnf")
|
||||||
)
|
)
|
||||||
|
|
||||||
// dbHVars Required environment variables for database
|
// dbHVars Required environment variables for database
|
||||||
@@ -49,7 +54,6 @@ var dbHVars = []string{
|
|||||||
"DB_HOST",
|
"DB_HOST",
|
||||||
"DB_PASSWORD",
|
"DB_PASSWORD",
|
||||||
"DB_USERNAME",
|
"DB_USERNAME",
|
||||||
"DB_NAME",
|
|
||||||
}
|
}
|
||||||
var tdbRVars = []string{
|
var tdbRVars = []string{
|
||||||
"TARGET_DB_HOST",
|
"TARGET_DB_HOST",
|
||||||
@@ -61,13 +65,6 @@ var tdbRVars = []string{
|
|||||||
var dbConf *dbConfig
|
var dbConf *dbConfig
|
||||||
var targetDbConf *targetDbConfig
|
var targetDbConf *targetDbConfig
|
||||||
|
|
||||||
// sshVars Required environment variables for SSH remote server storage
|
|
||||||
var sshVars = []string{
|
|
||||||
"SSH_USER",
|
|
||||||
"SSH_HOST_NAME",
|
|
||||||
"SSH_PORT",
|
|
||||||
"REMOTE_PATH",
|
|
||||||
}
|
|
||||||
var ftpVars = []string{
|
var ftpVars = []string{
|
||||||
"FTP_HOST_NAME",
|
"FTP_HOST_NAME",
|
||||||
"FTP_USER",
|
"FTP_USER",
|
||||||
|
|||||||
@@ -60,10 +60,10 @@
|
|||||||
|
|
||||||
<p>We recommend investigating the issue as soon as possible to prevent potential data loss or service disruptions.</p>
|
<p>We recommend investigating the issue as soon as possible to prevent potential data loss or service disruptions.</p>
|
||||||
|
|
||||||
<p>For more information, visit the <a href="https://jkaninda.github.io/pg-bkup">pg-bkup documentation</a>.</p>
|
<p>For more information, visit the <a href="https://jkaninda.github.io/mysql-bkup">mysql-bkup documentation</a>.</p>
|
||||||
|
|
||||||
<footer>
|
<footer>
|
||||||
© 2024 <a href="https://github.com/jkaninda/pg-bkup">pg-bkup</a> | Automated Backup System
|
© 2024 <a href="https://github.com/jkaninda/mysql-bkup">mysql-bkup</a> | Automated Backup System
|
||||||
</footer>
|
</footer>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user