Skip to content
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN \

RUN \
apt-get -y update && \
apt-get -y install mydumper && \
apt-get -y install mydumper git sudo && \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why git?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced the hooks to be able to create a simple script that commits the changes after every backup automatically. So it was required to have git available inside the docker.

rm -rf /var/lib/apt/lists/*

#
Expand Down
49 changes: 43 additions & 6 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
set -e

#
# Retreive and check mode, which can either be "BACKUP" or "RESTORE".
# Retreive and check mode, which can either be "BACKUP", "COMPRESSED_BACKUP" or "RESTORE".
# Based on the mode, different default options will be set.
#

MODE=${MODE:-BACKUP}

case "${MODE^^}" in
'BACKUP')
OPTIONS=${OPTIONS:-}
;;
'COMPRESSED_BACKUP')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just overwriting OPTIONS?

OPTIONS=${OPTIONS:--c}
;;
'RESTORE')
Expand Down Expand Up @@ -47,7 +50,7 @@ echo
# Display the container informations on standard out.
#

CONTAINER=$(export | sed -nr "/ENV_MYSQL_DATABASE/{s/^.+ -x (.+)_ENV.+/\1/p;q}")
CONTAINER=$(export | sed -nr "/ENV_MYSQL_ROOT_PASSWORD/{s/^.+ -x (.+)_ENV.+/\1/p;q}")

if [[ -z "${CONTAINER}" ]]
then
Expand Down Expand Up @@ -86,7 +89,23 @@ umask ${UMASK}
# Building common CLI options to use for mydumper and myloader.
#

CLI_OPTIONS="-v 3 -h ${!DB_ADDR} -P ${!DB_PORT} -u root -p ${!DB_PASS} -B ${!DB_NAME} ${OPTIONS}"
CLI_OPTIONS="-v 3 -h ${!DB_ADDR} -P ${!DB_PORT} -u root -p ${!DB_PASS} ${OPTIONS}"

if [ -z "${!DB_NAME}" ]; then
echo "No DB_NAME available, backup all DBs"
else
CLI_OPTIONS="-B ${!DB_NAME} ${CLI_OPTIONS}"
fi

#
# Call before hooks
#
if [ -d "/hooks" ] && ls /hooks/*.before 1> /dev/null 2>&1; then
for hookfile in /hooks/*.before; do
eval $hookfile
echo "Called hook $hookfile"
done
fi

#
# When MODE is set to "BACKUP", then mydumper has to be used to backup the database.
Expand All @@ -96,7 +115,7 @@ echo "${MODE^^}"
echo "======="
echo

if [[ "${MODE^^}" == "BACKUP" ]]
if [[ "${MODE^^}" == "BACKUP" ]] || [[ "${MODE^^}" == "COMPRESSED_BACKUP" ]]
then

printf "===> Creating base directory... "
Expand All @@ -112,7 +131,7 @@ then
echo "DONE"

echo "===> Starting backup..."
exec su -pc "mydumper ${CLI_OPTIONS}" ${USER}
sudo -u ${USER} mydumper ${CLI_OPTIONS}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the container properly "stoppable" (i.e. docker stop container vs docker kill container) with just sudo?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I haven't tested this yet. Changing from exec to sudo was required to be able to call the hooks afterwards, because exec will not continue with the execution of this script.


#
# When MODE is set to "RESTORE", then myloader has to be used to restore the database.
Expand Down Expand Up @@ -140,6 +159,24 @@ then
fi

echo "===> Restoring database from ${RESTORE_DIR}..."
exec su -pc "myloader --directory=${RESTORE_DIR} ${CLI_OPTIONS}" ${USER}
sudo -u ${USER} myloader --directory=${RESTORE_DIR} ${CLI_OPTIONS}

fi

echo "===> Backup finished"

#
# Call after hooks
#
if [ -d "/hooks" ] && ls /hooks/*.after 1> /dev/null 2>&1; then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWESOME IDEA! 👍

for hookfile in /hooks/*.after; do
echo "===> Calling hook ${hookfile}... "
eval $hookfile
echo "===> Calling hook ${hookfile}... DONE"
done

echo "===> All hooks processed, finished."
else
echo "===> No hooks found, finished."
fi