fix: Improve code; Fix error handling

This commit is contained in:
ge 2022-08-21 04:51:26 +03:00
parent e5b244f358
commit 4b39560d67

View File

@ -16,98 +16,64 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
handler::mysqldump() { src_mysqldump() {
# Create SQL-dump at location __main_target_path using mysqldump(1) # Create SQL-dump at location __main_target_path via mysqldump(1)
# Compression is optional. # Compression is optional.
# Handle 'mysql' URI scheme. # Handle 'mysql' and 'mariadb' URI schemes.
# #
# Usage: handler::mysqldump URI # Usage: src_mysqldump URI
log "Run handler handler::mysqldump()" log "Run handler ${FUNCNAME[0]}()"
local uri local uri
local dst_path local dst_path
local compr_util
local compr_cmd
local file_ext
uri="$1"
# shellcheck disable=SC2154 # shellcheck disable=SC2154
dst_path="$__main_target_path" dst_path="$__main_target_path"
uri="$1"
parse_uri "$uri" parse_uri "$uri"
# shellcheck disable=SC2154
if [[ ! "$scheme" =~ ^(mysql|mariadb)$ ]]; then
log -p "Error: Wrong URI scheme: $scheme" >&2; exit 1
fi
[ -z "$port" ] && port=3306 # Set default MySQL port. [ -z "$port" ] && port=3306 # Set default MySQL port.
# shellcheck disable=SC2154 # shellcheck disable=SC2154
[ -n "$__verbose" ] && echo "Dumping database '${path##*/}'"\ log "Dumping database '${path##*/}' owned by ${username}@${hostname} ..."
"owned by ${username}@${hostname} ..." | log -p
# Select compression utility and set filename extension. sqldump="${dst_path}/$(gen_backup_name ".sql")" # Set dump name
if [ -n "$compression" ]; then mysqldump_options="${mysqldump_options:-}"
set_compression "$compression"
file_ext=".sql${file_ext}"
else
compr_util=
file_ext='.sql'
fi
dump_name="${dst_path}/$(gen_backup_name "$file_ext")" # See https://dev.mysql.com/doc/refman/8.0/en/environment-variables.html
# shellcheck disable=SC2154
export MYSQL_PWD="$password"
[ -n "$__verbose" ] && echo "Dump: $dump_name" # Set command to execute
log "Dump: $dump_name" # NOTE! `mysqldump_options` variable must be unquoted!
if [ -n "$compr_util" ]; then
compr_cmd="$compr_util -c" # e.g. gzip -c
else
compr_cmd="cat"
fi
mysqldump_opts="${mysqldump_opts:-}"
# NOTE! mysqldump_opts and compr_cmd variables must be unquoted!
# shellcheck disable=SC2086,SC2154 # shellcheck disable=SC2086,SC2154
try mysqldump $mysqldump_opts \ set -- mysqldump $mysqldump_options \
--host="$hostname" \ --host="$hostname" \
--port="$port" \ --port="$port" \
"${path##*/}" \
--user="$username" \ --user="$username" \
--password="$password" | "${path##*/}"
$compr_cmd > "$dump_name"
if [ ! -s "$dump_name" ]; then log "Run command: $*"
err -e "Something went wrong:" \
"Dump size is 0 bytes. Removing $dump_name" # shellcheck disable=SC2154
rm "$dump_name" if "$@" 2>> "$__log_file" > "$sqldump"; then
# Compress file
sqldump="$(compress_file "$sqldump")"
# Append path to 'backups' array
backups+=("$sqldump")
log "Dump saved as: $sqldump"
else
remove_if_empty "$sqldump"
handle_error \
"Error: Something went wrong when executing command:\n\t$*"
fi fi
}
# POSTGRESQL unset MYSQL_PWD
#bk_dump_postgresql() { }
# # Do PostgreSQL dump.
#
# bk_log "Dumping database '${path##*/}' owned by ${user}@${host} (PostgreSQL) ..."
#
# dump_name="$entry_local"/"$(bk_get_name "$db_name" .psql.gz)"
# [ "$port" ] || port=5432 # Set default PostgreSQL port.
# export PGPASSWORD="$password"
#
# pg_dump \
# --host="$host" \
# --port="$port" \
# --dbname="${path##*/}" \
# --username="$user" \
# --no-password | gzip -c > "$dump_name" |& bk_log
#
# unset PGPASSWORD
#
# if [ -s "$dump_name" ]; then
# bk_log "Dumping database '${path##*/}' owned by ${user}@${host} (PostgreSQL) [Done]"
# bk_log "Dump saved as: $dump_name"
# bk_upload_file "$dump_name"
#
# else
# rm "$dump_name"
# bk_err "Something went wrong. Dump size is 0 bytes. Removing $dump_name"
# fi
#}