Upgrade 3CX to v18 and get it hosted free!

Asterisk Database

Author image

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.

Families

Applications

These are database operations that you can use from the dial plan in extensions.conf:

  • DBdel: Delete a key 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

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

Replication

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:

    ###Hotdesk##
    #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/mykey.pub [email protected] "perl -e 'while(<STDIN>) { if ($_ =~ /^/SIP/Registry/(S+)s+: (S+)$/ ) {system("asterisk -rx x22database put SIP Registry/$1 $2x22n");}}'"

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


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.