fix: missing mariadb scripts

This commit is contained in:
2024-10-22 14:54:11 -03:00
parent 7319a6fcb7
commit aaceb570af
2 changed files with 102 additions and 0 deletions

94
scripts/backup-mariadb.sh Normal file
View File

@@ -0,0 +1,94 @@
#! /bin/sh
#
# MariaDB Backup
# Do backup of one or all databases of a given MariaDB server
#
#
# List Databases
#
# only argument is a database url
DB_URL="$1";
# load database values as environment variables while checking URL validity
#
# will populate variables
# - $DB_USERNAME
# - $DB_PASSWORD
# - $DB_HOST
# - $DB_PORT
# - $DB_NAME
# - any query var as a prefixed variable $db_arg_${name}=${value}
dburl-parser.sh ${DB_URL} > /tmp/ofelia.dotenv;
source /tmp/ofelia.dotenv;
rm /tmp/ofelia.dotenv;
# set backup destination
if test -z "${BACKUP_DATABASES_PATH}"; then
echo "failed initializing backup -- \$BACKUP_DATABASES_PATH is empty";
exit 1;
fi
## set subdirectory
backup_path="/$(
echo "/${BACKUP_DATABASES_PATH}/${db_arg_directory:-\/}/"\
| sed 's/\/\{1,\}/\//g'\
| sed 's/^\/\(.*\)\/$/\1/'
)/"
## directory path
mkdir -p ${backup_path};
## file name
backup_prefix="${db_arg_prefix:-backup-}";
backup_extension="${db_arg_file_extension:-SQL}";
# set databases list
if test "${DB_NAME}" = '*'; then
## build a list with every database name if * is informed
databases=$(mysql\
--user=${DB_USERNAME}\
--password=${DB_PASSWORD}\
--host="${DB_HOST}"\
--execute="SHOW DATABASES;"\
--vertical\
--column-names=false\
| grep -ve ^\*\
| xargs
);
else
## use the database informed or force using --all-databases param
databases="${DB_NAME:-::ALL_DATABASES::}";
fi
# proccess each database
for database_item in ${databases}; do
if test -z "${database_item}"; then
## skip empty lines
continue;
elif test "${database_item}" = "::ALL_DATABASES::"; then
# backup everything in a single file if no database was set
database="--all-databases";
filename_database="all-databases";
else
# backup each database name to its own file
database="--databases ${database_item}";
filename_database="${database_item}";
fi
cat | xargs mariadb-dump << HEREDOC
$(test -n ${DB_USERNAME} && echo "--user=${DB_USERNAME}")
$(test -n ${DB_PASSWORD} && echo "--password=${DB_PASSWORD}")
$(test -n ${DB_HOST} && echo "--host=${DB_HOST}")
$(test -n ${DB_PORT} && echo "--port=${DB_PORT}")
--result-file=$(
printf '%s/%s%s-%s_%s.%s'\
${backup_path}\
${backup_prefix}\
${DB_HOST}\
${filename_database}\
$(date +%s)\
${backup_extension}
)
${database}
HEREDOC
done