Examining Berkeley DB files from the CLI
Asked Answered
D

9

63

I have a set of Berkeley DB files on my Linux file system that I'd like to examine.

What useful tools exist for getting a quick overview of the contents? I can write Perl scripts that use BDB modules for examining them, but I'm looking for some CLI utility to be able to take a look inside without having to start writing scripts.

Delilahdelimit answered 1/9, 2008 at 9:10 Comment(0)
D
28

Check out the db-utils package. If you use apt, you can install it with the following: apt-get install db-util (or apt-get install db4.8-util or whatever version you have or prefer.)

Additional links:

Dissymmetry answered 1/9, 2008 at 9:24 Comment(2)
An example of how to make sense of the output of db4.8_dump or whatever from that package would be handy. The output format isn't even described in the dump man page itself, and even that doesn't describe 'bytevalue' format or whatever it is..... For prettier output without needing to find and install a package, see the answers using python, e.g. by trjh.Earthman
my package was names by db-utilPeralta
L
63

Use the db_dump program. It is contained in the package core/db (Arch), db-util (Debian, Ubuntu), sys-libs/db (Gentoo, note that here the binary is called db4.8_dump or whatever version you are using).

On some systems the man pages are not installed, in that case the documentation can be found here. By default, db_dump outputs some hex numbers, which is not really useful if you are trying to analyse the content of a database. Use the -p argument to change this.

Show everything that’s in the file database.db:

db_dump -p database.db

List the databases in the file database.db:

db_dump -l database.db

Show only the content of the database mydb in the file database.db:

db_dump -p -s mydb database.db
Limpet answered 20/12, 2015 at 1:47 Comment(1)
should be top answerDena
D
28

Check out the db-utils package. If you use apt, you can install it with the following: apt-get install db-util (or apt-get install db4.8-util or whatever version you have or prefer.)

Additional links:

Dissymmetry answered 1/9, 2008 at 9:24 Comment(2)
An example of how to make sense of the output of db4.8_dump or whatever from that package would be handy. The output format isn't even described in the dump man page itself, and even that doesn't describe 'bytevalue' format or whatever it is..... For prettier output without needing to find and install a package, see the answers using python, e.g. by trjh.Earthman
my package was names by db-utilPeralta
B
16

I found @strickli's answer to be the most helpful, as I didn't want to add any new packages to the machine with the database I was on. However, the db file I was reading was of type btree, not hash, so I had to use bsddb

# file foo.db
foo.db: Berkeley DB (Btree, version 9, native byte-order)

# python
>>> import bsddb
>>> for k, v in bsddb.btopen("*<db filename here...>*").iteritems():
...     print k,v
...
Bulbar answered 11/4, 2014 at 16:48 Comment(0)
L
10

As mentioned in the other answers, the db-utils package (db4-utils under RHEL) has some tools. However, db_dump can be unhelpful, since the output is 'bytevalue' format.

For a quick'n'dirty viewer, use python:

me@machine$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
>>> import dbhash
>>> for k, v in dbhash.open( *<db filename here...>* ).iteritems(): print k, v
...

Note that dbhash is deprecated since python 2.6.

Lutanist answered 5/11, 2013 at 16:7 Comment(0)
R
6

The db_hotbackup utility creates "hot backup" or "hot failover" snapshots of Berkeley DB database environments. Install it with the following

apt-get install db-util

then run following command to take hot backup

db_hotbackup [-cDEguVv] [-d data_dir ...] [-h home] [-l log_dir] [-P password] -b backup_dir

Renowned answered 20/2, 2012 at 3:52 Comment(0)
F
6

Once you have installed the db utils you can simple do a db_dump on the db file.

Formic answered 15/2, 2013 at 0:55 Comment(0)
M
4

Note that the initial answer says to use "db-utils" package, but the example shows the correct "db-util" package. (with no "s")

Merino answered 20/3, 2013 at 20:4 Comment(0)
D
1

Under Amazon Linux you can install it with:

yum install db43-utils

Depend answered 27/4, 2015 at 19:54 Comment(0)
A
1

Python 3

from bsddb3 import db
import collections
d = db.DB()
d.open('./file.dat', 'dbname', db.DB_BTREE, db.DB_THREAD | db.DB_RDONLY)
d.keys()
collections.OrderedDict((k, d[k]) for k in d.keys())
Ancestor answered 23/2, 2022 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.