-
Notifications
You must be signed in to change notification settings - Fork 2
/
backuper.py
executable file
·375 lines (332 loc) · 12.6 KB
/
backuper.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
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
#!/usr/bin/python
import docker
import argparse
import sys
import pickle
import tarfile
import os
import re
import texttable
# from subprocess import call
from distutils.version import LooseVersion
import pprint
pp = pprint.PrettyPrinter(indent=4)
# Arguments parsing
argsparser = argparse.ArgumentParser(
description="backup/restore/list a container and its volumes")
subparsers = argsparser.add_subparsers(
help='sub-command help : ', dest="command")
listparser = subparsers.add_parser(
'list', help='Lists the volumes of the container')
listparser.add_argument(
"container", help="Name of the container")
backupparser = subparsers.add_parser(
'backup', help="Backups a container to a tar file")
backupparser.add_argument(
"-p", "--pausecontainer",
help="Should we stop the source container before extracting/saving "
"its volumes and restart it after backup (useful for files "
"to be closed prior the backup)"
"if docker is >=1.3, the pause/unpause system will be used"
"if docker is <1.3, a stop/restart command will be used (breaking links)",
default=False, action="store_true")
backupparser.add_argument(
"-i", "--includevolumes",
help="include volumes in backup (without this option only backups in "
"/var/lib/docker/vfs on host are backed up. The syntax is a string of "
"elements that will be matched against all volumes/bindings. Elements are "
"seperated by a coma ', ' and can be regex")
backupparser.add_argument(
"-s", "--storage",
help="where to store/restore data, defaults to current path (for BACKUP "
"running inside a container, this parameter isn't used)",
metavar="Absolute_Storage_Path")
backupparser.add_argument("container", help="Name of the container")
restoreparser = subparsers.add_parser(
'restore',
help='Restore a container from tar backup')
restoreparser.add_argument(
"-d", "--destcontainer",
help="name of the restored container, defaults to source container name",
metavar="destcontainername")
restoreparser.add_argument(
"-s", "--storage",
help="where to store/restore data, defaults to current path (for BACKUP "
"running inside a container, this parameter isn't used)",
metavar="Absolute_Storage_Path")
restoreparser.add_argument(
"-r", "--restoreinplace",
help="if the backed up container had mounted (bound) directories on host, "
"should we restore these bindings AND the data in it "
"(overwriting data on host maybe)",
default=False, action="store_true")
restoreparser.add_argument("container", help="Name of the container")
args = argsparser.parse_args()
# Initialize docker client
c = docker.Client(base_url='unix://var/run/docker.sock',
version='1.9',
timeout=30)
# Determines if we run within a docker container
# Might not be truly cleany as a way to check but it works ;)
def dockerized():
if 'docker' in open('/proc/1/cgroup').read():
return True
def is1_3(c):
docker_Version = c.version()['Version']
if LooseVersion(docker_Version) < LooseVersion("1.3.0"):
return True
else:
return True
# Currently unused, this sub seems self explanatory
def getowndockerid():
dockerid = ""
for line in open('/proc/1/cgroup'):
if "docker" in line:
dockerid = re.search(r".*/docker/(.*)$", line)
return dockerid.group(1)
if dockerid == "":
return False
# Returns the terminal size WxH
# Found on http: //stackoverflow.com/a/566752/2646228
def getTerminalSize():
import os
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
cr = struct.unpack(
'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
# Check if a container exists (running or not)
def check_container_exists(c, name):
containers = c.containers(all=True)
for i, container in enumerate(containers):
names = container['Names']
for j, n in enumerate(names):
if n == "/" + name:
return True
return False
# source container name
name = args.container
# Location of the tar files (for a container running)
datadir = "/backup"
if args.command == "backup":
if not check_container_exists(c, name):
print "Container " + name + " not found !"
sys.exit(3)
container = c.inspect_container(name)
container_name = container['Name']
container_tarfile = ""
volumes = container['Volumes']
if dockerized():
tar = tarfile.open(datadir + "/" + name + ".tar", "w: gz")
container_tarfile = datadir + "/" + name + ".tar"
else:
if args.storage:
tar = tarfile.open(args.storage + "/" + name + ".tar", "w:gz")
container_tarfile = args.storage + "/" + name + ".tar"
else:
tar = tarfile.open(name + ".tar", "w: gz")
container_tarfile = name + ".tar"
print "Backing up : " + container_name + " to : " + container_tarfile
pickle.dump(container, open("metadata", "wb"))
tar.add("metadata")
# Compute and prepare the volumes to backup
bkpvolumes = {}
for i, v in enumerate(volumes):
if args.includevolumes:
for j, r in enumerate(args.includevolumes.split(', ')):
if (re.search(str(r), v)) or (re.search(str(r), volumes[v])):
bkpvolumes[v] = volumes[v]
else:
if str(volumes[v]).find('/var/lib/docker/vfs/dir/') >= 0:
bkpvolumes[v] = volumes[v]
if not bkpvolumes:
print "No Volumes Selected !!!"
if args.includevolumes:
print "Please review your --includevolumes "
"option value : '" + args.includevolumes + "'"
else:
print "Please use the --includevolumes option"
print "or use the 'list' command to check your container's volumes"
sys.exit(4)
if args.pausecontainer:
if is1_3(c):
print "Pausing container " + name + " before backup as requested"
c.pause(name)
else:
print "Stopping container " + name + " before backup as requested"
c.stop(name)
c.wait(name)
for i, v in enumerate(bkpvolumes):
print v, bkpvolumes[v]
if dockerized():
tar.add(v)
else:
tar.add(bkpvolumes[v], v)
tar.close()
if args.pausecontainer:
if is1_3(c):
print "UNPausing container " + name + " ..."
c.unpause(name)
else:
print "Restarting container " + name + " ..."
c.restart(name)
elif args.command == "restore":
# third argument is the restored container name
destname = args.container
if args.destcontainer:
destname = args.destcontainer
if check_container_exists(c, destname):
print "Destination container : " + destname + " already exists ... "
"cannot continue !!"
sys.exit(2)
if dockerized() and not args.storage:
print "Restore Storage is missing !"
sys.exit(1)
container_tarfile = ""
if dockerized():
tar = tarfile.open(datadir + "/" + name + ".tar")
container_tarfile = datadir + "/" + name + ".tar"
else:
if args.storage:
tar = tarfile.open(args.storage + "/" + name + ".tar")
container_tarfile = args.storage + "/" + name + ".tar"
else:
tar = tarfile.open(name + ".tar")
container_tarfile = name + ".tar"
print "Restoring " + container_tarfile + " into " + destname
metadatafile = tar.extractfile("metadata")
metadata = pickle.load(metadatafile)
imagename = metadata["Config"]["Image"]
volumes = metadata['Volumes']
envs = metadata['Config']['Env']
ports = metadata['NetworkSettings']['Ports']
envlist = []
vlist = []
binds = {}
portslist = []
portsbindings = {}
# Re-inject Env Vars
if ports:
for i, v in enumerate(ports):
if v.split('/')[1] == 'tcp':
portslist.append(int(v.split('/')[0]))
elif v.split('/')[1] == 'udp':
portslist.append((int(v.split('/')[0]), 'udp'))
if ports[v] is list:
for j, p in enumerate(ports[v]):
print p['HostIp']
print p['HostPort']
if v.split('/')[1] == 'tcp':
portsbindings[int(p['HostPort'])] = (
p['HostIp'], v.split('/')[0])
elif v.split('/')[1] == 'udp':
portsbindings[p['HostPort'] + "/udp"] = (
p['HostIp'], v.split('/')[0])
else:
if v.split('/')[1] == 'tcp':
portsbindings[int(v.split('/')[0])] = ports[v]
elif v.split('/')[1] == 'udp':
portsbindings[v] = ports[v]
for i, v in enumerate(envs):
envlist.append(v)
# Preparing nice volumes restored output
table = texttable.Texttable()
table.set_cols_align(["l", "l"])
(cwidth, cheight) = getTerminalSize()
cwidth = (cwidth-8)/2
table.set_cols_width([cwidth, cwidth])
table.header(
["Mount point (in container)", "Bound to (on docker host)"])
for i, v in enumerate(volumes):
vlist.append(v)
# check if volume has a binding, and add it to bindings
# for inplace restore
if str(volumes[v]).find('/var/lib/docker/vfs/dir/') < 0:
if args.restoreinplace:
binding = {volumes[v]: {'bind': v}}
binds.update(binding)
restored_container = c.create_container(
imagename, tty=True, volumes=vlist,
environment=envlist, name=destname, ports=portslist)
print "Starting " + destname + " container first time to "
"fetch volumes information..."
c.start(restored_container, binds=binds, port_bindings=portsbindings)
# Recreate volumes_from (as it does not work when binds
# + volumes_from are used together
infodest = c.inspect_container(restored_container)
c.stop(restored_container)
print "Waiting " + destname + " container to stop ..."
c.wait(restored_container)
volumes = infodest['Volumes']
vlist = []
bindrestore = {}
for i, v in enumerate(volumes):
table.add_row([v, volumes[v]])
vlist.append(v)
binding = {volumes[v]: {'bind': v}}
bindrestore.update(binding)
print table.draw()
# Add tar storage to bindings list
if dockerized():
datadir = args.storage
bindrestore.update({str(datadir): {'bind': '/backup2'}})
else:
if args.storage:
bindrestore.update({args.storage: {'bind': '/backup2'}})
else:
bindrestore.update(
{str(os.path.dirname(
os.path.realpath(__file__))): {'bind': '/backup2'}})
restorer_container = c.create_container(
'ubuntu', detach=False, stdin_open=True, tty=True,
command="tar xvf /backup2/" + name + ".tar", volumes=vlist)
print "Starting Restoration container (" + restorer_container['Id'] + ")"
c.start(restorer_container, binds=bindrestore)
print "Waiting for the end of restore container ..."
c.wait(restorer_container)
print c.logs(restorer_container['Id'])
c.remove_container(restorer_container)
print "Starting " + destname + " container..."
c.start(restored_container, port_bindings=portsbindings)
elif args.command == "list":
if not check_container_exists(c, name):
print "Container " + name + " not found !"
sys.exit(3)
(cwidth, cheight) = getTerminalSize()
container = c.inspect_container(name)
container_name = container['Name']
container_tarfile = ""
volumes = container['Volumes']
if volumes:
print "Volumes on container " + name + " ..."
table = texttable.Texttable()
table.set_cols_align(["l", "l"])
cwidth = (cwidth-8)/2
table.set_cols_width([cwidth, cwidth])
table.header(
["Mount point (in container)", "Bound to (on docker host)"])
for i, v in enumerate(volumes):
table.add_row([v, volumes[v]])
print table.draw()
else:
print "You did not choose any action to be performed "
"[--backup|--restore|--list] !!!"
argsparser.print_help()
sys.exit()