feat: Add src_pg_dump, add extra.sh source
This commit is contained in:
		
							
								
								
									
										80
									
								
								src/lib/handlers/sources/pg_dump.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								src/lib/handlers/sources/pg_dump.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,80 @@
 | 
			
		||||
#!/usr/bin/env bash
 | 
			
		||||
 | 
			
		||||
# pg_dump.sh - create SQL dump of PostgreSQL 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/>.
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user