Skip to content
Snippets Groups Projects
Commit a4759c80 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch 'pgsql-dump-with-snapshots' into 'master'

Pgsql dump with snapshots

See merge request !147
parents e1e22df9 d6a02e85
No related branches found
No related tags found
1 merge request!147Pgsql dump with snapshots
......@@ -19,6 +19,7 @@
# 2024-03-14 ah v2.0: use profiles for local and remote databases
# 2024-03-18 ah fix for db detection from file and cli restore
# 2024-10-02 ah reset $rc before calling db plugin
# 2024-12-13 ah set gzip compression from 9 to 4
# ======================================================================
# --- variables:
......@@ -76,7 +77,7 @@
# $myrc is last returncode - set in fetchrc
if [ $myrc -eq 0 ]; then
echo -n "gzip $_outfile ... "
gzip -9 -f "${_outfile}"
gzip -4 -f "${_outfile}"
fetchrc
else
color.echo error "ERROR occured while dumping - no gzip of $_outfile"
......
......@@ -14,6 +14,7 @@
# 2022-03-17 v1.3 WIP: add lines with prefix __DB__
# 2023-09-20 v1.4 FIX could not change directory to "/root": Permission denied
# 2024-10-02 ah v1.5 rename backup and restore function
# 2024-12-13 ah v1.6 backup uses a snapshot db
# ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then
......@@ -34,9 +35,14 @@ fi
# FUNCTION
# --------------------------------------------------------------------------------
# backup all databases
function pgsql.backup(){
local DATABASE
local SNAPSHOTDB
local OUTFILE
local snapshotprefix="snapshot_"
create_targetdir
# ----- change owner of directory because of su command
......@@ -45,24 +51,44 @@ function pgsql.backup(){
# ----- GO
# prevent could not change directory to "/root": Permission denied
(
cd /tmp
sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;"
for DATABASE in $(su ${PGUSER} -c "psql ${BACKUP_PARAMS} -At -c '$sSqlGetDblist' postgres" 2>/dev/null)
for DATABASE in $( su ${PGUSER} -c "psql ${BACKUP_PARAMS} -At -c '$sSqlGetDblist' postgres" 2>/dev/null | grep -v "^$snapshotprefix" )
do
echo -n "__DB__${SERVICENAME} backup $DATABASE ... "
SNAPSHOTDB="${snapshotprefix}${DATABASE}"
OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql"
if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${DATABASE} >$OUTFILE"; then
fetchrc >/dev/null
db._compressDumpfile "$OUTFILE"
echo -n " snapshot ... "
# drop snapshot db first - just in case
su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1
su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${SNAPSHOTDB} WITH TEMPLATE ${DATABASE};\"" >/dev/null
fetchrc >/dev/null
if [ $myrc -eq 0 ]; then
echo -n "backup ... "
if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$OUTFILE"; then
fetchrc >/dev/null
echo -n "delete snapshot ... "
su ${PGUSER} -c "dropdb ${SNAPSHOTDB}"
db._compressDumpfile "$OUTFILE"
else
fetchrc
su ${PGUSER} -c "dropdb ${SNAPSHOTDB}"
fi
else
fetchrc
cecho error "ERROR!"
fi
done
cd -
)
}
# restore database dump file into database
# param string database dump file (gzipped)
# param string optional: database to import; default: database is parsed from file
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment