Copy PostgreSQL databases with zstd before updates
pg_dump -Fc -Z zstd:1 instead of its default gzip: on the dev Core a 1.3 GB database took 6 s and 158 MB instead of 27 s and 223 MB, and restores as fast. A pg_dump without zstd falls back to its default. The "takes a while" note now starts at 1 GB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+15
-5
@@ -1103,8 +1103,10 @@ ROLLBACK_FILES = [".env", "docker-compose.yml"]
|
||||
# Each runs in the database's container (the same version as the server);
|
||||
# root's MariaDB password is read there, so it's not in a log.
|
||||
DUMP = {
|
||||
# zstd at level 1: on the dev Core, a 1.3 GB database took 6 s and
|
||||
# 158 MB, against 27 s and 223 MB with pg_dump's default (gzip)
|
||||
"postgres": ("db.dump", ["docker", "exec", "postgresql",
|
||||
"pg_dump", "-U", "postgres", "-Fc", "{db}"]),
|
||||
"pg_dump", "-U", "postgres", "-Fc", "-Z", "zstd:1", "{db}"]),
|
||||
"mysql": ("db.sql", ["docker", "exec", "pdnsmysql", "sh", "-c",
|
||||
'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" exec mariadb-dump -uroot '
|
||||
"--single-transaction --routines --triggers --events --databases {db}"]),
|
||||
@@ -1138,7 +1140,7 @@ DB_SIZE = {
|
||||
# Left free on the apps' disk after the database's copy, for the Core
|
||||
ROLLBACK_RESERVE = 1024 ** 3
|
||||
# Above this, say that keeping the copy takes a while
|
||||
BIG_DATABASE = 200 * 1024 ** 2
|
||||
BIG_DATABASE = 1024 ** 3
|
||||
|
||||
|
||||
def size_text(n):
|
||||
@@ -1151,6 +1153,11 @@ def database_size(need, db):
|
||||
return int(text) if out.returncode == 0 and text.isdigit() else None
|
||||
|
||||
|
||||
def dump_database(cmd, dbname, path, log):
|
||||
with open(path, "wb") as out, open(log, "a") as err:
|
||||
return subprocess.run([c.replace("{db}", dbname) for c in cmd], stdout=out, stderr=err)
|
||||
|
||||
|
||||
def keep_for_rollback(m, p, backup, log):
|
||||
"""Copies of the app's service folder, settings, database and (where the
|
||||
filesystem makes it cheap) /data, in backup. Refuses the update if the
|
||||
@@ -1178,9 +1185,12 @@ def keep_for_rollback(m, p, backup, log):
|
||||
say(f"Keeping a copy of its database ({size_text(size)}) in case the update "
|
||||
"fails; this takes a while...")
|
||||
started = time.monotonic()
|
||||
with open(os.path.join(backup, file), "wb") as out, open(log, "a") as err:
|
||||
result = subprocess.run([c.replace("{db}", m["dbname"]) for c in cmd],
|
||||
stdout=out, stderr=err)
|
||||
result = dump_database(cmd, m["dbname"], os.path.join(backup, file), log)
|
||||
if result.returncode != 0 and "-Z" in cmd:
|
||||
# A PostgreSQL built without zstd: its default compression
|
||||
at = cmd.index("-Z")
|
||||
result = dump_database(cmd[:at] + cmd[at + 2:], m["dbname"],
|
||||
os.path.join(backup, file), log)
|
||||
if result.returncode != 0:
|
||||
raise Refused(f"Couldn't back up {name}'s database before updating, "
|
||||
f"so nothing was changed (log on the Core: {log}).")
|
||||
|
||||
Reference in New Issue
Block a user