Skip to content

Commit

Permalink
Merge pull request #64 from Sybrand/mariadb
Browse files Browse the repository at this point in the history
MariaDB support
  • Loading branch information
WadeBarnes authored Dec 4, 2020
2 parents b3608f8 + 0ef0a81 commit 40100a7
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ labels:
- MongoDB
- PostgresSQL
- MSSQL - Currently MSSQL requires that the nfs db volume be shared with the database for backups to function correctly.
- MariaDB

# Backup Container Options
You can run the Backup Container for supported databases separately or in a mixed environment.
Expand Down Expand Up @@ -321,6 +322,9 @@ Plugin Examples:
- [backup.mssql.plugin](./docker/backup.mssql.plugin)
- MSSQL backup implementation.

- [backup.mariadb.plugin](./docker/backup.mariadb.plugin)
- MariaDB backup implementation. This plugin should also work with mysql, but is currently untested.

- [backup.null.plugin](./docker/backup.null.plugin)
- Sample/Template backup implementation that simply outputs log messages for the various operations.

Expand Down
41 changes: 41 additions & 0 deletions docker/Dockerfile_MariaDB
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from registry.fedoraproject.org/f31/mariadb

# Change timezone to PST for convenience
ENV TZ=PST8PDT

# Set the workdir to be root
WORKDIR /

# Load the backup scripts into the container (must be executable).
COPY backup.* /

COPY webhook-template.json /

# ========================================================================================================
# Install go-crond (from https://github.com/BCDevOps/go-crond)
# - Adds some additional logging enhancements on top of the upstream project;
# https://github.com/webdevops/go-crond
#
# CRON Jobs in OpenShift:
# - https://blog.danman.eu/cron-jobs-in-openshift/
# --------------------------------------------------------------------------------------------------------
ARG SOURCE_REPO=BCDevOps
ARG GOCROND_VERSION=0.6.3
ADD https://github.com/$SOURCE_REPO/go-crond/releases/download/$GOCROND_VERSION/go-crond-64-linux /usr/bin/go-crond

USER root

RUN chmod ug+x /usr/bin/go-crond
# ========================================================================================================

# ========================================================================================================
# Perform operations that require root privilages here ...
# --------------------------------------------------------------------------------------------------------
RUN echo $TZ > /etc/timezone
# ========================================================================================================

# Important - Reset to the base image's user account.
USER 26

# Set the default CMD.
CMD sh /backup.sh
13 changes: 13 additions & 0 deletions docker/backup.container.utils
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ function isMsSql(){
)
}

function isMariaDb(){
(
# If a seperate mysql plugin is added, this check may be insufficient to establish the container type.
if isInstalled "mysql"; then
return 0
else
return 1
fi
)
}

function getContainerType(){
(
local _containerType=${UNKNOWN_DB}
Expand All @@ -43,6 +54,8 @@ function getContainerType(){
_containerType=${MONGO_DB}
elif isMsSql; then
_containerType=${MSSQL_DB}
elif isMariaDb; then
_containerType=${MARIA_DB}
else
_containerType=${UNKNOWN_DB}
_rtnCd=1
Expand Down
218 changes: 218 additions & 0 deletions docker/backup.mariadb.plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#!/bin/bash
# =================================================================================================================
# MariaDB Backup and Restore Functions:
# - Dynamically loaded as a plug-in
# - Refer to existing plug-ins for implementation examples.
# -----------------------------------------------------------------------------------------------------------------
export serverDataDirectory="/var/lib/mysql/data"

function onBackupDatabase(){
(
local OPTIND
local unset flags
while getopts : FLAG; do
case $FLAG in
? ) flags+="-${OPTARG} ";;
esac
done
shift $((OPTIND-1))

_databaseSpec=${1}
_backupFile=${2}

_hostname=$(getHostname ${_databaseSpec})
_database=$(getDatabaseName ${_databaseSpec})
_port=$(getPort ${_databaseSpec})
_portArg=${_port:+"-P ${_port}"}
_username=$(getUsername ${_databaseSpec})
_password=$(getPassword ${_databaseSpec})
echoGreen "Backing up '${_hostname}${_port:+:${_port}}${_database:+/${_database}}' to '${_backupFile}' ..."

MYSQL_PWD=${_password} mysqldump -h "${_hostname}" -u "${_username}" ${_portArg} "${_database}" | gzip > ${_backupFile}
return ${?}
)
}

function onRestoreDatabase(){
(
local OPTIND
local unset flags
while getopts : FLAG; do
case $FLAG in
? ) flags+="-${OPTARG} ";;
esac
done
shift $((OPTIND-1))

_databaseSpec=${1}
_fileName=${2}
_adminPassword=${3}

_hostname=$(getHostname ${flags} ${_databaseSpec})
_database=$(getDatabaseName ${_databaseSpec})
_port=$(getPort ${flags} ${_databaseSpec})
_portArg=${_port:+"-P ${_port}"}
_username=$(getUsername ${_databaseSpec})
_password=$(getPassword ${_databaseSpec})
echo -e "Restoring '${_fileName}' to '${_hostname}${_port:+:${_port}}${_database:+/${_database}}' ...\n" >&2


# Restore
gunzip -c "${_fileName}" | MYSQL_PWD=${_password} mysql -h ${_hostname} -u ${_username} ${_portArg} ${_database}
_rtnCd=${PIPESTATUS[1]}

return ${_rtnCd}
)
}

function onStartServer(){
(
local OPTIND
local unset flags
while getopts : FLAG; do
case $FLAG in
? ) flags+="-${OPTARG} ";;
esac
done
shift $((OPTIND-1))

_databaseSpec=${1}

# Start a local MariaDB instance
MYSQL_USER=$(getUsername "${_databaseSpec}") \
MYSQL_PASSWORD=$(getPassword "${_databaseSpec}") \
MYSQL_DATABASE=$(getDatabaseName "${_databaseSpec}") \
MYSQL_ROOT_PASSWORD=$(getPassword "${_databaseSpec}") \
run-mysqld >/dev/null 2>&1 &
)
}

function onStopServer(){
(
local OPTIND
local unset flags
while getopts : FLAG; do
case $FLAG in
? ) flags+="-${OPTARG} ";;
esac
done
shift $((OPTIND-1))

mysqladmin --defaults-file=${MYSQL_DEFAULTS_FILE:-/etc/my.cnf} -u root --socket=/var/lib/mysql/mysql.sock flush-privileges shutdown
)
}

function onCleanup(){
(
if ! dirIsEmpty ${serverDataDirectory}; then
# Delete the database files and configuration
echo -e "Cleaning up ...\n" >&2
rm -rf ${serverDataDirectory}/*
else
echo -e "Already clean ...\n" >&2
fi
)
}

function onPingDbServer(){
(
local OPTIND
local unset flags
while getopts : FLAG; do
case $FLAG in
? ) flags+="-${OPTARG} ";;
esac
done
shift $((OPTIND-1))

_databaseSpec=${1}

_hostname=$(getHostname ${flags} ${_databaseSpec})
_database=$(getDatabaseName ${_databaseSpec})
_port=$(getPort ${flags} ${_databaseSpec})
_portArg=${_port:+"-P ${_port}"}
_username=$(getUsername ${_databaseSpec})
_password=$(getPassword ${_databaseSpec})

if MYSQL_PWD=${_password} mysql -h ${_hostname} -u ${_username} ${_portArg} ${_database} -e "SELECT 1;" >/dev/null 2>&1; then
return 0
else
return 1
fi
)
}

function onVerifyBackup(){
(
local OPTIND
local unset flags
while getopts : FLAG; do
case $FLAG in
? ) flags+="-${OPTARG} ";;
esac
done
shift $((OPTIND-1))

_databaseSpec=${1}

_hostname=$(getHostname -l ${_databaseSpec})
_database=$(getDatabaseName ${_databaseSpec})
_port=$(getPort -l ${_databaseSpec})
_portArg=${_port:+"-P ${_port}"}
_username=$(getUsername ${_databaseSpec})
_password=$(getPassword ${_databaseSpec})

debugMsg "backup.mariadb.plugin - onVerifyBackup"
tables=$(MYSQL_PWD=${_password} mysql -h "${_hostname}" -u "${_username}" ${_portArg} "${_database}" -e "SHOW TABLES;")
rtnCd=${?}

# Get the size of the restored database
if (( ${rtnCd} == 0 )); then
size=$(getDbSize -l "${_databaseSpec}")
rtnCd=${?}
fi

if (( ${rtnCd} == 0 )); then
numResults=$(echo "${tables}"| wc -l)
if [[ ! -z "${tables}" ]] && (( numResults >= 1 )); then
# All good
verificationLog="\nThe restored database contained ${numResults} tables, and is ${size} in size."
else
# Not so good
verificationLog="\nNo tables were found in the restored database."
rtnCd="3"
fi
fi

echo ${verificationLog}
return ${rtnCd}
)
}

function onGetDbSize(){
(
local OPTIND
local unset flags
while getopts : FLAG; do
case $FLAG in
? ) flags+="-${OPTARG} ";;
esac
done
shift $((OPTIND-1))

_databaseSpec=${1}

_hostname=$(getHostname ${flags} ${_databaseSpec})
_database=$(getDatabaseName ${_databaseSpec})
_port=$(getPort ${flags} ${_databaseSpec})
_portArg=${_port:+"-P ${_port}"}
_username=$(getUsername ${_databaseSpec})
_password=$(getPassword ${_databaseSpec})

MYSQL_PWD=${_password} mysql -h "${_hostname}" -u "${_username}" ${_portArg} "${_database}" -e "SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS \"size in mb\" FROM information_schema.tables WHERE table_schema=\"${_database}\" GROUP BY table_schema;"

echo ${size}
return ${rtnCd}
)
}
# =================================================================================================================
1 change: 1 addition & 0 deletions docker/backup.settings
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export UNKNOWN_DB="null"
export MONGO_DB="mongo"
export POSTGRE_DB="postgres"
export MSSQL_DB="mssql"
export MARIA_DB="mariadb"
export CONTAINER_TYPE="$(getContainerType)"

# Other:
Expand Down
Empty file modified openshift/backup-deploy.overrides.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion openshift/templates/backup/backup-cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ objects:
template: "${JOB_NAME}-job"
cronjob: "${JOB_NAME}"
spec:
backoffLimit: ${JOB_BACKOFF_LIMIT}
backoffLimit: ${{JOB_BACKOFF_LIMIT}}
template:
spec:
containers:
Expand Down
4 changes: 2 additions & 2 deletions openshift/templates/backup/backup-deploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
{
"name": "DATABASE_USER_KEY_NAME",
"displayName": "Database User Key Name",
"description": "The datbase user key name stoed in database deployment resources specified by DATABASE_DEPLOYMENT_NAME.",
"description": "The database user key name stored in database deployment resources specified by DATABASE_DEPLOYMENT_NAME.",
"required": true,
"value": "database-user"
},
Expand Down Expand Up @@ -533,7 +533,7 @@
{
"name": "VERIFICATION_VOLUME_MOUNT_PATH",
"displayName": "Verification Volume Mount Path",
"description": "The path on which to mount the verification volume. This is used by the database server to contain the database configuration and data files. For Mongo, please use /var/lib/mongodb/data . For MSSQL, please use /var/opt/mssql/data",
"description": "The path on which to mount the verification volume. This is used by the database server to contain the database configuration and data files. For Mongo, please use /var/lib/mongodb/data . For MSSQL, please use /var/opt/mssql/data. For MariaDB, please use /var/lib/mysql/data",
"required": true,
"value": "/var/lib/pgsql/data"
},
Expand Down

0 comments on commit 40100a7

Please sign in to comment.