Repository navigation
Expand file tree
/
Copy pathdocker-backup.py
More file actions
377 lines (293 loc) · 13.1 KB
/
Copy pathdocker-backup.py
File metadata and controls
377 lines (293 loc) · 13.1 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python3
"""
docker_backup.py - Python re-implementation of docker-backup (originally Go)
Creates backups of Docker containers: metadata (json) + a copy of all
volume mounts, or optionally a single .tar file. Can also restore a
backup.
Requires: python3 and the `docker` CLI in PATH. Talks to the Docker CLI
via subprocess -- no extra pip dependency needed.
Usage:
./docker_backup.py backup <container-id>
./docker_backup.py backup --all [--stopped]
./docker_backup.py backup <container-id> --tar
./docker_backup.py restore <backup>.json [--start]
./docker_backup.py restore <backup>.tar [--start]
"""
import argparse
import io
import json
import os
import re
import shutil
import subprocess
import sys
import tarfile
import tempfile
from datetime import datetime
from pathlib import Path
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def run(cmd, capture=True, check=True):
"""Runs a command and returns stdout (or raises a RuntimeError)."""
result = subprocess.run(cmd, capture_output=capture, text=True)
if check and result.returncode != 0:
raise RuntimeError(f"Command failed: {' '.join(cmd)}\n{result.stderr.strip()}")
return result.stdout.strip() if capture else None
def docker_inspect(ref):
"""Returns the full `docker inspect` output as a dict."""
out = run(["docker", "inspect", ref])
data = json.loads(out)
if not data:
raise RuntimeError(f"Could not find container/image '{ref}'.")
return data[0]
def sanitize(name):
"""Makes a string safe to use as a file/directory name."""
name = name.strip("/")
return re.sub(r"[^A-Za-z0-9._-]+", "_", name)
def mount_folder_name(mount):
"""Subdirectory name for a mount: Docker volume name if available,
otherwise the sanitized destination path (bind mount)."""
if mount.get("Name"):
return sanitize(mount["Name"])
return sanitize(mount["Destination"])
def get_full_image_name(image):
"""Tries to find a full image:tag if no tag was specified."""
if ":" in image:
return image
try:
info = docker_inspect(image)
tags = info.get("RepoTags") or []
if tags:
return tags[0]
except RuntimeError:
pass
return image
# ---------------------------------------------------------------------------
# Backup
# ---------------------------------------------------------------------------
def backup_root_for(container_name, output):
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M")
return Path(output) / timestamp / sanitize(container_name)
def do_backup(container_id, args):
info = docker_inspect(container_id)
name = info["Name"]
image = get_full_image_name(info["Config"]["Image"])
mounts = info.get("Mounts", [])
print(f"Creating backup of {name.lstrip('/')} ({image}, {container_id[:12]})")
filename = sanitize(f"{image}-{container_id}")
backup_root = backup_root_for(name, args.output)
if args.tar:
return backup_tar(info, filename, backup_root, args)
backup_root.mkdir(parents=True, exist_ok=True)
json_path = backup_root / f"{filename}.backup.json"
json_path.write_text(json.dumps(info, indent=2))
collected_paths = []
for m in mounts:
src = m["Source"]
if args.verbose:
print(f"Mount ({m.get('Type')}) {src} -> {m['Destination']}")
# Some containers bind-mount special files instead of directories,
# e.g. /var/run/docker.sock (common for Portainer, Watchtower, Diun,
# Traefik, autoheal, etc.). shutil.copytree() requires a directory,
# so skip anything that isn't one instead of crashing the whole run.
if not os.path.isdir(src):
print(f"Warning: mount '{src}' is not a directory (socket/device?), skipping it.")
continue
for root, _dirs, files in os.walk(src):
for f in files:
collected_paths.append(os.path.join(root, f))
dest_dir = backup_root / mount_folder_name(m)
shutil.copytree(src, dest_dir, dirs_exist_ok=True)
files_path = backup_root / f"{filename}.backup.files"
with open(files_path, "w") as fl:
fl.write(str(json_path.resolve()) + "\n")
for p in collected_paths:
fl.write(p + "\n")
print(f"Created backup: {json_path}")
if args.launch:
cmd = args.launch.replace("%tag", filename).replace("%list", str(files_path))
print("Launching external command and waiting for it to finish:")
print(cmd)
subprocess.run(cmd, shell=True, check=True)
return json_path
def backup_tar(info, filename, backup_root, args):
backup_root.mkdir(parents=True, exist_ok=True)
tar_path = backup_root / f"{filename}.tar"
with tarfile.open(tar_path, "w") as tf:
data = json.dumps(info, indent=2).encode()
ti = tarfile.TarInfo(name="container.json")
ti.size = len(data)
tf.addfile(ti, fileobj=io.BytesIO(data))
for m in info.get("Mounts", []):
src = m["Source"]
if args.verbose:
print(f"Mount ({m.get('Type')}) {src} -> {m['Destination']}")
# Same reasoning as in do_backup(): skip sockets/devices/missing
# paths instead of letting tarfile blow up on a non-regular file.
if not os.path.exists(src):
print(f"Warning: mount '{src}' does not exist, skipping it.")
continue
tf.add(src, arcname=src.lstrip("/"))
print(f"Created backup: {tar_path}")
return tar_path
# ---------------------------------------------------------------------------
# Restore
# ---------------------------------------------------------------------------
def create_container_from_info(info, args):
"""Creates a new container based on the stored `docker inspect`
metadata. Port mappings, environment, entrypoint, cmd, working dir and
labels are restored.
Note (same limitation as the original Go tool): mounts are not
restored 1-to-1 as bind mounts. Only volumes already defined in the
image itself (VOLUME instruction) are automatically created by
Docker -- the data for those is copied back afterwards. Other bind
mounts need to be added back manually (see warnings during restore)."""
config = info["Config"]
host_config = info.get("HostConfig", {})
name = info["Name"].lstrip("/")
image = config["Image"]
try:
docker_inspect(image)
except RuntimeError:
print(f"Pulling image: {image}")
run(["docker", "pull", image], capture=False)
cmd = ["docker", "create", "--name", name]
for env in config.get("Env") or []:
cmd += ["-e", env]
for key, val in (config.get("Labels") or {}).items():
cmd += ["--label", f"{key}={val}"]
for container_port, bindings in (host_config.get("PortBindings") or {}).items():
proto = container_port.split("/")[-1] if "/" in container_port else "tcp"
port_only = container_port.split("/")[0]
for b in bindings or [{}]:
host_port = b.get("HostPort", "")
host_ip = b.get("HostIp", "")
spec = f"{host_port}:{port_only}/{proto}"
if host_ip:
spec = f"{host_ip}:{spec}"
cmd += ["-p", spec]
if config.get("WorkingDir"):
cmd += ["-w", config["WorkingDir"]]
if config.get("Entrypoint"):
cmd += ["--entrypoint", " ".join(config["Entrypoint"])]
cmd.append(image)
if config.get("Cmd"):
cmd += config["Cmd"]
print("Restoring container:", name)
new_id = run(cmd)
print(f"Created container with ID: {new_id}")
return new_id
def match_new_mounts(old_mounts, new_info):
"""Maps old mounts to new mounts based on Destination."""
new_mounts = new_info.get("Mounts", [])
mapping = {}
for old in old_mounts:
for new in new_mounts:
if old["Destination"] == new["Destination"]:
mapping[old["Destination"]] = new
break
return mapping
def do_restore(backup_file, args):
path = Path(backup_file)
if path.suffix == ".json":
info = json.loads(path.read_text())
return restore_from_info(info, path.parent, args)
if path.suffix == ".tar":
return restore_tar(path, args)
raise RuntimeError("Unknown file type, please provide a .json or .tar file")
def restore_from_info(info, backup_dir, args):
old_mounts = info.get("Mounts", [])
new_id = create_container_from_info(info, args)
new_info = docker_inspect(new_id)
mapping = match_new_mounts(old_mounts, new_info)
for old in old_mounts:
new = mapping.get(old["Destination"])
src_data = backup_dir / mount_folder_name(old)
if not new:
print(f"Warning: no automatically created mount for "
f"{old['Destination']} -- data in '{src_data}' must be "
f"restored manually (e.g. bind-mount it yourself).")
continue
if not src_data.exists():
print(f"Warning: no backup data found in {src_data}, skipping.")
continue
print(f"Restoring: {src_data} -> {new['Source']}")
shutil.copytree(src_data, new["Source"], dirs_exist_ok=True)
if args.start:
start_container(new_id)
return new_id
def restore_tar(tar_path, args):
with tarfile.open(tar_path, "r") as tf:
info = json.loads(tf.extractfile("container.json").read())
old_mounts = info.get("Mounts", [])
new_id = create_container_from_info(info, args)
new_info = docker_inspect(new_id)
mapping = match_new_mounts(old_mounts, new_info)
with tempfile.TemporaryDirectory() as tmp:
tf.extractall(tmp)
for old in old_mounts:
new = mapping.get(old["Destination"])
extracted = Path(tmp) / old["Source"].lstrip("/")
if not new:
print(f"Warning: no automatically created mount for "
f"{old['Destination']} -- data in '{extracted}' "
f"must be restored manually.")
continue
if not extracted.exists():
print(f"Warning: no data found for {old['Destination']} in tar, skipping.")
continue
print(f"Restoring: {extracted} -> {new['Source']}")
shutil.copytree(extracted, new["Source"], dirs_exist_ok=True)
if args.start:
start_container(new_id)
return new_id
def start_container(container_id):
print(f"Starting container: {container_id[:12]}")
run(["docker", "start", container_id], capture=False)
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(description="Backup & restore Docker containers")
sub = parser.add_subparsers(dest="command", required=True)
b = sub.add_parser("backup", help="create a backup of a container")
b.add_argument("container", nargs="?", help="container ID or name")
b.add_argument("-a", "--all", action="store_true", help="back up all running containers")
b.add_argument("-s", "--stopped", action="store_true", help="with --all: also include stopped containers")
b.add_argument("-t", "--tar", action="store_true", help="create a .tar backup instead of json + copied mounts")
b.add_argument("-o", "--output", default="./backups", help="root directory for backups (default: ./backups)")
b.add_argument("-l", "--launch", default=None, help="external command after backup; %%tag and %%list are substituted")
b.add_argument("-v", "--verbose", action="store_true", help="print detailed progress")
r = sub.add_parser("restore", help="restore a backup")
r.add_argument("backup_file", help=".json or .tar backup file")
r.add_argument("-s", "--start", action="store_true", help="start the container after restoring")
args = parser.parse_args()
if args.command == "backup":
if args.all:
ps_cmd = ["docker", "ps", "-q"]
if args.stopped:
ps_cmd.append("-a")
ids = [i for i in run(ps_cmd).splitlines() if i]
for cid in ids:
# Isolate failures per container so one problematic container
# (e.g. one with a docker.sock bind mount, permission issues,
# or any other unexpected error) doesn't abort the whole
# --all run.
try:
do_backup(cid, args)
except Exception as e:
print(f"Error backing up container {cid}: {e}", file=sys.stderr)
continue
else:
if not args.container:
parser.error("provide a container ID/name, or use --all")
do_backup(args.container, args)
elif args.command == "restore":
do_restore(args.backup_file, args)
if __name__ == "__main__":
try:
main()
except RuntimeError as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)