Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rwm
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Radoslav Bodó
rwm
Commits
015eb80b
Commit
015eb80b
authored
Apr 30, 2024
by
Radoslav Bodó
Browse files
Options
Downloads
Patches
Plain Diff
scripts: postgresql backup cleanup
parent
6398a149
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
scripts/backup_mysql.py
+1
-0
1 addition, 0 deletions
scripts/backup_mysql.py
scripts/backup_postgresql.py
+26
-19
26 additions, 19 deletions
scripts/backup_postgresql.py
scripts/restore_postgresql.sh
+7
-23
7 additions, 23 deletions
scripts/restore_postgresql.sh
with
34 additions
and
42 deletions
scripts/backup_mysql.py
+
1
−
0
View file @
015eb80b
...
...
@@ -94,6 +94,7 @@ def cleanup():
return
os
.
unlink
(
ARCHIVE
)
# pylint: disable=duplicate-code
def
main
():
"""
main
"""
...
...
This diff is collapsed.
Click to expand it.
scripts/backup_postgresql.py
+
26
−
19
View file @
015eb80b
#!/usr/bin/env python3
"""
rwm postgresql backup helper
"""
import
gzip
import
os
import
shutil
import
subprocess
...
...
@@ -18,31 +17,23 @@ USERNAME = os.environ.get("PGUSER", "postgres")
def
list_databases
():
"""
list postgresql databases
"""
cmd
=
[
"
psql
"
,
"
-U
"
,
USERNAME
,
"
-h
"
,
"
127.0.0.1
"
,
"
-q
"
,
"
-A
"
,
"
-t
"
,
"
-c
"
,
"
SELECT datname FROM pg_database WHERE datistemplate = false;
"
]
cmd
=
[
"
su
"
,
"
-c
"
,
'
psql -q -A -t -c
"
SELECT datname FROM pg_database WHERE datistemplate = false;
"'
,
USERNAME
,
]
proc
=
subprocess
.
run
(
cmd
,
check
=
True
,
stdout
=
subprocess
.
PIPE
,
text
=
True
)
return
proc
.
stdout
.
splitlines
()
def
backup_database
(
database
):
"""
backup single database and compress it
"""
cmd
=
[
"
pg_dump
"
,
"
-U
"
,
USERNAME
,
"
-h
"
,
"
127.0.0.1
"
,
database
]
"""
backup single database
"""
cmd
=
[
"
su
"
,
"
-c
"
,
f
"
pg_dump --clean --create
{
database
}
"
,
USERNAME
]
try
:
with
gzip
.
open
(
f
"
{
BACKUPDIR
}
/
{
database
}
.sql.gz
"
,
"
wb
"
)
as
fd
:
with
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
universal_newlines
=
True
)
as
proc
:
for
stdout_line
in
iter
(
proc
.
stdout
.
readline
,
''
):
fd
.
write
(
stdout_line
.
encode
(
'
utf-8
'
))
# Dumps roles
if
database
==
"
postgres
"
:
roles_cmd
=
[
"
pg_dumpall
"
,
"
-U
"
,
USERNAME
,
"
-h
"
,
"
127.0.0.1
"
,
"
--roles-only
"
]
with
subprocess
.
Popen
(
roles_cmd
,
stdout
=
subprocess
.
PIPE
,
universal_newlines
=
True
)
as
roles_proc
:
for
stdout_line
in
iter
(
roles_proc
.
stdout
.
readline
,
''
):
fd
.
write
(
stdout_line
.
encode
(
'
utf-8
'
))
with
open
(
f
"
{
BACKUPDIR
}
/
{
database
}
.sql
"
,
"
wb
"
)
as
fd
:
subprocess
.
run
(
cmd
,
stdout
=
fd
,
check
=
True
)
except
subprocess
.
CalledProcessError
:
print
(
f
"
ERROR: cannot dump
{
database
}
"
,
file
=
sys
.
stderr
)
return
1
...
...
@@ -50,6 +41,20 @@ def backup_database(database):
return
0
def
backup_global_data
():
"""
backup global data
"""
try
:
cmd
=
[
"
su
"
,
"
-c
"
,
"
pg_dumpall --clean --globals-only
"
,
USERNAME
]
with
open
(
f
"
{
BACKUPDIR
}
/_globals.sql
"
,
"
wb
"
)
as
fd
:
subprocess
.
run
(
cmd
,
stdout
=
fd
,
check
=
True
)
except
subprocess
.
CalledProcessError
:
print
(
"
ERROR: cannot dump database global data
"
,
file
=
sys
.
stderr
)
return
1
return
0
def
create
():
"""
dump database to archive
"""
...
...
@@ -63,6 +68,8 @@ def create():
databases
+=
1
errors
+=
backup_database
(
db
)
errors
+=
backup_global_data
()
subprocess
.
run
([
"
tar
"
,
"
czf
"
,
ARCHIVE
,
BACKUPDIR
],
check
=
True
)
shutil
.
rmtree
(
BACKUPDIR
)
print
(
"
archive created:
"
)
...
...
This diff is collapsed.
Click to expand it.
scripts/restore_postgresql.sh
+
7
−
23
View file @
015eb80b
#!/bin/bash
if
[
"$#"
-ne
1
]
;
then
echo
"Usage:
$0
<tar_file>"
exit
1
fi
set
-ex
umask
077
tar_file
=
"
$1
"
if
[
!
-f
"
$tar_file
"
]
;
then
echo
"Error: '
$tar_file
' does not exist."
exit
1
fi
temp_dir
=
$(
mktemp
-d
)
tar
xzf
"
$tar_file
"
-C
"
$temp_dir
"
chown
-R
postgres
"
${
temp_dir
}
"
tar
-xzf
"
$tar_file
"
-C
"
$temp_dir
"
sql_files
=
$(
find
"
$temp_dir
"
-type
f
-name
'*.sql.gz'
)
for
dump_file
in
$sql_files
;
do
db_name
=
$(
basename
"
$dump_file
"
.sql.gz
)
createdb
"
$db_name
"
gunzip
-c
"
$dump_file
"
| psql
-q
-d
"
$db_name
"
find
"
$temp_dir
"
-type
f
-name
'*.sql'
|
while
read
-r
dump_file
;
do
su
-c
"psql < '
$dump_file
'"
postgres
done
rm
-rf
"
$temp_dir
"
\ No newline at end of file
echo
"Databases restored."
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