This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumper.py
60 lines (41 loc) · 1.55 KB
/
dumper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
import os
import subprocess
import pathlib
from datetime import datetime
BASE_BACKUP_PATH = "/pg_backup"
LATEST_LINK = "latest.dump"
databases = [
db_name.strip()
for db_name in os.environ.get("BACKUP_DATABASES", "").split(",")
if db_name.strip()
]
print("Databases to dump: {}".format(databases), flush=True)
def dump_database(db_name: str) -> subprocess.CompletedProcess:
dest_path = "{base}/{db_name}".format(base=BASE_BACKUP_PATH, db_name=db_name)
pathlib.Path(dest_path).mkdir(parents=True, exist_ok=True)
filename = "backup_{:%Y-%m-%d_%H:%M}.dump".format(datetime.now())
dest_file = "{path}/{file}".format(path=dest_path, file=filename,)
if db_name == "__all__":
dump_cmd = ["pg_dumpall", "--clean"]
else:
dump_cmd = ["pg_dump", "-d", db_name, "-w", "--format", "c"]
with open(dest_file, "w") as outfile:
exe_result = subprocess.run(dump_cmd, stdout=outfile)
if exe_result.returncode == 0:
os.chdir(dest_path)
if os.path.exists(LATEST_LINK):
os.unlink(LATEST_LINK)
os.symlink(filename, LATEST_LINK)
return exe_result
for database in databases:
print("# Backup database: {0} ... ".format(database), end="", flush=True)
result_msg = "ok"
try:
proc_result = dump_database(database)
except Exception as err:
result_msg = "fail [err: {0}]".format(err)
else:
if proc_result.returncode != 0:
result_msg = "fail [ret code: {0}]".format(proc_result.returncode)
print(result_msg)