parent
59d30a49c3
commit
3e83b5b27b
@ -0,0 +1,70 @@ |
||||
.TH MDB_DUMP 1 "2014/06/20" "LMDB 0.9.14" |
||||
.\" Copyright 2014 Howard Chu, Symas Corp. All Rights Reserved. |
||||
.\" Copying restrictions apply. See COPYRIGHT/LICENSE. |
||||
.SH NAME |
||||
mdb_dump \- LMDB environment export tool |
||||
.SH SYNOPSIS |
||||
.B mdb_dump |
||||
.BR \ envpath |
||||
[\c |
||||
.BR \-V ] |
||||
[\c |
||||
.BI \-f \ file\fR] |
||||
[\c |
||||
.BR \-l ] |
||||
[\c |
||||
.BR \-n ] |
||||
[\c |
||||
.BR \-p ] |
||||
[\c |
||||
.BR \-a \ | |
||||
.BI \-s \ subdb\fR] |
||||
.SH DESCRIPTION |
||||
The |
||||
.B mdb_dump |
||||
utility reads a database and writes its contents to the |
||||
standard output using a portable flat-text format |
||||
understood by the |
||||
.BR mdb_load (1) |
||||
utility. |
||||
.SH OPTIONS |
||||
.TP |
||||
.BR \-V |
||||
Write the library version number to the standard output, and exit. |
||||
.TP |
||||
.BR \-f \ file |
||||
Write to the specified file instead of to the standard output. |
||||
.TP |
||||
.BR \-l |
||||
List the databases stored in the environment. Just the |
||||
names will be listed, no data will be output. |
||||
.TP |
||||
.BR \-n |
||||
Dump an LMDB database which does not use subdirectories. |
||||
.TP |
||||
.BR \-p |
||||
If characters in either the key or data items are printing characters (as defined by isprint(3)), output them directly. This option permits users to use standard text editors and tools to modify the contents of databases. |
||||
|
||||
Note: different systems may have different notions about what characters are considered printing characters, and databases dumped in this manner may be less portable to external systems. |
||||
.TP |
||||
.BR \-a |
||||
Dump all of the subdatabases in the environment. |
||||
.TP |
||||
.BR \-s \ subdb |
||||
Dump a specific subdatabase. If no database is specified, only the main database is dumped. |
||||
.SH DIAGNOSTICS |
||||
Exit status is zero if no errors occur. |
||||
Errors result in a non-zero exit status and |
||||
a diagnostic message being written to standard error. |
||||
|
||||
Dumping and reloading databases that use user-defined comparison functions will result in new databases that use the |
||||
default comparison functions. \fB In this case it is quite likely that the reloaded database will be damaged beyond |
||||
repair permitting neither record storage nor retrieval.\fP |
||||
|
||||
The only available workaround is to modify the source for the |
||||
.BR mdb_load (1) |
||||
utility to load the database using the correct comparison functions. |
||||
.SH "SEE ALSO" |
||||
.BR mdb_load (1) |
||||
.SH AUTHOR |
||||
Howard Chu of Symas Corporation <http://www.symas.com> |
@ -0,0 +1,276 @@ |
||||
/* mdb_dump.c - memory-mapped database dump tool */ |
||||
/*
|
||||
* Copyright 2011-2014 Howard Chu, Symas Corp. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted only as authorized by the OpenLDAP |
||||
* Public License. |
||||
* |
||||
* A copy of this license is available in the file LICENSE in the |
||||
* top-level directory of the distribution or, alternatively, at |
||||
* <http://www.OpenLDAP.org/license.html>.
|
||||
*/ |
||||
#include <stdio.h> |
||||
#include <errno.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <ctype.h> |
||||
#include <unistd.h> |
||||
#include "lmdb.h" |
||||
|
||||
#define PRINT 1 |
||||
static int mode; |
||||
|
||||
typedef struct flagbit { |
||||
int bit; |
||||
char *name; |
||||
} flagbit; |
||||
|
||||
flagbit dbflags[] = { |
||||
{ MDB_REVERSEKEY, "reversekey" }, |
||||
{ MDB_DUPSORT, "dupsort" }, |
||||
{ MDB_INTEGERKEY, "integerkey" }, |
||||
{ MDB_DUPFIXED, "dupfixed" }, |
||||
{ MDB_INTEGERDUP, "integerdup" }, |
||||
{ MDB_REVERSEDUP, "reversedup" }, |
||||
{ 0, NULL } |
||||
}; |
||||
|
||||
static const char hexc[] = "0123456789abcdef"; |
||||
|
||||
static void hex(unsigned char c) |
||||
{ |
||||
putchar(hexc[c >> 4]); |
||||
putchar(hexc[c & 0xf]); |
||||
} |
||||
|
||||
static void text(MDB_val *v) |
||||
{ |
||||
unsigned char *c, *end; |
||||
|
||||
putchar(' '); |
||||
c = v->mv_data; |
||||
end = c + v->mv_size; |
||||
while (c < end) { |
||||
if (isprint(*c)) { |
||||
putchar(*c); |
||||
} else { |
||||
putchar('\\'); |
||||
hex(*c); |
||||
} |
||||
c++; |
||||
} |
||||
putchar('\n'); |
||||
} |
||||
|
||||
static void byte(MDB_val *v) |
||||
{ |
||||
unsigned char *c, *end; |
||||
|
||||
putchar(' '); |
||||
c = v->mv_data; |
||||
end = c + v->mv_size; |
||||
while (c < end) { |
||||
hex(*c++); |
||||
} |
||||
putchar('\n'); |
||||
} |
||||
|
||||
/* Dump in BDB-compatible format */ |
||||
static int dumpit(MDB_txn *txn, MDB_dbi dbi, char *name) |
||||
{ |
||||
MDB_cursor *mc; |
||||
MDB_stat ms; |
||||
MDB_val key, data; |
||||
unsigned int flags; |
||||
int rc, i; |
||||
|
||||
rc = mdb_dbi_flags(txn, dbi, &flags); |
||||
if (rc) return rc; |
||||
|
||||
rc = mdb_stat(txn, dbi, &ms); |
||||
if (rc) return rc; |
||||
|
||||
printf("VERSION=3\n"); |
||||
printf("format=%s\n", mode & PRINT ? "print" : "bytevalue"); |
||||
if (name) |
||||
printf("database=%s\n", name); |
||||
printf("type=btree\n"); |
||||
|
||||
if (flags & MDB_DUPSORT) |
||||
printf("duplicates=1\n"); |
||||
|
||||
for (i=0; dbflags[i].bit; i++) |
||||
if (flags & dbflags[i].bit) |
||||
printf("%s=1\n", dbflags[i].name); |
||||
|
||||
printf("db_pagesize=%d\n", ms.ms_psize); |
||||
printf("HEADER=END\n"); |
||||
|
||||
rc = mdb_cursor_open(txn, dbi, &mc); |
||||
if (rc) return rc; |
||||
|
||||
while ((rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT) == MDB_SUCCESS)) { |
||||
if (mode & PRINT) { |
||||
text(&key); |
||||
text(&data); |
||||
} else { |
||||
byte(&key); |
||||
byte(&data); |
||||
} |
||||
} |
||||
printf("DATA=END\n"); |
||||
if (rc == MDB_NOTFOUND) |
||||
rc = MDB_SUCCESS; |
||||
|
||||
return rc; |
||||
} |
||||
|
||||
static void usage(char *prog) |
||||
{ |
||||
fprintf(stderr, "usage: %s dbpath [-V] [-f output] [-l] [-n] [-p] [-a|-s subdb]\n", prog); |
||||
exit(EXIT_FAILURE); |
||||
} |
||||
|
||||
int main(int argc, char *argv[]) |
||||
{ |
||||
int i, rc; |
||||
MDB_env *env; |
||||
MDB_txn *txn; |
||||
MDB_dbi dbi; |
||||
char *prog = argv[0]; |
||||
char *envname; |
||||
char *subname = NULL; |
||||
int alldbs = 0, envflags = 0, list = 0; |
||||
|
||||
if (argc < 2) { |
||||
usage(prog); |
||||
} |
||||
|
||||
/* -a: dump main DB and all subDBs
|
||||
* -s: dump only the named subDB |
||||
* -n: use NOSUBDIR flag on env_open |
||||
* -p: use printable characters |
||||
* -f: write to file instead of stdout |
||||
* -V: print version and exit |
||||
* (default) dump only the main DB |
||||
*/ |
||||
while ((i = getopt(argc, argv, "af:lnps:V")) != EOF) { |
||||
switch(i) { |
||||
case 'V': |
||||
printf("%s\n", MDB_VERSION_STRING); |
||||
exit(0); |
||||
break; |
||||
case 'l': |
||||
list = 1; |
||||
/*FALLTHROUGH*/; |
||||
case 'a': |
||||
if (subname) |
||||
usage(prog); |
||||
alldbs++; |
||||
break; |
||||
case 'f': |
||||
if (freopen(optarg, "w", stdout) == NULL) { |
||||
fprintf(stderr, "%s: %s: reopen: %s\n", |
||||
prog, optarg, strerror(errno)); |
||||
exit(EXIT_FAILURE); |
||||
} |
||||
break; |
||||
case 'n': |
||||
envflags |= MDB_NOSUBDIR; |
||||
break; |
||||
case 'p': |
||||
mode |= PRINT; |
||||
break; |
||||
case 's': |
||||
if (alldbs) |
||||
usage(prog); |
||||
subname = optarg; |
||||
break; |
||||
default: |
||||
usage(prog); |
||||
} |
||||
} |
||||
|
||||
if (optind != argc - 1) |
||||
usage(prog); |
||||
|
||||
envname = argv[optind]; |
||||
rc = mdb_env_create(&env); |
||||
|
||||
if (alldbs || subname) { |
||||
mdb_env_set_maxdbs(env, 2); |
||||
} |
||||
|
||||
rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664); |
||||
if (rc) { |
||||
printf("mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc)); |
||||
goto env_close; |
||||
} |
||||
|
||||
rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
||||
if (rc) { |
||||
printf("mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc)); |
||||
goto env_close; |
||||
} |
||||
|
||||
rc = mdb_open(txn, subname, 0, &dbi); |
||||
if (rc) { |
||||
printf("mdb_open failed, error %d %s\n", rc, mdb_strerror(rc)); |
||||
goto txn_abort; |
||||
} |
||||
|
||||
if (alldbs) { |
||||
MDB_cursor *cursor; |
||||
MDB_val key; |
||||
int count = 0; |
||||
|
||||
rc = mdb_cursor_open(txn, dbi, &cursor); |
||||
if (rc) { |
||||
printf("mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc)); |
||||
goto txn_abort; |
||||
} |
||||
while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) { |
||||
char *str; |
||||
MDB_dbi db2; |
||||
if (memchr(key.mv_data, '\0', key.mv_size)) |
||||
continue; |
||||
count++; |
||||
str = malloc(key.mv_size+1); |
||||
memcpy(str, key.mv_data, key.mv_size); |
||||
str[key.mv_size] = '\0'; |
||||
rc = mdb_open(txn, str, 0, &db2); |
||||
if (rc == MDB_SUCCESS) { |
||||
if (list) { |
||||
printf("%s\n", str); |
||||
list++; |
||||
} else { |
||||
rc = dumpit(txn, db2, str); |
||||
} |
||||
mdb_close(env, db2); |
||||
} |
||||
free(str); |
||||
if (rc) continue; |
||||
} |
||||
mdb_cursor_close(cursor); |
||||
if (!count) { |
||||
fprintf(stderr, "%s: %s does not contain multiple databases\n", prog, envname); |
||||
rc = MDB_NOTFOUND; |
||||
} else if (rc == MDB_NOTFOUND) { |
||||
rc = MDB_SUCCESS; |
||||
} |
||||
} else { |
||||
rc = dumpit(txn, dbi, subname); |
||||
} |
||||
if (rc && rc != MDB_NOTFOUND) |
||||
fprintf(stderr, "%s: %s: %s\n", prog, envname, mdb_strerror(rc)); |
||||
|
||||
mdb_close(env, dbi); |
||||
txn_abort: |
||||
mdb_txn_abort(txn); |
||||
env_close: |
||||
mdb_env_close(env); |
||||
|
||||
return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
||||
} |
Loading…
Reference in new issue