Integret scheduled mode for periodic backups

This commit is contained in:
2023-12-24 11:52:11 +01:00
parent 902be39db4
commit 0e65331675
4 changed files with 148 additions and 5 deletions

View File

@@ -33,6 +33,8 @@ MySQL Backup tool, backup database to S3 or Object Storage
| --path | | Set s3 path without file name. eg: /custom_path |
| --dbname | -d | Set database name |
| --port | -p | Set database port (default: 3306) |
| --mode | -m | Set timeout execution mode. default or scheduled (default: default) |
| --period | | Set crontab period for scheduled mode only. (default: "*/30 * * * *") |
| --timeout | -t | Set timeout (default: 60s) |
| --help | -h | Print this help message and exit |
| --version | -V | Print version information and exit |
@@ -156,6 +158,8 @@ Simple S3 backup usage
bkup --operation backup --storage s3 --dbname mydatabase
```
```yaml
version: '3'
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
@@ -179,10 +183,64 @@ bkup --operation backup --storage s3 --dbname 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 "*/30 * * * *" flag.
Make an automated backup on Docker
## Syntax of crontab (field description)
The syntax is:
- 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])
Easy to remember format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
## Example of scheduling 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:latest bkup --operation backup --dbname $DB_NAME --mode scheduled --period "*/30 * * * *"
```
> With Docker compose
```yaml
version: '3'
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command:
- /bin/sh
- -c
- bkup --operation backup --dbname database_name --mode scheduled --period "*/30 * * * *"
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mariadb
- DB_USERNAME=mariadb
- DB_PASSWORD=password
```
## Manual
> backup_script.sh
```sh
@@ -207,6 +265,7 @@ Your crontab looks like this:
```
## Kubernetes CronJob
For Kubernetes you don't need to run it in scheduled mode.
Simple Kubernetes CronJob usage:

View File

@@ -16,7 +16,8 @@ ENV VERSION="1.0"
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 +29,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,5 +1,4 @@
#!/usr/bin/env bash
set -e
TIME=$(date +%Y%m%d_%H%M%S)
MY_SQL_DUMP=/usr/bin/mysqldump
@@ -10,6 +9,8 @@ export STORAGE=local
export STORAGE_PATH=/backup
export S3_PATH=/mysql-bkup
export TIMEOUT=60
export EXECUTION_MODE="default"
export SCHEDULE_PERIOD="*/30 * * * *"
export FILE_COMPRESION=true
usage_info()
{
@@ -92,6 +93,16 @@ flags()
[ $# = 0 ] && error "No database name specified"
export DB_PORT="$1"
shift;;
(-m|--mode)
shift
[ $# = 0 ] && error "No Scheduled mode specified"
export EXECUTION_MODE="$1"
shift;;
(--period)
shift
[ $# = 0 ] && error "No Scheduled period entered"
export SCHEDULE_PERIOD="$1"
shift;;
(-t|--timeout)
shift
[ $# = 0 ] && error "No timeout specified"
@@ -177,8 +188,58 @@ 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/backup_cron.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 "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' ]
@@ -198,4 +259,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