-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
virtnbdbackup
executable file
·524 lines (474 loc) · 16.4 KB
/
virtnbdbackup
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/usr/bin/python3
"""
Copyright (C) 2023 Michael Ablassmeier <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import sys
import signal
import logging
import argparse
from typing import List
from datetime import datetime
from functools import partial
from concurrent.futures import ThreadPoolExecutor, as_completed
from nbd import __version__ as __nbdversion__
from libvirtnbdbackup import sighandle
from libvirtnbdbackup import argopt
from libvirtnbdbackup import __version__
from libvirtnbdbackup import virt
from libvirtnbdbackup.objects import DomainDisk
from libvirtnbdbackup.virt import checkpoint
from libvirtnbdbackup import output
from libvirtnbdbackup.output import stream
from libvirtnbdbackup import common as lib
from libvirtnbdbackup.logcount import logCount
from libvirtnbdbackup import exceptions
from libvirtnbdbackup.backup import partialfile
from libvirtnbdbackup.backup import job
from libvirtnbdbackup.backup import disk
from libvirtnbdbackup.backup import metadata
from libvirtnbdbackup.backup import check
from libvirtnbdbackup.ssh.exceptions import sshError
from libvirtnbdbackup.virt.exceptions import (
domainNotFound,
connectionFailed,
)
from libvirtnbdbackup.output.exceptions import OutputException
def main() -> None:
"""Handle backup operation and settings."""
parser = argparse.ArgumentParser(
description="Backup libvirt/qemu virtual machines",
epilog=(
"Examples:\n"
" # full backup of domain 'webvm' with all attached disks:\n"
"\t%(prog)s -d webvm -l full -o /backup/\n"
" # incremental backup:\n"
"\t%(prog)s -d webvm -l inc -o /backup/\n"
" # differential backup:\n"
"\t%(prog)s -d webvm -l diff -o /backup/\n"
" # full backup, exclude disk 'vda':\n"
"\t%(prog)s -d webvm -l full -x vda -o /backup/\n"
" # full backup, backup only disk 'vdb':\n"
"\t%(prog)s -d webvm -l full -i vdb -o /backup/\n"
" # full backup, compression enabled:\n"
"\t%(prog)s -d webvm -l full -z -o /backup/\n"
" # full backup, create archive:\n"
"\t%(prog)s -d webvm -l full -o - > backup.zip\n"
" # full backup of vm operating on remote libvirtd:\n"
"\t%(prog)s -U qemu+ssh://root@remotehost/system "
"--ssh-user root -d webvm -l full -o /backup/\n"
),
formatter_class=argparse.RawTextHelpFormatter,
)
opt = parser.add_argument_group("General options")
opt.add_argument("-d", "--domain", required=True, type=str, help="Domain to backup")
opt.add_argument(
"-l",
"--level",
default="copy",
choices=["copy", "full", "inc", "diff", "auto"],
type=str,
help="Backup level. (default: %(default)s)",
)
opt.add_argument(
"-t",
"--type",
default="stream",
type=str,
choices=["stream", "raw"],
help="Output type: stream or raw. (default: %(default)s)",
)
opt.add_argument(
"-r",
"--raw",
default=False,
action="store_true",
help="Include full provisioned disk images in backup. (default: %(default)s)",
)
opt.add_argument(
"-o", "--output", required=True, type=str, help="Output target directory"
)
opt.add_argument(
"-C",
"--checkpointdir",
required=False,
default=None,
type=str,
help="Persistent libvirt checkpoint storage directory",
)
opt.add_argument(
"--scratchdir",
default="/var/tmp",
required=False,
type=str,
help="Target dir for temporary scratch file. (default: %(default)s)",
)
opt.add_argument(
"-S",
"--start-domain",
default=False,
required=False,
action="store_true",
help="Start virtual machine if it is offline. (default: %(default)s)",
)
opt.add_argument(
"-i",
"--include",
default=None,
type=str,
help="Backup only disk with target dev name (-i vda)",
)
opt.add_argument(
"-x",
"--exclude",
default=None,
type=str,
help="Exclude disk(s) with target dev name (-x vda,vdb)",
)
opt.add_argument(
"-f",
"--socketfile",
default=f"/var/tmp/virtnbdbackup.{os.getpid()}",
type=str,
help="Use specified file for NBD Server socket (default: %(default)s)",
)
opt.add_argument(
"-n",
"--noprogress",
default=False,
help="Disable progress bar",
action="store_true",
)
opt.add_argument(
"-z",
"--compress",
default=False,
type=int,
const=2,
nargs="?",
help="Compress with lz4 compression level. (default: %(default)s)",
action="store",
)
opt.add_argument(
"-w",
"--worker",
type=int,
default=None,
help=(
"Amount of concurrent workers used "
"to backup multiple disks. (default: amount of disks)"
),
)
opt.add_argument(
"-F",
"--freeze-mountpoint",
type=str,
default=None,
help=(
"If qemu agent available, freeze only filesystems on specified mountpoints within"
" virtual machine (default: all)"
),
)
opt.add_argument(
"-e",
"--strict",
default=False,
help=(
"Change exit code if warnings occur during backup operation. "
"(default: %(default)s)"
),
action="store_true",
)
opt.add_argument(
"-T",
"--threshold",
type=int,
default=None,
help=("Execute backup only if threshold is reached."),
)
remopt = parser.add_argument_group("Remote Backup options")
argopt.addRemoteArgs(remopt)
logopt = parser.add_argument_group("Logging options")
logopt.add_argument(
"-L",
"--syslog",
default=False,
action="store_true",
help="Additionally send log messages to syslog (default: %(default)s)",
)
logopt.add_argument(
"--quiet",
default=False,
action="store_true",
help="Disable logging to stderr (default: %(default)s)",
)
argopt.addLogColorArgs(logopt)
debopt = parser.add_argument_group("Debug options")
debopt.add_argument(
"-q",
"--qemu",
default=False,
action="store_true",
help="Use Qemu tools to query extents.",
)
debopt.add_argument(
"-s",
"--startonly",
default=False,
help="Only initialize backup job via libvirt, do not backup any data",
action="store_true",
)
debopt.add_argument(
"-k",
"--killonly",
default=False,
help="Kill any running block job",
action="store_true",
)
debopt.add_argument(
"-p",
"--printonly",
default=False,
help="Quit after printing estimated checkpoint size.",
action="store_true",
)
argopt.addDebugArgs(debopt)
repository = output.target()
args = lib.argparse(parser)
lib.setThreadName()
args.stdout = args.output == "-"
args.sshClient = None
args.diskInfo = []
args.offline = False
if args.quiet is True:
args.noprogress = True
fileStream = stream.get(args, repository)
try:
if not args.stdout:
fileStream.create(args.output)
except OutputException as e:
logging.error("Can't open output file: [%s]", e)
sys.exit(1)
if args.worker is not None and args.worker < 1:
args.worker = 1
now = datetime.now().strftime("%m%d%Y%H%M%S")
logFile = f"{args.output}/backup.{args.level}.{now}.log"
fileLog = lib.getLogFile(logFile) or sys.exit(1)
counter = logCount() # pylint: disable=unreachable
lib.configLogger(args, fileLog, counter)
lib.printVersion(__version__)
logging.info("Backup level: [%s]", args.level)
if args.compress is not False:
logging.info("Compression enabled, level [%s]", args.compress)
if args.raw is True and args.level in ("inc", "diff"):
logging.warning(
"Raw disks can't be included during incremental or differential backup."
)
logging.warning("Excluding raw disks.")
args.raw = False
try:
check.arguments(args)
except exceptions.BackupException as e:
logging.error(e)
sys.exit(1)
if partialfile.exists(args):
sys.exit(1)
if not args.checkpointdir:
args.checkpointdir = f"{args.output}/checkpoints"
else:
logging.info("Store checkpoints in: [%s]", args.checkpointdir)
fileStream.create(args.checkpointdir)
def connectionError(_, reason, args):
"""Callback if the libvirt connection drops mid
data transfer, used to potentially cleanup the
leftover backup job.
"""
virConnectCloseReason = (
"Misc I/O error",
"End-of-file from server",
"Keepalive timer triggered",
"Client side connection close",
"Unknown",
)
logging.error(
"Libvirt connection error [%s], trying to reconnect",
virConnectCloseReason[reason],
)
try:
virtClient = virt.client(args)
except connectionFailed as e:
logging.error("Unrecoverable connection error: %s", e)
sys.exit(1)
domObj = virtClient.getDomain(args.domain)
if not args.offline:
logging.error("Attempting to stop backup task")
virtClient.stopBackup(domObj)
sys.exit(1)
try:
virtClient = virt.client(args)
domObj = virtClient.getDomain(args.domain)
except domainNotFound as e:
logging.error("%s", e)
sys.exit(1)
except connectionFailed as e:
logging.error("Can't connect libvirt daemon: [%s]", e)
sys.exit(1)
virtClient._conn.registerCloseCallback( # pylint: disable=W0212
connectionError, args
)
logging.info("Libvirt library version: [%s]", virtClient.libvirtVersion)
logging.info("NBD library version: [%s]", __nbdversion__)
try:
check.vmfeature(virtClient, domObj)
checkpoint.checkForeign(args, domObj)
check.vmstate(args, virtClient, domObj)
check.targetDir(args)
except exceptions.BackupException as e:
logging.error(e)
sys.exit(1)
except exceptions.CheckpointException:
sys.exit(1)
signal.signal(
signal.SIGINT,
partial(sighandle.Backup.catch, args, domObj, virtClient, logging),
)
vmConfig = virtClient.getDomainConfig(domObj)
disks: List[DomainDisk] = virtClient.getDomainDisks(args, vmConfig)
args.info = virtClient.getDomainInfo(vmConfig)
if virtClient.getTPMDevice(vmConfig):
logging.warning("Emulated TPM device attached: User action required.")
logging.warning(
"Please manually backup contents of: [/var/lib/libvirt/swtpm/%s/]",
domObj.UUIDString(),
)
try:
check.diskformat(args, disks)
except exceptions.BackupException as e:
logging.info(e)
if not disks:
logging.error("Unable to detect disks suitable for backup.")
metadata.saveFiles(args, vmConfig, disks, fileStream, logFile)
sys.exit(1)
try:
check.blockjobs(args, virtClient, domObj, disks)
except exceptions.BackupException as e:
logging.error(e)
sys.exit(1)
logging.info(
"Backup will save [%s] attached disks.",
len(disks),
)
if args.worker is None or args.worker > int(len(disks)):
args.worker = int(len(disks))
logging.info("Concurrent backup processes: [%s]", args.worker)
if args.killonly is True:
logging.info("Stopping backup job")
if not virtClient.stopBackup(domObj):
sys.exit(1)
sys.exit(0)
try:
checkpoint.create(args, domObj)
except exceptions.CheckpointException as errmsg:
logging.error(errmsg)
sys.exit(1)
if args.printonly and args.cpt.parent and not args.offline:
size = checkpoint.getSize(domObj, args.cpt.parent)
logging.info("Estimated checkpoint backup size: [%s] Bytes", size)
sys.exit(0)
if args.threshold and args.cpt.parent and not args.offline:
size = checkpoint.getSize(domObj, args.cpt.parent)
if size < args.threshold:
logging.info(
"Backup size [%s] does not meet required threshold [%s], skipping backup.",
size,
args.threshold,
)
sys.exit(0)
if virtClient.remoteHost != "":
args.sshClient = lib.sshSession(args, virtClient.remoteHost)
if not args.sshClient:
logging.error("Remote backup detected but ssh session setup failed")
sys.exit(1)
logging.info(
"Remote NBD Endpoint host: [%s]",
virtClient.remoteHost,
)
if args.offline is True:
logging.info(
"Remote ports used for backup: [%s-%s]",
args.nbd_port,
args.nbd_port + args.worker,
)
else:
logging.info("Local NBD Endpoint socket: [%s]", args.socketfile)
if args.offline is not True:
logging.info("Temporary scratch file target directory: [%s]", args.scratchdir)
fileStream.create(args.scratchdir)
if not job.start(args, virtClient, domObj, disks):
sys.exit(1)
if args.level not in ("copy", "diff") and args.offline is False:
logging.info("Started backup job with checkpoint, saving information.")
try:
checkpoint.save(args)
except exceptions.CheckpointException as e:
logging.error("Extending checkpoint file failed: [%s]", e)
sys.exit(1)
if not checkpoint.backup(args, domObj):
virtClient.stopBackup(domObj)
sys.exit(1)
if args.startonly is True:
logging.info("Started backup job for debugging, exiting.")
sys.exit(0)
backupSize: int = 0
try:
with ThreadPoolExecutor(max_workers=args.worker) as executor:
futures = {
executor.submit(
disk.backup, args, Disk, count, fileStream, virtClient
): Disk
for count, Disk in enumerate(disks)
}
for future in as_completed(futures):
size, state = future.result()
backupSize += size
if state is not True:
raise exceptions.DiskBackupFailed("Backup of one disk failed")
except exceptions.BackupException as e:
logging.error("Disk backup failed: [%s]", e)
except sshError as e:
logging.error("Remote Disk backup failed: [%s]", e)
except Exception as e: # pylint: disable=broad-except
logging.critical("Unknown Exception during backup: %s", e)
logging.exception(e)
if args.offline is False:
logging.info("Backup jobs finished, stopping backup task.")
virtClient.stopBackup(domObj)
virtClient.close()
metadata.saveFiles(args, vmConfig, disks, fileStream, logFile)
if domObj.autostart() == 1:
metadata.backupAutoStart(args)
if counter.count.errors > 0:
logging.error("Error during backup")
sys.exit(1)
if args.sshClient:
args.sshClient.disconnect()
if counter.count.warnings > 0 and args.strict is True:
logging.info(
"[%s] Warnings detected during backup operation, forcing exit code 2",
counter.count.warnings,
)
sys.exit(2)
logging.info("Total saved disk data: [%s]", lib.humanize(backupSize))
logging.info("Finished successfully")
if __name__ == "__main__":
main()