diff --git a/README.md b/README.md index bc45391..0bec7a4 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/docker/Dockerfile b/src/docker/Dockerfile index 4bcf75a..60217f5 100644 --- a/src/docker/Dockerfile +++ b/src/docker/Dockerfile @@ -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 diff --git a/src/mysql_bkup.sh b/src/mysql_bkup.sh index e9ad9c7..587e34a 100755 --- a/src/mysql_bkup.sh +++ b/src/mysql_bkup.sh @@ -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" < "$TASK" < "$CRON_JOB" <> /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 \ No newline at end of file + fi +elif [ $EXECUTION_MODE == 'scheduled' ] +then + scheduled_mode +else +echo "Error, unknow execution mode!" +exit 1 +fi \ No newline at end of file diff --git a/src/supervisord.conf b/src/supervisord.conf new file mode 100644 index 0000000..84b35a1 --- /dev/null +++ b/src/supervisord.conf @@ -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 \ No newline at end of file