/
backup-manager.cc
445 lines (374 loc) · 11.8 KB
/
backup-manager.cc
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
// Copyright (c) 2013, Cornell University
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of HyperDex nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// C
#include <cstdlib>
// POSIX
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
// STL
#include <string>
// po6
#include <po6/io/fd.h>
// HyperDex
#include <hyperdex/admin.hpp>
#include "tools/common.h"
using hyperdex::connect_opts;
struct daemon_backup
{
daemon_backup() : sid(), addr(), path() {}
daemon_backup(uint64_t s,
const std::string& a,
const std::string& p)
: sid(s), addr(a), path(p) {}
~daemon_backup() {}
uint64_t sid;
std::string addr;
std::string path;
};
static bool
get_time(std::string* now)
{
time_t tmp_t;
struct tm* tmp_tm;
tmp_t = time(NULL);
tmp_tm = localtime(&tmp_t);
if (tmp_tm == NULL)
{
perror("could not read time");
return false;
}
char tmp[1024];
if (strftime(tmp, 1024, "%Y-%m-%dT%H:%M:%S", tmp_tm) == 0)
{
std::cerr << "could not read time" << std::endl;
return false;
}
*now = std::string(tmp);
return true;
}
static bool
take_backup(const connect_opts& conn,
const char* name,
std::vector<daemon_backup>* daemons)
{
hyperdex::Admin h(conn.host(), conn.port());
hyperdex_admin_returncode rrc;
const char* backups = NULL;
int64_t rid = h.backup(name, &rrc, &backups);
if (rid < 0)
{
std::cerr << "Backup failed.\n"
<< "Please read the above messages for the cause and potential solutions\n";
return false;
}
hyperdex_admin_returncode lrc;
int64_t lid = h.loop(-1, &lrc);
if (lid < 0)
{
std::cerr << "Backup failed.\n"
<< "Please read the above messages for the cause and potential solutions\n";
return false;
}
assert(rid == lid);
if (rrc != HYPERDEX_ADMIN_SUCCESS)
{
std::cerr << "backup failed: " << h.error_message() << std::endl;
return false;
}
if (!backups)
{
return true;
}
const char* ptr = backups;
const char* end = ptr + strlen(ptr);
while (ptr < end)
{
const char* eol = strchr(ptr, '\n');
eol = eol ? eol : end;
std::string line(ptr, eol);
ptr = eol + 1;
long long unsigned int num;
std::vector<char> loc_buf(line.size());
std::vector<char> path_buf(line.size());
if (sscanf(line.c_str(), "%llu %s %[^\t\n]",
&num, &loc_buf[0], &path_buf[0]) < 3)
{
std::cerr << "could not parse backup information for daemons:\n"
<< backups << std::flush;
return false;
}
daemons->push_back(daemon_backup(num,
std::string(&loc_buf[0]),
std::string(&path_buf[0])));
}
return true;
}
static bool
read_latest(const std::string& base, bool* has_previous, std::string* previous)
{
std::string latest_path = po6::path::join(base, "LATEST");
po6::io::fd latest(open(latest_path.c_str(), O_RDONLY));
if (latest.get() < 0 && errno == ENOENT)
{
*has_previous = false;
return true;
}
else if (latest.get() < 0)
{
return false;
}
std::vector<char> buf(PATH_MAX);
ssize_t amt = latest.xread(&buf[0], buf.size());
if (amt < 0)
{
std::cerr << "could not read the LATEST backup: "
<< strerror(errno) << std::endl;
return false;
}
buf.resize(amt);
*has_previous = true;
*previous = po6::path::join(base, std::string(buf.begin(), buf.end()));
return true;
}
static bool
record_latest(const std::string& base, const std::string& now)
{
std::string latest_path = po6::path::join(base, "LATEST");
po6::io::fd latest(open(latest_path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR));
if (latest.get() < 0)
{
std::cerr << "could not record the backup in LATEST: "
<< strerror(errno) << std::endl;
return false;
}
if (latest.xwrite(now.c_str(), now.size()) < static_cast<ssize_t>(now.size()))
{
std::cerr << "could not record the backup in LATEST: "
<< strerror(errno) << std::endl;
return false;
}
latest.close();
return true;
}
static bool
fork_exec_wait(const std::vector<std::string>& args)
{
pid_t child = fork();
if (child > 0)
{
int status = 0;
if (waitpid(child, &status, 0) < 0)
{
std::cerr << "could not wait for child: " << strerror(errno) << std::endl;
return false;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
{
std::cerr << "child process failed" << std::endl;
return false;
}
return true;
}
else if (child == 0)
{
std::vector<const char*> arg_ptrs;
arg_ptrs.reserve(args.size() + 1);
for (size_t i = 0; i < args.size(); ++i)
{
arg_ptrs.push_back(args[i].c_str());
}
arg_ptrs.push_back(NULL);
execvp(arg_ptrs[0], const_cast<char* const*>(&arg_ptrs[0]));
std::cerr << "could not exec: " << strerror(errno) << std::endl;
exit(EXIT_FAILURE);
}
else
{
std::cerr << "could not fork: " << strerror(errno) << std::endl;
return false;
}
}
int
main(int argc, const char* argv[])
{
bool _cleanup = true;
const char* _data = ".";
const char* _user = NULL;
connect_opts conn;
e::argparser ap;
ap.autohelp();
ap.add("Connect to a cluster:", conn.parser());
ap.arg().name('b', "backup-dir")
.description("store backups in this directory (default: .)")
.metavar("dir").as_string(&_data);
ap.arg().name('u', "user")
.description("username to use for ssh connections (default: this user)")
.metavar("user").as_string(&_user);
if (!ap.parse(argc, argv))
{
return EXIT_FAILURE;
}
if (!conn.validate())
{
std::cerr << "invalid host:port specification\n" << std::endl;
ap.usage();
return EXIT_FAILURE;
}
using po6::path::join;
try
{
bool success = true;
std::string base(_data);
if (!po6::path::realpath(base, &base))
{
std::cerr << "error: " << po6::strerror(errno) << std::endl;
return EXIT_FAILURE;
}
if (chdir(base.c_str()) < 0)
{
std::cerr << "could not change directory to: " << _data << std::endl;
return EXIT_FAILURE;
}
std::string now;
std::vector<daemon_backup> daemons;
if (!get_time(&now))
{
return EXIT_FAILURE;
}
if (!take_backup(conn, now.c_str(), &daemons))
{
return EXIT_FAILURE;
}
bool has_previous = false;
std::string previous;
if (!read_latest(base, &has_previous, &previous))
{
return EXIT_FAILURE;
}
std::string backupdir(join(base, now));
if (mkdir(backupdir.c_str(), S_IRWXU) < 0)
{
std::cerr << "could not make local directory for the backup: "
<< strerror(errno) << std::endl;
return EXIT_FAILURE;
}
if (rename(join(base, now + ".coordinator.bin").c_str(),
join(backupdir, "coordinator.bin").c_str()) < 0)
{
std::cerr << "could not rename coordinator backup: "
<< strerror(errno) << std::endl;
return EXIT_FAILURE;
}
for (size_t i = 0; i < daemons.size(); ++i)
{
char buf[21];
sprintf(buf, "%lu", daemons[i].sid);
std::string daemon_dir(join(base, now, buf));
bool prev = false;
std::string link_dest;
if (has_previous)
{
struct stat stbuf;
std::string daemon_prev(join(previous, buf));
int status = stat(daemon_prev.c_str(), &stbuf);
if (status < 0 && errno == ENOENT)
{
prev = false;
}
else if (status < 0)
{
prev = false;
std::cerr << "could not stat prev backup for " << daemons[i].sid << ": "
<< strerror(errno) << std::endl;
success = false;
}
else
{
prev = true;
link_dest = "--link-dest=";
link_dest += daemon_prev.c_str();
}
}
std::vector<std::string> args;
args.push_back("rsync");
args.push_back("-a");
args.push_back("--delete");
if (prev)
{
args.push_back(link_dest);
}
args.push_back("--");
std::string rsync_url = daemons[i].addr + ":" + daemons[i].path + "/";
if (_user)
{
args.push_back(std::string(_user) + "@" + rsync_url);
}
else
{
args.push_back(rsync_url);
}
args.push_back(daemon_dir.c_str());
if (!fork_exec_wait(args))
{
success = false;
}
}
if (success && _cleanup)
{
for (size_t i = 0; i < daemons.size(); ++i)
{
std::vector<std::string> args;
args.push_back("ssh");
if (_user)
{
args.push_back("-l");
args.push_back(_user);
}
args.push_back(daemons[i].addr);
args.push_back("rm");
args.push_back("-r");
args.push_back("--");
args.push_back(daemons[i].path);
if (!fork_exec_wait(args))
{
success = false;
}
}
}
if (success && !record_latest(base, now))
{
success = false;
}
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
catch (std::exception& e)
{
std::cerr << "error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}