Skip to content

Commit

Permalink
Use dbconfig for Debian based distributions
Browse files Browse the repository at this point in the history
dbconfig-common is a standard mechanism for Debian based distributions,
to assists applications that require a database.

The user still has the choice to configure the database manually, if not,
dbconfig detects what database backends are installed, and the user gets
asked, what connection parameter he wants to use.

If dbconfig is configured correctly, database schema updated are done automatically
on package updates. With export DEBIAN_FRONTEND=noninteractive the user don't get ask,
and dbconfig configures the database backend as good as he can.

Signed-off-by: Marco van Wieringen <marco.van.wieringen@bareos.com>
  • Loading branch information
joergsteffens authored and Marco van Wieringen committed May 8, 2014
1 parent b589fbf commit 1f02cc8
Show file tree
Hide file tree
Showing 24 changed files with 531 additions and 61 deletions.
120 changes: 120 additions & 0 deletions README.dbconfig
@@ -0,0 +1,120 @@
On Debian based systems (Debian, Ubuntu, Univention Corporate Server),
database configuration can be done with help of the dbconfig system.

* Package: dbconfig-common
* Homepage/Documentation: http://people.debian.org/~seanius/policy/dbconfig-common.html/

Install/update scenarios:
* fresh install
* preinstalled 2001
* preinstalled 2002
* 12: 2001 -> 2002, 13: 2001 -> 2002: update from 12 to 13

Behavior:
* config file: /etc/dbconfig-common/bareos.conf
* sql files stored at
* /usr/share/dbconfig-common/data/bareos-database-common/upgrade/pgsql/2001
* /usr/share/dbconfig-common/data/bareos-database-common/upgrade/pgsql/2002
* /usr/share/dbconfig-common/data/bareos-database-common/upgrade/mysql/2001
* /usr/share/dbconfig-common/data/bareos-database-common/upgrade/mysql/2002
* /usr/share/dbconfig-common/data/bareos-database-common/install/pgsql
* /usr/share/dbconfig-common/data/bareos-database-common/install/mysql

Upgrade:
* bareos-database-common.postinst
* every file from /usr/share/dbconfig-common/data/bareos-database-common/upgrade/DATABASE/*, that is larger than parameter $2 (package version of replaced package) will be installed.
* even if the filename "is larger" than current package version
* dbconfig does not store the installed version. It uses only the old and the current package version.
* in Baroes, different branches can each do a database version update, example:
* 12.4.6: 2001
* 12.4.7: 2002
* 12.4.8: 2003
* 13.2.2: 2001
* 13.2.3: 2002
* 13.2.4: 2003
* using standard dbconfig this could result in following
* updating from 12.4.6 to 13.2.3 would result in a database update from
* 2001 (12.4.6) -> 2002 (12.4.7) -> 2003 (12.4.8) -> 2001 (13.2.2) ... => failure
* Bareos modifies the dbconfig behavior by not working with package versions, but database versions:
* bareos-database-common.config, bareos-database-common.postinst:
* instead of passing parameter $2 (old package version), this gots translated to database version with the help of a map file (versions.map).
* with the help of this, every database schema update is only be done once
* how to handle package update from version without dbconfig to version with it?
Bareos dbconfig will be introduced with some version >= 14.1.0.
The latest database version for 12.4 and 13.2 will be 2002.
Therefore we claim, that the first version using db_config will be 2003, even if it is only 2002. Using this, all existing database updates get applied.
if dpkg --compare-versions "$param2_orig" lt "14.1.0"; then
dbc_first_version="2003"
...
fi

Database Permissions by dbconfig:

MySQL:
GRANT USAGE ON *.* TO 'bareos'@'localhost' IDENTIFIED BY PASSWORD '*3E80BB05233BE488EE70C1D6494E2F2DB00FEBB4'
GRANT ALL PRIVILEGES ON `bareos`.* TO 'bareos'@'localhost'

PostgreSQL:
bareos will be the database owner

Testing:
# bareos-database-dbconfig
# ~/dbconf
# fakeroot debian/rules binary
# /var/lib/dpkg/info/*.postinst ...
/var/log/dbconfig-common/dbc.log

eval "`dbconfig-generate-include /etc/dbconfig-common/bareos-database-common.conf`"



Behavior
========

noninteractive
==============

export DEBIAN_FRONTEND=noninteractive
echo "bareos-database-common bareos-database-common/mysql/admin-pass select linuxlinux" | debconf-set-selections

postgresql
==========

* install
* /etc/dbconfig-common/bareos-database-common.conf created
* db: setup
* update from 12.4:
* updates db_version from 2001 to 2002.
* update from 13.2:
* db_version is already 2002. It detects, nothing to do.
* update dbconfig already configured
* ?

mysql
=====

* install
* /etc/dbconfig-common/bareos-database-common.conf created
* db: setup
* dbpass must be set to bareos-dir.conf
* update from 12.4:
* updates db_version from 2001 to 2002.
* update from 13.2:
* db_version is already 2002. It detects, nothing to do.
* update dbconfig already configured
* ?

sqlite3
=======

* install
* /etc/dbconfig-common/bareos-database-common.conf created
* db: setup
* creates link from /var/lib/bareos/bareos.db to bareos
* update from 12.4:
* updates db_version from 2001 to 2002.
* update from 13.2:
* db_version is already 2002. It detects, nothing to do.
* creates link from /var/lib/bareos/bareos.db to bareos
* update dbconfig already configured
* ?
17 changes: 12 additions & 5 deletions autoconf/configure.in
Expand Up @@ -3731,7 +3731,8 @@ debian)
platforms/debian/Makefile \
platforms/debian/bareos-fd \
platforms/debian/bareos-sd \
platforms/debian/bareos-dir"
platforms/debian/bareos-dir \
platforms/debian/set_dbconfig_vars.sh"
;;
freebsd)
VER=`echo $DISTVER | cut -c 1`
Expand Down Expand Up @@ -3877,9 +3878,11 @@ ubuntu)
PSCMD="ps -e -o pid,command"
PFILES="${PFILES} \
platforms/ubuntu/Makefile \
platforms/ubuntu/bareos-fd \
platforms/ubuntu/bareos-sd \
platforms/ubuntu/bareos-dir"
platforms/debian/Makefile \
platforms/debian/bareos-fd \
platforms/debian/bareos-sd \
platforms/debian/bareos-dir \
platforms/debian/set_dbconfig_vars.sh"
;;
univention)
TAPEDRIVE="/dev/nst0"
Expand All @@ -3889,7 +3892,8 @@ univention)
platforms/debian/Makefile \
platforms/debian/bareos-fd \
platforms/debian/bareos-sd \
platforms/debian/bareos-dir"
platforms/debian/bareos-dir \
platforms/debian/set_dbconfig_vars.sh"
;;
unknown)
TAPEDRIVE="/dev/nst0"
Expand Down Expand Up @@ -3969,6 +3973,8 @@ autoconf/Make.common \
Makefile \
manpages/Makefile \
debian/bareos-common.preinst \
debian/bareos-database-common.config \
debian/bareos-database-common.postinst \
debian/bareos-filedaemon.preinst \
debian/bareos-director.preinst \
debian/bareos-storage.preinst \
Expand Down Expand Up @@ -4018,6 +4024,7 @@ src/cats/make_bareos_tables \
src/cats/drop_bareos_tables \
src/cats/drop_bareos_database \
src/cats/install-default-backend \
src/cats/ddl/versions.map \
src/findlib/Makefile \
src/lmdb/Makefile \
src/ndmp/Makefile \
Expand Down
98 changes: 98 additions & 0 deletions debian/bareos-database-common.config.in
@@ -0,0 +1,98 @@
#!/bin/sh

set -e

. /usr/share/debconf/confmodule

#set -x
#echo "bareos-database-common.config: $@" >&2

if [ -r @scriptdir@/bareos-config-lib.sh ]; then
. @scriptdir@/bareos-config-lib.sh
if [ -f /usr/share/dbconfig-common/dpkg/config ]; then
. /usr/share/dbconfig-common/dpkg/config

dbc_debug=1
# prevent errors by using || true
dbc_dbname=`get_database_name bareos` || true
dbc_dbuser=`get_database_user bareos` || true

# only required for sqlite3
dbc_basepath=`get_working_dir`

# convert Bareos databases types into dbconfig_dbtypes
dbc_dbtypes=""
DB_INSTALLED=`get_databases_installed`
if echo "$DB_INSTALLED" | grep -q postgresql; then dbc_dbtypes="${dbc_dbtypes}pgsql, "; fi
if echo "$DB_INSTALLED" | grep -q mysql ; then dbc_dbtypes="${dbc_dbtypes}mysql, "; fi
if echo "$DB_INSTALLED" | grep -q sqlite3 ; then dbc_dbtypes="${dbc_dbtypes}sqlite3, "; fi
# remove trailing ,
dbc_dbtypes=`echo ${dbc_dbtypes} | sed 's/, *$//'`

# action
param1="$1"
# $2: when action is "configure": most-recently-configured-version
param2="$2"
param2_orig=$param2
shift 2

# When upgrading from an older version (param2),
# do not pass the version number of the old package.
# Instead we pass the database version number of the old package.
if [ "$param2" ]; then
case "$param1" in
configure|reconfigure)
param2=`get_database_version_by_release "$param2"`

# dbconfig is available since Bareos version >= 14.1.0.
# TODO: currently the database version for the first version using dbconfig
# is identical with the last version without dbconfig.
# Therefore we fake dbc_first_version and set it to 2003.
# As soon as there is a database version 2003,
# this will be true anyway.
if dpkg --compare-versions "$param2_orig" lt "14.1.0"; then
bareos_migrate_to_dbconfig="yes"
dbc_first_version="2003"
dbc_load_include="sh:/usr/lib/bareos/scripts/set_dbconfig_vars.sh"
# empty passwords require special treatment, see below
bareos_database_password=`get_database_password` || true
fi
;;
*)
;;
esac
fi

dbc_go bareos-database-common $param1 $param2 $@

if [ "$bareos_migrate_to_dbconfig" = "yes" ]; then
# TODO: check if settings app-pass to "" is really required.
# Or is it only required for "DEBIAN_FRONTEND=noninteractive" ?
case "`get_database_driver`" in
postgresql)
if [ -z "${bareos_database_password}" ]; then
#db_set bareos-database-common/pgsql/authmethod-user "ident"
# workaround: if an empty database password is defined, explicitly set it
db_set bareos-database-common/pgsql/app-pass ""
fi
;;
mysql)
if [ -z "${bareos_database_password}" ]; then
# workaround: if an empty mysql password is defined, explicitly set it
db_set bareos-database-common/mysql/app-pass ""
fi
;;
sqlite3)
# dbconfig expects sqlite files as "dbname.db"
# while Bareos expects them as "dbname".
# A link is created, so that both names matches.
if [ -e "${dbc_basepath}/${dbc_dbname}.db" ]; then
if ! [ -e "${dbc_basepath}/${dbc_dbname}" ]; then
ln -s "${dbc_basepath}/${dbc_dbname}.db" "${dbc_basepath}/${dbc_dbname}"
fi
fi
;;
esac
fi
fi
fi
2 changes: 2 additions & 0 deletions debian/bareos-database-common.install
Expand Up @@ -7,4 +7,6 @@
/usr/lib/bareos/scripts/drop_bareos_tables
/usr/lib/bareos/scripts/grant_bareos_privileges
/usr/lib/bareos/scripts/make_bareos_tables
/usr/lib/bareos/scripts/set_dbconfig_vars.sh
/usr/lib/bareos/scripts/update_bareos_tables
/usr/lib/bareos/scripts/ddl/versions.map
56 changes: 56 additions & 0 deletions debian/bareos-database-common.postinst.in
@@ -0,0 +1,56 @@
#!/bin/sh

set -e

# summary of how this script can be called:
# * <postinst> `configure' <most-recently-configured-version>
# * <old-postinst> `abort-upgrade' <new version>
# * <conflictor's-postinst> `abort-remove' `in-favour' <package>
# <new-version>
# * <postinst> `abort-remove'
# * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
# <failed-install-package> <version> `removing'
# <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

#set -x
#echo "bareos-database-common.postinst: $@" >&2

. /usr/share/debconf/confmodule

if [ -r @scriptdir@/bareos-config-lib.sh ]; then
. @scriptdir@/bareos-config-lib.sh
if [ -f /usr/share/dbconfig-common/dpkg/postinst ]; then
. /usr/share/dbconfig-common/dpkg/postinst

# action
param1="$1"
# when action is "configure": most-recently-configured-version
param2="$2"
shift 2
if [ "$param2" ]; then
case "$param1" in
configure|reconfigure)
param2=`get_database_version_by_release "$param2"`
;;
*)
;;
esac
fi

# dbc_pgsql_createdb_encoding: required for postgresql
dbc_pgsql_createdb_encoding="SQL_ASCII"

# dbc_dbfile_owner: only required for sqlite3
dbc_dbfile_owner=`get_user_dir`:`get_group_dir`

# run dbconfig
dbc_go bareos-database-common $param1 $param2 $@

# apply the dbconfig settings
apply_dbconfig_settings
fi
fi

#DEBHELPER#
13 changes: 13 additions & 0 deletions debian/bareos-database-common.postrm
@@ -0,0 +1,13 @@
#!/bin/sh

set -e

if [ -f /usr/share/debconf/confmodule ]; then
. /usr/share/debconf/confmodule
fi
if [ -f /usr/share/dbconfig-common/dpkg/postrm ]; then
. /usr/share/dbconfig-common/dpkg/postrm
dbc_go bareos-database-common $@
fi

#DEBHELPER#
11 changes: 11 additions & 0 deletions debian/bareos-database-common.prerm
@@ -0,0 +1,11 @@
#!/bin/sh

set -e

. /usr/share/debconf/confmodule
if [ -f /usr/share/dbconfig-common/dpkg/prerm ]; then
. /usr/share/dbconfig-common/dpkg/prerm
dbc_go bareos-database-common $@
fi

#DEBHELPER#
2 changes: 2 additions & 0 deletions debian/bareos-database-mysql.install
@@ -1,2 +1,4 @@
/usr/lib/libb*-mysql*.so
/usr/lib/bareos/scripts/ddl/*/mysql*.sql
/usr/share/dbconfig-common/data/bareos-database-common/install/mysql
/usr/share/dbconfig-common/data/bareos-database-common/upgrade-dbadmin/mysql/*
2 changes: 2 additions & 0 deletions debian/bareos-database-postgresql.install
@@ -1,2 +1,4 @@
/usr/lib/libb*-postgresql*.so
/usr/lib/bareos/scripts/ddl/*/postgresql*.sql
/usr/share/dbconfig-common/data/bareos-database-common/install/pgsql
/usr/share/dbconfig-common/data/bareos-database-common/upgrade-dbadmin/pgsql/*
2 changes: 2 additions & 0 deletions debian/bareos-database-sqlite3.install
@@ -1,2 +1,4 @@
/usr/lib/libb*-sqlite3*.so
/usr/lib/bareos/scripts/ddl/*/sqlite3*.sql
/usr/share/dbconfig-common/data/bareos-database-common/install/sqlite3
/usr/share/dbconfig-common/data/bareos-database-common/upgrade-dbadmin/sqlite3/*
1 change: 1 addition & 0 deletions debian/bareos-director.postinst
Expand Up @@ -42,6 +42,7 @@ case "$1" in
/usr/lib/bareos/scripts/bareos-config initialize_local_hostname
/usr/lib/bareos/scripts/bareos-config initialize_passwords
/usr/lib/bareos/scripts/bareos-config initialize_database_driver
/usr/lib/bareos/scripts/bareos-config apply_dbconfig_settings
enable_rc_scripts
;;
abort-upgrade|abort-remove|abort-deconfigure)
Expand Down

0 comments on commit 1f02cc8

Please sign in to comment.