Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat backup creation #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ENV SQL_SCRIPTS_DIR=${CONFIG_DIR}/sql
ENV MYSQL_CLIENT_CONFIG=/etc/mysql/my.cnf
ENV CUSTOM_CLIENT_CONFIG=${CONFIG_DIR}/my.cnf
ENV APP_SCRIPTS_DIR=${CONFIG_DIR}/scripts
ENV BACKUP_DIR=${CONFIG_DIR}/backups
ENV USER=mysql-user

# Update and Install myslq-client
Expand All @@ -25,6 +26,11 @@ RUN chmod +x *.sh \
&& chgrp ${USER} ${MYSQL_CLIENT_CONFIG} \
&& chmod g+rw ${MYSQL_CLIENT_CONFIG}

# Install minio client
RUN wget https://dl.min.io/client/mc/release/linux-amd64/mc \
&& chmod +x mc \
&& mv mc /usr/local/bin

USER ${USER}
WORKDIR ${SQL_SCRIPTS_DIR}

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ Available Environmental Variables at the moment will be listed below.
| `ACTION_DATABASE_NAME` | any | _N/A_ | Name of database to create or drop. |


**Backup variables**
| Variable Name | Options | Default | Description |
|------------------------|-----------------------|----------------|-------------------------------------------------------------------------------------|
| `BACKUP_ENABLED` | yes,no | no | Backups MySQL. |
| `BACKUP_NAME` | any (optional) | _N/A_ | Set backup file name. |
| `BACKUP_TYPE` | full,database,table | full | Specify backup targe type. |
| `BACKUP_DATABASE_NAME` | any | _N/A_ | Name of the DB to backup. Comma separate if mulyiple of them. e.g db1,db2... |
| `BACKUP_TABLE_NAME` | any | _N/A_ | Name of the table to backup. Comma separate if mulyiple of them. e.g tab1,tab2... |
| `BACKUP_TO_S3` | yes,no | no | Use S3 for backups. S3 variables must be set to this work. |


**S3 variables**
| Variable Name | Options | Default | Description |
|-------------------------|----------------------|-----------------------|-------------------------------------------------------------------------------|
| `S3_ENDPOINT` | any (FQDN like) | _N/A_ | S3 endpoint name in a FQDN format. |
| `S3_REGION` | any | us-east-2 | S3 region name. |
| `S3_BUCKET` | any | _N/A_ | S3 Bucket name. |
| `S3_FOLDER` | any (optional) | _N/A_ | S3 folder inside Bucket. |
| `S3_ACCESS_KEY` | any | _N/A_ | S3 Access Key. |
| `S3_SECRET_KEY` | any | _N/A_ | S3 Secret Key. |
| `S3_SSL` | yes,no | yes | Specifies if SSL should be used for the endpoint connection (recommended). |

**Others variables**
| Variable Name | Options | Default | Description |
|----------------------------|------------|---------------|---------------------------------|
Expand Down
64 changes: 64 additions & 0 deletions src/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash

# Select Backup type
if [ $BACKUP_TYPE ]; then
echo "Running backup type: $BACKUP_TYPE"
backup_signature = ${BACKUP_NAME:-$(date +%Y-%m-%d_%s)}
backup_name = 'backup_'


echo "Creating backup directory..."
mkdir -p $BACKUP_DIR

case ${BACKUP_TYPE} in
"full")
echo "Running full backup..."
echo "Creating backup file..."
backup_name=${BACKUP_NAME:-"mysql-bkf_${backup_signature}.sql.gz"}
mysqldump --defaults-extra-file=$MYSQL_CLIENT_CONFIG --all-databases > $BACKUP_DIR/$backup_name
echo "Backup created."
;;
"database")
echo "Running database backup..."
echo "Creating backup file..."
backup_name=${BACKUP_NAME:-"mysql-bkd_${backup_signature}.sql.gz"}
databases=$(echo $BACKUP_DATABASE_NAME | tr "," " ")
mysqldump --defaults-extra-file=$MYSQL_CLIENT_CONFIG --databases $databases > $BACKUP_DIR/$backup_name
echo "Backup created."
;;
"table")
echo "Running table backup..."
echo "Creating backup file..."
database=$(echo "$BACKUP_DATABASE_NAME" | cut -d',' -f1)
tables=$(echo $BACKUP_TABLE_NAME | tr "," " ")
backup_name=${BACKUP_NAME:-"mysql-bkt_${backup_signature}.sql.gz"}
mysqldump --defaults-extra-file=$MYSQL_CLIENT_CONFIG $database $tables > $BACKUP_DIR/$backup_name
echo "Backup created."
;;
*)
echo "Unknown Backup type: $BACKUP_TYPE"
exit 1
;;
esac
else
echo "No backup type selected. Skipping..."
exit 1
fi

# Select backup file system
if [ $BACKUP_TO_S3 == 'yes' ]; then
echo "Backing up to S3..."

if [ $S3_SSL != 'yes' ]; then
S3_SSL_FLAG="--insecure"
else
S3_SSL_FLAG=""
fi

alias_name='db_backup'

# Add S3 alias
mc alias set $alias_name "$S3_ENDPOINT" "$S3_ACCESS_KEY" "$S3_SECRET_KEY" --api "s3v4" $S3_SSL_FLAG

mc cp $BACKUP_DIR $alias_name/$S3_FOLDER
fi
5 changes: 5 additions & 0 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ if [ $ACTION_USER ]; then
esac
fi

if [ $BACKUP_ENABLED == 'yes' ]; then
echo "Running backup..."
$APP_SCRIPTS_DIR/backup.sh
fi

exit 0;