From 4832a3a75171570251aa80e685f0bb5775022b46 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 21 Aug 2022 04:45:09 +0300 Subject: [PATCH] feat: Add src_pg_dump, add extra.sh source --- src/lib/handlers/sources/pg_dump.sh | 80 +++++++++++++++++++++++++++++ src/lib/lib.sh | 5 ++ 2 files changed, 85 insertions(+) create mode 100644 src/lib/handlers/sources/pg_dump.sh diff --git a/src/lib/handlers/sources/pg_dump.sh b/src/lib/handlers/sources/pg_dump.sh new file mode 100644 index 0000000..22d6278 --- /dev/null +++ b/src/lib/handlers/sources/pg_dump.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +# pg_dump.sh - create SQL dump of PostgreSQL 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 . + +src_pg_dump() { + # Create SQL-dump at location __main_target_path via pg_dump(1) + # Compression is optional. + # Handle 'postgres' URI scheme. + # + # Usage: src_pg_dump URI + + log "Run handler ${FUNCNAME[0]}()" + + local uri + local dst_path + + # shellcheck disable=SC2154 + dst_path="$__main_target_path" + uri="$1" + + parse_uri "$uri" + + # shellcheck disable=SC2154 + if [[ ! "$scheme" =~ ^postgres$ ]]; then + log -p "Error: Wrong URI scheme: $scheme" >&2; exit 1 + fi + + [ -z "$port" ] && port=5432 # Set default PostgreSQL port. + + # shellcheck disable=SC2154 + log "Dumping database '${path##*/}' owned by ${username}@${hostname} ..." + + sqldump="${dst_path}/$(gen_backup_name ".psql")" # Set dump name + pg_dump_options="${pg_dump_options:-}" + + # See https://www.postgresql.org/docs/current/libpq-envars.html + # shellcheck disable=SC2154 + export PGPASSWORD="$password" + + # Set command to execute + # NOTE! `pg_dump_options` variable must be unquoted! + # shellcheck disable=SC2086,SC2154 + set -- pg_dump $pg_dump_options \ + --host="$hostname" \ + --port="$port" \ + --dbname="${path##*/}" \ + --username="$username" \ + --no-password + + log "Run command: $*" + + # shellcheck disable=SC2154 + if "$@" 2>> "$__log_file" > "$sqldump"; then + # Compress dump + sqldump="$(compress_file "$sqldump")" + # Append path to 'backups' array + backups+=("$sqldump") + log "Dump file: $sqldump" + else + remove_if_empty "$sqldump" + handle_error \ + "Error: Something went wrong when executing command:\n\t$*" + fi + + unset PGPASSWORD +} diff --git a/src/lib/lib.sh b/src/lib/lib.sh index f2291e1..e8e3ed2 100644 --- a/src/lib/lib.sh +++ b/src/lib/lib.sh @@ -25,4 +25,9 @@ LIBRARY="${LIBRARY:-./lib}" . "$LIBRARY/uri.sh" . "$LIBRARY/handlers/sources/tar.sh" . "$LIBRARY/handlers/sources/mysqldump.sh" +. "$LIBRARY/handlers/sources/pg_dump.sh" . "$LIBRARY/handlers/targets/cp.sh" + +if [ -f "$LIBRARY/extra.sh" ]; then + . "$LIBRARY/extra.sh" +fi