]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding backup of PostgreSQL global data
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 21 May 2019 11:51:12 +0000 (13:51 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 21 May 2019 11:51:12 +0000 (13:51 +0200)
bin/backup_pgsql.sh

index f0154141a4fadabd169c78a7a17c9db9c3370857..844ca655c31ac38b755154d8c2387914f78fc93a 100755 (executable)
@@ -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