From: Frank Brehm Date: Thu, 16 Nov 2017 13:24:34 +0000 (+0100) Subject: Performing underlying backup X-Git-Tag: 0.1.2~75 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=cd260a888807f1d74555bb306361cdedfd2b36b1;p=pixelpark%2Fadmin-tools.git Performing underlying backup --- diff --git a/bin/backup_pgsql.sh b/bin/backup_pgsql.sh index 468b1d2..f152c74 100755 --- a/bin/backup_pgsql.sh +++ b/bin/backup_pgsql.sh @@ -89,6 +89,8 @@ BACKUP_DIR="${BACKUP_ROOTDIR}/${YEAR}/${MONTH}/${DAY}" TMP_DIR= LOGFILE="/dev/null" +BYTES_TOTAL="0" + #------------------------------------------------------------------- detect_color() { @@ -199,6 +201,11 @@ get_options() { ;; -q|--quiet) QUIET="y" + RED="" + YELLOW="" + GREEN="" + BLUE="" + NORMAL="" shift ;; --nocolor) @@ -280,6 +287,7 @@ debug() { #------------------------------------------------------------------------------ info() { if [[ "${QUIET}" == "y" ]] ; then + echo -e " * [$(my_date)] [${BASENAME}:INFO] : $@" >> "${LOGFILE}" return 0 fi echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" | tee -a "${LOGFILE}" @@ -313,6 +321,15 @@ RM() { eval ${cmd} "$@" 2>&1 | tee -a "${LOGFILE}" } +#------------------------------------------------------------------------------ +MV() { + local cmd="mv" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd+=" --verbose" + fi + eval ${cmd} "$@" 2>&1 | tee -a "${LOGFILE}" +} + #------------------------------------------------------------------------------ RMDIR() { local cmd="rmdir" @@ -325,6 +342,7 @@ RMDIR() { #------------------------------------------------------------------------------ empty_line() { if [[ "${QUIET}" == "y" ]] ; then + echo >> "${LOGFILE}" return 0 fi echo 2>&1 | tee -a "${LOGFILE}" @@ -427,6 +445,79 @@ cleanup_old_backups() { } +#------------------------------------------------------------------------------ +backup_databases() { + + local db= + for db in "${DATABASES[@]}" ; do + backup_database "${db}" + done + + empty_line + local k_bytes=$(( ${BYTES_TOTAL} / 1024 )) + local m_bytes=$(( ${k_bytes} / 1024 )) + local msg=$( printf "Total compressed size: %10d Bytes => %7d KiB => %4d MiB" \ + "${BYTES_TOTAL}" "${k_bytes}" "${m_bytes}" ) + info "${msg}" +} + +#------------------------------------------------------------------------------ +backup_database() { + + local db="$1" + + empty_line + info "Backing up database '${GREEN}${db}${NORMAL}' ..." + + local output_sql="${db}-${TIMESTAMP}.sql" + local output_sql_compressed="${output_sql}.bz2" + local out_sql_tmp="${TMP_DIR}/${output_sql}" + local out_sql_tmp_compressed="${TMP_DIR}/${output_sql_compressed}" + local out_sql_tgt="${BACKUP_DIR}/${output_sql}" + local out_sql_tgt_compressed="${BACKUP_DIR}/${output_sql_compressed}" + + local verbose_option="" + if [[ "${VERBOSE}" == "y" ]] ; then + verbose_option="--verbose" + fi + + pg_dump ${verbose_option} --blobs --clean \ + --create --if-exists --serializable-deferrable \ + "${db}" 2>&1 >"${out_sql_tmp}" | tee -a "${LOGFILE}" + + local blocks=$(stat -c "%b" "${out_sql_tmp}") + local bs=$(stat -c "%B" "${out_sql_tmp}") + local bytes=$(stat -c "%s" "${out_sql_tmp}") + local b_bytes=$(( ${blocks} * ${bs} )) + local k_bytes=$(( ${b_bytes} / 1024 )) + local m_bytes=$(( ${k_bytes} / 1024 )) + local msg=$( printf "Original size of %-50s %10d Bytes => %7d KiB => %4d MiB" \ + "'${output_sql}':" "${bytes}" "${k_bytes}" "${m_bytes}" ) + info "${msg}" + + debug "Compressing '${out_sql_tmp}' ..." + bzip2 ${verbose_option} --best "${out_sql_tmp}" 2>&1 | tee -a "${LOGFILE}" + + blocks=$(stat -c "%b" "${out_sql_tmp_compressed}") + bs=$(stat -c "%B" "${out_sql_tmp_compressed}") + bytes=$(stat -c "%s" "${out_sql_tmp_compressed}") + b_bytes=$(( ${blocks} * ${bs} )) + k_bytes=$(( ${b_bytes} / 1024 )) + m_bytes=$(( ${k_bytes} / 1024 )) + + BYTES_TOTAL=$(( ${BYTES_TOTAL} + ${b_bytes} )) + + local msg=$( printf "Compressed size of %-50s %10d Bytes => %7d KiB => %4d MiB" \ + "'${output_sql}':" "${bytes}" "${k_bytes}" "${m_bytes}" ) + info "${msg}" + + debug "Moving '${out_sql_tmp_compressed}' => '${BACKUP_DIR}' ..." + MV -i "${out_sql_tmp_compressed}" "${BACKUP_DIR}" + +} + + + ################################################################################ ## ## Main @@ -442,6 +533,7 @@ main() { info "Starting backup ..." get_databases cleanup_old_backups + backup_databases empty_line debug "Deactivating trap."