TMP_DIR=
LOGFILE="/dev/null"
+BYTES_TOTAL="0"
+
#-------------------------------------------------------------------
detect_color() {
;;
-q|--quiet)
QUIET="y"
+ RED=""
+ YELLOW=""
+ GREEN=""
+ BLUE=""
+ NORMAL=""
shift
;;
--nocolor)
#------------------------------------------------------------------------------
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}"
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"
#------------------------------------------------------------------------------
empty_line() {
if [[ "${QUIET}" == "y" ]] ; then
+ echo >> "${LOGFILE}"
return 0
fi
echo 2>&1 | tee -a "${LOGFILE}"
}
+#------------------------------------------------------------------------------
+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
info "Starting backup ..."
get_databases
cleanup_old_backups
+ backup_databases
empty_line
debug "Deactivating trap."