-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdb-auto-backup.py
executable file
·257 lines (198 loc) · 7.37 KB
/
db-auto-backup.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
import bz2
import fnmatch
import gzip
import lzma
import os
import secrets
import sys
from datetime import datetime
from io import StringIO
from pathlib import Path
from typing import IO, Callable, Dict, Iterable, NamedTuple, Optional
import docker
import pycron
import requests
from docker.models.containers import Container
from dotenv import dotenv_values
from tqdm.auto import tqdm
class BackupProvider(NamedTuple):
name: str
patterns: list[str]
backup_method: Callable[[Container], str]
file_extension: str
def get_container_env(container: Container) -> Dict[str, Optional[str]]:
"""
Get all environment variables from a container.
Variables at runtime, rather than those defined in the container.
"""
_, (env_output, _) = container.exec_run("env", demux=True)
return dict(dotenv_values(stream=StringIO(env_output.decode())))
def binary_exists_in_container(container: Container, binary_name: str) -> bool:
"""
Get all environment variables from a container.
Variables at runtime, rather than those defined in the container.
"""
exit_code, _ = container.exec_run(["which", binary_name])
return exit_code == 0
def temp_backup_file_name() -> str:
"""
Create a temporary file to save backups to,
then atomically replace backup file
"""
return ".auto-backup-" + secrets.token_hex(4)
def open_file_compressed(file_path: Path, algorithm: str) -> IO[bytes]:
file_path.touch(mode=0o600)
if algorithm == "gzip":
return gzip.open(file_path, mode="wb") # type:ignore
elif algorithm in ["lzma", "xz"]:
return lzma.open(file_path, mode="wb")
elif algorithm == "bz2":
return bz2.open(file_path, mode="wb")
elif algorithm == "plain":
return file_path.open(mode="wb")
raise ValueError(f"Unknown compression method {algorithm}")
def get_compressed_file_extension(algorithm: str) -> str:
if algorithm == "gzip":
return ".gz"
elif algorithm in ["lzma", "xz"]:
return ".xz"
elif algorithm == "bz2":
return ".bz2"
elif algorithm == "plain":
return ""
raise ValueError(f"Unknown compression method {algorithm}")
def get_success_hook_url() -> Optional[str]:
if success_hook_url := os.environ.get("SUCCESS_HOOK_URL"):
return success_hook_url
if healthchecks_id := os.environ.get("HEALTHCHECKS_ID"):
healthchecks_host = os.environ.get("HEALTHCHECKS_HOST", "hc-ping.com")
return f"https://{healthchecks_host}/{healthchecks_id}"
if uptime_kuma_url := os.environ.get("UPTIME_KUMA_URL"):
return uptime_kuma_url
return None
def backup_psql(container: Container) -> str:
env = get_container_env(container)
user = env.get("POSTGRES_USER", "postgres")
return f"pg_dumpall -U {user}"
def backup_mysql(container: Container) -> str:
env = get_container_env(container)
# The mariadb container supports both
if "MARIADB_ROOT_PASSWORD" in env:
auth = "-p$MARIADB_ROOT_PASSWORD"
elif "MYSQL_ROOT_PASSWORD" in env:
auth = "-p$MYSQL_ROOT_PASSWORD"
else:
raise ValueError(f"Unable to find MySQL root password for {container.name}")
if binary_exists_in_container(container, "mariadb-dump"):
backup_binary = "mariadb-dump"
else:
backup_binary = "mysqldump"
return f"bash -c '{backup_binary} {auth} --all-databases'"
def backup_redis(container: Container) -> str:
"""
Note: `SAVE` command locks the database, which isn't ideal.
Hopefully the commit is fast enough!
"""
return "sh -c 'redis-cli SAVE > /dev/null && cat /data/dump.rdb'"
BACKUP_PROVIDERS: list[BackupProvider] = [
BackupProvider(
name="postgres",
patterns=[
"postgres",
"tensorchord/pgvecto-rs",
"nextcloud/aio-postgresql",
"timescale/timescaledb",
],
backup_method=backup_psql,
file_extension="sql",
),
BackupProvider(
name="mysql",
patterns=["mysql", "mariadb", "linuxserver/mariadb"],
backup_method=backup_mysql,
file_extension="sql",
),
BackupProvider(
name="redis",
patterns=["redis"],
backup_method=backup_redis,
file_extension="rdb",
),
]
BACKUP_DIR = Path(os.environ.get("BACKUP_DIR", "/var/backups"))
SCHEDULE = os.environ.get("SCHEDULE", "0 0 * * *")
SHOW_PROGRESS = sys.stdout.isatty()
COMPRESSION = os.environ.get("COMPRESSION", "plain")
INCLUDE_LOGS = bool(os.environ.get("INCLUDE_LOGS"))
def get_backup_provider(container_names: Iterable[str]) -> Optional[BackupProvider]:
for name in container_names:
for provider in BACKUP_PROVIDERS:
if any(fnmatch.fnmatch(name, pattern) for pattern in provider.patterns):
return provider
return None
def get_container_names(container: Container) -> Iterable[str]:
names = set()
for tag in container.image.tags:
registry, image = docker.auth.resolve_repository_name(tag)
# HACK: Strip "library" from official images
if registry == docker.auth.INDEX_NAME:
image = image.removeprefix("library/")
image, tag_name = image.split(":", 1)
names.add(image)
return names
@pycron.cron(SCHEDULE)
def backup(now: datetime) -> None:
print("Starting backup...")
docker_client = docker.from_env()
containers = docker_client.containers.list()
backed_up_containers = []
print(f"Found {len(containers)} containers.")
for container in containers:
container_names = get_container_names(container)
backup_provider = get_backup_provider(container_names)
if backup_provider is None:
continue
backup_file = (
BACKUP_DIR
/ f"{container.name}.{backup_provider.file_extension}{get_compressed_file_extension(COMPRESSION)}"
)
backup_temp_file_path = BACKUP_DIR / temp_backup_file_name()
backup_command = backup_provider.backup_method(container)
_, output = container.exec_run(backup_command, stream=True, demux=True)
description = f"{container.name} ({backup_provider.name})"
with open_file_compressed(
backup_temp_file_path, COMPRESSION
) as backup_temp_file:
with tqdm.wrapattr(
backup_temp_file,
method="write",
desc=description,
disable=not SHOW_PROGRESS,
) as f:
for stdout, _ in output:
if stdout is None:
continue
f.write(stdout)
os.replace(backup_temp_file_path, backup_file)
if not SHOW_PROGRESS:
print(description)
backed_up_containers.append(container.name)
duration = (datetime.now() - now).total_seconds()
print(
f"Backup of {len(backed_up_containers)} containers complete in {duration:.2f} seconds."
)
if success_hook_url := get_success_hook_url():
if INCLUDE_LOGS:
response = requests.post(
success_hook_url, data="\n".join(backed_up_containers)
)
else:
response = requests.get(success_hook_url)
response.raise_for_status()
if __name__ == "__main__":
if os.environ.get("SCHEDULE"):
print(f"Running backup with schedule '{SCHEDULE}'.")
pycron.start()
else:
backup(datetime.now())