Files
db-backup/scripts/ofelia-config.sh
Lucilio Correia 60b0b467f8
Some checks failed
Caprover: Build & Deploy (backup-databases) / build (push) Failing after 50s
fix: do recursive creating of backup folders
2024-11-08 08:53:27 -03:00

69 lines
2.1 KiB
Bash
Executable File

#! /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 -p $(dirname $output_path);
touch ${output_path};
fi
done
if test -z "${output_path}"; then
output_path=/dev/stdout;
fi
echo "${BACKUP_DATABASES}" | while read entry; 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 || exit 1;
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;