From bdeb39581d07754176e3ac86e87ce66a58dd8201 Mon Sep 17 00:00:00 2001 From: ge Date: Wed, 22 Jun 2022 23:53:20 +0300 Subject: [PATCH] feat: Add mysqldump.sh, move handlers to targets and sources dirs. --- src/lib/handlers/sources/mysqldump.sh | 108 ++++++++++++++++++++++++++ src/lib/handlers/{ => sources}/tar.sh | 0 src/lib/handlers/{ => targets}/cp.sh | 0 src/lib/lib.sh | 5 +- 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/lib/handlers/sources/mysqldump.sh rename src/lib/handlers/{ => sources}/tar.sh (100%) rename src/lib/handlers/{ => targets}/cp.sh (100%) diff --git a/src/lib/handlers/sources/mysqldump.sh b/src/lib/handlers/sources/mysqldump.sh new file mode 100644 index 0000000..62eb183 --- /dev/null +++ b/src/lib/handlers/sources/mysqldump.sh @@ -0,0 +1,108 @@ +#!/usr/bin/env bash + +# mysqldump.sh - create SQL dump of MariaDB/MySQL database. +# Copyright (c) 2022 ge +# +# 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 . + +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" + dst_path="$__main_target_path" + + parse_uri "$uri" + + [ -z "$port" ] && port=3306 # Set default MySQL port. + + [ -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 + + # NOTE! mysqldump_opts and compr_cmd variables must be unquoted! + 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 vvv + +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 +} diff --git a/src/lib/handlers/tar.sh b/src/lib/handlers/sources/tar.sh similarity index 100% rename from src/lib/handlers/tar.sh rename to src/lib/handlers/sources/tar.sh diff --git a/src/lib/handlers/cp.sh b/src/lib/handlers/targets/cp.sh similarity index 100% rename from src/lib/handlers/cp.sh rename to src/lib/handlers/targets/cp.sh diff --git a/src/lib/lib.sh b/src/lib/lib.sh index b81419c..33772d1 100644 --- a/src/lib/lib.sh +++ b/src/lib/lib.sh @@ -22,5 +22,6 @@ LIBRARY="${LIBRARY:-./lib}" . "$LIBRARY/backup.sh" . "$LIBRARY/source.sh" . "$LIBRARY/uri.sh" -. "$LIBRARY/handlers/tar.sh" -. "$LIBRARY/handlers/cp.sh" +. "$LIBRARY/handlers/sources/tar.sh" +. "$LIBRARY/handlers/sources/mysqldump.sh" +. "$LIBRARY/handlers/targets/cp.sh"