Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaninda committed Dec 17, 2023
0 parents commit d412d20
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.history
backup
data
compose.yaml
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# MySQL Backup
MySQL Backup docker container image

[![Build](https://github.com/jkaninda/mysql-bkup/actions/workflows/build.yml/badge.svg)](https://github.com/jkaninda/mysql-bkup/actions/workflows/build.yml)
![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/jkaninda/mysql-bkup?style=flat-square)
![Docker Pulls](https://img.shields.io/docker/pulls/jkaninda/mysql-bkup?style=flat-square)

- [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 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions compose-compose.yaml
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions k8s-job.yaml
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions src/backup.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions src/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions src/restore.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d412d20

Please sign in to comment.