-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathzsnap.py
executable file
·183 lines (163 loc) · 5.6 KB
/
zsnap.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
# -*- coding: utf-8 -*-
import sys, os
sys.path.append(
os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.pardir, "src")
)
import optparse
import time
from zfs_tools.models import Dataset, Pool, Snapshot, PoolSet
from zfs_tools.connection import ZFSConnection
from zfs_tools.util import stderr, verbose_stderr, set_verbose
def main():
# ===================== configuration =====================
parser = optparse.OptionParser("usage: %prog [-k NUMSNAPS] <datasetname>")
parser.add_option(
"-k",
"--keep",
action="store",
dest="keep",
default=7,
help="how many snapshots to keep (default: %default), 0 means delete all",
)
parser.add_option(
"-p",
"--prefix",
action="store",
dest="prefix",
default="autosnapshot-",
help="prefix to prepend to snapshot names (default: %default)",
)
parser.add_option(
"-P",
"--property",
action="store",
dest="property",
help="property (name=value) to apply to snapshots",
)
parser.add_option(
"-v",
"--verbose",
action="store_true",
dest="verbose",
default=False,
help="be verbose (default: %default)",
)
parser.add_option(
"-t",
"--timeformat",
action="store",
dest="timeformat",
default="%Y-%m-%d-%H%M%S",
help="postfix time format to append to snapshot names (default: %default, MUST be sortable using a general sort)",
)
parser.add_option(
"--utc",
action="store_true",
dest="utc",
default=False,
help="Use UTC for timestamps (default: no)",
)
parser.add_option(
"-n",
"--dry-run",
action="store_true",
dest="dryrun",
default=False,
help="don't actually manipulate any file systems",
)
parser.add_option(
"--nosnapshot",
action="store_true",
dest="nosnapshot",
default=False,
help="don't create new snapshot, only delete according to keep value (default: %default)",
)
parser.add_option(
"-w",
"--warnondestroyfailure",
action="store_true",
dest="warnondestroyfailure",
default=False,
help="warn rather than abort on destroy failure (default: %default)",
)
parser.epilog = """The --prefix and --property options are also used (if specified) to filter the snapshots to delete.
That is, only those snapshots that start with PREFIX and have a property named NAME with a value of
VALUE will be considered for deletion."""
opts, args = parser.parse_args(sys.argv[1:])
try:
keep = int(opts.keep)
assert keep >= 0
except (ValueError, AssertionError) as e:
parser.error("keep must be greater than 0")
sys.exit(os.EX_USAGE)
if len(args) == 1:
try:
source_host, source_dataset_name = args[0].split(":", 1)
except ValueError:
source_host, source_dataset_name = "localhost", args[0]
else:
parser.error("arguments are wrong")
sys.exit(os.EX_USAGE)
set_verbose(opts.verbose)
snapshot_prefix = opts.prefix
snapshot_postfix = lambda: time.strftime(
opts.timeformat, time.gmtime() if opts.utc else time.localtime()
)
snapshot_properties = {}
if opts.property:
split = opts.property.split("=", 1)
if len(split) != 2:
parser.error("--property should be of the form name=value")
sys.exit(os.EX_USAGE)
snapshot_properties[split[0]] = split[1]
# ===================== end configuration =================
# ================ start program algorithm ===================
src_conn = ZFSConnection(
source_host, subset=source_dataset_name, properties=snapshot_properties.keys()
)
snapshot_unique_name = snapshot_prefix + snapshot_postfix()
flt = lambda x: x.name.startswith(snapshot_prefix) and (
not snapshot_properties
or x.get_property(snapshot_properties.keys()[0])
== snapshot_properties.values()[0]
)
verbose_stderr("Assessing that the specified dataset exists...")
try:
source_dataset = src_conn.pools.lookup(source_dataset_name)
verbose_stderr("%s: OK" % source_dataset)
except KeyError:
verbose_stderr(
"No.\nError: the source dataset does not exist. Snapshot cannot continue."
)
sys.exit(2)
if keep > 0 and not opts.nosnapshot:
verbose_stderr(
"Snapshotting dataset %s:%s as %s"
% (source_host, source_dataset_name, snapshot_unique_name)
)
if not opts.dryrun:
src_conn.snapshot_recursively(
source_dataset_name, snapshot_unique_name, snapshot_properties
)
# FIXME: what follows is retarded design
src_conn.pools # trigger update
ssn = sorted(
[
(x.get_property("creation"), x.name, x)
for x in source_dataset.get_snapshots(flt)
]
)
if opts.dryrun and keep > 0 and not opts.nosnapshot:
# simulate the addition of a new dataset
keep = keep - 1
if keep > 0:
destroy_ssn = ssn[:-keep]
else:
destroy_ssn = ssn
for x in destroy_ssn:
path = x[-1].get_path()
verbose_stderr("Destroying obsolete snapshot: %s" % path)
if not opts.dryrun:
ok = src_conn.destroy_recursively(path, returnok=opts.warnondestroyfailure)
if not ok:
stderr("Failed to destroy obsolete snapshot: %s" % path)