Debian Codegraph @

build_vxworks/util/db_hotbackup.c

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * $Id$
7 */
8
9 #include "db_config.h"
10
11 #include "db_int.h"
12 #include "dbinc/log.h"
13 #include "dbinc/db_page.h"
14 #include "dbinc/qam.h"
15
16 #ifndef lint
17 static const char copyright[] =
18 "Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.\n";
19 #endif
20
21 enum which_open { OPEN_ORIGINAL, OPEN_HOT_BACKUP };
22
23 int db_hotbackup_env_init __P((DB_ENV **,
24 char *, char **, char ***, char *, enum which_open, int));
25 int db_hotbackup_main __P((int, char *[]));
26 int db_hotbackup_usage __P((void));
27 int db_hotbackup_version_check __P((void));
28 void __db_util_msg __P((const DB_ENV *, const char *));
29
30 const char *progname;
31
32 void __db_util_msg(dbenv, msgstr)
33 const DB_ENV *dbenv;
34 const char *msgstr;
35 {
36 COMPQUIET(dbenv, NULL);
37 printf("%s: %s\n", progname, msgstr);
38 }
39
40 int
41 db_hotbackup(args)
42 char *args;
43 {
44 int argc;
45 char **argv;
46
47 __db_util_arg("db_hotbackup", args, &argc, &argv);
48 return (db_hotbackup_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
49 }
50
51 #include <stdio.h>
52 #define ERROR_RETURN ERROR
53
54 int
55 db_hotbackup_main(argc, argv)
56 int argc;
57 char *argv[];
58 {
59 extern char *optarg;
60 extern int optind, __db_getopt_reset;
61 time_t now;
62 DB_ENV *dbenv;
63 u_int data_cnt, data_next;
64 int ch, checkpoint, db_config, debug, env_copy, exitval;
65 int ret, update, verbose;
66 char *backup_dir, **data_dir, *home, *log_dir, *passwd;
67 char home_buf[DB_MAXPATHLEN], time_buf[CTIME_BUFLEN];
68 u_int32_t flags;
69
70 /*
71 * Make sure all verbose message are output before any error messages
72 * in the case where the output is being logged into a file. This
73 * call has to be done before any operation is performed on the stream.
74 *
75 * Use unbuffered I/O because line-buffered I/O requires a buffer, and
76 * some operating systems have buffer alignment and size constraints we
77 * don't want to care about. There isn't enough output for the calls
78 * to matter.
79 */
80 setbuf(stdout, NULL);
81
82 if ((progname = __db_rpath(argv[0])) == NULL)
83 progname = argv[0];
84 else
85 ++progname;
86
87 if ((ret = db_hotbackup_version_check()) != 0)
88 return (ret);
89
90 /* We default to the safe environment copy. */
91 env_copy = 1;
92
93 checkpoint = db_config = data_cnt = data_next = debug =
94 exitval = update = verbose = 0;
95 data_dir = NULL;
96 backup_dir = home = passwd = NULL;
97 log_dir = NULL;
98 __db_getopt_reset = 1;
99 while ((ch = getopt(argc, argv, "b:cDd:Fgh:l:P:uVv")) != EOF)
100 switch (ch) {
101 case 'b':
102 backup_dir = optarg;
103 break;
104 case 'c':
105 checkpoint = 1;
106 break;
107 case 'D':
108 db_config = 1;
109 break;
110 case 'd':
111 /*
112 * User can specify a list of directories -- keep an
113 * array, leaving room for the trailing NULL.
114 */
115 if (data_dir == NULL || data_next >= data_cnt - 2) {
116 data_cnt = data_cnt == 0 ? 20 : data_cnt * 2;
117 if ((data_dir = realloc(data_dir,
118 data_cnt * sizeof(*data_dir))) == NULL) {
119 fprintf(stderr, "%s: %s\n",
120 progname, strerror(errno));
121 exitval = (EXIT_FAILURE);
122 goto clean;
123 }
124 }
125 data_dir[data_next++] = optarg;
126 break;
127 case 'F':
128 /* The default is to use environment copy. */
129 env_copy = 0;
130 break;
131 case 'g':
132 debug = 1;
133 break;
134 case 'h':
135 home = optarg;
136 break;
137 case 'l':
138 log_dir = optarg;
139 break;
140 case 'P':
141 if (passwd != NULL) {
142 fprintf(stderr, "%s: %s", progname,
143 DB_STR("5133",
144 "Password may not be specified twice\n"));
145 free(passwd);
146 return (EXIT_FAILURE);
147 }
148 passwd = strdup(optarg);
149 memset(optarg, 0, strlen(optarg));
150 if (passwd == NULL) {
151 fprintf(stderr, "%s: ", progname);
152 fprintf(stderr, DB_STR_A("5026",
153 "strdup: %s\n", "%s\n"), strerror(errno));
154 exitval = (EXIT_FAILURE);
155 goto clean;
156 }
157 break;
158 case 'u':
159 update = 1;
160 break;
161 case 'V':
162 printf("%s\n", db_version(NULL, NULL, NULL));
163 exitval = (EXIT_SUCCESS);
164 goto clean;
165 case 'v':
166 verbose = 1;
167 break;
168 case '?':
169 default:
170 exitval = db_hotbackup_usage();
171 goto clean;
172 }
173 argc -= optind;
174 argv += optind;
175
176 if (argc != 0) {
177 exitval = db_hotbackup_usage();
178 goto clean;
179 }
180
181 /* NULL-terminate any list of data directories. */
182 if (data_dir != NULL) {
183 data_dir[data_next] = NULL;
184 /*
185 * -d is relative to the current directory, to run a checkpoint
186 * we must have directories relative to the environment.
187 */
188 if (checkpoint == 1) {
189 fprintf(stderr, "%s: %s",
190 DB_STR("5027", "cannot specify -d and -c\n"),
191 progname);
192
193 exitval = db_hotbackup_usage();
194 goto clean;
195 }
196 }
197
198 if (db_config && (data_dir != NULL || log_dir != NULL)) {
199 fprintf(stderr, "%s: %s", DB_STR("5028",
200 "cannot specify -D and -d or -l\n"), progname);
201 exitval = db_hotbackup_usage();
202 goto clean;
203 }
204
205 /* Handle possible interruptions. */
206 __db_util_siginit();
207
208 /*
209 * The home directory defaults to the environment variable DB_HOME.
210 * The log directory defaults to the home directory.
211 *
212 * We require a source database environment directory and a target
213 * backup directory.
214 */
215 if (home == NULL) {
216 home = home_buf;
217 if ((ret = __os_getenv(
218 NULL, "DB_HOME", &home, sizeof(home_buf))) != 0) {
219 fprintf(stderr, "%s: ", progname);
220 fprintf(stderr, DB_STR_A("5029",
221 "failed to get environment variable DB_HOME: %s\n",
222 "%s"), db_strerror(ret));
223 exitval = (EXIT_FAILURE);
224 goto clean;
225 }
226 /*
227 * home set to NULL if __os_getenv failed to find DB_HOME.
228 */
229 }
230 if (home == NULL) {
231 fprintf(stderr, "%s: %s", DB_STR("5030",
232 "no source database environment specified\n"), progname);
233 exitval = db_hotbackup_usage();
234 goto clean;
235 }
236 if (backup_dir == NULL) {
237 fprintf(stderr, "%s: %s", DB_STR("5031",
238 "no target backup directory specified\n"),
239 progname);
240 exitval = db_hotbackup_usage();
241 goto clean;
242 }
243
244 if (verbose) {
245 (void)time(&now);
246 printf("%s: ", progname);
247 printf(DB_STR_A("5032", "hot backup started at %s",
248 "%s"), __os_ctime(&now, time_buf));
249 }
250
251 /* Open the source environment. */
252 if (db_hotbackup_env_init(&dbenv, home,
253 (db_config || log_dir != NULL) ? &log_dir : NULL,
254 &data_dir, passwd, OPEN_ORIGINAL, verbose) != 0)
255 goto err;
256
257 if (env_copy) {
258 if ((ret = dbenv->get_open_flags(dbenv, &flags)) != 0)
259 goto err;
260 if (flags & DB_PRIVATE) {
261 fprintf(stderr, "%s: %s", progname, DB_STR("5129",
262 "Cannot copy data from a PRIVATE environment\n"));
263 goto err;
264 }
265 }
266
267 if (log_dir != NULL) {
268 if (db_config && __os_abspath(log_dir)) {
269 fprintf(stderr, "%s: %s", progname, DB_STR("5033",
270 "DB_CONFIG must not contain an absolute "
271 "path for the log directory\n"));
272 goto err;
273 }
274 }
275
276 /*
277 * If the -c option is specified, checkpoint the source home
278 * database environment, and remove any unnecessary log files.
279 */
280 if (checkpoint) {
281 if (verbose) {
282 printf("%s: ", progname);
283 printf(DB_STR_A("5035", "%s: force checkpoint\n",
284 "%s"), home);
285 }
286 if ((ret =
287 dbenv->txn_checkpoint(dbenv, 0, 0, DB_FORCE)) != 0) {
288 dbenv->err(dbenv, ret, "DB_ENV->txn_checkpoint");
289 goto err;
290 }
291 if (!update) {
292 if (verbose) {
293 printf("%s: ", progname);
294 printf(DB_STR_A("5036",
295 "%s: remove unnecessary log files\n",
296 "%s"), home);
297 }
298 if ((ret = dbenv->log_archive(dbenv,
299 NULL, DB_ARCH_REMOVE)) != 0) {
300 dbenv->err(dbenv, ret, "DB_ENV->log_archive");
301 goto err;
302 }
303 }
304 }
305
306 flags = DB_CREATE | DB_BACKUP_CLEAN | DB_BACKUP_FILES;
307 if (update)
308 LF_SET(DB_BACKUP_UPDATE);
309
310 if (!db_config)
311 LF_SET(DB_BACKUP_SINGLE_DIR);
312 if ((ret = dbenv->backup(dbenv, backup_dir, flags)) != 0)
313 goto err;
314
315 /* Close the source environment. */
316 if ((ret = dbenv->close(dbenv, 0)) != 0) {
317 fprintf(stderr,
318 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
319 dbenv = NULL;
320 goto err;
321 }
322 /* Perform catastrophic recovery on the hot backup. */
323 if (verbose) {
324 printf("%s: ", progname);
325 printf(DB_STR_A("5040", "%s: run catastrophic recovery\n",
326 "%s"), backup_dir);
327 }
328 if (db_hotbackup_env_init(&dbenv,
329 backup_dir, NULL, NULL, passwd, OPEN_HOT_BACKUP, verbose) != 0)
330 goto err;
331
332 /*
333 * Remove any unnecessary log files from the hot backup.
334 * For debugging purposes, leave them around.
335 */
336 if (debug == 0) {
337 if (verbose) {
338 printf("%s: ", progname);
339 printf(DB_STR_A("5041",
340 "%s: remove unnecessary log files\n",
341 "%s"), backup_dir);
342 }
343
344 if ((ret =
345 dbenv->log_archive(dbenv, NULL, DB_ARCH_REMOVE)) != 0) {
346 dbenv->err(dbenv, ret, "DB_ENV->log_archive");
347 goto err;
348 }
349 }
350
351 if (0) {
352 err: exitval = 1;
353 }
354
355 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
356 exitval = 1;
357 fprintf(stderr,
358 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
359 }
360
361 if (exitval == 0) {
362 if (verbose) {
363 (void)time(&now);
364 printf("%s: ", progname);
365 printf(DB_STR_A("5042",
366 "hot backup completed at %s", "%s"),
367 __os_ctime(&now, time_buf));
368 }
369 } else {
370 fprintf(stderr, "%s: %s", progname,
371 DB_STR("5043", "HOT BACKUP FAILED!\n"));
372 }
373
374 /* Resend any caught signal. */
375 __db_util_sigresend();
376
377 clean:
378 if (data_cnt > 0)
379 free(data_dir);
380 if (passwd != NULL)
381 free(passwd);
382
383 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
384 }
385
386 /*
387 * env_init --
388 * Open a database environment.
389 */
390 int
391 db_hotbackup_env_init(dbenvp, home, log_dirp, data_dirp, passwd, which, verbose)
392 DB_ENV **dbenvp;
393 char *home, **log_dirp, ***data_dirp, *passwd;
394 enum which_open which;
395 int verbose;
396 {
397 DB_ENV *dbenv;
398 const char *log_dir, **data_dir;
399 char buf[DB_MAXPATHLEN];
400 int homehome, ret;
401
402 *dbenvp = NULL;
403
404 /*
405 * Create an environment object and initialize it for error reporting.
406 */
407 if ((ret = db_env_create(&dbenv, 0)) != 0) {
408 fprintf(stderr,
409 "%s: db_env_create: %s\n", progname, db_strerror(ret));
410 return (1);
411 }
412
413 if (verbose) {
414 (void)dbenv->set_verbose(dbenv, DB_VERB_BACKUP, 1);
415 dbenv->set_msgcall(dbenv, __db_util_msg);
416 }
417 dbenv->set_errfile(dbenv, stderr);
418 setbuf(stderr, NULL);
419 dbenv->set_errpfx(dbenv, progname);
420
421 /* Any created intermediate directories are created private. */
422 if ((ret = dbenv->set_intermediate_dir_mode(dbenv, "rwx------")) != 0) {
423 dbenv->err(dbenv, ret, "DB_ENV->set_intermediate_dir_mode");
424 return (1);
425 }
426
427 /*
428 * If a log directory has been specified, and it's not the same as the
429 * home directory, set it for the environment.
430 */
431 if (log_dirp != NULL && *log_dirp != NULL &&
432 (ret = dbenv->set_lg_dir(dbenv, *log_dirp)) != 0) {
433 dbenv->err(dbenv, ret, "DB_ENV->set_lg_dir: %s", *log_dirp);
434 return (1);
435 }
436
437 /* Optionally set the password. */
438 if (passwd != NULL &&
439 (ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) {
440 dbenv->err(dbenv, ret, "DB_ENV->set_encrypt");
441 return (1);
442 }
443
444 switch (which) {
445 case OPEN_ORIGINAL:
446 if (data_dirp != NULL && *data_dirp != NULL) {
447 /*
448 * Backward compatibility: older versions
449 * did not have data dirs relative to home.
450 * Check to see if there is a sub-directory with
451 * the same name has the home directory. If not
452 * trim the home directory from the data directory
453 * passed in.
454 */
455 (void) sprintf(buf, "%s/%s", home, home);
456 homehome = 0;
457 (void)__os_exists(dbenv->env, buf, &homehome);
458
459 for (data_dir = (const char **)*data_dirp;
460 *data_dir != NULL; data_dir++) {
461 if (!homehome &&
462 !strncmp(home, *data_dir, strlen(home))) {
463 if (strchr(PATH_SEPARATOR,
464 (*data_dir)[strlen(home)]) != NULL)
465 (*data_dir) += strlen(home) + 1;
466 /* Just in case an extra / was added. */
467 else if (strchr(PATH_SEPARATOR,
468 home[strlen(home) - 1]) != NULL)
469 (*data_dir) += strlen(home);
470 }
471
472 if ((ret = dbenv->add_data_dir(
473 dbenv, *data_dir)) != 0) {
474 dbenv->err(dbenv, ret,
475 "DB_ENV->add_data_dir: %s",
476 *data_dir);
477 return (1);
478 }
479 }
480 }
481 /*
482 * Opening the database environment we're trying to back up.
483 * We try to attach to a pre-existing environment; if that
484 * fails, we create a private environment and try again.
485 */
486 if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
487 (ret == DB_VERSION_MISMATCH || ret == DB_REP_LOCKOUT ||
488 (ret = dbenv->open(dbenv, home, DB_CREATE |
489 DB_INIT_LOG | DB_INIT_TXN | DB_PRIVATE | DB_USE_ENVIRON,
490 0)) != 0)) {
491 dbenv->err(dbenv, ret, "DB_ENV->open: %s", home);
492 return (1);
493 }
494 if (log_dirp != NULL) {
495 (void)dbenv->get_lg_dir(dbenv, &log_dir);
496 if (*log_dirp == NULL)
497 *log_dirp = (char*)log_dir;
498 else if (strcmp(*log_dirp, log_dir)) {
499 fprintf(stderr, DB_STR_A("5057",
500 "%s: cannot specify -l with conflicting DB_CONFIG file\n",
501 "%s\n"), progname);
502 return (db_hotbackup_usage());
503 } else {
504 /*
505 * Do we have -l and an existing DB_CONFIG?
506 * That is a usage problem, but for backward
507 * compatibility, keep going if log_dir happens
508 * to be the same as the DB_CONFIG path.
509 */
510 (void)snprintf(buf, sizeof(buf), "%s%c%s",
511 home, PATH_SEPARATOR[0], "DB_CONFIG");
512 if (__os_exists(dbenv->env, buf, NULL) == 0)
513 fprintf(stderr,
514 "%s: %s", DB_STR("5058",
515 "use of -l with DB_CONFIG file is deprecated\n"),
516 progname);
517 }
518 }
519 if (data_dirp != NULL && *data_dirp == NULL)
520 (void)dbenv->get_data_dirs(
521 dbenv, (const char ***)data_dirp);
522 break;
523 case OPEN_HOT_BACKUP:
524 /*
525 * Opening the backup copy of the database environment. We
526 * better be the only user, we're running recovery.
527 * Ensure that there at least minimal cache for worst
528 * case page size.
529 */
530 if ((ret =
531 dbenv->set_cachesize(dbenv, 0, 64 * 1024 * 10, 0)) != 0) {
532 dbenv->err(dbenv,
533 ret, "DB_ENV->set_cachesize: %s", home);
534 return (1);
535 }
536 if (verbose == 1)
537 (void)dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
538 if ((ret = dbenv->open(dbenv, home, DB_CREATE |
539 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE |
540 DB_RECOVER_FATAL | DB_USE_ENVIRON, 0)) != 0) {
541 dbenv->err(dbenv, ret, "DB_ENV->open: %s", home);
542 return (1);
543 }
544 break;
545 }
546
547 *dbenvp = dbenv;
548 return (0);
549 }
550
551 int
552 db_hotbackup_usage()
553 {
554 (void)fprintf(stderr, "usage: %s [-cDuVv]\n\t%s\n", progname,
555 "[-d data_dir ...] [-h home] [-l log_dir] [-P password] -b backup_dir");
556 return (EXIT_FAILURE);
557 }
558
559 int
560 db_hotbackup_version_check()
561 {
562 int v_major, v_minor, v_patch;
563
564 /* Make sure we're loaded with the right version of the DB library. */
565 (void)db_version(&v_major, &v_minor, &v_patch);
566 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
567 fprintf(stderr, "%s: ", progname);
568 fprintf(stderr, DB_STR_A("5071",
569 "version %d.%d doesn't match library version %d.%d\n",
570 "%d %d %d %d\n"),
571 DB_VERSION_MAJOR, DB_VERSION_MINOR,
572 v_major, v_minor);
573 return (EXIT_FAILURE);
574 }
575 return (0);
576 }