Files
boring_backup/src/lib/handlers/sources/mysqldump.sh

113 lines
3.2 KiB
Bash

#!/usr/bin/env bash
# mysqldump.sh - create SQL dump of MariaDB/MySQL database.
# Copyright (c) 2022 ge <https://nixhacks.net/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
handler::mysqldump() {
# Create SQL-dump at location __main_target_path using mysqldump(1)
# Compression is optional.
# Handle 'mysql' URI scheme.
#
# Usage: handler::mysqldump URI
log "Run handler handler::mysqldump()"
local uri
local dst_path
local compr_util
local compr_cmd
local file_ext
uri="$1"
# shellcheck disable=SC2154
dst_path="$__main_target_path"
parse_uri "$uri"
[ -z "$port" ] && port=3306 # Set default MySQL port.
# shellcheck disable=SC2154
[ -n "$__verbose" ] && echo "Dumping database '${path##*/}'"\
"owned by ${username}@${hostname} ..." | log -p
# Select compression utility and set filename extension.
if [ -n "$compression" ]; then
set_compression "$compression"
file_ext=".sql${file_ext}"
else
compr_util=
file_ext='.sql'
fi
dump_name="${dst_path}/$(gen_backup_name "$file_ext")"
[ -n "$__verbose" ] && echo "Dump: $dump_name"
log "Dump: $dump_name"
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
try mysqldump $mysqldump_opts \
--host="$hostname" \
--port="$port" \
"${path##*/}" \
--user="$username" \
--password="$password" |
$compr_cmd > "$dump_name"
if [ ! -s "$dump_name" ]; then
err -e "Something went wrong: Dump size is 0 bytes. Removing $dump_name"
rm "$dump_name"
fi
}
# POSTGRESQL
#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
#}