mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 21:49:40 +01:00
Initial commit
This commit is contained in:
38
.github/workflows/build.yml
vendored
Normal file
38
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
name: Build
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
docker_tag:
|
||||
description: 'Docker tag'
|
||||
required: true
|
||||
default: 'latest'
|
||||
type: string
|
||||
env:
|
||||
BUILDKIT_IMAGE: jkaninda/mysql-bkup
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
-
|
||||
name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v3
|
||||
-
|
||||
name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
-
|
||||
name: Login to DockerHub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
-
|
||||
name: Build and push
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
push: true
|
||||
platforms: linux/amd64,linux/arm64
|
||||
tags: |
|
||||
"${{env.BUILDKIT_IMAGE}}:latest"
|
||||
"${{env.BUILDKIT_IMAGE}}:1.0"
|
||||
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/.history
|
||||
backup
|
||||
data
|
||||
compose.yaml
|
||||
107
README.md
Normal file
107
README.md
Normal file
@@ -0,0 +1,107 @@
|
||||
# MySQL Backup
|
||||
MySQL Backup docker container image
|
||||
|
||||
[](https://github.com/jkaninda/mysql-bkup/actions/workflows/build.yml)
|
||||

|
||||

|
||||
|
||||
- [Docker Hub](https://hub.docker.com/r/jkaninda/mysql-bkup)
|
||||
- [Github](https://github.com/jkaninda/mysql-bkup)
|
||||
|
||||
## Storage:
|
||||
- local
|
||||
- s3
|
||||
|
||||
## Backup database :
|
||||
```yaml
|
||||
version: '3'
|
||||
services:
|
||||
mariadb:
|
||||
container_name: mariadb
|
||||
image: mariadb:latest
|
||||
environment:
|
||||
MYSQL_DATABASE: mariadb
|
||||
MYSQL_USER: mariadb
|
||||
MYSQL_PASSWORD: password
|
||||
MYSQL_ROOT_PASSWORD: password
|
||||
mysql-bkup:
|
||||
image: jkaninda/mysql-bkup:latest
|
||||
container_name: mysql-bkup
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- backup
|
||||
volumes:
|
||||
- ./backup:/backup
|
||||
environment:
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mariadb
|
||||
- DB_DATABASE=mariadb
|
||||
- DB_USERNAME=mariadb
|
||||
- DB_PASSWORD=password
|
||||
```
|
||||
## Restore database :
|
||||
```yaml
|
||||
version: '3'
|
||||
services:
|
||||
mariadb:
|
||||
container_name: mariadb
|
||||
image: mariadb:latest
|
||||
environment:
|
||||
MYSQL_DATABASE: mariadb
|
||||
MYSQL_USER: mariadb
|
||||
MYSQL_PASSWORD: password
|
||||
MYSQL_ROOT_PASSWORD: password
|
||||
mysql-bkup:
|
||||
image: jkaninda/mysql-bkup:latest
|
||||
container_name: mysql-bkup
|
||||
command: ["restore"]
|
||||
volumes:
|
||||
- ./backup:/backup
|
||||
environment:
|
||||
- FILE_NAME=mariadb_20231217_040238.sql
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mariadb
|
||||
- DB_DATABASE=mariadb
|
||||
- DB_USERNAME=mariadb
|
||||
- DB_PASSWORD=password
|
||||
```
|
||||
## Run
|
||||
```sh
|
||||
docker-compose up -d
|
||||
```
|
||||
## Run on Kubernetes
|
||||
|
||||
```yaml
|
||||
apiVersion: batch/v1
|
||||
kind: CronJob
|
||||
metadata:
|
||||
name: mysql-bkup-job
|
||||
spec:
|
||||
schedule: "0 0 * * *"
|
||||
jobTemplate:
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
backoffLimit: 4
|
||||
containers:
|
||||
- name: mysql-bkup
|
||||
image: jkaninda/mysql-bkup:latest
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- backup;
|
||||
env:
|
||||
- name: DB_PORT
|
||||
value: "3306"
|
||||
- name: DB_HOST
|
||||
value: "mysql-svc"
|
||||
- name: DB_DATABASE
|
||||
value: "mariadb"
|
||||
- name: DB_USERNAME
|
||||
value: "mariadb"
|
||||
# Please use secret!
|
||||
- name: DB_PASSWORD
|
||||
value: "password"
|
||||
restartPolicy: Never
|
||||
```
|
||||
11
build.sh
Executable file
11
build.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
if [ $# -eq 0 ]
|
||||
then
|
||||
tag='latest'
|
||||
else
|
||||
tag=$1
|
||||
fi
|
||||
|
||||
docker build -f src/docker/Dockerfile -t jkaninda/mysql-bkup:$tag .
|
||||
|
||||
docker-compose up -d
|
||||
30
compose-compose.yaml
Normal file
30
compose-compose.yaml
Normal file
@@ -0,0 +1,30 @@
|
||||
version: '3'
|
||||
services:
|
||||
mariadb:
|
||||
container_name: mariadb
|
||||
image: mariadb:latest
|
||||
environment:
|
||||
MYSQL_DATABASE: mariadb
|
||||
MYSQL_USER: mariadb
|
||||
MYSQL_PASSWORD: password
|
||||
MYSQL_ROOT_PASSWORD: password
|
||||
networks:
|
||||
- web
|
||||
mysql-bkup:
|
||||
image: jkaninda/mysql-bkup:latest
|
||||
container_name: mysql-bkup
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- backup
|
||||
volumes:
|
||||
- ./backup:/backup
|
||||
environment:
|
||||
- FILE_NAME=napata_20231217_051339.sql
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mysql
|
||||
- DB_DATABASE=bkup
|
||||
- DB_USERNAME=jonas
|
||||
- DB_PASSWORD=password
|
||||
networks:
|
||||
- web
|
||||
31
k8s-job.yaml
Normal file
31
k8s-job.yaml
Normal file
@@ -0,0 +1,31 @@
|
||||
apiVersion: batch/v1
|
||||
kind: CronJob
|
||||
metadata:
|
||||
name: mysql-bkup-job
|
||||
spec:
|
||||
schedule: "0 0 * * *"
|
||||
jobTemplate:
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
backoffLimit: 4
|
||||
containers:
|
||||
- name: mysql-bkup
|
||||
image: jkaninda/mysql-bkup:latest
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- backup;
|
||||
env:
|
||||
- name: DB_PORT
|
||||
value: "3306"
|
||||
- name: DB_HOST
|
||||
value: "mysql-svc"
|
||||
- name: DB_DATABASE
|
||||
value: "mariadb"
|
||||
- name: DB_USERNAME
|
||||
value: "mariadb"
|
||||
# Please use secret!
|
||||
- name: DB_PASSWORD
|
||||
value: "password"
|
||||
restartPolicy: Never
|
||||
13
src/backup.sh
Normal file
13
src/backup.sh
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
TIME=$(date +%Y%m%d_%H%M%S)
|
||||
MY_SQL_DUMP=/usr/bin/mysqldump
|
||||
#OPTION=${OPTION}
|
||||
set -e
|
||||
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
|
||||
echo "Please make sure all environment variables are set "
|
||||
else
|
||||
## Backup database
|
||||
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} > /backup/${DB_DATABASE}_${TIME}.sql
|
||||
echo "Database has been saved"
|
||||
fi
|
||||
exit
|
||||
31
src/docker/Dockerfile
Normal file
31
src/docker/Dockerfile
Normal file
@@ -0,0 +1,31 @@
|
||||
FROM ubuntu:22.04
|
||||
ENV DB_HOST=""
|
||||
ENV DB_DATABASE=""
|
||||
ENV DB_USERNAME=""
|
||||
ENV DB_PASSWORD=""
|
||||
ENV DB_PORT="3306"
|
||||
ENV STORAGE=local
|
||||
ENV BUCKETNAME=""
|
||||
ENV ACCESS_KEY=""
|
||||
ENV SECRET_KEY=""
|
||||
ENV S3_ENDPOINT=https://s3.amazonaws.com
|
||||
ARG DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
RUN apt-get update -qq
|
||||
RUN apt-get install build-essential libcurl4-openssl-dev libxml2-dev mime-support -y
|
||||
RUN apt install s3fs mysql-client -y
|
||||
RUN mkdir /s3mnt
|
||||
RUN mkdir /tmp/s3cache
|
||||
RUN chmod 777 /s3mnt
|
||||
RUN chmod 777 /tmp/s3cache
|
||||
|
||||
COPY src/backup.sh /usr/local/bin/
|
||||
COPY src/restore.sh /usr/local/bin/
|
||||
RUN chmod +x /usr/local/bin/backup.sh
|
||||
RUN chmod +x /usr/local/bin/restore.sh
|
||||
|
||||
RUN ln -s /usr/local/bin/backup.sh /usr/local/bin/backup
|
||||
RUN ln -s /usr/local/bin/restore.sh /usr/local/bin/restore
|
||||
|
||||
RUN mkdir /backup
|
||||
WORKDIR /backup
|
||||
29
src/entrypoint.sh
Normal file
29
src/entrypoint.sh
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/bin/sh
|
||||
TIME=$(date +%Y%m%d_%H%M%S)
|
||||
MY_SQL_DUMP=/usr/bin/mysqldump
|
||||
set -e
|
||||
|
||||
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
|
||||
echo "Please make sure all environment variables are set "
|
||||
else
|
||||
if [ $OPTION != 'backup' ]
|
||||
then
|
||||
## Restore databas
|
||||
echo "Restoring database..."
|
||||
if [ -f "/backup/$FILE_NAME" ]; then
|
||||
cat /backup/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE}
|
||||
echo "Database has been restored"
|
||||
|
||||
else
|
||||
echo "Error, file not found in /backup folder"
|
||||
fi
|
||||
else
|
||||
## Backup database
|
||||
echo "Start backup database..."
|
||||
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} > /backup/${DB_DATABASE}_${TIME}.sql
|
||||
echo "Database has been saved"
|
||||
|
||||
|
||||
fi
|
||||
fi
|
||||
bash
|
||||
16
src/restore.sh
Normal file
16
src/restore.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
TIME=$(date +%Y%m%d_%H%M%S)
|
||||
MY_SQL_DUMP=/usr/bin/mysqldump
|
||||
set -e
|
||||
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
|
||||
echo "Please make sure all environment variables are set "
|
||||
else
|
||||
## Restore database
|
||||
if [ -f "/backup/$FILE_NAME" ]; then
|
||||
cat /backup/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE}
|
||||
echo "Database has been restored"
|
||||
else
|
||||
echo "Error, file not found in /backup folder"
|
||||
fi
|
||||
fi
|
||||
exit
|
||||
Reference in New Issue
Block a user