fix: Improve code; Fix error handling
This commit is contained in:
		@@ -16,98 +16,64 @@
 | 
			
		||||
# 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)
 | 
			
		||||
src_mysqldump() {
 | 
			
		||||
    # Create SQL-dump at location __main_target_path via mysqldump(1)
 | 
			
		||||
    # 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 dst_path
 | 
			
		||||
    local compr_util
 | 
			
		||||
    local compr_cmd
 | 
			
		||||
    local file_ext
 | 
			
		||||
 | 
			
		||||
    uri="$1"
 | 
			
		||||
    # shellcheck disable=SC2154
 | 
			
		||||
    dst_path="$__main_target_path"
 | 
			
		||||
    uri="$1"
 | 
			
		||||
 | 
			
		||||
    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.
 | 
			
		||||
 | 
			
		||||
    # shellcheck disable=SC2154
 | 
			
		||||
    [ -n "$__verbose" ] && echo "Dumping database '${path##*/}'"\
 | 
			
		||||
        "owned by ${username}@${hostname} ..." | log -p
 | 
			
		||||
    log "Dumping database '${path##*/}' owned by ${username}@${hostname} ..."
 | 
			
		||||
 | 
			
		||||
    # 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
 | 
			
		||||
    sqldump="${dst_path}/$(gen_backup_name ".sql")" # Set dump name
 | 
			
		||||
    mysqldump_options="${mysqldump_options:-}"
 | 
			
		||||
 | 
			
		||||
    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"
 | 
			
		||||
    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!
 | 
			
		||||
    # Set command to execute
 | 
			
		||||
    # NOTE! `mysqldump_options` variable must be unquoted!
 | 
			
		||||
    # shellcheck disable=SC2086,SC2154
 | 
			
		||||
    try mysqldump $mysqldump_opts \
 | 
			
		||||
    set --  mysqldump $mysqldump_options \
 | 
			
		||||
        --host="$hostname" \
 | 
			
		||||
        --port="$port" \
 | 
			
		||||
        "${path##*/}" \
 | 
			
		||||
        --user="$username" \
 | 
			
		||||
        --password="$password" |
 | 
			
		||||
        $compr_cmd > "$dump_name"
 | 
			
		||||
        "${path##*/}"
 | 
			
		||||
 | 
			
		||||
    if [ ! -s "$dump_name" ]; then
 | 
			
		||||
        err -e "Something went wrong:" \
 | 
			
		||||
            "Dump size is 0 bytes. Removing $dump_name"
 | 
			
		||||
        rm "$dump_name"
 | 
			
		||||
    log "Run command: $*"
 | 
			
		||||
 | 
			
		||||
    # shellcheck disable=SC2154
 | 
			
		||||
    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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# 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
 | 
			
		||||
#}
 | 
			
		||||
    unset MYSQL_PWD
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user