add: backup scripts and docker image

This commit is contained in:
2024-10-17 19:38:31 -03:00
parent 3a2d6f32f0
commit 604a514632
7 changed files with 200 additions and 0 deletions

69
scripts/ofelia-config.sh Executable file
View File

@@ -0,0 +1,69 @@
#! /bin/sh
#
# Do nothing if there is no URLs to proccess
#
if test -z "${BACKUP_DATABASES}"; then
echo >&2 "missing a list of db urls on \$BACKUP_DATABASES env var"\
&& exit 1;
fi
#
# Create output file if --saved option was set
#
while test $# -gt 0; do
arg_value="$1";
shift;
if test "${arg_value}" = "--save"; then
output_path=${OFELIA_CONFIG_PATH:-/etc/ofelia/config.ini};
mkdir $(dirname $output_path);
touch ${output_path};
fi
done
if test -z "${output_path}"; then
output_path=/dev/stdout;
fi
for entry in "${BACKUP_DATABASES}"; do
# normalize spaces
backup_data=$(echo ${entry} | xargs);
# skip if it is a empty line
if test -z "${backup_data}"; then
continue;
fi
# remove everything after first space to filter URL
DB_URL=${backup_data%% *};
# remove everything before the first space to get cron label
BACKUP_SCHEDULE=${backup_data#* };
# load url data as variables
dburl-parser.sh ${DB_URL} > /tmp/ofelia.dotenv;
source /tmp/ofelia.dotenv;
rm /tmp/ofelia.dotenv;
# fail if $DB_URL has no schema
if test -z "${DB_SCHEMA}"; then
1>&2 echo "invalid entry in \$BACKUP_DATABASES: ${entry}";
exit 7;
fi
# strip final : and convert schema to lowercase
schema="$(echo ${DB_SCHEMA%:} | tr '[:upper:]' '[:lower:]')";
# install schema related tools in advance
install-${schema}-tools.sh
# save backup command : should there be one script per database type
bkp_command="backup-${schema}.sh --url=${DB_URL}";
# BACKUP_SCHEDULE defaults to @daily
bkp_schedule="${BACKUP_SCHEDULE:-@daily}";
# set a title to ofelia job
bkp_title="backup-${DB_HOST}";
if test -n "${DB_NAME}"; then
bkp_title="${bkp_title}-${DB_NAME}";
fi
# add a entry to this backup in ofelia config file
cat >> ${output_path} << HEREDOC
# sending data to ${output_path}
[job-local "${bkp_title}"]
$(if test "${DEBUG}" = "on"; then echo "save-only-on-error = false"; fi)
no-overlap = true
command = ${bkp_command}
schedule = ${bkp_schedule}
HEREDOC
done;