Skip to content
Snippets Groups Projects
Commit 015eb80b authored by Radoslav Bodó's avatar Radoslav Bodó
Browse files

scripts: postgresql backup cleanup

parent 6398a149
No related branches found
No related tags found
No related merge requests found
...@@ -94,6 +94,7 @@ def cleanup(): ...@@ -94,6 +94,7 @@ def cleanup():
return os.unlink(ARCHIVE) return os.unlink(ARCHIVE)
# pylint: disable=duplicate-code
def main(): def main():
"""main""" """main"""
......
#!/usr/bin/env python3 #!/usr/bin/env python3
"""rwm postgresql backup helper""" """rwm postgresql backup helper"""
import gzip
import os import os
import shutil import shutil
import subprocess import subprocess
...@@ -18,31 +17,23 @@ USERNAME = os.environ.get("PGUSER", "postgres") ...@@ -18,31 +17,23 @@ USERNAME = os.environ.get("PGUSER", "postgres")
def list_databases(): def list_databases():
"""list postgresql 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) proc = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, text=True)
return proc.stdout.splitlines() return proc.stdout.splitlines()
def backup_database(database): def backup_database(database):
"""backup single database and compress it""" """backup single database"""
cmd = ["pg_dump", "-U", USERNAME, "-h", "127.0.0.1", database]
cmd = ["su", "-c", f"pg_dump --clean --create {database}", USERNAME]
try: try:
with gzip.open(f"{BACKUPDIR}/{database}.sql.gz", "wb") as fd: with open(f"{BACKUPDIR}/{database}.sql", "wb") as fd:
with subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True) as proc: subprocess.run(cmd, stdout=fd, check=True)
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'))
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
print(f"ERROR: cannot dump {database}", file=sys.stderr) print(f"ERROR: cannot dump {database}", file=sys.stderr)
return 1 return 1
...@@ -50,6 +41,20 @@ def backup_database(database): ...@@ -50,6 +41,20 @@ def backup_database(database):
return 0 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(): def create():
"""dump database to archive""" """dump database to archive"""
...@@ -63,6 +68,8 @@ def create(): ...@@ -63,6 +68,8 @@ def create():
databases += 1 databases += 1
errors += backup_database(db) errors += backup_database(db)
errors += backup_global_data()
subprocess.run(["tar", "czf", ARCHIVE, BACKUPDIR], check=True) subprocess.run(["tar", "czf", ARCHIVE, BACKUPDIR], check=True)
shutil.rmtree(BACKUPDIR) shutil.rmtree(BACKUPDIR)
print("archive created:") print("archive created:")
......
#!/bin/bash #!/bin/bash
set -ex
if [ "$#" -ne 1 ]; then umask 077
echo "Usage: $0 <tar_file>"
exit 1
fi
tar_file="$1" tar_file="$1"
if [ ! -f "$tar_file" ]; then
echo "Error: '$tar_file' does not exist."
exit 1
fi
temp_dir=$(mktemp -d) temp_dir=$(mktemp -d)
tar xzf "$tar_file" -C "$temp_dir"
chown -R postgres "${temp_dir}"
tar -xzf "$tar_file" -C "$temp_dir" find "$temp_dir" -type f -name '*.sql' | while read -r dump_file; do
su -c "psql < '$dump_file'" postgres
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"
done done
rm -rf "$temp_dir" rm -rf "$temp_dir"
\ No newline at end of file
echo "Databases restored."
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