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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.
*
* $Id$
*/
#include "db_config.h"
#include "db_int.h"
#include "dbinc/log.h"
#include "dbinc/db_page.h"
#include "dbinc/qam.h"
#ifndef lint
static const char copyright[] =
"Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.\n";
#endif
enum which_open { OPEN_ORIGINAL, OPEN_HOT_BACKUP };
int env_init __P((DB_ENV **,
char *, char **, char ***, char *, enum which_open, int));
int main __P((int, char *[]));
int usage __P((void));
int version_check __P((void));
void __db_util_msg __P((const DB_ENV *, const char *));
const char *progname;
void __db_util_msg(dbenv, msgstr)
const DB_ENV *dbenv;
const char *msgstr;
{
COMPQUIET(dbenv, NULL);
printf("%s: %s\n", progname, msgstr);
}
int
main(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind;
time_t now;
DB_ENV *dbenv;
u_int data_cnt, data_next;
int ch, checkpoint, db_config, debug, env_copy, exitval;
int ret, update, verbose;
char *backup_dir, **data_dir, *home, *log_dir, *passwd;
char home_buf[DB_MAXPATHLEN], time_buf[CTIME_BUFLEN];
u_int32_t flags;
/*
* Make sure all verbose message are output before any error messages
* in the case where the output is being logged into a file. This
* call has to be done before any operation is performed on the stream.
*
* Use unbuffered I/O because line-buffered I/O requires a buffer, and
* some operating systems have buffer alignment and size constraints we
* don't want to care about. There isn't enough output for the calls
* to matter.
*/
setbuf(stdout, NULL);
if ((progname = __db_rpath(argv[0])) == NULL)
progname = argv[0];
else
++progname;
if ((ret = version_check()) != 0)
return (ret);
/* We default to the safe environment copy. */
env_copy = 1;
checkpoint = db_config = data_cnt = data_next = debug =
exitval = update = verbose = 0;
data_dir = NULL;
backup_dir = home = passwd = NULL;
log_dir = NULL;
while ((ch = getopt(argc, argv, "b:cDd:Fgh:l:P:uVv")) != EOF)
switch (ch) {
case 'b':
backup_dir = optarg;
break;
case 'c':
checkpoint = 1;
break;
case 'D':
db_config = 1;
break;
case 'd':
/*
* User can specify a list of directories -- keep an
* array, leaving room for the trailing NULL.
*/
if (data_dir == NULL || data_next >= data_cnt - 2) {
data_cnt = data_cnt == 0 ? 20 : data_cnt * 2;
if ((data_dir = realloc(data_dir,
data_cnt * sizeof(*data_dir))) == NULL) {
fprintf(stderr, "%s: %s\n",
progname, strerror(errno));
exitval = (EXIT_FAILURE);
goto clean;
}
}
data_dir[data_next++] = optarg;
break;
case 'F':
/* The default is to use environment copy. */
env_copy = 0;
break;
case 'g':
debug = 1;
break;
case 'h':
home = optarg;
break;
case 'l':
log_dir = optarg;
break;
case 'P':
if (passwd != NULL) {
fprintf(stderr, "%s: %s", progname,
DB_STR("5133",
"Password may not be specified twice\n"));
free(passwd);
return (EXIT_FAILURE);
}
passwd = strdup(optarg);
memset(optarg, 0, strlen(optarg));
if (passwd == NULL) {
fprintf(stderr, "%s: ", progname);
fprintf(stderr, DB_STR_A("5026",
"strdup: %s\n", "%s\n"), strerror(errno));
exitval = (EXIT_FAILURE);
goto clean;
}
break;
case 'u':
update = 1;
break;
case 'V':
printf("%s\n", db_version(NULL, NULL, NULL));
exitval = (EXIT_SUCCESS);
goto clean;
case 'v':
verbose = 1;
break;
case '?':
default:
exitval = usage();
goto clean;
}
argc -= optind;
argv += optind;
if (argc != 0) {
exitval = usage();
goto clean;
}
/* NULL-terminate any list of data directories. */
if (data_dir != NULL) {
data_dir[data_next] = NULL;
/*
* -d is relative to the current directory, to run a checkpoint
* we must have directories relative to the environment.
*/
if (checkpoint == 1) {
fprintf(stderr, "%s: %s",
DB_STR("5027", "cannot specify -d and -c\n"),
progname);
exitval = usage();
goto clean;
}
}
if (db_config && (data_dir != NULL || log_dir != NULL)) {
fprintf(stderr, "%s: %s", DB_STR("5028",
"cannot specify -D and -d or -l\n"), progname);
exitval = usage();
goto clean;
}
/* Handle possible interruptions. */
__db_util_siginit();
/*
* The home directory defaults to the environment variable DB_HOME.
* The log directory defaults to the home directory.
*
* We require a source database environment directory and a target
* backup directory.
*/
if (home == NULL) {
home = home_buf;
if ((ret = __os_getenv(
NULL, "DB_HOME", &home, sizeof(home_buf))) != 0) {
fprintf(stderr, "%s: ", progname);
fprintf(stderr, DB_STR_A("5029",
"failed to get environment variable DB_HOME: %s\n",
"%s"), db_strerror(ret));
exitval = (EXIT_FAILURE);
goto clean;
}
/*
* home set to NULL if __os_getenv failed to find DB_HOME.
*/
}
if (home == NULL) {
fprintf(stderr, "%s: %s", DB_STR("5030",
"no source database environment specified\n"), progname);
exitval = usage();
goto clean;
}
if (backup_dir == NULL) {
fprintf(stderr, "%s: %s", DB_STR("5031",
"no target backup directory specified\n"),
progname);
exitval = usage();
goto clean;
}
if (verbose) {
(void)time(&now);
printf("%s: ", progname);
printf(DB_STR_A("5032", "hot backup started at %s",
"%s"), __os_ctime(&now, time_buf));
}
/* Open the source environment. */
if (env_init(&dbenv, home,
(db_config || log_dir != NULL) ? &log_dir : NULL,
&data_dir, passwd, OPEN_ORIGINAL, verbose) != 0)
goto err;
if (env_copy) {
if ((ret = dbenv->get_open_flags(dbenv, &flags)) != 0)
goto err;
if (flags & DB_PRIVATE) {
fprintf(stderr, "%s: %s", progname, DB_STR("5129",
"Cannot copy data from a PRIVATE environment\n"));
goto err;
}
}
if (log_dir != NULL) {
if (db_config && __os_abspath(log_dir)) {
fprintf(stderr, "%s: %s", progname, DB_STR("5033",
"DB_CONFIG must not contain an absolute "
"path for the log directory\n"));
goto err;
}
}
/*
* If the -c option is specified, checkpoint the source home
* database environment, and remove any unnecessary log files.
*/
if (checkpoint) {
if (verbose) {
printf("%s: ", progname);
printf(DB_STR_A("5035", "%s: force checkpoint\n",
"%s"), home);
}
if ((ret =
dbenv->txn_checkpoint(dbenv, 0, 0, DB_FORCE)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->txn_checkpoint");
goto err;
}
if (!update) {
if (verbose) {
printf("%s: ", progname);
printf(DB_STR_A("5036",
"%s: remove unnecessary log files\n",
"%s"), home);
}
if ((ret = dbenv->log_archive(dbenv,
NULL, DB_ARCH_REMOVE)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->log_archive");
goto err;
}
}
}
flags = DB_CREATE | DB_BACKUP_CLEAN | DB_BACKUP_FILES;
if (update)
LF_SET(DB_BACKUP_UPDATE);
if (!db_config)
LF_SET(DB_BACKUP_SINGLE_DIR);
if ((ret = dbenv->backup(dbenv, backup_dir, flags)) != 0)
goto err;
/* Close the source environment. */
if ((ret = dbenv->close(dbenv, 0)) != 0) {
fprintf(stderr,
"%s: dbenv->close: %s\n", progname, db_strerror(ret));
dbenv = NULL;
goto err;
}
/* Perform catastrophic recovery on the hot backup. */
if (verbose) {
printf("%s: ", progname);
printf(DB_STR_A("5040", "%s: run catastrophic recovery\n",
"%s"), backup_dir);
}
if (env_init(&dbenv,
backup_dir, NULL, NULL, passwd, OPEN_HOT_BACKUP, verbose) != 0)
goto err;
/*
* Remove any unnecessary log files from the hot backup.
* For debugging purposes, leave them around.
*/
if (debug == 0) {
if (verbose) {
printf("%s: ", progname);
printf(DB_STR_A("5041",
"%s: remove unnecessary log files\n",
"%s"), backup_dir);
}
if ((ret =
dbenv->log_archive(dbenv, NULL, DB_ARCH_REMOVE)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->log_archive");
goto err;
}
}
if (0) {
err: exitval = 1;
}
if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
exitval = 1;
fprintf(stderr,
"%s: dbenv->close: %s\n", progname, db_strerror(ret));
}
if (exitval == 0) {
if (verbose) {
(void)time(&now);
printf("%s: ", progname);
printf(DB_STR_A("5042",
"hot backup completed at %s", "%s"),
__os_ctime(&now, time_buf));
}
} else {
fprintf(stderr, "%s: %s", progname,
DB_STR("5043", "HOT BACKUP FAILED!\n"));
}
/* Resend any caught signal. */
__db_util_sigresend();
clean:
if (data_cnt > 0)
free(data_dir);
if (passwd != NULL)
free(passwd);
return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
/*
* env_init --
* Open a database environment.
*/
int
env_init(dbenvp, home, log_dirp, data_dirp, passwd, which, verbose)
DB_ENV **dbenvp;
char *home, **log_dirp, ***data_dirp, *passwd;
enum which_open which;
int verbose;
{
DB_ENV *dbenv;
const char *log_dir, **data_dir;
char buf[DB_MAXPATHLEN];
int homehome, ret;
*dbenvp = NULL;
/*
* Create an environment object and initialize it for error reporting.
*/
if ((ret = db_env_create(&dbenv, 0)) != 0) {
fprintf(stderr,
"%s: db_env_create: %s\n", progname, db_strerror(ret));
return (1);
}
if (verbose) {
(void)dbenv->set_verbose(dbenv, DB_VERB_BACKUP, 1);
dbenv->set_msgcall(dbenv, __db_util_msg);
}
dbenv->set_errfile(dbenv, stderr);
setbuf(stderr, NULL);
dbenv->set_errpfx(dbenv, progname);
/* Any created intermediate directories are created private. */
if ((ret = dbenv->set_intermediate_dir_mode(dbenv, "rwx------")) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->set_intermediate_dir_mode");
return (1);
}
/*
* If a log directory has been specified, and it's not the same as the
* home directory, set it for the environment.
*/
if (log_dirp != NULL && *log_dirp != NULL &&
(ret = dbenv->set_lg_dir(dbenv, *log_dirp)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->set_lg_dir: %s", *log_dirp);
return (1);
}
/* Optionally set the password. */
if (passwd != NULL &&
(ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->set_encrypt");
return (1);
}
switch (which) {
case OPEN_ORIGINAL:
if (data_dirp != NULL && *data_dirp != NULL) {
/*
* Backward compatibility: older versions
* did not have data dirs relative to home.
* Check to see if there is a sub-directory with
* the same name has the home directory. If not
* trim the home directory from the data directory
* passed in.
*/
(void) sprintf(buf, "%s/%s", home, home);
homehome = 0;
(void)__os_exists(dbenv->env, buf, &homehome);
for (data_dir = (const char **)*data_dirp;
*data_dir != NULL; data_dir++) {
if (!homehome &&
!strncmp(home, *data_dir, strlen(home))) {
if (strchr(PATH_SEPARATOR,
(*data_dir)[strlen(home)]) != NULL)
(*data_dir) += strlen(home) + 1;
/* Just in case an extra / was added. */
else if (strchr(PATH_SEPARATOR,
home[strlen(home) - 1]) != NULL)
(*data_dir) += strlen(home);
}
if ((ret = dbenv->add_data_dir(
dbenv, *data_dir)) != 0) {
dbenv->err(dbenv, ret,
"DB_ENV->add_data_dir: %s",
*data_dir);
return (1);
}
}
}
/*
* Opening the database environment we're trying to back up.
* We try to attach to a pre-existing environment; if that
* fails, we create a private environment and try again.
*/
if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
(ret == DB_VERSION_MISMATCH || ret == DB_REP_LOCKOUT ||
(ret = dbenv->open(dbenv, home, DB_CREATE |
DB_INIT_LOG | DB_INIT_TXN | DB_PRIVATE | DB_USE_ENVIRON,
0)) != 0)) {
dbenv->err(dbenv, ret, "DB_ENV->open: %s", home);
return (1);
}
if (log_dirp != NULL) {
(void)dbenv->get_lg_dir(dbenv, &log_dir);
if (*log_dirp == NULL)
*log_dirp = (char*)log_dir;
else if (strcmp(*log_dirp, log_dir)) {
fprintf(stderr, DB_STR_A("5057",
"%s: cannot specify -l with conflicting DB_CONFIG file\n",
"%s\n"), progname);
return (usage());
} else {
/*
* Do we have -l and an existing DB_CONFIG?
* That is a usage problem, but for backward
* compatibility, keep going if log_dir happens
* to be the same as the DB_CONFIG path.
*/
(void)snprintf(buf, sizeof(buf), "%s%c%s",
home, PATH_SEPARATOR[0], "DB_CONFIG");
if (__os_exists(dbenv->env, buf, NULL) == 0)
fprintf(stderr,
"%s: %s", DB_STR("5058",
"use of -l with DB_CONFIG file is deprecated\n"),
progname);
}
}
if (data_dirp != NULL && *data_dirp == NULL)
(void)dbenv->get_data_dirs(
dbenv, (const char ***)data_dirp);
break;
case OPEN_HOT_BACKUP:
/*
* Opening the backup copy of the database environment. We
* better be the only user, we're running recovery.
* Ensure that there at least minimal cache for worst
* case page size.
*/
if ((ret =
dbenv->set_cachesize(dbenv, 0, 64 * 1024 * 10, 0)) != 0) {
dbenv->err(dbenv,
ret, "DB_ENV->set_cachesize: %s", home);
return (1);
}
if (verbose == 1)
(void)dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
if ((ret = dbenv->open(dbenv, home, DB_CREATE |
DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE |
DB_RECOVER_FATAL | DB_USE_ENVIRON, 0)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->open: %s", home);
return (1);
}
break;
}
*dbenvp = dbenv;
return (0);
}
int
usage()
{
(void)fprintf(stderr, "usage: %s [-cDuVv]\n\t%s\n", progname,
"[-d data_dir ...] [-h home] [-l log_dir] [-P password] -b backup_dir");
return (EXIT_FAILURE);
}
int
version_check()
{
int v_major, v_minor, v_patch;
/* Make sure we're loaded with the right version of the DB library. */
(void)db_version(&v_major, &v_minor, &v_patch);
if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
fprintf(stderr, "%s: ", progname);
fprintf(stderr, DB_STR_A("5071",
"version %d.%d doesn't match library version %d.%d\n",
"%d %d %d %d\n"),
DB_VERSION_MAJOR, DB_VERSION_MINOR,
v_major, v_minor);
return (EXIT_FAILURE);
}
return (0);
}
|