Compare commits

...

38 Commits
v0.1 ... v0.3

Author SHA1 Message Date
4421ca1916 Merge pull request #17 from jkaninda/develop
Refactoring of code
2023-12-25 02:32:07 +01:00
aafe505dc3 Refactoring of code 2023-12-25 02:31:44 +01:00
7dc7c42c5c Merge pull request #16 from jkaninda/develop
Refactoring of code
2023-12-24 19:27:16 +01:00
509d9ad3d5 Refactoring of code 2023-12-24 19:25:54 +01:00
193d46bde6 Merge pull request #15 from jkaninda/develop
Change Default cronjob time
2023-12-24 15:01:01 +01:00
b81c6fc04c Change Default cronjob time 2023-12-24 14:59:05 +01:00
d1cbe8bbda Merge pull request #14 from jkaninda/develop
Update version
2023-12-24 14:44:45 +01:00
275fecd837 Update version 2023-12-24 14:44:15 +01:00
4e0740ac3b Merge pull request #13 from jkaninda/develop
Merge pull request #12 from jkaninda/scheduled-mode
2023-12-24 12:31:09 +01:00
4d24713d2d Merge pull request #12 from jkaninda/scheduled-mode
Scheduled mode
2023-12-24 12:30:53 +01:00
934f5db75f Merge pull request #11 from jkaninda/scheduled-mode
Update README
2023-12-24 12:30:24 +01:00
d50e7dbdad Update version 2023-12-24 12:28:54 +01:00
df899209ac Merge pull request #10 from jkaninda/scheduled-mode
Scheduled mode
2023-12-24 12:19:41 +01:00
5c559b93fd Update version 2023-12-24 12:19:12 +01:00
771d4b7d12 Update version 2023-12-24 12:07:20 +01:00
a72bf856a2 Update README.md 2023-12-24 12:03:35 +01:00
4e5f51582f Update README.md 2023-12-24 12:02:02 +01:00
65e4b9709c Update README.md 2023-12-24 12:01:31 +01:00
6d3f4a2e36 Update README.md 2023-12-24 12:00:50 +01:00
75cc9dc584 Update README.md 2023-12-24 11:59:00 +01:00
d7ba0e13a8 Update README.md 2023-12-24 11:54:23 +01:00
0e65331675 Integret scheduled mode for periodic backups 2023-12-24 11:52:11 +01:00
acbbc6a7a2 Merge pull request #8 from jkaninda/develop
Develop
2023-12-23 22:22:17 +01:00
902be39db4 Refactoring of code 2023-12-23 22:21:15 +01:00
a3d34a6600 Clean code 2023-12-23 22:06:56 +01:00
5fdc402497 Merge pull request #7 from jkaninda/develop
Develop
2023-12-22 10:46:48 +01:00
871d5b5b57 Update README 2023-12-22 10:46:13 +01:00
c4c6b58fa7 Update README 2023-12-22 10:45:45 +01:00
a0144f817f Update README 2023-12-22 10:44:38 +01:00
6372da013a Merge pull request #6 from jkaninda/develop
Update README
2023-12-22 10:29:41 +01:00
3572b24f18 Update README 2023-12-22 10:28:49 +01:00
8922ad1aaf Merge pull request #5 from jkaninda/develop
Update README
2023-12-22 10:27:04 +01:00
87c9104640 Update README 2023-12-22 10:26:40 +01:00
30755c4293 Merge pull request #4 from jkaninda/develop
Update README
2023-12-22 06:16:47 +01:00
cff384c6e9 Update README.md 2023-12-22 06:16:15 +01:00
d7aa63929b Refactoring 2023-12-22 06:07:47 +01:00
7dc86c6cde Update action 2023-12-22 06:06:10 +01:00
c8ffec2dc9 Refactoring 2023-12-22 06:04:35 +01:00
10 changed files with 261 additions and 117 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -36,4 +36,4 @@ jobs:
platforms: linux/amd64,linux/arm64
tags: |
"${{env.BUILDKIT_IMAGE}}:latest"
"${{env.BUILDKIT_IMAGE}}:v0.1"
"${{env.BUILDKIT_IMAGE}}:v0.3"

4
.gitignore vendored
View File

@@ -2,4 +2,6 @@
backup
data
compose.yaml
.env
.env
test.md
.DS_Store

180
README.md
View File

@@ -1,16 +1,24 @@
# MySQL Backup
MySQL Backup tool, backup database to S3 or Object Storage
- Docker
- Kubernetes
[![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)
> Runs on:
- Docker
- Kubernetes
> Links:
- [Docker Hub](https://hub.docker.com/r/jkaninda/mysql-bkup)
- [Github](https://github.com/jkaninda/mysql-bkup)
## PostgreSQL solution :
- [PostgreSQL](https://github.com/jkaninda/pg-bkup)
## Storage:
- local
- s3
@@ -24,8 +32,10 @@ MySQL Backup tool, backup database to S3 or Object Storage
| --storage | -s | Set storage. local or s3 (default: local) |
| --file | -f | Set file name for restoration |
| --path | | Set s3 path without file name. eg: /custom_path |
| --database | -db | Set database name |
| --dbname | -d | Set database name |
| --port | -p | Set database port (default: 3306) |
| --mode | -m | Set execution mode. default or scheduled (default: default) |
| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") |
| --timeout | -t | Set timeout (default: 60s) |
| --help | -h | Print this help message and exit |
| --version | -V | Print version information and exit |
@@ -35,20 +45,20 @@ MySQL Backup tool, backup database to S3 or Object Storage
Simple backup usage
```sh
bkup --operation backup
bkup --operation backup --dbname database_name
```
```sh
bkup -o backup
bkup -o backup -d database_name
```
### S3
```sh
bkup --operation backup --storage s3
bkup --operation backup --storage s3 --dbname database_name
```
## Docker run:
```sh
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup:latest bkup -o backup -db database_name
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup:latest bkup -o backup -d database_name
```
## Docker compose file:
@@ -57,25 +67,24 @@ version: '3'
services:
mariadb:
container_name: mariadb
image: mariadb:latest
image: mariadb
environment:
MYSQL_DATABASE: mariadb
MYSQL_USER: mariadb
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
mysql-bkup:
image: jkaninda/mysql-bkup:latest
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command:
- /bin/sh
- -c
- bkup --operation backup -db mariadb
- bkup --operation backup -d database_name
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mariadb
- DB_DATABASE=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
```
@@ -84,7 +93,7 @@ services:
Simple database restore operation usage
```sh
bkup --operation restore --file database_20231217_115621.sql
bkup --operation restore --dbname database_name --file database_20231217_115621.sql
```
```sh
@@ -99,7 +108,7 @@ bkup --operation restore --storage s3 --file database_20231217_115621.sql
## Docker run:
```sh
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup:latest bkup -o backup -db database_name -f napata_20231219_022941.sql.gz
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup bkup -o backup -d database_name -f mydb_20231219_022941.sql.gz
```
## Docker compose file:
@@ -116,19 +125,19 @@ services:
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
mysql-bkup:
image: jkaninda/mysql-bkup:latest
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command:
- /bin/sh
- -c
- bkup --operation restore --file database_20231217_115621.sql
- bkup --operation restore --file database_20231217_115621.sql --dbname database_name
volumes:
- ./backup:/backup
environment:
#- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command
- DB_PORT=3306
- DB_HOST=mariadb
- DB_DATABASE=mariadb
- DB_NAME=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
```
@@ -140,30 +149,32 @@ docker-compose up -d
## Backup to S3
```sh
docker run --rm --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=db_hostname" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" -e "ACCESS_KEY=your_access_key" -e "SECRET_KEY=your_secret_key" -e "BUCKETNAME=your_bucket_name" -e "S3_ENDPOINT=https://eu2.contabostorage.com" jkaninda/mysql-bkup:latest bkup -o backup -s s3 -db invoice
docker run --rm --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=db_hostname" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" -e "ACCESS_KEY=your_access_key" -e "SECRET_KEY=your_secret_key" -e "BUCKETNAME=your_bucket_name" -e "S3_ENDPOINT=https://eu2.contabostorage.com" jkaninda/mysql-bkup bkup -o backup -s s3 -d database_name
```
> To change s3 backup path add this flag : --path myPath . default path is /mysql_bkup
> To change s3 backup path add this flag : --path /myPath . default path is /mysql_bkup
Simple S3 backup usage
```sh
bkup --operation backup --storage s3 --database mydatabase
bkup --operation backup --storage s3 --dbname mydatabase
```
```yaml
version: '3'
services:
mysql-bkup:
image: jkaninda/mysql-bkup:latest
image: jkaninda/mysql-bkup
container_name: mysql-bkup
tty: true
privileged: true
devices:
- "/dev/fuse"
command:
- /bin/sh
- -c
- mysql_bkup --operation restore --source s3 -f database_20231217_115621.sql.gz
- mysql_bkup --operation restore --storage s3 -f database_20231217_115621.sql.gz
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_DATABASE=mariadb
- DB_NAME=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
- ACCESS_KEY=${ACCESS_KEY}
@@ -172,34 +183,92 @@ bkup --operation backup --storage s3 --database mydatabase
- S3_ENDPOINT=${S3_ENDPOINT}
```
## Run "docker run" from crontab
## Run in Scheduled mode
Make an automated backup (every night at 1).
This tool can be run as CronJob in Kubernetes for a regular backup which makes deployment on Kubernetes easy as Kubernetes has CronJob resources.
For Docker, you need to run it in scheduled mode by adding `--mode scheduled` flag and specify the periodical backup time by adding `--period "0 1 * * *"` flag.
> backup_script.sh
Make an automated backup on Docker
```sh
#!/bin/sh
DB_USERNAME='db_username'
DB_PASSWORD='password'
DB_HOST='db_hostname'
DB_NAME='db_name'
BACKUP_DIR='/some/path/backup/'
## Syntax of crontab (field description)
docker run --rm --name mysql-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/mysql-bkup:latest bkup -o backup -db $DB_NAME
```
The syntax is:
```sh
chmod +x backup_script.sh
```
- 1: Minute (0-59)
- 2: Hours (0-23)
- 3: Day (0-31)
- 4: Month (0-12 [12 == December])
- 5: Day of the week(0-7 [7 or 0 == sunday])
Your crontab looks like this:
Easy to remember format:
```conf
0 1 * * * /path/to/backup_script.sh
* * * * * command to be executed
```
```conf
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
```
> At every 30th minute
```conf
*/30 * * * *
```
> “At minute 0.” every hour
```conf
0 * * * *
```
> “At 01:00.” every day
```conf
0 1 * * *
```
## Example of scheduled mode
> Docker run :
```sh
docker run --rm --name mysql-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/mysql-bkup bkup --operation backup --dbname $DB_NAME --mode scheduled --period "0 1 * * *"
```
> With Docker compose
```yaml
version: "3"
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
privileged: true
devices:
- "/dev/fuse"
command:
- /bin/sh
- -c
- bkup --operation backup --storage s3 --path /mys3_custome_path --dbname database_name --mode scheduled --period "*/30 * * * *"
environment:
- DB_PORT=3306
- DB_HOST=mysqlhost
- DB_USERNAME=userName
- DB_PASSWORD=${DB_PASSWORD}
- ACCESS_KEY=${ACCESS_KEY}
- SECRET_KEY=${SECRET_KEY}
- BUCKETNAME=${BUCKETNAME}
- S3_ENDPOINT=${S3_ENDPOINT}
```
## Kubernetes CronJob
For Kubernetes you don't need to run it in scheduled mode.
Simple Kubernetes CronJob usage:
@@ -209,30 +278,39 @@ kind: CronJob
metadata:
name: mysql-bkup-job
spec:
schedule: "0 0 * * *"
schedule: "0 1 * * *"
jobTemplate:
spec:
template:
spec:
backoffLimit: 4
containers:
- name: mysql-bkup
image: jkaninda/mysql-bkup:latest
image: jkaninda/mysql-bkup
securityContext:
privileged: true
command:
- /bin/sh
- -c
- bkup --operation backup
- bkup -o backup -s s3 --path /custom_path
env:
- name: DB_PORT
value: "3306"
value: "3306"
- name: DB_HOST
value: "mysql-svc"
- name: DB_DATABASE
value: "mariadb"
value: ""
- name: DB_NAME
value: ""
- name: DB_USERNAME
value: "mariadb"
# Please use secret instead!
value: ""
# Please use secret!
- name: DB_PASSWORD
value: "password"
- name: ACCESS_KEY
value: ""
- name: SECRET_KEY
value: ""
- name: BUCKETNAME
value: ""
- name: S3_ENDPOINT
value: "https://s3.amazonaws.com"
restartPolicy: Never
```

View File

@@ -8,4 +8,4 @@ fi
docker build -f src/docker/Dockerfile -t jkaninda/mysql-bkup:$tag .
docker-compose up -d
docker compose up -d

View File

@@ -1,26 +0,0 @@
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
- bkup --operation backup
volumes:
- ./backup:/backup
environment:
#- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command
- DB_PORT=3306
- DB_HOST=mariadb
- DB_DATABASE=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password

View File

@@ -1,31 +1,40 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: mysql-bkup-job
name: db-bkup-job
spec:
schedule: "0 0 * * *"
schedule: "0 1 * * *"
jobTemplate:
spec:
template:
spec:
backoffLimit: 4
containers:
- name: mysql-bkup
image: jkaninda/mysql-bkup:latest
image: jkaninda/mysql-bkup
securityContext:
privileged: true
command:
- /bin/sh
- -c
- bkup --operation backup
- bkup -o backup -s s3 --path /custom_path
env:
- name: DB_PORT
value: "3306"
value: "3306"
- name: DB_HOST
value: "mysql-svc"
- name: DB_DATABASE
value: "mariadb"
value: ""
- name: DB_NAME
value: ""
- name: DB_USERNAME
value: "mariadb"
value: ""
# Please use secret!
- name: DB_PASSWORD
value: "password"
- name: ACCESS_KEY
value: ""
- name: SECRET_KEY
value: ""
- name: BUCKETNAME
value: ""
- name: S3_ENDPOINT
value: "https://s3.amazonaws.com"
restartPolicy: Never

View File

@@ -1,22 +1,21 @@
FROM ubuntu:24.04
ENV DB_HOST=""
ENV DB_DATABASE=""
ENV DB_NAME=""
ENV DB_USERNAME=""
ENV DB_PASSWORD=""
ENV DB_PORT="3306"
ENV DESTINATION=local
ENV STORAGE=local
ENV SOURCE=local
ENV BUCKETNAME=""
ENV ACCESS_KEY=""
ENV SECRET_KEY=""
ENV S3_ENDPOINT=https://s3.amazonaws.com
ARG DEBIAN_FRONTEND=noninteractive
ENV VERSION="1.0"
ENV VERSION="v0.3"
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 apt install s3fs mysql-client supervisor cron -y
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
@@ -28,6 +27,8 @@ RUN chmod 777 /tmp/s3cache
COPY src/mysql_bkup.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/mysql_bkup.sh
ADD src/supervisord.conf /etc/supervisor/supervisord.conf
RUN ln -s /usr/local/bin/mysql_bkup.sh /usr/local/bin/mysql_bkup
RUN ln -s /usr/local/bin/mysql_bkup.sh /usr/local/bin/bkup

View File

@@ -1,16 +1,16 @@
#!/bin/sh
#!/usr/bin/env bash
set -e
TIME=$(date +%Y%m%d_%H%M%S)
MY_SQL_DUMP=/usr/bin/mysqldump
arg0=$(basename "$0" .sh)
blnk=$(echo "$arg0" | sed 's/./ /g')
export OPERATION=backup
export DESTINATION=local
export STORAGE=local
export STORAGE_PATH=/backup
export SOURCE=local
export S3_PATH=/mysql-bkup
export TIMEOUT=60
export EXECUTION_MODE="default"
export SCHEDULE_PERIOD="0 1 * * *"
export FILE_COMPRESION=true
usage_info()
{
@@ -45,8 +45,10 @@ help()
echo " -s |--storage -- Set storage (default: local)"
echo " -f |--file -- Set file name "
echo " |--path -- Set s3 path, without file name"
echo " -db|--database -- Set database name "
echo " -d |--dbname -- Set database name "
echo " -p |--port -- Set database port (default: 3306)"
echo " -m |--mode -- Set execution mode (default: default)"
echo " |--period -- Set schedule period time (default: '0 1 * * *')"
echo " -t |--timeout -- Set timeout (default: 120s)"
echo " -h |--help -- Print this help message and exit"
echo " -V |--version -- Print version information and exit"
@@ -63,18 +65,14 @@ flags()
[ $# = 0 ] && error "No operation specified - restore or backup"
export OPERATION="$1"
shift;;
(-d|--destination)
(-d|--dbname)
shift
[ $# = 0 ] && error "No destination specified - local or s3 | default local"
export DESTINATION="$1"
export SOURCE="$1"
export STORAGE="$1"
[ $# = 0 ] && error "No database name specified"
export DB_NAME="$1"
shift;;
(-s|--storage)
shift
[ $# = 0 ] && error "No storage specified - local or s3 | default local"
export SOURCE="$1"
export DESTINATION="$1"
export STORAGE="$1"
shift;;
(-f|--file)
@@ -90,13 +88,23 @@ flags()
(-db|--database)
shift
[ $# = 0 ] && error "No database name specified"
export DB_DATABASE="$1"
export DB_NAME="$1"
shift;;
(-p|--port)
shift
[ $# = 0 ] && error "No database name specified"
export DB_PORT="$1"
shift;;
(-m|--mode)
shift
[ $# = 0 ] && error "No execution mode specified"
export EXECUTION_MODE="$1"
shift;;
(--period)
shift
[ $# = 0 ] && error "No schedule period entered"
export SCHEDULE_PERIOD="$1"
shift;;
(-t|--timeout)
shift
[ $# = 0 ] && error "No timeout specified"
@@ -115,14 +123,15 @@ flags()
backup()
{
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
echo "Please make sure all required options are set "
if [[ -z $DB_HOST ]] || [[ -z $DB_NAME ]] || [[ -z $DB_USERNAME ]] || [[ -z $DB_PASSWORD ]]; then
echo "Please make sure all required environment variables are set "
else
## Test database connection
mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} -e"quit"
mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} -e"quit"
## Backup database
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} | gzip > ${STORAGE_PATH}/${DB_DATABASE}_${TIME}.sql.gz
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} | gzip > ${STORAGE_PATH}/${DB_NAME}_${TIME}.sql.gz
echo "$TIME: ${DB_NAME}_${TIME}.sql.gz" | tee -a "${STORAGE_PATH}/history.txt"
echo "Database has been saved"
fi
exit 0
@@ -130,15 +139,15 @@ exit 0
restore()
{
if [ -z "${DB_HOST}" ] || [ -z "${DB_DATABASE}" ] || [ -z "${DB_USERNAME}" ] || [ -z "${DB_PASSWORD}" ]; then
echo "Please make sure all required options are set "
if [[ -z $DB_HOST ]] || [[ -z $DB_NAME ]] || [[ -z $DB_USERNAME ]] || [[ -z $DB_PASSWORD ]]; then
echo "Please make sure all required environment variables are set "
else
## Restore database
if [ -f "${STORAGE_PATH}/$FILE_NAME" ]; then
if gzip -t ${STORAGE_PATH}/$FILE_NAME; then
zcat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE}
zcat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME}
else
cat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE}
cat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME}
fi
echo "Database has been restored"
else
@@ -163,7 +172,7 @@ s3_restore()
mount_s3()
{
if [ -z "${ACCESS_KEY}"] || [ -z "${SECRET_KEY}"]; then
if [[ -z $ACCESS_KEY ]] || [[ -z $SECRET_KEY ]]; then
echo "Please make sure all environment variables are set "
echo "BUCKETNAME=$BUCKETNAME \nACCESS_KEY=$nACCESS_KEY \nSECRET_KEY=$SECRET_KEY"
else
@@ -181,8 +190,59 @@ else
export STORAGE_PATH=/s3mnt$S3_PATH
fi
}
create_crontab_script()
{
TASK=/usr/local/bin/backup_cron.sh
touch $TASK
if [ $STORAGE == 's3' ]
then
cat > "$TASK" <<EOF
#!/usr/bin/env bash
set -e
bkup --operation backup --dbname $DB_NAME --port $DB_PORT --storage s3 --path $S3_PATH
EOF
else
cat > "$TASK" <<EOF
#!/usr/bin/env bash
set -e
bkup --operation backup --dbname $DB_NAME --port $DB_PORT
EOF
fi
chmod +x /usr/local/bin/backup_cron.sh
ln -s /usr/local/bin/backup_cron.sh /usr/local/bin/backup_cron
## Create crontab job
CRON_JOB=/etc/cron.d/backup_cron
touch $CRON_JOB
cat > "$CRON_JOB" <<EOF
$SCHEDULE_PERIOD root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> /var/log/mysql-bkup.log"
EOF
chmod 0644 /etc/cron.d/*
crontab /etc/cron.d/backup_cron
}
scheduled_mode()
{
if [ $OPERATION == 'backup' ]
then
create_crontab_script
echo ""
echo "**********************************"
echo " Starting MySQL Bkup... "
echo "***********************************"
echo "Running in Scheduled mode"
echo "Log file in /var/log/mysql-bkup.log"
echo "Execution period $SCHEDULE_PERIOD"
supervisord -c /etc/supervisor/supervisord.conf
else
echo "Scheduled mode supports only backup operation"
exit 1
fi
}
flags "$@"
# ?
if [ $EXECUTION_MODE == 'default' ]
then
if [ $OPERATION != 'backup' ]
then
if [ $STORAGE != 's3' ]
@@ -202,4 +262,11 @@ flags "$@"
echo "Backup to s3 storage"
s3_backup
fi
fi
fi
elif [ $EXECUTION_MODE == 'scheduled' ]
then
scheduled_mode
else
echo "Error, unknow execution mode!"
exit 1
fi

13
src/supervisord.conf Normal file
View File

@@ -0,0 +1,13 @@
[supervisord]
nodaemon=true
user=root
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
[program:cron]
command = /bin/bash -c "declare -p | grep -Ev '^declare -[[:alpha:]]*r' > /run/supervisord.env && /usr/sbin/cron -f -L 15"
autostart=true
autorestart=true
user = root
stderr_logfile=/var/log/cron.err.log
stdout_logfile=/var/log/cron.out.log