add: allow backuping to a sub-directory
This commit is contained in:
@@ -13,19 +13,39 @@
|
||||
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
|
||||
## directory path
|
||||
BACKUP_DATABASES_PATH=${BACKUP_DATABASES_PATH:-/var/data/backup-databases};
|
||||
mkdir -p ${BACKUP_DATABASES_PATH};
|
||||
## file name
|
||||
BACKUP_DATABASES_PREFIX="backup-";
|
||||
BACKUP_DATABASES_SUFFIX="SQL";
|
||||
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}\
|
||||
@@ -37,16 +57,21 @@ if test "${DB_NAME}" = '*'; then
|
||||
| xargs
|
||||
);
|
||||
else
|
||||
databases="${DB_NAME:-:ALL_DATABASES:}";
|
||||
## 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
|
||||
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
|
||||
@@ -57,12 +82,12 @@ $(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_DATABASES_PATH}\
|
||||
${BACKUP_DATABASES_PREFIX}\
|
||||
${backup_path}\
|
||||
${backup_prefix}\
|
||||
${DB_HOST}\
|
||||
${filename_database}\
|
||||
$(date +%s)\
|
||||
${BACKUP_DATABASES_SUFFIX}
|
||||
${backup_extension}
|
||||
)
|
||||
${database}
|
||||
HEREDOC
|
||||
|
||||
Reference in New Issue
Block a user