From ae127243e4ae00d21203c197faea58a4e97d8a77 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 21 May 2019 13:51:12 +0200 Subject: [PATCH] Adding backup of PostgreSQL global data --- bin/backup_pgsql.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/bin/backup_pgsql.sh b/bin/backup_pgsql.sh index f015414..844ca65 100755 --- a/bin/backup_pgsql.sh +++ b/bin/backup_pgsql.sh @@ -453,6 +453,57 @@ cleanup_old_backups() { } +#------------------------------------------------------------------------------ +backup_globals() { + + empty_line + info "Backing up ${GREEN}globals${NORMAL} ..." + + local output_sql="globals-${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_dumpall --globals-only ${verbose_option} 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}" + +} + #------------------------------------------------------------------------------ backup_databases() { @@ -540,6 +591,7 @@ main() { prepare_dirs info "Starting backup ..." get_databases + backup_globals cleanup_old_backups backup_databases -- 2.39.5