-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
qmprestore
executable file
·172 lines (153 loc) · 4.58 KB
/
qmprestore
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
#!/usr/bin/env python3
"""
qmpbackup: Full an incremental backup using Qemus
dirty bitmap feature
Copyright (C) 2022 Michael Ablassmeier
Authors:
Michael Ablassmeier <[email protected]>
This work is licensed under the terms of the GNU GPL, version 3. See
the LICENSE file in the top-level directory.
"""
import os
import sys
import argparse
from libqmpbackup import version
from libqmpbackup import image
from libqmpbackup import lib
parser = argparse.ArgumentParser(prog=sys.argv[0])
subparsers = parser.add_subparsers(help="sub-command help")
parser_snapshot = subparsers.add_parser("snapshotrebase", help="snapshotrebase")
parser_rebase = subparsers.add_parser("rebase", help="rebase")
parser_merge = subparsers.add_parser("merge", help="merge")
parser_commit = subparsers.add_parser("commit", help="commit")
parser_rebase.set_defaults(which="rebase")
parser_rebase.add_argument(
"--dir", type=str, help="directory which contains images", required=True
)
parser_snapshot.set_defaults(which="snapshotrebase")
parser_commit.set_defaults(which="commit")
parser_snapshot.add_argument(
"--dir", type=str, help="directory which contains images", required=True
)
parser_snapshot.add_argument(
"--until",
type=str,
help="point in time restore until specified backup file",
required=False,
)
parser_snapshot.add_argument(
"--dry-run",
action="store_true",
help="do not run commands, only show them",
required=False,
)
parser_snapshot.add_argument(
"--filter",
type=str,
help="only process images matching string",
required=False,
default="",
)
parser_commit.add_argument(
"--dir", type=str, help="directory which contains images", required=True
)
parser_commit.add_argument(
"--until",
type=str,
help="point in time restore until specified backup file",
required=False,
)
parser_commit.add_argument(
"--dry-run",
action="store_true",
help="do not run commands, only show them",
required=False,
)
parser_commit.add_argument(
"--filter",
type=str,
help="only process images matching string",
required=False,
default="",
)
parser_merge.set_defaults(which="merge")
parser_merge.add_argument(
"--dir", type=str, help="directory which contains images", required=True
)
parser_merge.add_argument(
"--targetfile",
type=str,
help="Restore image to specified target file",
required=True,
)
parser_merge.add_argument(
"--until",
type=str,
help="point in time restore until specified backup file",
required=False,
)
parser_merge.add_argument(
"--filter",
type=str,
help="only process images matching string",
required=False,
default="",
)
parser_rebase.add_argument(
"--until",
type=str,
help="point in time restore until specified backup file",
required=False,
)
parser_rebase.add_argument(
"--dry-run",
action="store_true",
help="do not run commands, only show them",
required=False,
)
parser_rebase.add_argument(
"--filter",
type=str,
help="only process images matching string",
required=False,
default="",
)
argv = parser.parse_args()
try:
action = argv.which
except AttributeError:
parser.print_help()
sys.exit(1)
log = lib.setup_log(False)
log.info("Version: %s Arguments: %s", version.VERSION, " ".join(sys.argv))
if not os.path.exists(argv.dir):
log.error("Specified target folder does not exist: [%s]", argv.dir)
sys.exit(1)
if argv.filter != "":
log.info("Filter images in [%s] based on string: [%s]", argv.dir, argv.filter)
if action in ("rebase", "snapshotrebase", "commit"):
if argv.dry_run:
log.info("Dry run activated, not applying any changes")
if action == "merge":
log.info("Merging images in source folder: [%s] to [%s]", argv.dir, argv.targetfile)
if not os.path.exists(os.path.dirname(argv.targetfile)):
os.makedirs(os.path.dirname(argv.targetfile), exist_ok=True)
if image.merge(argv):
log.info("Image file merge successful.")
sys.exit(0)
if action == "rebase":
log.info("Rebasing images in source folder: [%s]", argv.dir)
if image.rebase(argv):
log.info("Image file rebase successful.")
sys.exit(0)
if action == "snapshotrebase":
log.info("Rebasing using snapshot images in source folder: [%s]", argv.dir)
if image.snapshot_rebase(argv):
log.info("Image file rebase successful.")
sys.exit(0)
if action == "commit":
log.info("Rebasing and committing images in source folder: [%s]", argv.dir)
if image.commit(argv):
log.info("Image file rebase and commit successful.")
sys.exit(0)
sys.exit(1)