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

pg_dump in temp dir to harden backup directory

parent 3fb73b6c
Branches
No related tags found
1 merge request!154pg_dump in temp dir to harden backup directory
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# ================================================================================ # ================================================================================
# #
# LOCALDUMP :: POSTGRES # LOCALDUMP :: POSTGRES
# create gzipped plain text backups from each scheme # Create gzipped plain text backups from each scheme
# #
# -------------------------------------------------------------------------------- # --------------------------------------------------------------------------------
# ah - Axel Hahn <axel.hahn@iml.unibe.ch> # ah - Axel Hahn <axel.hahn@iml.unibe.ch>
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
# 2024-12-13 ah v1.6 backup uses a snapshot db # 2024-12-13 ah v1.6 backup uses a snapshot db
# 2024-12-16 ah v1.7 on snapshot mode it can fallback for single database to normal pg_dump of origin # 2024-12-16 ah v1.7 on snapshot mode it can fallback for single database to normal pg_dump of origin
# 2024-12-17 ah v1.8 handle env variables to disable snapshots or set times for disconnect # 2024-12-17 ah v1.8 handle env variables to disable snapshots or set times for disconnect
# 2025-02-03 ah v1.9 pg_dump in temp directory; dump file will be moved to backup dir
# ================================================================================ # ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then if [ -z "$BACKUP_TARGETDIR" ]; then
...@@ -65,9 +66,6 @@ function pgsql.backup(){ ...@@ -65,9 +66,6 @@ function pgsql.backup(){
create_targetdir create_targetdir
# ----- change owner of directory because of su command
chown ${PGUSER}. "${BACKUP_TARGETDIR}"
# ----- GO # ----- GO
# prevent could not change directory to "/root": Permission denied # prevent could not change directory to "/root": Permission denied
...@@ -112,12 +110,13 @@ function pgsql.backup(){ ...@@ -112,12 +110,13 @@ function pgsql.backup(){
test $bUseSnapshot4DB -eq 1 && SNAPSHOTDB="${snapshotprefix}${DATABASE}" test $bUseSnapshot4DB -eq 1 && SNAPSHOTDB="${snapshotprefix}${DATABASE}"
OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql" OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql"
TMPOUTFILE="/tmp/$(get_outfile ${DATABASE}).sql"
myrc=0 myrc=0
# drop snapshot db first - just in case # drop snapshot db first - just in case
if [ $bUseSnapshot4DB -eq 1 ]; then if [ $bUseSnapshot4DB -eq 1 ]; then
echo -n " snapshot ." echo -n "snapshot ."
su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1 su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1
echo -n "." echo -n "."
test $bDisconnect -ne 0 && su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \" test $bDisconnect -ne 0 && su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"
...@@ -137,14 +136,20 @@ function pgsql.backup(){ ...@@ -137,14 +136,20 @@ function pgsql.backup(){
if [ $myrc -eq 0 ]; then if [ $myrc -eq 0 ]; then
echo -n "backup ... " echo -n "backup ... "
if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$OUTFILE"; then if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$TMPOUTFILE"; then
fetchrc >/dev/null fetchrc >/dev/null
test $bUseSnapshot4DB -eq 1 && ( test $bUseSnapshot4DB -eq 1 && (
echo -n "delete snapshot ... " echo -n "delete snapshot ... "
su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" su ${PGUSER} -c "dropdb ${SNAPSHOTDB}"
) )
db._compressDumpfile "$OUTFILE" echo -n "move temp to target ... "
if mv "$TMPOUTFILE" "$OUTFILE"; then
db._compressDumpfile "$OUTFILE"
else
fetchrc
cecho error "Failed."
fi
else else
fetchrc fetchrc
test $bUseSnapshot4DB -eq 1 && su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" test $bUseSnapshot4DB -eq 1 && su ${PGUSER} -c "dropdb ${SNAPSHOTDB}"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment