Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
iml-backup
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
iml-backup
Commits
a86b50bc
Commit
a86b50bc
authored
4 months ago
by
Hahn Axel (hahn)
Browse files
Options
Downloads
Patches
Plain Diff
postgres db backup uses a snapshot db
parent
e1e22df9
No related branches found
Branches containing commit
No related tags found
1 merge request
!147
Pgsql dump with snapshots
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
plugins/localdump/pgsql.sh
+33
-7
33 additions, 7 deletions
plugins/localdump/pgsql.sh
with
33 additions
and
7 deletions
plugins/localdump/pgsql.sh
+
33
−
7
View file @
a86b50bc
...
@@ -14,6 +14,7 @@
...
@@ -14,6 +14,7 @@
# 2022-03-17 v1.3 WIP: add lines with prefix __DB__
# 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
# 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-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
if
[
-z
"
$BACKUP_TARGETDIR
"
]
;
then
...
@@ -34,9 +35,14 @@ fi
...
@@ -34,9 +35,14 @@ fi
# FUNCTION
# FUNCTION
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# backup all databases
function
pgsql.backup
(){
function
pgsql.backup
(){
local
DATABASE
local
SNAPSHOTDB
local
OUTFILE
local
snapshotprefix
=
"snapshot_"
create_targetdir
create_targetdir
# ----- change owner of directory because of su command
# ----- change owner of directory because of su command
...
@@ -45,24 +51,44 @@ function pgsql.backup(){
...
@@ -45,24 +51,44 @@ function pgsql.backup(){
# ----- GO
# ----- GO
# prevent could not change directory to "/root": Permission denied
# prevent could not change directory to "/root": Permission denied
(
cd
/tmp
cd
/tmp
sSqlGetDblist
=
"select datname from pg_database where not datistemplate and datallowconn order by datname;"
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
do
echo
-n
"__DB__
${
SERVICENAME
}
backup
$DATABASE
... "
echo
-n
"__DB__
${
SERVICENAME
}
backup
$DATABASE
... "
SNAPSHOTDB
=
"
${
snapshotprefix
}${
DATABASE
}
"
OUTFILE
=
"
${
BACKUP_TARGETDIR
}
/
$(
get_outfile
${
DATABASE
}
)
.sql"
OUTFILE
=
"
${
BACKUP_TARGETDIR
}
/
$(
get_outfile
${
DATABASE
}
)
.sql"
if
su
${
PGUSER
}
-c
"pg_dump
${
BACKUP_PARAMS
}
-Fp
${
DATABASE
}
>
$OUTFILE
"
;
then
fetchrc
>
/dev/null
echo
-n
" snapshot ... "
db._compressDumpfile
"
$OUTFILE
"
# 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
else
fetchrc
cecho error
"ERROR!"
fi
fi
done
done
cd
-
)
}
}
# restore database dump file into database
# restore database dump file into database
# param string database dump file (gzipped)
# param string database dump file (gzipped)
# param string optional: database to import; default: database is parsed from file
# param string optional: database to import; default: database is parsed from file
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment