📄 db_hotbackup.c
字号:
/*- * See the file LICENSE for redistribution information. * * Copyright (c) 1996,2007 Oracle. All rights reserved. * * $Id: db_hotbackup.c,v 1.51 2007/05/17 15:15:01 bostic Exp $ */#include "db_config.h"#include "db_int.h"#include "dbinc/log.h"#include "dbinc/db_page.h"#include "dbinc/qam.h"#ifndef lintstatic const char copyright[] = "Copyright (c) 1996,2007 Oracle. All rights reserved.\n";#endifenum which_open { OPEN_ORIGINAL, OPEN_HOT_BACKUP };int db_hotbackup_backup_dir_clean __P((DB_ENV *, char *, char *, int *, int, int));int db_hotbackup_data_copy __P((DB_ENV *, char *, char *, char *, int));int db_hotbackup_env_init __P((DB_ENV **, char *, char **, char ***, char *, enum which_open));int db_hotbackup_main __P((int, char *[]));int db_hotbackup_read_data_dir __P((DB_ENV *, char *, char *, char *, int, int));int db_hotbackup_read_log_dir __P((DB_ENV *, char *, char *, char *, int *, int, int));int db_hotbackup_usage __P((void));int db_hotbackup_version_check __P((void));const char *progname;intdb_hotbackup(args) char *args;{ int argc; char **argv; __db_util_arg("db_hotbackup", args, &argc, &argv); return (db_hotbackup_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);}#include <stdio.h>#define ERROR_RETURN ERRORintdb_hotbackup_main(argc, argv) int argc; char *argv[];{ extern char *optarg; extern int optind, __db_getopt_reset; time_t now; DB_ENV *dbenv; u_int data_cnt, data_next; int ch, checkpoint, copy_min, db_config, exitval; int remove_max, ret, update, verbose; char *backup_dir, **data_dir, **dir, *home, *log_dir, *passwd; char home_buf[DB_MAXPATHLEN], time_buf[CTIME_BUFLEN]; /* * 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 = db_hotbackup_version_check()) != 0) return (ret); checkpoint = db_config = data_cnt = data_next = exitval = update = verbose = 0; data_dir = NULL; backup_dir = home = passwd = NULL; log_dir = NULL; copy_min = remove_max = 0; __db_getopt_reset = 1; while ((ch = getopt(argc, argv, "b:cDd:h: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)); return (EXIT_FAILURE); } } data_dir[data_next++] = optarg; break; case 'h': home = optarg; break; case 'l': log_dir = optarg; break; case 'P': passwd = strdup(optarg); memset(optarg, 0, strlen(optarg)); if (passwd == NULL) { fprintf(stderr, "%s: strdup: %s\n", progname, strerror(errno)); return (EXIT_FAILURE); } break; case 'u': update = 1; break; case 'V': printf("%s\n", db_version(NULL, NULL, NULL)); return (EXIT_SUCCESS); case 'v': verbose = 1; break; case '?': default: return (db_hotbackup_usage()); } argc -= optind; argv += optind; if (argc != 0) return (db_hotbackup_usage()); /* 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: cannot specify -d and -c\n", progname); return (db_hotbackup_usage()); } } if (db_config && (data_dir != NULL || log_dir != NULL)) { fprintf(stderr, "%s: cannot specify -D and -d or -l\n", progname); return (db_hotbackup_usage()); } /* 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 failed to get environment variable DB_HOME: %s\n", progname, db_strerror(ret)); return (EXIT_FAILURE); } /* * home set to NULL if __os_getenv failed to find DB_HOME. */ } if (home == NULL) { fprintf(stderr, "%s: no source database environment specified\n", progname); return (db_hotbackup_usage()); } if (backup_dir == NULL) { fprintf(stderr, "%s: no target backup directory specified\n", progname); return (db_hotbackup_usage()); } if (verbose) { (void)time(&now); printf("%s: hot backup started at %s", progname, __db_ctime(&now, time_buf)); } /* Open the source environment. */ if (db_hotbackup_env_init(&dbenv, home, (db_config || log_dir != NULL) ? &log_dir : NULL, db_config ? &data_dir : NULL, passwd, OPEN_ORIGINAL) != 0) goto shutdown; if (db_config && __os_abspath(log_dir)) { fprintf(stderr, "%s: DB_CONFIG must not contain an absolute path for the log directory", progname); goto shutdown; } /* * If the -c option is specified, checkpoint the source home * database environment, and remove any unnecessary log files. */ if (checkpoint) { if (verbose) printf("%s: %s: force checkpoint\n", progname, home); if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, DB_FORCE)) != 0) { dbenv->err(dbenv, ret, "DB_ENV->txn_checkpoint"); goto shutdown; } if (!update) { if (verbose) printf("%s: %s: remove unnecessary log files\n", progname, home); if ((ret = dbenv->log_archive(dbenv, NULL, DB_ARCH_REMOVE)) != 0) { dbenv->err(dbenv, ret, "DB_ENV->log_archive"); goto shutdown; } } } /* * If the target directory for the backup does not exist, create it * with mode read-write-execute for the owner. Ignore errors here, * it's simpler and more portable to just always try the create. If * there's a problem, we'll fail with reasonable errors later. */ (void)__os_mkdir(NULL, backup_dir, __db_omode("rwx------")); /* * If -u was specified, remove all log files; if -u was not specified, * remove all files. * * Potentially there are two directories to clean, the log directory * and the target directory. First, clean up the log directory if * it's different from the target directory, then clean up the target * directory. */ if (db_config && log_dir != NULL && db_hotbackup_backup_dir_clean( dbenv, backup_dir, log_dir, &remove_max, update, verbose) != 0) goto shutdown; if (db_hotbackup_backup_dir_clean(dbenv, backup_dir, NULL, &remove_max, update, verbose) != 0) goto shutdown; /* * If the -u option was not specified, copy all database files found in * the database environment home directory, or any directory specified * using the -d option, into the target directory for the backup. */ if (!update) { if (db_hotbackup_read_data_dir(dbenv, home, backup_dir, home, verbose, db_config) != 0) goto shutdown; if (data_dir != NULL) for (dir = data_dir; *dir != NULL; ++dir) { /* * Don't allow absolute path names taken from * the DB_CONFIG file -- running recovery with * them would corrupt the source files. */ if (db_config && __os_abspath(*dir)) { fprintf(stderr, "%s: data directory '%s' is absolute path, not permitted with -D option\n", progname, *dir); goto shutdown; } if (db_hotbackup_read_data_dir(dbenv, home, backup_dir, *dir, verbose, db_config) != 0) goto shutdown; } } /* * Copy all log files found in the directory specified by the -l option * (or in the database environment home directory, if no -l option was * specified), into the target directory for the backup. * * The log directory defaults to the home directory. */ if (db_hotbackup_read_log_dir(dbenv, db_config ? home : NULL, backup_dir, log_dir == NULL ? home : log_dir, ©_min, update, verbose) != 0) goto shutdown; /* * If we're updating a snapshot, the lowest-numbered log file copied * into the backup directory should be less than, or equal to, the * highest-numbered log file removed from the backup directory during * cleanup. */ if (update && remove_max < copy_min && !(remove_max == 0 && copy_min == 1)) { fprintf(stderr, "%s: the largest log file removed (%d) must be greater\n", progname, remove_max); fprintf(stderr, "%s: than or equal the smallest log file copied (%d)\n", progname, copy_min); goto shutdown; } /* 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 shutdown; } /* Perform catastrophic recovery on the hot backup. */ if (verbose) printf("%s: %s: run catastrophic recovery\n", progname, backup_dir); if (db_hotbackup_env_init( &dbenv, backup_dir, NULL, NULL, passwd, OPEN_HOT_BACKUP) != 0) goto shutdown; /* * Remove any unnecessary log files from the hot backup. */ if (verbose) printf("%s: %s: remove unnecessary log files\n", progname, backup_dir); if ((ret = dbenv->log_archive(dbenv, NULL, DB_ARCH_REMOVE)) != 0) { dbenv->err(dbenv, ret, "DB_ENV->log_archive"); goto shutdown; } if (0) {shutdown: 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: hot backup completed at %s", progname, __db_ctime(&now, time_buf)); } } else { fprintf(stderr, "%s: HOT BACKUP FAILED!\n", progname); } /* Resend any caught signal. */ __db_util_sigresend(); return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);}/* * env_init -- * Open a database environment. */intdb_hotbackup_env_init(dbenvp, home, log_dirp, data_dirp, passwd, which) DB_ENV **dbenvp; char *home, **log_dirp, ***data_dirp, *passwd; enum which_open which;{ DB_ENV *dbenv; int 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); } dbenv->set_errfile(dbenv, stderr); setbuf(stderr, NULL); dbenv->set_errpfx(dbenv, progname); /* * 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: /* * 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -