Asterisk database
Created by: oej,Last modification on Tue 24 of Feb, 2009 [10:52 UTC] by olivier2831
Also known as AstDB; the Asterisk database uses version 1 of the Berkeley DB which works very much like the Windows registry.
From the source code of db.c:
Database data are grouped in families and identified with a key that is unique within the family.
There are other commands you can use from the command line interface.
See DB_File example; Tip: Put your pearl script into /var/lib/asterisk and have it access the file "astdb" directly from there using the BTREE database type.
On Debian platform, this db1_dump185 tool should be found in /usr/src/asterisk/main/db1-ast directory, with appropriate make command is launched. Alternatively, using package db4.5-util installs a db4.5_dump which should be equivalent to db1_dump185 (please edit this, if not exact).
FreePBX apparently has a PHP-based backup and restore script called restoreastdb.php.
Also the CLI command 'asterisk -rx "database show" > dbdata.txt' can be used for backup purposes - if you figure out an elegant method to upload the results again into astdb.
Apart from that you can also simply copy the file 'astdb' from one Asterisk to another.
From the source code of db.c:
DB3 is licensed under Sleepycat Public License and is thus incompatible with GPL. To avoid having to make another exception (and complicate licensing even further) we elect to use DB1 which is BSD licensed
Database data are grouped in families and identified with a key that is unique within the family.
Families
- cidname; used by LookupCIDName
- blacklist: used by LookupBlacklist
Applications
These are database operations that you can use from the dial plan in extensions.conf:- DBdel: Delete a key from the database
- DBdeltree: Delete a family or keytree from the database
- DBget: Retrieve a value from the database
- DBput: Store a value in the database
There are other commands you can use from the command line interface.
Examples
- Web based Asterisk Database maintenance
- Simple web interface for editing values in the Asterisk database
- See Asterisk call forwarding
Script libraries: PERL and PHP
- PERL: The module DB_File supports Berkeley DB version 1, as used in Asterisk. You have to make sure you compile it for version 1 and libdb1.a (as found in the db1-ast directory in your source tree.
To build DB_File using the db1-ast:
edit config.in
INCLUDE = /usr/src/asterisk-cvs/asterisk/db1-ast/include
LIB = /usr/src/asterisk-cvs/asterisk/db1-ast
DBNAME= -ldb1
perl Makefile.PL
make && make pure_site_install
edit config.in
INCLUDE = /usr/src/asterisk-cvs/asterisk/db1-ast/include
LIB = /usr/src/asterisk-cvs/asterisk/db1-ast
DBNAME= -ldb1
perl Makefile.PL
make && make pure_site_install
See DB_File example; Tip: Put your pearl script into /var/lib/asterisk and have it access the file "astdb" directly from there using the BTREE database type.
- PHP: Look at at the (deprecated) "dbm" DBA handler
Backup/Restore
If you use the builtin database to store stuff like CFD numbers and speeddials it makes sense to create a backup once in a while. The database is normally stored in the file /var/lib/asterisk/astdb. Also, since it's a normal Berkely db1 (version185) file its contents can be viewed/dumped with the standard db1_dump185 tool. Thus db1_dump185 -p /var/lib/asterisk/astdb will show the complete database tree.On Debian platform, this db1_dump185 tool should be found in /usr/src/asterisk/main/db1-ast directory, with appropriate make command is launched. Alternatively, using package db4.5-util installs a db4.5_dump which should be equivalent to db1_dump185 (please edit this, if not exact).
FreePBX apparently has a PHP-based backup and restore script called restoreastdb.php.
Also the CLI command 'asterisk -rx "database show" > dbdata.txt' can be used for backup purposes - if you figure out an elegant method to upload the results again into astdb.
Apart from that you can also simply copy the file 'astdb' from one Asterisk to another.
See also:
- Asterisk app_dbodbc: An implementation of the same functionality using unixODBC
- Asterisk - documentation of application commands
- DBQuery: An application which does allow you to execute sql queries againt a MySQL Server out of the dialplan.
- Asterisk | FAQ | Tips & Tricks

Comments