diff --git a/scripts/backup_mysql.py b/scripts/backup_mysql.py index e9d0ccca9ddd1dd5c9158eccaedc0bc7dcdacc73..0653222df2be467569bec294dac546153121a855 100755 --- a/scripts/backup_mysql.py +++ b/scripts/backup_mysql.py @@ -94,6 +94,7 @@ def cleanup(): return os.unlink(ARCHIVE) +# pylint: disable=duplicate-code def main(): """main""" diff --git a/scripts/backup_postgresql.py b/scripts/backup_postgresql.py index c6a5a4b0abb3f8ba38660bb5d39ea140b6ae3aa3..de43c1667ce70b5c67c34b587037299ddc3c6466 100755 --- a/scripts/backup_postgresql.py +++ b/scripts/backup_postgresql.py @@ -1,7 +1,6 @@ #!/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:") diff --git a/scripts/restore_postgresql.sh b/scripts/restore_postgresql.sh index ced0809bd22f91e4f51f41c9b664b956a14ec404..727f5d69b890874ccd0c2605f77017a919eb2f90 100755 --- a/scripts/restore_postgresql.sh +++ b/scripts/restore_postgresql.sh @@ -1,31 +1,15 @@ #!/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" - -echo "Databases restored." +rm -rf "$temp_dir" \ No newline at end of file