Asterisk database

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:
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.



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.


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:
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


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.


The CLI commands can be used for simplistic replication. For example, a short bash script to 'replicate' the subkeys hotdesk-user and user-hotdesk on localhost to $host may look something like:

    #Remove entire subtrees on remote server:
    ssh -i /etc/asterisk/syncKey asterisk@$host \
        '/usr/sbin/asterisk -rx "database deltree hotdesk-user";'\
        '/usr/sbin/asterisk -rx "database deltree user-hotdesk"'
    #Now pipe lines from this server and add them to remote server at the other end
    (/usr/sbin/asterisk -rx "database show hotdesk-user"; /usr/sbin/asterisk -rx "database show user-hotdesk")|
       ssh -i /etc/asterisk/syncKey asterisk@$host \
           "perl -e 'while(<STDIN>){"\
               "system(\"/usr/sbin/asterisk\", \"-rx\", \"database put \$1 \$2 \$3\")"\
                    "if \$_ =~ m|[/]([^\s]+)[/]([^\s/]+)\s+:\s*(.+)|;}'"

There are a number of flaws with this technique but it should work with small amounts of data where complete integrity doesn't need to be ensured. It should also be noted that there are likely to be some escaping issues with the script above; it is wisest to limit this technique to situations where you can guarantee that the input is 'safe'. In the above situation hotdesk-user/user-hotdesk is assured to contain only a 4 digit number

Here's an alternative method if you are having trouble with escape characters:

/usr/sbin/asterisk -rx "database show SIP" | ssh -i ~/.ssh/ root@ "perl -e 'while(<STDIN>) { if (\$_ =~ /^\/SIP\/Registry\/(\S+)\s+:\ (\S+)\$/ ) {system(\"asterisk -rx \x22database put SIP Registry/\$1 \$2\x22\n\");}}'"

See also:

Alternatively, you can work with all databases in Asterisk dial plan (MS SQL, MySQL, Oracle, Sybase, Postgress...) using Visual Dialplan. It is rapid application development platform for Asterisk dialplan development. Visual Dialplan provides similar interface and approach like Visual Basic provides for rapid application development in Windows and comes with real world dialplan examples (extensions.conf files that can be used without visual dialplan) .

More information |
Free download of Visual Dialplan for Asterisk

Created by: oej, Last modification: Fri 01 of Jun, 2012 (12:09) by admin
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+