📄 db_hotbackup.c
字号:
*/ if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 && (ret == DB_VERSION_MISMATCH || (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 && *log_dirp == NULL) (void)dbenv->get_lg_dir(dbenv, (const char **)log_dirp); 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 ((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);}/* * backup_dir_clean -- * Clean out the backup directory. */intdb_hotbackup_backup_dir_clean(dbenv, backup_dir, log_dir, remove_maxp, update, verbose) DB_ENV *dbenv; char *backup_dir, *log_dir; int *remove_maxp, update, verbose;{ int cnt, fcnt, ret, v; char **names, *dir, buf[DB_MAXPATHLEN], path[DB_MAXPATHLEN]; /* We may be cleaning a log directory separate from the target. */ if (log_dir != NULL) { if ((size_t)snprintf(buf, sizeof(buf), "%s%c%s", backup_dir, PATH_SEPARATOR[0] ,log_dir) >= sizeof(buf)) { dbenv->errx(dbenv, "%s%c%s: path too long", backup_dir, PATH_SEPARATOR[0] ,log_dir); return (1); } dir = buf; } else dir = backup_dir; /* Get a list of file names. */ if ((ret = __os_dirlist(dbenv, dir, &names, &fcnt)) != 0) { if (log_dir != NULL && !update) return (0); dbenv->err(dbenv, ret, "%s: directory read", dir); return (1); } for (cnt = fcnt; --cnt >= 0;) { /* * Skip non-log files (if update was specified). */ if (strncmp(names[cnt], LFPREFIX, sizeof(LFPREFIX) - 1)) { if (update) continue; } else { /* Track the highest-numbered log file removed. */ v = atoi(names[cnt] + sizeof(LFPREFIX) - 1); if (*remove_maxp < v) *remove_maxp = v; } if ((size_t)snprintf(path, sizeof(path), "%s%c%s", dir, PATH_SEPARATOR[0], names[cnt]) >= sizeof(path)) { dbenv->errx(dbenv, "%s%c%s: path too long", dir, PATH_SEPARATOR[0], names[cnt]); return (1); } if (verbose) printf("%s: removing %s\n", progname, path); if (__os_unlink(dbenv, path) != 0) return (1); } __os_dirfree(dbenv, names, fcnt); if (verbose && *remove_maxp != 0) printf("%s: highest numbered log file removed: %d\n", progname, *remove_maxp); return (0);}/* * read_data_dir -- * Read a directory looking for databases to copy. */intdb_hotbackup_read_data_dir(dbenv, home, backup_dir, dir, verbose, db_config) DB_ENV *dbenv; char *home, *backup_dir, *dir; int verbose, db_config;{ int cnt, fcnt, ret; char *bd, **names; char buf[DB_MAXPATHLEN], bbuf[DB_MAXPATHLEN]; bd = backup_dir; if (db_config && dir != home) { /* Build a path name to the destination. */ if ((size_t)(cnt = snprintf(bbuf, sizeof(bbuf), "%s%c%s%c", backup_dir, PATH_SEPARATOR[0], dir, PATH_SEPARATOR[0])) >= sizeof(buf)) { dbenv->errx(dbenv, "%s%c%s: path too long", backup_dir, PATH_SEPARATOR[0], dir); return (1); } bd = bbuf; /* Create the path. */ if ((ret = dbenv->set_intermediate_dir( dbenv, __db_omode("rwx------"), 0)) != 0 || (ret = __db_mkpath(dbenv, bd)) != 0) { dbenv->err(dbenv, ret, "%s: cannot create", bd); return (1); } /* step on the trailing '/' */ bd[cnt - 1] = '\0'; /* Build a path name to the source. */ if ((size_t)snprintf(buf, sizeof(buf), "%s%c%s", home, PATH_SEPARATOR[0], dir) >= sizeof(buf)) { dbenv->errx(dbenv, "%s%c%s: path too long", home, PATH_SEPARATOR[0], dir); return (1); } dir = buf; } /* Get a list of file names. */ if ((ret = __os_dirlist(dbenv, dir, &names, &fcnt)) != 0) { dbenv->err(dbenv, ret, "%s: directory read", dir); return (1); } for (cnt = fcnt; --cnt >= 0;) { /* * Skip files in DB's name space (but not Queue * extent files, we need them). */ if (!strncmp(names[cnt], LFPREFIX, sizeof(LFPREFIX) - 1)) continue; if (!strncmp(names[cnt], DB_REGION_PREFIX, sizeof(DB_REGION_PREFIX) - 1) && strncmp(names[cnt], QUEUE_EXTENT_PREFIX, sizeof(QUEUE_EXTENT_PREFIX) - 1)) continue; /* * Skip DB_CONFIG. */ if (!db_config && !strncmp(names[cnt], "DB_CONFIG", sizeof("DB_CONFIG"))) continue; /* Copy the file. */ if (db_hotbackup_data_copy(dbenv, names[cnt], dir, bd, verbose) != 0) return (1); } __os_dirfree(dbenv, names, fcnt); return (0);}/* * read_log_dir -- * * Read a directory looking for log files to copy. If home * is passed then we are possibly using a log dir in the destination, * following DB_CONFIG configuration. */intdb_hotbackup_read_log_dir(dbenv, home, backup_dir, log_dir, copy_minp, update, verbose) DB_ENV *dbenv; char *home, *backup_dir, *log_dir; int *copy_minp, update, verbose;{ u_int32_t aflag; int cnt, ret, v; char **begin, **names, *backupd, *logd; char from[DB_MAXPATHLEN], to[DB_MAXPATHLEN]; if (home != NULL && log_dir != NULL) { if ((size_t)snprintf(from, sizeof(from), "%s%c%s", home, PATH_SEPARATOR[0], log_dir) >= sizeof(from)) { dbenv->errx(dbenv, "%s%c%s: path too long", home, PATH_SEPARATOR[0], log_dir); return (1); } logd = strdup(from); if ((size_t)(cnt = snprintf(to, sizeof(to), "%s%c%s%c", backup_dir, PATH_SEPARATOR[0], log_dir, PATH_SEPARATOR[0])) >= sizeof(to)) { dbenv->errx(dbenv, "%s%c%s: path too long", backup_dir, PATH_SEPARATOR[0], log_dir); return (1); } backupd = strdup(to); /* Create the backup log directory. */ if ((ret = dbenv->set_intermediate_dir( dbenv, __db_omode("rwx------"), 0)) != 0 || (ret = __db_mkpath(dbenv, backupd)) != 0) { dbenv->err(dbenv, ret, "%s: cannot create", backupd); return (1); } /* Step on the trailing '/'. */ backupd[cnt - 1] = '\0'; } else { backupd = backup_dir; logd = log_dir; }again: aflag = DB_ARCH_LOG; /* * If this is an update and we are deleting files, first process * those files that can be removed, then repeat with the rest. */ if (update) aflag = 0; /* Get a list of file names to be copied. */ if ((ret = dbenv->log_archive(dbenv, &names, aflag)) != 0) { dbenv->err(dbenv, ret, "DB_ENV->log_archive"); return (1); } if (names == NULL) goto done; begin = names; for (; *names != NULL; names++) { /* Track the lowest-numbered log file copied. */ v = atoi(*names + sizeof(LFPREFIX) - 1); if (*copy_minp == 0 || *copy_minp > v) *copy_minp = v; if ((size_t)snprintf(from, sizeof(from), "%s%c%s", logd, PATH_SEPARATOR[0], *names) >= sizeof(from)) { dbenv->errx(dbenv, "%s%c%s: path too long", logd, PATH_SEPARATOR[0], *names); return (1); } /* * If we're going to remove the file, attempt to rename the * instead of copying and then removing. The likely failure * is EXDEV (source and destination are on different volumes). * Fall back to a copy, regardless of the error. We don't * worry about partial contents, the copy truncates the file * on open. */ if (update) { if ((size_t)snprintf(to, sizeof(to), "%s%c%s", backupd, PATH_SEPARATOR[0], *names) >= sizeof(to)) { dbenv->errx(dbenv, "%s%c%s: path too long", backupd, PATH_SEPARATOR[0], *names); return (1); } if (__os_rename(dbenv, from, to, 1) == 0) { if (verbose) printf("%s: moving %s to %s\n", progname, from, to); continue; } } /* Copy the file. */ if (db_hotbackup_data_copy(dbenv, *names, logd, backupd, verbose) != 0) return (1); if (update) { if (verbose) printf("%s: removing %s\n", progname, from); if ((ret = __os_unlink(dbenv, from)) != 0) { dbenv->err(dbenv, ret, "unlink of %s failed", from); return (1); } } } free(begin);done: if (update) { update = 0; goto again; } if (verbose && *copy_minp != 0) printf("%s: lowest numbered log file copied: %d\n", progname, *copy_minp); if (logd != log_dir) free(logd); if (backupd != backup_dir) free(backupd); return (0);}/* * data_copy -- * Copy a file into the backup directory. */intdb_hotbackup_data_copy(dbenv, file, from_dir, to_dir, verbose) DB_ENV *dbenv; char *file, *from_dir, *to_dir; int verbose;{ DB_FH *rfhp, *wfhp; size_t nr, nw; int ret; char *buf; ret = 0; rfhp = wfhp = NULL; if (verbose) printf("%s: copying %s%c%s to %s%c%s\n", progname, from_dir, PATH_SEPARATOR[0], file, to_dir, PATH_SEPARATOR[0], file); /* * We MUST copy multiples of the page size, atomically, to ensure a * database page is not updated by another thread of control during * the copy. * * !!! * The current maximum page size for Berkeley DB is 64KB; we will have * to increase this value if the maximum page size is ever more than a * megabyte */ if ((buf = malloc(MEGABYTE)) == NULL) { dbenv->err(dbenv, errno, "%lu buffer allocation", (u_long)MEGABYTE); return (1); } /* Open the input file. */ if (snprintf(buf, MEGABYTE, "%s%c%s", from_dir, PATH_SEPARATOR[0], file) >= MEGABYTE) { dbenv->errx(dbenv, "%s%c%s: path too long", from_dir, PATH_SEPARATOR[0], file); goto err; } if ((ret = __os_open(dbenv, buf, 0, DB_OSO_RDONLY, 0, &rfhp)) != 0) { dbenv->err(dbenv, ret, "%s", buf); goto err; } /* Open the output file. */ if (snprintf(buf, MEGABYTE, "%s%c%s", to_dir, PATH_SEPARATOR[0], file) >= MEGABYTE) { dbenv->errx(dbenv, "%s%c%s: path too long", to_dir, PATH_SEPARATOR[0], file); goto err; } if ((ret = __os_open(dbenv, buf, 0, DB_OSO_CREATE | DB_OSO_TRUNC, __db_omode(OWNER_RW), &wfhp)) != 0) { dbenv->err(dbenv, ret, "%s", buf); goto err; } /* Copy the data. */ while ((ret = __os_read(dbenv, rfhp, buf, MEGABYTE, &nr)) == 0 && nr > 0) if ((ret = __os_write(dbenv, wfhp, buf, nr, &nw)) != 0) break; if (0) {err: ret = 1; } if (buf != NULL) free(buf); if (rfhp != NULL && __os_closehandle(dbenv, rfhp) != 0) ret = 1; /* We may be running on a remote filesystem; force the flush. */ if (wfhp != NULL) { if (__os_fsync(dbenv, wfhp) != 0) ret = 1; if (__os_closehandle(dbenv, wfhp) != 0) ret = 1; } return (ret);}intdb_hotbackup_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);}intdb_hotbackup_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: version %d.%d doesn't match library version %d.%d\n", progname, DB_VERSION_MAJOR, DB_VERSION_MINOR, v_major, v_minor); return (EXIT_FAILURE); } return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -