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

Support secrets #36

Closed
wants to merge 5 commits into from
Closed
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
40 changes: 40 additions & 0 deletions src/backup
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@
set -e
set -o pipefail

backup_log() {
local type="$1"; shift
printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
}

backup_error() {
backup_log ERROR "$@" >&2
exit 1
}

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
backup_error "Both $var and $fileVar are set (but are exclusive)"
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

docker_setup_env() {
file_env 'MYSQL_ENV_MYSQL_HOST' '%'
file_env 'MYSQL_ENV_MYSQL_USER'
file_env 'MYSQL_ENV_MYSQL_DATABASE'
file_env 'MYSQL_ENV_MYSQL_PASSWORD'
}

docker_setup_env;

cleanup() {
echo "Cleanup backup older than $CLEANUP_OLDER_THAN days"
toberemove=$(find /backups/ -type f -not -name ".*" -mtime +$CLEANUP_OLDER_THAN | wc -l)
Expand Down
40 changes: 40 additions & 0 deletions src/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
#!/bin/bash

backup_log() {
local type="$1"; shift
printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
}

backup_error() {
backup_log ERROR "$@" >&2
exit 1
}

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
backup_error "Both $var and $fileVar are set (but are exclusive)"
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

docker_setup_env() {
file_env 'MYSQL_ENV_MYSQL_HOST' '%'
file_env 'MYSQL_ENV_MYSQL_USER'
file_env 'MYSQL_ENV_MYSQL_DATABASE'
file_env 'MYSQL_ENV_MYSQL_PASSWORD'
}

docker_setup_env;

if ! [ -f backup-cron ]
then
echo "Creating cron entry to start backup at: $BACKUP_TIME"
Expand Down
40 changes: 40 additions & 0 deletions src/restore
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
#!/bin/bash
set -e

backup_log() {
local type="$1"; shift
printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
}

backup_error() {
backup_log ERROR "$@" >&2
exit 1
}

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
backup_error "Both $var and $fileVar are set (but are exclusive)"
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

docker_setup_env() {
file_env 'MYSQL_ENV_MYSQL_HOST' '%'
file_env 'MYSQL_ENV_MYSQL_USER'
file_env 'MYSQL_ENV_MYSQL_DATABASE'
file_env 'MYSQL_ENV_MYSQL_PASSWORD'
}

docker_setup_env;

if ! [[ "$1" ]]
then
echo "Error: Backup name missing"
Expand Down