litebackup 1.3.0-x86_64-darwin
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +109 -0
- data/lib/litebackup/bin/litebackup +4 -0
- data/lib/litebackup/binary.rb +108 -0
- data/lib/litebackup/client.rb +520 -0
- data/lib/litebackup/credential.rb +65 -0
- data/lib/litebackup/errors.rb +132 -0
- data/lib/litebackup/failover.rb +416 -0
- data/lib/litebackup/foreground.rb +185 -0
- data/lib/litebackup/generated/error_codes.rb +38 -0
- data/lib/litebackup/generated/models.rb +724 -0
- data/lib/litebackup/integrations/rails.rb +142 -0
- data/lib/litebackup/integrations/sqlite3.rb +55 -0
- data/lib/litebackup/integrations/sqlite_file.rb +81 -0
- data/lib/litebackup/launch.rb +146 -0
- data/lib/litebackup/paths.rb +86 -0
- data/lib/litebackup/pool.rb +790 -0
- data/lib/litebackup/release.rb +98 -0
- data/lib/litebackup/transport.rb +352 -0
- data/lib/litebackup/umask.rb +53 -0
- data/lib/litebackup/version.rb +8 -0
- data/lib/litebackup/wire.rb +436 -0
- data/lib/litebackup/wire_model.rb +55 -0
- data/lib/litebackup.rb +61 -0
- metadata +66 -0
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "pathname"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Litebackup
|
|
8
|
+
Durability = Models::Durability
|
|
9
|
+
|
|
10
|
+
# How a replica keeps the app from writing its file.
|
|
11
|
+
module WriteProtection
|
|
12
|
+
LOCK = Models::ReplicaRequest::WRITE_PROTECTION_LOCK
|
|
13
|
+
DETECT = Models::ReplicaRequest::WRITE_PROTECTION_DETECT
|
|
14
|
+
NONE = Models::ReplicaRequest::WRITE_PROTECTION_NONE
|
|
15
|
+
ALL = [LOCK, DETECT, NONE].freeze
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Request building and answer decoding, apart from the transport: every
|
|
19
|
+
# operation is a method returning a Call, the request to send and how to
|
|
20
|
+
# decode its answer.
|
|
21
|
+
module Wire
|
|
22
|
+
SDK_VERSION_HEADER = "Litebackup-SDK-Version"
|
|
23
|
+
CLIENT_VERSION_HEADER = "litebackup-client-version"
|
|
24
|
+
SDK_VERSION_VALUE = "ruby/#{VERSION}".freeze
|
|
25
|
+
# Added to a call's server-side timeout for its client-side deadline, so
|
|
26
|
+
# the daemon's own timeout answer arrives first.
|
|
27
|
+
DEADLINE_GRACE_SECONDS = 30.0
|
|
28
|
+
EVENT_WAIT_SECONDS = 30.0
|
|
29
|
+
FIRST_RETRY_SECONDS = 0.25
|
|
30
|
+
MAXIMUM_RETRY_SECONDS = 10.0
|
|
31
|
+
# Limits one answer.
|
|
32
|
+
RESPONSE_LIMIT = 64 << 20
|
|
33
|
+
|
|
34
|
+
ROLE_ORIGIN = Models::DatabaseStatus::ROLE_ORIGIN
|
|
35
|
+
STATE_OFFLOADED = Models::DatabaseStatus::STATE_OFFLOADED
|
|
36
|
+
STATE_CONFLICTED = Models::DatabaseStatus::STATE_CONFLICTED
|
|
37
|
+
JOURNAL_MODE_ROLLBACK = "rollback"
|
|
38
|
+
|
|
39
|
+
# One request and how to decode its answer. timeout_seconds is the
|
|
40
|
+
# server-side timeout, which the client waits for plus
|
|
41
|
+
# DEADLINE_GRACE_SECONDS; answer_deadline_seconds replaces that deadline,
|
|
42
|
+
# as a failover's role confirmation limit does.
|
|
43
|
+
Call = Data.define(:http_method, :path, :query, :body, :timeout_seconds, :decoder, :answer_deadline_seconds) do
|
|
44
|
+
def self.get(path, query = {}, decoder:, timeout_seconds: nil)
|
|
45
|
+
new(http_method: "GET", path: path, query: query, body: nil, timeout_seconds: timeout_seconds,
|
|
46
|
+
decoder: decoder, answer_deadline_seconds: nil)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.post(path, body, decoder:, timeout_seconds: nil)
|
|
50
|
+
new(http_method: "POST", path: path, query: {}, body: body, timeout_seconds: timeout_seconds,
|
|
51
|
+
decoder: decoder, answer_deadline_seconds: nil)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def target
|
|
55
|
+
query.empty? ? path : "#{path}?#{URI.encode_www_form(query)}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def deadline_seconds
|
|
59
|
+
return answer_deadline_seconds unless answer_deadline_seconds.nil?
|
|
60
|
+
|
|
61
|
+
timeout_seconds.nil? ? nil : timeout_seconds + DEADLINE_GRACE_SECONDS
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def encoded_body
|
|
65
|
+
body.nil? ? nil : JSON.generate(body)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
module_function
|
|
70
|
+
|
|
71
|
+
# A decoder into a generated model.
|
|
72
|
+
def model(model_class)
|
|
73
|
+
->(value) { model_class.from_wire(value) }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
NOTHING = ->(_value) {}
|
|
77
|
+
|
|
78
|
+
# Decodes an answer, raising Error for anything but 200.
|
|
79
|
+
def decode_answer(call, status, body)
|
|
80
|
+
raise response_error(status, body) unless status == 200
|
|
81
|
+
|
|
82
|
+
begin
|
|
83
|
+
call.decoder.call(JSON.parse(body.dup.force_encoding(Encoding::UTF_8)))
|
|
84
|
+
rescue JSON::ParserError, TypeError, KeyError, ArgumentError, NoMethodError => e
|
|
85
|
+
raise Error.new(ErrorCode::INVALID_RESPONSE, "decoding #{call.path}: #{e.message}", status)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# A non-200 answer: a JSON object's code, message, and remaining fields,
|
|
90
|
+
# or http_<status> with the text.
|
|
91
|
+
def response_error(status, body)
|
|
92
|
+
text = body.dup.force_encoding(Encoding::UTF_8).scrub
|
|
93
|
+
fallback = Error.new("http_#{status}", text.strip, status)
|
|
94
|
+
begin
|
|
95
|
+
decoded = JSON.parse(text)
|
|
96
|
+
rescue JSON::ParserError
|
|
97
|
+
return fallback
|
|
98
|
+
end
|
|
99
|
+
return fallback unless decoded.is_a?(Hash)
|
|
100
|
+
|
|
101
|
+
code = decoded.delete("code")
|
|
102
|
+
message = decoded.delete("message")
|
|
103
|
+
unless code.is_a?(String) && !code.empty?
|
|
104
|
+
return message.is_a?(String) ? Error.new("http_#{status}", message, status) : fallback
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
Error.new(code, message.is_a?(String) ? message : "", status, decoded)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Orders dotted numeric versions, ignoring any -suffix; parts that are not
|
|
111
|
+
# numbers count as zero.
|
|
112
|
+
def compare_versions(left, right)
|
|
113
|
+
parts = lambda { |version|
|
|
114
|
+
version.split("-", 2).first.to_s.split(".").map do |part|
|
|
115
|
+
part.match?(/\A\d+\z/) ? part.to_i : 0
|
|
116
|
+
end
|
|
117
|
+
}
|
|
118
|
+
left_parts = parts.call(left)
|
|
119
|
+
right_parts = parts.call(right)
|
|
120
|
+
length = [left_parts.length, right_parts.length].max
|
|
121
|
+
left_parts.fill(0, left_parts.length...length) <=> right_parts.fill(0, right_parts.length...length)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Refuses a daemon older than MINIMUM_DAEMON_VERSION, saying how to replace it.
|
|
125
|
+
def check_daemon_version(client_version, session)
|
|
126
|
+
return if !client_version.empty? && compare_versions(client_version, MINIMUM_DAEMON_VERSION) >= 0
|
|
127
|
+
|
|
128
|
+
reported = client_version.empty? ? "does not report its version" : "is version #{client_version}"
|
|
129
|
+
drain = session ? "litebackup drain --session #{session}" : "litebackup drain"
|
|
130
|
+
raise Error.new(
|
|
131
|
+
ErrorCode::DAEMON_UNSUPPORTED,
|
|
132
|
+
"the litebackup daemon #{reported}, and this SDK needs #{MINIMUM_DAEMON_VERSION} or later. " \
|
|
133
|
+
"Stop it with `#{drain}` so the next call starts the new binary, " \
|
|
134
|
+
"or restart the service if `litebackup install service` runs it"
|
|
135
|
+
)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# The connect probe every SDK sends.
|
|
139
|
+
def connect_probe
|
|
140
|
+
Call.get("/v1/status/show", { "limit" => "1" }, decoder: model(Models::Status))
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Whether an optional argument holds something: not nil and not "".
|
|
144
|
+
def supplied?(value)
|
|
145
|
+
!value.nil? && value != ""
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# A path as text: a String, or anything with to_path, such as a Pathname.
|
|
149
|
+
def path_text(source)
|
|
150
|
+
return source if source.is_a?(String)
|
|
151
|
+
return source.to_path if source.respond_to?(:to_path)
|
|
152
|
+
|
|
153
|
+
raise Litebackup.invalid_argument("expected a path, got #{source.class}")
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Resolves a path against the working directory and cleans it lexically;
|
|
157
|
+
# the daemon needs absolute paths. A ~ is a name like any other.
|
|
158
|
+
def absolute_path(source)
|
|
159
|
+
path = path_text(source)
|
|
160
|
+
path = File.join(Dir.pwd, path) unless path.start_with?("/")
|
|
161
|
+
Pathname.new(path).cleanpath.to_s
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# A duration in seconds, or nil when unset or zero, so the daemon's
|
|
165
|
+
# default applies. Durations are seconds as a Numeric, such as 90 or 1.5,
|
|
166
|
+
# or an ActiveSupport::Duration.
|
|
167
|
+
def seconds(duration, name)
|
|
168
|
+
return nil if duration.nil?
|
|
169
|
+
unless duration.is_a?(Numeric)
|
|
170
|
+
raise Litebackup.invalid_argument("#{name} must be a number of seconds, got #{duration.class}")
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
duration_seconds = duration.to_f
|
|
174
|
+
if duration_seconds.negative? || duration_seconds.nan?
|
|
175
|
+
raise Litebackup.invalid_argument("#{name} must not be negative")
|
|
176
|
+
end
|
|
177
|
+
raise Litebackup.invalid_argument("#{name} must be finite") if duration_seconds.infinite?
|
|
178
|
+
|
|
179
|
+
duration_seconds.zero? ? nil : duration_seconds
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def millis(duration_seconds)
|
|
183
|
+
duration_seconds.nil? ? nil : (duration_seconds * 1000).round
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# UTC with Z, fractional seconds only when they are not zero.
|
|
187
|
+
def wire_time(time)
|
|
188
|
+
time = parse_time(time) if time.is_a?(String)
|
|
189
|
+
time = time.to_time if !time.is_a?(Time) && time.respond_to?(:to_time)
|
|
190
|
+
raise Litebackup.invalid_argument("expected a Time, got #{time.class}") unless time.is_a?(Time)
|
|
191
|
+
|
|
192
|
+
utc = time.getutc
|
|
193
|
+
text = utc.strftime("%Y-%m-%dT%H:%M:%S")
|
|
194
|
+
text += ".#{format("%09d", utc.nsec).sub(/0+\z/, "")}" unless utc.nsec.zero?
|
|
195
|
+
"#{text}Z"
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def parse_time(text)
|
|
199
|
+
Time.iso8601(text)
|
|
200
|
+
rescue ArgumentError
|
|
201
|
+
raise Litebackup.invalid_argument("expected an ISO 8601 time with its zone, got #{text.inspect}")
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# A request body without its unset fields: nil, false, and "" are left out.
|
|
205
|
+
def compact_body(fields)
|
|
206
|
+
fields.reject { |_key, value| value.nil? || value == false || value == "" }
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# A page limit the daemon accepts: a whole number from 1 to 1000.
|
|
210
|
+
def page_limit(limit)
|
|
211
|
+
unless limit.is_a?(Integer) && limit.between?(1, 1000)
|
|
212
|
+
raise Litebackup.invalid_argument("the limit must be a whole number from 1 to 1000")
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
limit.to_s
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def status(after: "", limit: 100)
|
|
219
|
+
Call.get("/v1/status/show", { "after" => after.to_s, "limit" => page_limit(limit) }, decoder: model(Models::Status))
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def databases_page(after: "", limit: 100)
|
|
223
|
+
Call.get("/v1/databases/list", { "after" => after.to_s, "limit" => page_limit(limit) },
|
|
224
|
+
decoder: model(Models::DatabaseStatusList))
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def database(name)
|
|
228
|
+
Call.get("/v1/databases/status", { "name" => name }, decoder: model(Models::DatabaseStatus))
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def sync(name)
|
|
232
|
+
Call.post("/v1/databases/sync", { "name" => name }, decoder: model(Models::DatabaseStatus))
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def check_durability(durability)
|
|
236
|
+
return if durability.nil? || Durability::ALL.include?(durability)
|
|
237
|
+
|
|
238
|
+
raise Litebackup.invalid_argument("offload durability must be server or cloud")
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Checks an offload durability and returns it, or nil for the daemon's
|
|
242
|
+
# default, server.
|
|
243
|
+
def offload_durability_choice(durability)
|
|
244
|
+
check_durability(durability)
|
|
245
|
+
durability == Durability::SERVER ? nil : durability
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def offload(name, durability: nil)
|
|
249
|
+
check_durability(durability)
|
|
250
|
+
Call.post("/v1/databases/offload", { "name" => name }.merge(compact_body("durability" => durability)),
|
|
251
|
+
decoder: model(Models::DatabaseStatus))
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def writer(name)
|
|
255
|
+
Call.get("/v1/databases/writer", { "name" => name }, decoder: model(Models::Writer))
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def versions_page(name, after: 0, limit: nil)
|
|
259
|
+
query = { "name" => name }
|
|
260
|
+
query["after"] = after.to_s if after.positive?
|
|
261
|
+
query["limit"] = limit.to_s unless limit.nil?
|
|
262
|
+
Call.get("/v1/databases/versions", query, decoder: model(Models::VersionList))
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def restore(name, path:, version: nil, time: nil, umask: nil)
|
|
266
|
+
raise Litebackup.invalid_argument("restore needs a path to write to") unless supplied?(path)
|
|
267
|
+
raise Litebackup.invalid_argument("restore takes a version or a time, not both") if !version.nil? && !time.nil?
|
|
268
|
+
|
|
269
|
+
body = { "name" => name, "path" => absolute_path(path) }
|
|
270
|
+
body["version"] = version unless version.nil?
|
|
271
|
+
body["time"] = wire_time(time) unless time.nil?
|
|
272
|
+
body["umask"] = Umask.request_umask(umask)
|
|
273
|
+
Call.post("/v1/databases/restore", body, decoder: model(Models::RestoreResult))
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def revert(name, version: nil, time: nil)
|
|
277
|
+
raise Litebackup.invalid_argument("revert takes a version or a time, one of them") if version.nil? == time.nil?
|
|
278
|
+
|
|
279
|
+
body = { "name" => name }
|
|
280
|
+
unless version.nil?
|
|
281
|
+
unless version.is_a?(Integer) && version >= 1
|
|
282
|
+
raise Litebackup.invalid_argument("revert needs a positive whole version")
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
body["version"] = version
|
|
286
|
+
end
|
|
287
|
+
body["time"] = wire_time(time) unless time.nil?
|
|
288
|
+
Call.post("/v1/databases/revert", body, decoder: model(Models::RevertResult))
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def acquire(name, path: nil, maintenance: false, offload_on_release: false, offload_durability: Durability::SERVER,
|
|
292
|
+
machine_label: nil, timeout: nil, lock_timeout: nil, umask: nil, discard_local: false)
|
|
293
|
+
timeout_seconds = seconds(timeout, "the acquire timeout")
|
|
294
|
+
lock_timeout_seconds = seconds(lock_timeout, "the lock timeout")
|
|
295
|
+
# name is required, so it is sent even when empty and the daemon answers for it.
|
|
296
|
+
body = { "name" => name }.merge(compact_body(
|
|
297
|
+
"path" => supplied?(path) ? absolute_path(path) : nil,
|
|
298
|
+
"maintenance" => maintenance,
|
|
299
|
+
"discard_local" => discard_local,
|
|
300
|
+
"offload_on_release" => offload_on_release,
|
|
301
|
+
"offload_durability" => offload_durability_choice(offload_durability),
|
|
302
|
+
"machine_label" => machine_label,
|
|
303
|
+
"timeout_millis" => millis(timeout_seconds),
|
|
304
|
+
"lock_timeout_millis" => millis(lock_timeout_seconds)
|
|
305
|
+
))
|
|
306
|
+
body["umask"] = Umask.request_umask(umask)
|
|
307
|
+
# No timeout leaves the call without a deadline, as the daemon applies its own.
|
|
308
|
+
call_seconds = timeout_seconds.nil? ? nil : timeout_seconds + (lock_timeout_seconds || 0)
|
|
309
|
+
Call.post("/v1/databases/acquire", body, timeout_seconds: call_seconds, decoder: model(Models::AcquireResult))
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def release(name, durability: nil, offload: false, timeout: nil)
|
|
313
|
+
timeout_seconds = seconds(timeout, "the release timeout")
|
|
314
|
+
body = { "name" => name }.merge(compact_body(
|
|
315
|
+
"durability" => durability, "offload" => offload,
|
|
316
|
+
"timeout_millis" => millis(timeout_seconds)
|
|
317
|
+
))
|
|
318
|
+
Call.post("/v1/databases/release", body, timeout_seconds: timeout_seconds, decoder: model(Models::ReleaseResult))
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def start_backup(source, name: nil, prefix: nil, maximum_local_databases: nil,
|
|
322
|
+
offload_durability: Durability::SERVER, offload_rollback_journal: false)
|
|
323
|
+
raise Litebackup.invalid_argument("backup needs a path") unless supplied?(source)
|
|
324
|
+
raise Litebackup.invalid_argument("choose a name or a prefix") if supplied?(name) && supplied?(prefix)
|
|
325
|
+
|
|
326
|
+
body = compact_body(
|
|
327
|
+
"path" => absolute_path(source),
|
|
328
|
+
"name" => name,
|
|
329
|
+
"prefix" => prefix,
|
|
330
|
+
"maximum_local_databases" => maximum_local_databases == 0 ? nil : maximum_local_databases,
|
|
331
|
+
"offload_durability" => offload_durability_choice(offload_durability),
|
|
332
|
+
"offload_rollback_journal" => offload_rollback_journal
|
|
333
|
+
)
|
|
334
|
+
Call.post("/v1/backup-rules/start", body, decoder: model(Models::BackupRule))
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def stop_backup(name)
|
|
338
|
+
raise Litebackup.invalid_argument("stopping a backup needs a name") unless supplied?(name)
|
|
339
|
+
|
|
340
|
+
Call.post("/v1/backup-rules/stop", { "name" => name }, decoder: model(Models::StopBackupResult))
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def stop_rule(rule_id)
|
|
344
|
+
raise Litebackup.invalid_argument("stopping a rule needs its ID") unless supplied?(rule_id)
|
|
345
|
+
|
|
346
|
+
Call.post("/v1/backup-rules/stop", { "rule_id" => rule_id }, decoder: model(Models::StopBackupResult))
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
def list_rules
|
|
350
|
+
Call.get("/v1/backup-rules/list", decoder: ->(value) { Models::BackupList.from_wire(value).rules })
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
def start_replica(name, path: nil, write_protection: nil, timeout: nil)
|
|
354
|
+
timeout_seconds = seconds(timeout, "the replica timeout")
|
|
355
|
+
body = compact_body(
|
|
356
|
+
"name" => name, "path" => supplied?(path) ? absolute_path(path) : nil,
|
|
357
|
+
"write_protection" => write_protection, "timeout_millis" => millis(timeout_seconds)
|
|
358
|
+
)
|
|
359
|
+
decoder = lambda do |value|
|
|
360
|
+
started = Models::ReplicaResult.from_wire(value)
|
|
361
|
+
if started.database.nil?
|
|
362
|
+
raise Error.new(ErrorCode::INVALID_RESPONSE, "the daemon did not return the replica", 200)
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
started.database
|
|
366
|
+
end
|
|
367
|
+
Call.post("/v1/replicas/start", body, timeout_seconds: timeout_seconds, decoder: decoder)
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
def start_prefix_replica(prefix, directory:, write_protection: nil)
|
|
371
|
+
raise Litebackup.invalid_argument("replicating a prefix needs a directory") unless supplied?(directory)
|
|
372
|
+
|
|
373
|
+
body = compact_body("directory" => absolute_path(directory), "write_protection" => write_protection)
|
|
374
|
+
body = { "prefix" => prefix }.merge(body)
|
|
375
|
+
decoder = lambda do |value|
|
|
376
|
+
started = Models::ReplicaResult.from_wire(value)
|
|
377
|
+
if started.prefix_replica.nil?
|
|
378
|
+
raise Error.new(ErrorCode::INVALID_RESPONSE, "the daemon did not return the prefix replica", 200)
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
started.prefix_replica
|
|
382
|
+
end
|
|
383
|
+
Call.post("/v1/replicas/start", body, decoder: decoder)
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def list_replicas
|
|
387
|
+
Call.get("/v1/replicas/list", decoder: model(Models::ReplicaList))
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
def stop_replica(name)
|
|
391
|
+
Call.post("/v1/replicas/stop", { "name" => name }, decoder: NOTHING)
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
def stop_prefix_replica(prefix, directory:)
|
|
395
|
+
raise Litebackup.invalid_argument("stopping a prefix replica needs a directory") unless supplied?(directory)
|
|
396
|
+
|
|
397
|
+
Call.post("/v1/replicas/stop", { "prefix" => prefix, "directory" => absolute_path(directory) }, decoder: NOTHING)
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
def events_page(after: 0, wait: nil, limit: nil)
|
|
401
|
+
wait_seconds = seconds(wait, "the event wait")
|
|
402
|
+
query = {}
|
|
403
|
+
query["after"] = after.to_s unless after.zero?
|
|
404
|
+
query["wait_millis"] = millis(wait_seconds).to_s unless wait_seconds.nil?
|
|
405
|
+
query["limit"] = limit.to_s unless limit.nil? || limit.zero?
|
|
406
|
+
Call.get("/v1/events/list", query, timeout_seconds: wait_seconds, decoder: model(Models::EventList))
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
def shutdown(durability: nil, timeout: nil)
|
|
410
|
+
timeout_seconds = seconds(timeout, "the shutdown timeout")
|
|
411
|
+
body = compact_body("durability" => durability, "timeout_millis" => millis(timeout_seconds))
|
|
412
|
+
Call.post("/v1/session/shutdown", body, timeout_seconds: timeout_seconds, decoder: model(Models::ShutdownResult))
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
def start_failover(path, name: nil, timeout: nil, discard_local: false)
|
|
416
|
+
raise Litebackup.invalid_argument("failover needs a path") unless supplied?(path)
|
|
417
|
+
|
|
418
|
+
timeout_seconds = seconds(timeout, "the failover timeout")
|
|
419
|
+
body = compact_body(
|
|
420
|
+
"path" => absolute_path(path), "name" => name,
|
|
421
|
+
"timeout_millis" => millis(timeout_seconds), "discard_local" => discard_local
|
|
422
|
+
)
|
|
423
|
+
Call.post("/v1/failovers/start", body, timeout_seconds: timeout_seconds, decoder: model(Models::Failover))
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
# Errors that end an event watch instead of being retried.
|
|
427
|
+
TERMINAL_WATCH_CODES = [
|
|
428
|
+
ErrorCode::SDK_UNSUPPORTED, ErrorCode::CLOSED, ErrorCode::CANCELLED,
|
|
429
|
+
ErrorCode::INVALID_ARGUMENT, ErrorCode::CURSOR_EXPIRED
|
|
430
|
+
].freeze
|
|
431
|
+
|
|
432
|
+
def terminal_watch_error?(error)
|
|
433
|
+
TERMINAL_WATCH_CODES.include?(error.code)
|
|
434
|
+
end
|
|
435
|
+
end
|
|
436
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Litebackup
|
|
6
|
+
# Builds the generated models from decoded JSON. Unknown fields are
|
|
7
|
+
# ignored and missing ones get their type's zero value, so a newer daemon's
|
|
8
|
+
# answers keep decoding; enum fields keep values this SDK does not know.
|
|
9
|
+
module WireModel
|
|
10
|
+
# A model from a decoded JSON object.
|
|
11
|
+
def from_wire(value)
|
|
12
|
+
raise TypeError, "expected an object for #{name}, got #{value.class}" unless value.is_a?(Hash)
|
|
13
|
+
|
|
14
|
+
fields = self::WIRE_FIELDS.to_h do |field, (decoding, required)|
|
|
15
|
+
key = field.to_s
|
|
16
|
+
if value.key?(key)
|
|
17
|
+
[field, WireModel.decode_value(decoding, value[key])]
|
|
18
|
+
else
|
|
19
|
+
[field, required ? WireModel.zero_value(decoding) : nil]
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
new(**fields)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.decode_value(decoding, value)
|
|
26
|
+
return nil if value.nil?
|
|
27
|
+
|
|
28
|
+
case decoding
|
|
29
|
+
when :time then value.is_a?(String) ? Time.iso8601(value) : value
|
|
30
|
+
when :float then value.is_a?(Integer) ? value.to_f : value
|
|
31
|
+
when Array then decode_composite(decoding, value)
|
|
32
|
+
else value
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.decode_composite(decoding, value)
|
|
37
|
+
shape, inner = decoding
|
|
38
|
+
return Models.const_get(inner).from_wire(value) if shape == :model
|
|
39
|
+
raise TypeError, "expected a list, got #{value.class}" unless value.is_a?(Array)
|
|
40
|
+
|
|
41
|
+
value.map { |element| decode_value(inner, element) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.zero_value(decoding)
|
|
45
|
+
case decoding
|
|
46
|
+
when :string then ""
|
|
47
|
+
when :integer then 0
|
|
48
|
+
when :float then 0.0
|
|
49
|
+
when :boolean then false
|
|
50
|
+
when :hash then {}
|
|
51
|
+
when Array then decoding.first == :list ? [] : Models.const_get(decoding.last).from_wire({})
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
data/lib/litebackup.rb
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Back up, replicate, and restore SQLite databases with Litebackup.
|
|
4
|
+
#
|
|
5
|
+
# The client talks to the litebackup daemon of a session over its unix socket
|
|
6
|
+
# and starts it when it is not running, with the credential in
|
|
7
|
+
# LITEBACKUP_API_KEY:
|
|
8
|
+
#
|
|
9
|
+
# require "litebackup"
|
|
10
|
+
#
|
|
11
|
+
# Litebackup::Client.open do |litebackup|
|
|
12
|
+
# litebackup.backups.start("/var/lib/app/main.sqlite")
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# The sqlite3 gem's pool adapter is in litebackup/integrations/sqlite3, and
|
|
16
|
+
# the Rails integration in litebackup/integrations/rails.
|
|
17
|
+
|
|
18
|
+
require "fileutils"
|
|
19
|
+
require_relative "litebackup/version"
|
|
20
|
+
require_relative "litebackup/generated/error_codes"
|
|
21
|
+
require_relative "litebackup/wire_model"
|
|
22
|
+
require_relative "litebackup/generated/models"
|
|
23
|
+
require_relative "litebackup/errors"
|
|
24
|
+
require_relative "litebackup/umask"
|
|
25
|
+
require_relative "litebackup/wire"
|
|
26
|
+
require_relative "litebackup/credential"
|
|
27
|
+
require_relative "litebackup/paths"
|
|
28
|
+
require_relative "litebackup/binary"
|
|
29
|
+
require_relative "litebackup/launch"
|
|
30
|
+
require_relative "litebackup/transport"
|
|
31
|
+
require_relative "litebackup/foreground"
|
|
32
|
+
require_relative "litebackup/pool"
|
|
33
|
+
require_relative "litebackup/failover"
|
|
34
|
+
require_relative "litebackup/client"
|
|
35
|
+
require_relative "litebackup/release"
|
|
36
|
+
|
|
37
|
+
module Litebackup
|
|
38
|
+
# The socket API's types that the client's methods return, under the names
|
|
39
|
+
# every SDK exports.
|
|
40
|
+
EXPORTED_MODELS = %i[
|
|
41
|
+
AcquireResult ArchivedVersion BackupRule DatabaseStatus DatabaseStatusList DrainFailure Event EventList
|
|
42
|
+
PrefixReplica ReleaseResult ReplicaList RestorableVersion RestoreResult RevertResult ShutdownResult Status
|
|
43
|
+
StopBackupResult SyncedState VersionList Writer
|
|
44
|
+
].freeze
|
|
45
|
+
EXPORTED_MODELS.each { |model_name| const_set(model_name, Models.const_get(model_name)) }
|
|
46
|
+
|
|
47
|
+
# The litebackup binary a client starts the daemon with; see Binary.litebackup_binary.
|
|
48
|
+
def self.binary(environment = ENV, binary_path = nil)
|
|
49
|
+
Binary.litebackup_binary(environment, binary_path)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The one line that installs the litebackup command.
|
|
53
|
+
def self.install_command
|
|
54
|
+
Binary.install_command
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The session name and socket path a client uses.
|
|
58
|
+
def self.session_socket(session = nil, environment = ENV)
|
|
59
|
+
Paths.session_socket(session, environment.to_h)
|
|
60
|
+
end
|
|
61
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: litebackup
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.3.0
|
|
5
|
+
platform: x86_64-darwin
|
|
6
|
+
authors:
|
|
7
|
+
- Litebackup
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: 'A client of the litebackup daemon on this machine: continuous backups,
|
|
13
|
+
restores, replicas, leases of databases that move between machines, failovers, and
|
|
14
|
+
a Rails integration.'
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- LICENSE
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/litebackup.rb
|
|
22
|
+
- lib/litebackup/bin/litebackup
|
|
23
|
+
- lib/litebackup/binary.rb
|
|
24
|
+
- lib/litebackup/client.rb
|
|
25
|
+
- lib/litebackup/credential.rb
|
|
26
|
+
- lib/litebackup/errors.rb
|
|
27
|
+
- lib/litebackup/failover.rb
|
|
28
|
+
- lib/litebackup/foreground.rb
|
|
29
|
+
- lib/litebackup/generated/error_codes.rb
|
|
30
|
+
- lib/litebackup/generated/models.rb
|
|
31
|
+
- lib/litebackup/integrations/rails.rb
|
|
32
|
+
- lib/litebackup/integrations/sqlite3.rb
|
|
33
|
+
- lib/litebackup/integrations/sqlite_file.rb
|
|
34
|
+
- lib/litebackup/launch.rb
|
|
35
|
+
- lib/litebackup/paths.rb
|
|
36
|
+
- lib/litebackup/pool.rb
|
|
37
|
+
- lib/litebackup/release.rb
|
|
38
|
+
- lib/litebackup/transport.rb
|
|
39
|
+
- lib/litebackup/umask.rb
|
|
40
|
+
- lib/litebackup/version.rb
|
|
41
|
+
- lib/litebackup/wire.rb
|
|
42
|
+
- lib/litebackup/wire_model.rb
|
|
43
|
+
homepage: https://litebackup.com
|
|
44
|
+
licenses:
|
|
45
|
+
- MIT
|
|
46
|
+
metadata:
|
|
47
|
+
source_code_uri: https://github.com/litebackup/sdk
|
|
48
|
+
rubygems_mfa_required: 'true'
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: '3.2'
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubygems_version: 3.6.9
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: Back up, replicate, and restore SQLite databases with Litebackup
|
|
66
|
+
test_files: []
|