From 6f4dc6d428b4ad1bd36274e7bb4e85cf9b0a6cf8 Mon Sep 17 00:00:00 2001 From: Joerg Steffens Date: Thu, 30 Jan 2014 10:00:32 +0100 Subject: [PATCH] Rework database scripting. Having the actual DDL (Data Definition Language) in scripts is not very flexible so we move all DDL into seperate files. We also dropped all database specific scripting and as such the generic script now knows for all supported database types what to do on create/update/grant and drop. You can also have a read-only user on your database that is allowed to select stuff but not anything else that might be interesting for Management Queries. The update script now can also upgrade from a database that is several versions old. For now that means you can upgrade from a Bacula database to the first Bareos database schema and also to the new Database schema we are introducing with this patch. For the future it means the script will perform all needed updates for you if its has the wanted so called conversion available (e.g. the way to go from one version to an other.) We also added more functionality to the bareos-config script and splitted the function into a bareos-config-lib which can be included by scripts so they can get config information like the working dir etc. This is already used in all the new database scripts. We also no longer use explicit absolute pathnames for database utilities but make sure the utility can be found via the PATH, if your PATH already provides the utility it will be used and otherwise the value will be used that was determined at compile time. Fixes #236: Hardcoded path for postgres (verison) in scripts --- autoconf/configure.in | 38 +- debian/bareos-common.install | 1 + debian/bareos-database-mysql.install | 2 +- debian/bareos-database-postgresql.install | 2 +- debian/bareos-database-sqlite3.install | 2 +- platforms/packaging/bareos.spec | 12 +- scripts/Makefile.in | 1 + scripts/bareos-config-lib.sh.in | 489 ++++++++++++++++++ scripts/bareos-config.in | 278 +--------- src/cats/Makefile.in | 48 +- src/cats/create_bareos_database.in | 164 +++++- src/cats/create_ingres_database.in | 32 -- src/cats/create_mysql_database.in | 17 - src/cats/create_postgresql_database.in | 61 --- src/cats/create_sqlite3_database.in | 16 - .../creates/ingres.sql} | 142 ++--- .../creates/mysql.sql} | 54 +- .../creates/postgresql.sql} | 58 ++- .../creates/sqlite3.sql} | 45 +- .../drops/ingres.sql} | 22 +- .../drops/mysql.sql} | 19 +- src/cats/ddl/drops/postgresql.sql | 30 ++ src/cats/ddl/grants/ingres-readonly.sql | 32 ++ src/cats/ddl/grants/ingres.sql | 51 ++ src/cats/ddl/grants/mysql-readonly.sql | 5 + src/cats/ddl/grants/mysql.sql | 4 + src/cats/ddl/grants/postgresql-readonly.sql | 33 ++ src/cats/ddl/grants/postgresql.sql | 52 ++ src/cats/ddl/updates/mysql.14_2001.sql | 19 + src/cats/ddl/updates/mysql.2001_2002.sql | 37 ++ src/cats/ddl/updates/postgresql.14_2001.sql | 27 + src/cats/ddl/updates/postgresql.2001_2002.sql | 44 ++ src/cats/ddl/updates/sqlite3.14_2001.sql | 21 + src/cats/ddl/updates/sqlite3.2001_2002.sql | 40 ++ src/cats/delete_catalog_backup.in | 31 +- src/cats/drop_bareos_database.in | 102 +++- src/cats/drop_bareos_tables.in | 151 +++++- src/cats/drop_ingres_database.in | 17 - src/cats/drop_mysql_database.in | 17 - src/cats/drop_postgresql_database.in | 15 - src/cats/drop_postgresql_tables.in | 45 -- src/cats/drop_sqlite3_database.in | 8 - src/cats/drop_sqlite3_tables.in | 8 - src/cats/fix_postgresql_tables | 35 -- src/cats/grant_bareos_privileges.in | 126 ++++- src/cats/grant_ingres_privileges.in | 74 --- src/cats/grant_mysql_privileges.in | 27 - src/cats/grant_postgresql_privileges.in | 74 --- src/cats/grant_privileges | 10 - src/cats/grant_sqlite3_privileges.in | 7 - src/cats/ingres.in | 8 - src/cats/make_bareos_tables.in | 145 +++++- src/cats/make_catalog_backup.in | 57 +- src/cats/make_ingres_catalog_backup.in | 42 +- src/cats/mysql.in | 8 - src/cats/postgresql.in | 8 - src/cats/restore_ingres_catalog_backup.in | 42 +- src/cats/sqlite.in | 7 - src/cats/update_bareos_tables.in | 179 ++++++- src/cats/update_ingres_tables.in | 12 - src/cats/update_mysql_tables.in | 50 -- src/cats/update_postgresql_tables.in | 58 --- src/cats/update_sqlite3_tables.in | 50 -- 63 files changed, 1965 insertions(+), 1346 deletions(-) create mode 100644 scripts/bareos-config-lib.sh.in delete mode 100755 src/cats/create_ingres_database.in delete mode 100644 src/cats/create_mysql_database.in delete mode 100644 src/cats/create_postgresql_database.in delete mode 100644 src/cats/create_sqlite3_database.in rename src/cats/{make_ingres_tables.in => ddl/creates/ingres.sql} (85%) mode change 100755 => 100644 rename src/cats/{make_mysql_tables.in => ddl/creates/mysql.sql} (93%) rename src/cats/{make_postgresql_tables.in => ddl/creates/postgresql.sql} (93%) rename src/cats/{make_sqlite3_tables.in => ddl/creates/sqlite3.sql} (93%) rename src/cats/{drop_ingres_tables.in => ddl/drops/ingres.sql} (72%) mode change 100755 => 100644 rename src/cats/{drop_mysql_tables.in => ddl/drops/mysql.sql} (76%) create mode 100644 src/cats/ddl/drops/postgresql.sql create mode 100644 src/cats/ddl/grants/ingres-readonly.sql create mode 100644 src/cats/ddl/grants/ingres.sql create mode 100644 src/cats/ddl/grants/mysql-readonly.sql create mode 100644 src/cats/ddl/grants/mysql.sql create mode 100644 src/cats/ddl/grants/postgresql-readonly.sql create mode 100644 src/cats/ddl/grants/postgresql.sql create mode 100644 src/cats/ddl/updates/mysql.14_2001.sql create mode 100644 src/cats/ddl/updates/mysql.2001_2002.sql create mode 100644 src/cats/ddl/updates/postgresql.14_2001.sql create mode 100644 src/cats/ddl/updates/postgresql.2001_2002.sql create mode 100644 src/cats/ddl/updates/sqlite3.14_2001.sql create mode 100644 src/cats/ddl/updates/sqlite3.2001_2002.sql delete mode 100755 src/cats/drop_ingres_database.in delete mode 100644 src/cats/drop_mysql_database.in delete mode 100644 src/cats/drop_postgresql_database.in delete mode 100644 src/cats/drop_postgresql_tables.in delete mode 100644 src/cats/drop_sqlite3_database.in delete mode 100644 src/cats/drop_sqlite3_tables.in delete mode 100755 src/cats/fix_postgresql_tables delete mode 100755 src/cats/grant_ingres_privileges.in delete mode 100644 src/cats/grant_mysql_privileges.in delete mode 100644 src/cats/grant_postgresql_privileges.in delete mode 100644 src/cats/grant_privileges delete mode 100644 src/cats/grant_sqlite3_privileges.in delete mode 100755 src/cats/ingres.in delete mode 100644 src/cats/mysql.in delete mode 100644 src/cats/postgresql.in delete mode 100644 src/cats/sqlite.in delete mode 100755 src/cats/update_ingres_tables.in delete mode 100644 src/cats/update_mysql_tables.in delete mode 100644 src/cats/update_postgresql_tables.in delete mode 100644 src/cats/update_sqlite3_tables.in diff --git a/autoconf/configure.in b/autoconf/configure.in index 4fcb403345c..ba66299779f 100644 --- a/autoconf/configure.in +++ b/autoconf/configure.in @@ -3756,6 +3756,7 @@ debian/bareos-filedaemon.preinst \ debian/bareos-director.preinst \ debian/bareos-storage.preinst \ scripts/bareos-config \ +scripts/bareos-config-lib.sh \ scripts/btraceback \ scripts/bconsole \ scripts/bareos \ @@ -3792,26 +3793,6 @@ src/cats/Makefile \ src/cats/make_catalog_backup.pl \ src/cats/make_catalog_backup \ src/cats/delete_catalog_backup \ -src/cats/create_postgresql_database \ -src/cats/update_postgresql_tables \ -src/cats/make_postgresql_tables \ -src/cats/grant_postgresql_privileges \ -src/cats/drop_postgresql_tables \ -src/cats/drop_postgresql_database \ -src/cats/create_mysql_database \ -src/cats/update_mysql_tables \ -src/cats/make_mysql_tables \ -src/cats/grant_mysql_privileges \ -src/cats/drop_mysql_tables \ -src/cats/drop_mysql_database \ -src/cats/create_sqlite3_database \ -src/cats/update_sqlite3_tables \ -src/cats/make_sqlite3_tables \ -src/cats/grant_sqlite3_privileges \ -src/cats/drop_sqlite3_tables \ -src/cats/drop_sqlite3_database \ -src/cats/sqlite \ -src/cats/mysql \ src/cats/create_bareos_database \ src/cats/update_bareos_tables \ src/cats/grant_bareos_privileges \ @@ -3888,24 +3869,7 @@ c=src/cats chmod 755 $c/create_bareos_database $c/update_bareos_tables $c/make_bareos_tables chmod 755 $c/grant_bareos_privileges $c/drop_bareos_tables $c/drop_bareos_database - -chmod 755 $c/create_mysql_database $c/update_mysql_tables $c/make_mysql_tables -chmod 755 $c/grant_mysql_privileges $c/drop_mysql_tables $c/drop_mysql_database - -chmod 755 $c/create_sqlite3_database $c/update_sqlite3_tables $c/make_sqlite3_tables -chmod 755 $c/grant_sqlite3_privileges $c/drop_sqlite3_tables $c/drop_sqlite3_database - -chmod 755 $c/create_postgresql_database $c/update_postgresql_tables $c/make_postgresql_tables -chmod 755 $c/grant_postgresql_privileges $c/drop_postgresql_tables $c/drop_postgresql_database - -#chmod 755 $c/create_ingres_database $c/update_ingres_tables $c/make_ingres_tables -#chmod 755 $c/grant_ingres_privileges $c/drop_ingres_tables $c/drop_ingres_database - - chmod 755 $c/make_catalog_backup $c/delete_catalog_backup $c/make_catalog_backup.pl -chmod 755 $c/sqlite -chmod 755 $c/mysql - chmod 755 $c/install-default-backend if test "x$ac_cv_sys_largefile_CFLAGS" != "xno" ; then diff --git a/debian/bareos-common.install b/debian/bareos-common.install index b6e2fdf5ea7..db521caffda 100644 --- a/debian/bareos-common.install +++ b/debian/bareos-common.install @@ -1,4 +1,5 @@ /usr/lib/bareos/scripts/bareos-config +/usr/lib/bareos/scripts/bareos-config-lib.sh /usr/lib/bareos/scripts/btraceback.gdb /usr/lib/libbareos.so /usr/lib/libbareos-*.so diff --git a/debian/bareos-database-mysql.install b/debian/bareos-database-mysql.install index e9d838a5a29..d31f185a09f 100644 --- a/debian/bareos-database-mysql.install +++ b/debian/bareos-database-mysql.install @@ -1,2 +1,2 @@ /usr/lib/libb*-mysql*.so -/usr/lib/bareos/scripts/*_mysql_* +/usr/lib/bareos/scripts/ddl/*/mysql*.sql diff --git a/debian/bareos-database-postgresql.install b/debian/bareos-database-postgresql.install index 6d231bea18b..4666551eba1 100644 --- a/debian/bareos-database-postgresql.install +++ b/debian/bareos-database-postgresql.install @@ -1,2 +1,2 @@ /usr/lib/libb*-postgresql*.so -/usr/lib/bareos/scripts/*_postgresql_* +/usr/lib/bareos/scripts/ddl/*/postgresql*.sql diff --git a/debian/bareos-database-sqlite3.install b/debian/bareos-database-sqlite3.install index dce2178f907..007d5a5217c 100644 --- a/debian/bareos-database-sqlite3.install +++ b/debian/bareos-database-sqlite3.install @@ -1,2 +1,2 @@ /usr/lib/libb*-sqlite3*.so -/usr/lib/bareos/scripts/*_sqlite3_* +/usr/lib/bareos/scripts/ddl/*/sqlite3*.sql diff --git a/platforms/packaging/bareos.spec b/platforms/packaging/bareos.spec index 250d700c7f8..df5cebbf3bc 100644 --- a/platforms/packaging/bareos.spec +++ b/platforms/packaging/bareos.spec @@ -741,6 +741,7 @@ echo "This is a meta package to install a full bareos system" > %{buildroot}%{_d %dir /usr/lib/bareos/ %dir %{script_dir} %{script_dir}/bareos-config +%{script_dir}/bareos-config-lib.sh %{script_dir}/btraceback.gdb %if "%{_libdir}/bareos/" != "/usr/lib/bareos/" %dir %{_libdir}/bareos/ @@ -755,6 +756,11 @@ echo "This is a meta package to install a full bareos system" > %{buildroot}%{_d %files database-common # catalog independent files %defattr(-, root, root) +%dir %{script_dir}/ddl +%dir %{script_dir}/ddl/creates +%dir %{script_dir}/ddl/drops +%dir %{script_dir}/ddl/grants +%dir %{script_dir}/ddl/updates %{_libdir}/libbareossql.so %{_libdir}/libbareoscats.so %{_libdir}/libbareossql-%{_libversion}.so @@ -769,14 +775,14 @@ echo "This is a meta package to install a full bareos system" > %{buildroot}%{_d %files database-postgresql # postgresql catalog files %defattr(-, root, root) -%{script_dir}/*_postgresql_* +%{script_dir}/ddl/*/postgresql*.sql %{_libdir}/libbareoscats-postgresql.so %{_libdir}/libbareoscats-postgresql-%{_libversion}.so %files database-mysql # mysql catalog files %defattr(-, root, root) -%{script_dir}/*_mysql_* +%{script_dir}/ddl/*/mysql*.sql %{_libdir}/libbareoscats-mysql.so %{_libdir}/libbareoscats-mysql-%{_libversion}.so @@ -784,7 +790,7 @@ echo "This is a meta package to install a full bareos system" > %{buildroot}%{_d %files database-sqlite3 # sqlite3 catalog files %defattr(-, root, root) -%{script_dir}/*_sqlite3_* +%{script_dir}/ddl/*/sqlite3*.sql %{_libdir}/libbareoscats-sqlite3.so %{_libdir}/libbareoscats-sqlite3-%{_libversion}.so %endif diff --git a/scripts/Makefile.in b/scripts/Makefile.in index 7b6c5d3c8e1..5abbaab1022 100755 --- a/scripts/Makefile.in +++ b/scripts/Makefile.in @@ -62,6 +62,7 @@ install: installdirs $(INSTALL_DATA) btraceback.mdb $(DESTDIR)$(scriptdir)/btraceback.mdb #$(INSTALL_SCRIPT) -m 700 bareos-password-setup.sh ${DESTDIR}${scriptdir}/bareos-password-setup.sh $(INSTALL_SCRIPT) bareos-config ${DESTDIR}${scriptdir}/bareos-config + $(INSTALL_SCRIPT) bareos-config-lib.sh ${DESTDIR}${scriptdir}/bareos-config-lib.sh $(INSTALL_SCRIPT) btraceback $(DESTDIR)$(sbindir)/btraceback #$(INSTALL_SCRIPT) logrotate $(DESTDIR)/etc/logrotate.d/bareos-dir diff --git a/scripts/bareos-config-lib.sh.in b/scripts/bareos-config-lib.sh.in new file mode 100644 index 00000000000..507c79664c2 --- /dev/null +++ b/scripts/bareos-config-lib.sh.in @@ -0,0 +1,489 @@ +#!/bin/sh + +DIR_CFG=@sysconfdir@ +CFG_DIR=${DIR_CFG}/bareos-dir.conf +DIR_SCRIPTS=@scriptdir@ + +SEC_GROUPS="tape disk" + +WORKING_DIR="@working_dir@" +FILE_DAEMON_USER="@fd_user@" +FILE_DAEMON_GROUP="@fd_group@" +STORAGE_DAEMON_USER="@sd_user@" +STORAGE_DAEMON_GROUP="@sd_group@" +DIRECTOR_DAEMON_USER="@dir_user@" +DIRECTOR_DAEMON_GROUP="@dir_group@" + +DB_VERSION="@BDB_VERSION@" +SQL_DDL_DIR="@scriptdir@/ddl" +SQLITE_BINDIR="@SQLITE_BINDIR@" +MYSQL_BINDIR="@MYSQL_BINDIR@" +POSTGRESQL_BINDIR="@POSTGRESQL_BINDIR@" +INGRES_BINDIR="@INGRES_BINDIR@" + +PASSWORD_SUBST="\ +XXX_REPLACE_WITH_DIRECTOR_PASSWORD_XXX \ +XXX_REPLACE_WITH_CLIENT_PASSWORD_XXX \ +XXX_REPLACE_WITH_STORAGE_PASSWORD_XXX \ +XXX_REPLACE_WITH_DIRECTOR_MONITOR_PASSWORD_XXX \ +XXX_REPLACE_WITH_CLIENT_MONITOR_PASSWORD_XXX \ +XXX_REPLACE_WITH_STORAGE_MONITOR_PASSWORD_XXX \ +" + +os_type=`uname -s` + +is_function() +{ + func=$1 + test "$func" && type "$func" > /dev/null 2>&1 + return $? +} + +# does not work on all shells (only bash), +# therefore removed until a better solution is found +# list_functions() +# { +# if type typeset >/dev/null 2>&1; then +# # show available shell functions, +# # but exclude internal functions (name starts with "_" ...) +# typeset -F | cut -d " " -f 3 | grep "^[a-z]" +# else +# echo "function list not available" +# fi +# } + +get_user_fd() +{ + echo "${FILE_DAEMON_USER}" +} + +get_group_fd() +{ + echo "${FILE_DAEMON_GROUP}" +} + +get_user_sd() +{ + echo "${STORAGE_DAEMON_USER}" +} + +get_group_sd() +{ + echo "${STORAGE_DAEMON_GROUP}" +} + +get_user_dir() +{ + echo "${DIRECTOR_DAEMON_USER}" +} + +get_group_dir() +{ + echo "${DIRECTOR_DAEMON_GROUP}" +} + +get_working_dir() +{ + echo "${WORKING_DIR}" +} + +get_database_version() +{ + echo "${DB_VERSION}" +} + +get_database_ddl_dir() +{ + echo "${SQL_DDL_DIR}" +} + +get_database_utility_path() +{ + db_type="$1" + + case ${db_type} in + sqlite3) + utility="sqlite3" + bindir="${SQLITE_BINDIR}" + ;; + mysql) + utility="mysql" + bindir="${MYSQL_BINDIR}" + ;; + postgresql) + utility="psql" + pg_config --bindir > /dev/null 2>&1 + if [ $? = 0 ]; then + bindir=`pg_config --bindir` + else + bindir="${POSTGRESQL_BINDIR}" + fi + ;; + ingres) + utility="sql" + bindir="${INGRES_BINDIR}" + ;; + *) + ;; + esac + + # + # First see if the utility is already on the path + # + which ${utility} > /dev/null 2>&1 + if [ $? = 0 ]; then + echo "" + else + echo "${bindir}" + fi +} + +[ ${os_type} = Linux ] && \ +setup_sd_user() +{ + # + # guaranties that storage-daemon user and group exists + # and storage-daemon user belongs to the required groups. + # + # normally, storage-daemon user + # is already installed by the package preinstall script. + # + + # + # See what secondary groups exist for the sd user to be added to. + # + ADD_GROUPS="" + for sec_group in ${SEC_GROUPS}; do + cnt=`getent group ${sec_group} | wc -l` + if [ ${cnt} -gt 0 ]; then + [ -z "${ADD_GROUPS}" ] && ADD_GROUPS="-G ${sec_group}" || ADD_GROUPS="${ADD_GROUPS},${sec_group}" + fi + done + + getent group ${STORAGE_DAEMON_GROUP} > /dev/null || groupadd -r ${STORAGE_DAEMON_GROUP} + + # + # If the user doesn't exist create a new one otherwise modify it to have the wanted secondary groups. + # + if [ "${STORAGE_DAEMON_USER}" != "root" ]; then + getent passwd ${STORAGE_DAEMON_USER} > /dev/null + if [ $? -ne 0 ]; then + # create a new storage_daemon_user + useradd -r --comment "bareos" --home ${WORKING_DIR} -g ${STORAGE_DAEMON_GROUP} ${ADD_GROUPS} --shell /bin/false ${STORAGE_DAEMON_USER} + fi + + # if the user has already created before, + # make sure the correct primary group is set otherwise fix it. + if [ "`id -gn ${STORAGE_DAEMON_USER}`" != "${STORAGE_DAEMON_GROUP}" ]; then + usermod -g ${STORAGE_DAEMON_GROUP} ${STORAGE_DAEMON_USER} + fi + + # add the storage_daemon_user to additional groups (if defined) + [ "${ADD_GROUPS}" ] && usermod ${ADD_GROUPS} ${STORAGE_DAEMON_USER} + fi +} + +[ ${os_type} != Linux ] && \ +setup_sd_user() +{ + echo "setup_sd_user() is not supported on this platform" + exit 1 +} + +get_database_driver() +{ + if ! [ -r "${CFG_DIR}" ]; then + return + fi + + # get dbdriver, that is defined in director configuration. + # If string is still set to default, return nothing + DBDRIVER=`egrep -i '^[ ]*dbdriver[ ]*=' ${CFG_DIR} |\ + cut -d'=' -f2 | \ + sed -e 's/[ ]*"//' -e 's/"//' | \ + grep -v "XXX_REPLACE_WITH_DATABASE_DRIVER_XXX"` + + echo "$DBDRIVER" +} + +get_database_name() +{ + if ! [ -r "${CFG_DIR}" ]; then + return + fi + + # get dbname, that is defined in director configuration. + DBNAME=`egrep -i '^[ ]*dbname[ ]*=' ${CFG_DIR} |\ + cut -d'=' -f2 | \ + sed -e 's/[ ]*"//' -e 's/"//'` + echo "$DBNAME" +} + +get_databases_installed() +{ + # manually check different backends, to get the correct order + [ -e ${SQL_DDL_DIR}/creates/postgresql.sql ] && echo "postgresql" + [ -e ${SQL_DDL_DIR}/creates/mysql.sql ] && echo "mysql" + [ -e ${SQL_DDL_DIR}/creates/sqlite3.sql ] && echo "sqlite3" +} + +get_database_driver_default() +{ + DBDRIVER=`get_database_driver` + + if [ -z "$DBDRIVER" ]; then + DBDRIVER=`get_databases_installed | head -n 1` + fi + + if [ -z "$DBDRIVER" ]; then + # fallback and first choice + DBDRIVER="postgresql" + fi + + echo "$DBDRIVER" +} + +initialize_database_driver() +{ + DBDRIVER=`get_database_driver_default` + replace "XXX_REPLACE_WITH_DATABASE_DRIVER_XXX" "${DBDRIVER}" +} + +is_template_sql_file() +{ + input_file=$1 + + if [ -z "${input_file}" ]; then + return 1 + fi + + if ! egrep -q '@DB_NAME@|@DB_USER@|@DB_PASS@|@DB_VERSION@' ${input_file}; then + # no variables found in file, this file is not a template. + return 0 + else + # variables found, this file is a template, therefore return FALSE + return 1 + fi +} + +get_translated_sql_file() +{ + # replaces variables in a SQL DDL file + # and returns the result as stdout. + + input_file=$1 + + if [ -z "${input_file}" ]; then + return 1 + fi + + if [ ! -f ${input_file} ]; then + return 2 + fi + + db_type="${db_type:-`get_database_driver_default`}" + db_name="${db_name:-@db_name@}" + db_user="${db_user:-@db_user@}" + db_password="${db_password:-@db_password@}" + db_version=`get_database_version` + + case ${db_type} in + sqlite3) + ;; + mysql) + if [ "$db_password" != "" ]; then + pass="IDENTIFIED BY '$db_password'" + fi + ;; + postgresql) + if [ "$db_password" != "" ]; then + pass="PASSWORD '$db_password'" + fi + ;; + ingres) + if [ "$db_password" != "" ]; then + pass="WITH PASSWORD = '$db_password'" + fi + ;; + *) + ;; + esac + + sed -e "s/@DB_NAME@/${db_name}/" \ + -e "s/@DB_USER@/${db_user}/" \ + -e "s/@DB_PASS@/${pass}/" \ + -e "s/@DB_VERSION@/${db_version}/" \ + ${input_file} +} + +get_database_grant_priviliges() +{ + # Returns the DDL for granting priviliges for a database user. + # Can be used, to get the SQL commands + # to create additional database users. + # If requested, this user can be limited to read-only database access. + + #db_name="$1" + db_type="${1:-${db_type:-`get_database_driver_default`}}" + db_user="$2" + db_password="$3" + case "$4" in + "") + # full access + privileges="" + ;; + "readonly") + privileges="-readonly" + ;; + *) + echo "Unknown privileges parameter $4" + return 1 + ;; + esac + + case ${db_type} in + sqlite3) + ;; + mysql) + sql_definitions="${SQL_DDL_DIR}/grants/mysql${privileges}.sql" + ;; + postgresql) + sql_definitions="${SQL_DDL_DIR}/grants/postgresql${privileges}.sql" + ;; + ingres) + sql_definitions="${SQL_DDL_DIR}/grants/ingres${privileges}.sql" + ;; + *) + echo "Unknown database type $1" + return 1 + ;; + esac + + if [ ! -z "${sql_definitions}" ]; then + if [ ! -f ${sql_definitions} ]; then + echo "Unable to open database table definitions in file ${sql_definitions}" + return 1 + fi + get_translated_sql_file ${sql_definitions} + fi + + return +} + +translate_sql_files() +{ + # Translates all available DDL files for one database type. + # However, currently not used, because it reduced flexibility. + + SOURCE_DIR=${1:-$SQL_DDL_DIR} + DEST_DIR=${2:-"@working_dir@/ddl"} + + db_type="${db_type:-`get_database_driver_default`}" + + for i in `find "$SOURCE_DIR" -name "${db_type}*.sql" -printf "%P\n"`; do + dest_file=${DEST_DIR}/$i + mkdir -p `dirname $dest_file` + get_translated_sql_file ${SOURCE_DIR}/$i > $dest_file + # in case of errors, remove file + if [ $? -ne 0 ]; then + rm -f $dest_file + fi + done +} + +get_local_hostname() +{ + # put actual short hostname in configuration files + # try to get short hostname + hname=`hostname -s` + if [ -z "$hname" ]; then + # try to get long hostname + hname=`hostname|sed 's/\..*//g'` + if [ -z "$hname" ]; then + # set to "localhost" + hname='localhost' + fi + fi + echo "$hname" +} + +replace() +{ + if ! [ $# -eq 2 ]; then + return 1 + fi + + SEARCH="$1" + REPLACE="$2" + + for file in `grep -l ${SEARCH} ${DIR_CFG}/*.conf` + do + echo "replacing '${SEARCH}' with '${REPLACE}' in $file" + sed -i'' "s#${SEARCH}#${REPLACE}#g" ${file}; + done + return 0 +} + +initialize_local_hostname() +{ + # + # Replace all XXX_REPLACE_WITH_LOCAL_HOSTNAME by the local hostname. + # + hname=`get_local_hostname` + replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX-dir" "${hname}-dir" + replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX-fd" "${hname}-fd" + replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX-sd" "${hname}-sd" + replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX-mon" "${hname}-mon" + replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX" "${hname}" +} + +replace_password() +{ + if ! [ $# -eq 2 ]; then + return 1 + fi + + SEARCH="$1" + REPLACE="$2" + + for file in `grep -l ${SEARCH} ${DIR_CFG}/*.conf` + do + echo "replacing '${SEARCH}' in $file" + sed -i'' "s#${SEARCH}#${REPLACE}#g" ${file}; + done + return 0 +} + +initialize_passwords() +{ + # + # See if we need to generate a set of random passwords. + # + if [ ! -f ${DIR_CFG}/.rndpwd ]; then + for string in ${PASSWORD_SUBST} + do + pass=`openssl rand -base64 33` + echo "${string}=${pass}" >> ${DIR_CFG}/.rndpwd + done + chmod 400 ${DIR_CFG}/.rndpwd + fi + + # Source the passwords + . ${DIR_CFG}/.rndpwd + + for string in ${PASSWORD_SUBST} + do + eval "pass=\${$string}" + if [ ! -z "${pass}" ]; then + replace_password "${string}" "${pass}" + fi + done +} + +init() +{ + initialize_local_hostname + #initialize_director_hostname + initialize_passwords + initialize_database_driver +} diff --git a/scripts/bareos-config.in b/scripts/bareos-config.in index 0d20d8f537d..3230a2b3dcb 100644 --- a/scripts/bareos-config.in +++ b/scripts/bareos-config.in @@ -1,29 +1,12 @@ #!/bin/sh -DIR_CFG=@sysconfdir@ -CFG_DIR=${DIR_CFG}/bareos-dir.conf -DIR_SCRIPTS=@scriptdir@ +LIB=@scriptdir@/bareos-config-lib.sh -SEC_GROUPS="tape disk" - -WORKING_DIR="@working_dir@" -FILE_DAEMON_USER="@fd_user@" -FILE_DAEMON_GROUP="@fd_group@" -STORAGE_DAEMON_USER="@sd_user@" -STORAGE_DAEMON_GROUP="@sd_group@" -DIRECTOR_DAEMON_USER="@dir_user@" -DIRECTOR_DAEMON_GROUP="@dir_group@" - -PASSWORD_SUBST="\ -XXX_REPLACE_WITH_DIRECTOR_PASSWORD_XXX \ -XXX_REPLACE_WITH_CLIENT_PASSWORD_XXX \ -XXX_REPLACE_WITH_STORAGE_PASSWORD_XXX \ -XXX_REPLACE_WITH_DIRECTOR_MONITOR_PASSWORD_XXX \ -XXX_REPLACE_WITH_CLIENT_MONITOR_PASSWORD_XXX \ -XXX_REPLACE_WITH_STORAGE_MONITOR_PASSWORD_XXX \ -" - -os_type=`uname -s` +if ! [ -r "$LIB" ]; then + echo "failed to read library $LIB" + exit 1 +fi +. $LIB usage() { @@ -38,253 +21,6 @@ help() usage } -is_function() -{ - func=$1 - test "$func" && type "$func" > /dev/null 2>&1 - return $? -} - -# does not work on all shells (only bash), -# therefore removed until a better solution is found -# list_functions() -# { -# if type typeset >/dev/null 2>&1; then -# # show available shell functions, -# # but exclude internal functions (name starts with "_" ...) -# typeset -F | cut -d " " -f 3 | grep "^[a-z]" -# else -# echo "function list not available" -# fi -# } - -get_user_fd() -{ - echo "${FILE_DAEMON_USER}" -} - -get_group_fd() -{ - echo "${FILE_DAEMON_GROUP}" -} - -get_user_sd() -{ - echo "${STORAGE_DAEMON_USER}" -} - -get_group_sd() -{ - echo "${STORAGE_DAEMON_GROUP}" -} - -get_user_dir() -{ - echo "${DIRECTOR_DAEMON_USER}" -} - -get_group_dir() -{ - echo "${DIRECTOR_DAEMON_GROUP}" -} - -[ ${os_type} = Linux ] && \ -setup_sd_user() -{ - # - # guaranties that storage-daemon user and group exists - # and storage-daemon user belongs to the required groups. - # - # normally, storage-daemon user - # is already installed by the package preinstall script. - # - - # - # See what secondary groups exist for the sd user to be added to. - # - ADD_GROUPS="" - for sec_group in ${SEC_GROUPS}; do - cnt=`getent group ${sec_group} | wc -l` - if [ ${cnt} -gt 0 ]; then - [ -z "${ADD_GROUPS}" ] && ADD_GROUPS="-G ${sec_group}" || ADD_GROUPS="${ADD_GROUPS},${sec_group}" - fi - done - - getent group ${STORAGE_DAEMON_GROUP} > /dev/null || groupadd -r ${STORAGE_DAEMON_GROUP} - - # - # If the user doesn't exist create a new one otherwise modify it to have the wanted secondary groups. - # - if [ "${STORAGE_DAEMON_USER}" != "root" ]; then - getent passwd ${STORAGE_DAEMON_USER} > /dev/null - if [ $? -ne 0 ]; then - # create a new storage_daemon_user - useradd -r --comment "bareos" --home ${WORKING_DIR} -g ${STORAGE_DAEMON_GROUP} ${ADD_GROUPS} --shell /bin/false ${STORAGE_DAEMON_USER} - fi - - # if the user has already created before, - # make sure the correct primary group is set otherwise fix it. - if [ "`id -gn ${STORAGE_DAEMON_USER}`" != "${STORAGE_DAEMON_GROUP}" ]; then - usermod -g ${STORAGE_DAEMON_GROUP} ${STORAGE_DAEMON_USER} - fi - - # add the storage_daemon_user to additional groups (if defined) - [ "${ADD_GROUPS}" ] && usermod ${ADD_GROUPS} ${STORAGE_DAEMON_USER} - fi -} - -[ ${os_type} != Linux ] && \ -setup_sd_user() -{ - echo "setup_sd_user() is not supported on this platform" - exit 1 -} - -get_database_driver() -{ - if ! [ -r "${CFG_DIR}" ]; then - return - fi - - # get dbdriver, that is defined in director configuration. - # If string is still set to default, return nothing - DBDRIVER=`sed -r -n 's/^\s*dbdriver\s*=\s*["]?([^"]*)["]?/\1/ip' "${CFG_DIR}" | grep -v "XXX_REPLACE_WITH_DATABASE_DRIVER_XXX"` - - echo "$DBDRIVER" -} - -get_databases_installed() -{ - #for i in `ls ${DIR_SCRIPTS}/create_*_database` - #do - # sed -n -r 's#.*/create_([a-z]+)_database#\1#p' <<<$i | grep -v bareos - #done - - # manually check different backends, to get the correct order - [ -e ${DIR_SCRIPTS}/create_postgresql_database ] && echo "postgresql" - [ -e ${DIR_SCRIPTS}/create_mysql_database ] && echo "mysql" - [ -e ${DIR_SCRIPTS}/create_sqlite3_database ] && echo "sqlite3" -} - -get_database_driver_default() -{ - DBDRIVER=`get_database_driver` - - if [ -z "$DBDRIVER" ]; then - DBDRIVER=`get_databases_installed | head -n 1` - fi - - if [ -z "$DBDRIVER" ]; then - # fallback and first choice - DBDRIVER="postgresql" - fi - - echo "$DBDRIVER" -} - -initialize_database_driver() -{ - DBDRIVER=`get_database_driver_default` - replace "XXX_REPLACE_WITH_DATABASE_DRIVER_XXX" "${DBDRIVER}" -} - -get_local_hostname() -{ - # put actual short hostname in configuration files - # try to get short hostname - hname=`hostname -s` - if [ -z "$hname" ]; then - # try to get long hostname - hname=`hostname|sed 's/\..*//g'` - if [ -z "$hname" ]; then - # set to "localhost" - hname='localhost' - fi - fi - echo "$hname" -} - -replace() -{ - if ! [ $# -eq 2 ]; then - return 1 - fi - - SEARCH="$1" - REPLACE="$2" - - for file in `grep -l ${SEARCH} ${DIR_CFG}/*.conf` - do - echo "replacing '${SEARCH}' with '${REPLACE}' in $file" - sed -i'' "s#${SEARCH}#${REPLACE}#g" ${file}; - done - return 0 -} - -initialize_local_hostname() -{ - # - # Replace all XXX_REPLACE_WITH_LOCAL_HOSTNAME by the local hostname. - # - hname=`get_local_hostname` - replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX-dir" "${hname}-dir" - replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX-fd" "${hname}-fd" - replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX-sd" "${hname}-sd" - replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX-mon" "${hname}-mon" - replace "XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX" "${hname}" -} - -replace_password() -{ - if ! [ $# -eq 2 ]; then - return 1 - fi - - SEARCH="$1" - REPLACE="$2" - - for file in `grep -l ${SEARCH} ${DIR_CFG}/*.conf` - do - echo "replacing '${SEARCH}' in $file" - sed -i'' "s#${SEARCH}#${REPLACE}#g" ${file}; - done - return 0 -} - -initialize_passwords() -{ - # - # See if we need to generate a set of random passwords. - # - if [ ! -f ${DIR_CFG}/.rndpwd ]; then - for string in ${PASSWORD_SUBST} - do - pass=`openssl rand -base64 33` - echo "${string}=${pass}" >> ${DIR_CFG}/.rndpwd - done - chmod 400 ${DIR_CFG}/.rndpwd - fi - - # Source the passwords - . ${DIR_CFG}/.rndpwd - - for string in ${PASSWORD_SUBST} - do - eval "pass=\${$string}" - if [ ! -z "${pass}" ]; then - replace_password "${string}" "${pass}" - fi - done -} - -init() -{ - initialize_local_hostname - #initialize_director_hostname - initialize_passwords - initialize_database_driver -} - # number of args passed args=$# @@ -294,7 +30,7 @@ args=$# if [ $args -ge 1 ]; then # executes given arguments if is_function $1; then - $* + "$@" else echo "failed to execute $*. Not found." #usage diff --git a/src/cats/Makefile.in b/src/cats/Makefile.in index 48d2c9a7ea2..958d18647be 100644 --- a/src/cats/Makefile.in +++ b/src/cats/Makefile.in @@ -196,22 +196,6 @@ realclean: clean $(RMF) create_bareos_database update_bareos_tables make_bareos_tables $(RMF) grant_bareos_privileges drop_bareos_tables drop_bareos_database - $(RMF) create_mysql_database update_mysql_tables make_mysql_tables - $(RMF) grant_mysql_privileges drop_mysql_tables drop_mysql_database - - $(RMF) create_postgresql_database update_postgresql_tables make_postgresql_tables - $(RMF) grant_postgresql_privileges drop_postgresql_tables drop_postgresql_database - - $(RMF) create_ingres_database update_ingres_tables make_ingres_tables - $(RMF) grant_ingres_privileges drop_ingres_tables drop_ingres_database - - $(RMF) create_sqlite_database update_sqlite_tables make_sqlite_tables - $(RMF) grant_sqlite_privileges drop_sqlite_tables drop_sqlite_database - - $(RMF) create_sqlite3_database update_sqlite3_tables make_sqlite3_tables - $(RMF) grant_sqlite3_privileges drop_sqlite3_tables drop_sqlite3_database - - $(RMF) mysql sqlite postgresql ingres $(RMF) make_catalog_backup make_catalog_backup.pl delete_catalog_backup distclean: realclean @@ -242,17 +226,6 @@ libtool-install: all done install: @LIBTOOL_INSTALL_TARGET@ @LIBTOOL_INSTALL_DEFAULT_BACKEND_TARGET@ @INCLUDE_INSTALL_TARGET@ - for db_type in @DB_BACKENDS@; do \ - if [ -f create_$${db_type}_database ]; then \ - $(INSTALL_SCRIPT) create_$${db_type}_database $(DESTDIR)$(scriptdir)/create_$${db_type}_database; \ - $(INSTALL_SCRIPT) update_$${db_type}_tables $(DESTDIR)$(scriptdir)/update_$${db_type}_tables; \ - $(INSTALL_SCRIPT) make_$${db_type}_tables $(DESTDIR)$(scriptdir)/make_$${db_type}_tables; \ - $(INSTALL_SCRIPT) grant_$${db_type}_privileges $(DESTDIR)$(scriptdir)/grant_$${db_type}_privileges; \ - $(INSTALL_SCRIPT) drop_$${db_type}_tables $(DESTDIR)$(scriptdir)/drop_$${db_type}_tables; \ - $(INSTALL_SCRIPT) drop_$${db_type}_database $(DESTDIR)$(scriptdir)/drop_$${db_type}_database; \ - fi; \ - done - $(INSTALL_SCRIPT) create_bareos_database $(DESTDIR)$(scriptdir)/create_bareos_database $(INSTALL_SCRIPT) update_bareos_tables $(DESTDIR)$(scriptdir)/update_bareos_tables $(INSTALL_SCRIPT) make_bareos_tables $(DESTDIR)$(scriptdir)/make_bareos_tables @@ -260,6 +233,27 @@ install: @LIBTOOL_INSTALL_TARGET@ @LIBTOOL_INSTALL_DEFAULT_BACKEND_TARGET@ @INCL $(INSTALL_SCRIPT) drop_bareos_tables $(DESTDIR)$(scriptdir)/drop_bareos_tables $(INSTALL_SCRIPT) drop_bareos_database $(DESTDIR)$(scriptdir)/drop_bareos_database + # + # Make sure the DDL subdir exists. + # + @if ! test -d $(DESTDIR)$(scriptdir)/ddl; then \ + $(MKDIR) $(DESTDIR)$(libdir)/ddl; \ + fi; + + # + # Install all DDL schemas for Drop, Grant, Create and Update + # + @for db_type in @DB_BACKENDS@; do \ + for dir in drops grants creates updates; do \ + if ! test -d $(DESTDIR)$(scriptdir)/ddl/$$dir; then \ + $(MKDIR) $(DESTDIR)$(scriptdir)/ddl/$$dir; \ + fi; \ + if test `ls -1 ddl/$$dir/$$db_type*.sql 2>/dev/null | wc -l` -gt 0; then \ + $(INSTALL_DATA) ddl/$$dir/$$db_type*.sql $(DESTDIR)$(scriptdir)/ddl/$$dir; \ + fi; \ + done \ + done + @filename=make_catalog_backup.pl; \ if test -f $(DESTDIR)$(scriptdir)/$$filename; then \ destname=$$filename.new; \ diff --git a/src/cats/create_bareos_database.in b/src/cats/create_bareos_database.in index a92b83f1c7f..ad32f531b15 100644 --- a/src/cats/create_bareos_database.in +++ b/src/cats/create_bareos_database.in @@ -1,11 +1,40 @@ #!/bin/sh # -# This routine creates the Bareos database -# using PostgreSQL, Ingres, MySQL, or SQLite. +# BAREOS® - Backup Archiving REcovery Open Sourced +# +# Copyright (C) 2000-2011 Free Software Foundation Europe e.V. +# Copyright (C) 2013-2014 Bareos GmbH & Co. KG +# +# This program is Free Software; you can redistribute it and/or +# modify it under the terms of version three of the GNU Affero General Public +# License as published by the Free Software Foundation and included +# in the file LICENSE. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. +# +# This routine makes the appropriately configured +# Bareos database for PostgreSQL, Ingres, MySQL, or SQLite. # -default_db_type=`@scriptdir@/bareos-config get_database_driver_default` +# +# Source the Bareos config functions. +# +. @scriptdir@/bareos-config-lib.sh +db_name="${db_name:-@db_name@}" +db_user="${db_user:-@db_user@}" +dir_user=`get_user_dir` +dir_group=`get_group_dir` +default_db_type=`get_database_driver_default` +working_dir=`get_working_dir` # # See if the first argument is a valid backend name. @@ -13,24 +42,24 @@ default_db_type=`@scriptdir@/bareos-config get_database_driver_default` # if [ $# -gt 0 ]; then case $1 in - sqlite3) - db_type=$1 - shift - ;; - mysql) - db_type=$1 - shift - ;; - postgresql) - db_type=$1 - shift - ;; - ingres) - db_type=$1 - shift - ;; - *) - ;; + sqlite3) + db_type=$1 + shift + ;; + mysql) + db_type=$1 + shift + ;; + postgresql) + db_type=$1 + shift + ;; + ingres) + db_type=$1 + shift + ;; + *) + ;; esac fi @@ -42,4 +71,95 @@ if [ -z "${db_type}" ]; then fi echo "Creating ${db_type} database" -@scriptdir@/create_${db_type}_database $* + +bindir=`get_database_utility_path ${db_type}` +if [ ! -z "${bindir}" ]; then + PATH="$bindir:$PATH" +fi + +case ${db_type} in + sqlite3) + sqlite3 $* ${working_dir}/${db_name}.db ".tables" + # Allow non root access + chown ${dir_user}:${dir_group} ${working_dir}/${db_name}.db + chmod 0640 ${working_dir}/${db_name}.db + retval=0 + ;; + mysql) + mysql $* -e "CREATE DATABASE ${db_name};" + retval=$? + ;; + postgresql) + # + # use SQL_ASCII to be able to put any filename into + # the database even those created with unusual character sets + PSQLVERSION=`psql -d template1 -c 'SELECT version()' $* 2>/dev/null | \ + awk '/PostgreSQL/ { print $2 }' | \ + cut -d '.' -f 1,2` + + if [ -z "${PSQLVERSION}" ]; then + echo "Unable to determine PostgreSQL version." + exit 1 + fi + + # + # Note, LC_COLLATE and LC_TYPE are needed on 8.4 and beyond, but are not implemented in 8.3 or below. + # This must be updated for future versions of PostgreSQL + # + case ${PSQLVERSION} in + 9.*) + ENCODING="ENCODING 'SQL_ASCII' LC_COLLATE 'C' LC_CTYPE 'C'" + ;; + 8.[456789]) + ENCODING="ENCODING 'SQL_ASCII' LC_COLLATE 'C' LC_CTYPE 'C'" + ;; + *) + ENCODING="ENCODING 'SQL_ASCII'" + ;; + esac + + psql -f - -d template1 $* << END-OF-DATA +\set ON_ERROR_STOP on +CREATE DATABASE ${db_name} $ENCODING TEMPLATE template0; +ALTER DATABASE ${db_name} SET datestyle TO 'ISO, YMD'; +END-OF-DATA + retval=$? + + if psql -l ${dbname} | grep " ${db_name}.*SQL_ASCII" >/dev/null; then + echo "Database encoding OK" + else + echo " " + echo "Database encoding bad. Do not use this database" + echo " " + fi + ;; + ingres) + # + # Use SQL_ASCII to be able to put any filename into + # the database even those created with unusual character sets + # + ENCODING="ENCODING 'SQL_ASCII'" + + # + # use UTF8 if you are using standard Unix/Linux LANG specifications + # that use UTF8 -- this is normally the default and *should* be + # your standard. Bareos works correctly *only* with correct UTF8. + # + # Note, with this encoding, if you have any "weird" filenames on + # your system (names generated from Win32 or Mac OS), you may + # get Bareos batch insert failures. + # + #ENCODING="ENCODING 'UTF8'" + + createdb -u${db_user} $* ${db_name} + retval=$? + ;; +esac + +if [ ${retval} = 0 ]; then + echo "Creating of ${db_name} database succeeded." +else + echo "Creating of ${db_name} database failed." +fi + +exit ${retval} diff --git a/src/cats/create_ingres_database.in b/src/cats/create_ingres_database.in deleted file mode 100755 index 4ec9b8c5ce4..00000000000 --- a/src/cats/create_ingres_database.in +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/sh -# -# shell script to create Bareos database(s) -# - -bindir=@INGRES_BINDIR@ -PATH="$bindir:$PATH" -db_name=${db_name:-@db_name@} -db_user=${db_user:-@db_user@} - -# use SQL_ASCII to be able to put any filename into -# the database even those created with unusual character sets -ENCODING="ENCODING 'SQL_ASCII'" - -# use UTF8 if you are using standard Unix/Linux LANG specifications -# that use UTF8 -- this is normally the default and *should* be -# your standard. Bareos works correctly *only* with correct UTF8. -# -# Note, with this encoding, if you have any "weird" filenames on -# your system (names generated from Win32 or Mac OS), you may -# get Bareos batch insert failures. -# -#ENCODING="ENCODING 'UTF8'" - -if createdb -u${db_user} $* ${db_name} -then - echo "Creation of ${db_name} database succeeded." -else - echo "Creation of ${db_name} database failed." -fi - -exit 0 diff --git a/src/cats/create_mysql_database.in b/src/cats/create_mysql_database.in deleted file mode 100644 index a01c09423f0..00000000000 --- a/src/cats/create_mysql_database.in +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -# -# shell script to create Bareos database(s) -# - -bindir=@MYSQL_BINDIR@ -db_name=@db_name@ - -if $bindir/mysql $* -f </dev/null; then - echo "Database encoding OK" -else - echo " " - echo "Database encoding bad. Do not use this database" - echo " " -fi diff --git a/src/cats/create_sqlite3_database.in b/src/cats/create_sqlite3_database.in deleted file mode 100644 index 32096220932..00000000000 --- a/src/cats/create_sqlite3_database.in +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh -# -# shell script to create Bareos SQLite tables - -bindir=@SQLITE_BINDIR@ -PATH=$bindir:$PATH -cd @working_dir@ -db_name=@db_name@ - -sqlite3 $* ${db_name}.db < ${temp_sql_schema}; then + echo "Failed to translate SQL definitions in ${sql_definitions}" + exit 1 + fi +fi + +bindir=`get_database_utility_path ${db_type}` +if [ ! -z "${bindir}" ]; then + PATH="$bindir:$PATH" +fi + +case ${db_type} in + sqlite3) + rm -f ${working_dir}/${db_name}.db + retval=0 + ;; + mysql) + mysql $* --database=${db_name} -f < ${temp_sql_schema} + + retval=$? + if test $retval = 0; then + echo "Dropping of Bareos MySQL tables succeeded." + else + echo "Dropping of Bareos MySQL tables failed." + fi + ;; + postgresql) + psql -f ${temp_sql_schema} -d ${db_name} $* + + retval=$? + if test $retval = 0; then + echo "Dropping of Bareos PostgreSQL tables succeeded." + else + echo "Dropping of Bareos PostgreSQL tables failed." + fi + ;; + ingres) + sql -u${db_user} $* ${db_name} < ${temp_sql_schema} + + retval=$? + if test $retval = 0; then + echo "Dropping of Bareos Ingres tables succeeded." + else + echo "Dropping of Bareos Ingres tables failed." + fi + ;; +esac + +rm -f ${temp_sql_schema} + +exit ${retval} diff --git a/src/cats/drop_ingres_database.in b/src/cats/drop_ingres_database.in deleted file mode 100755 index ee77c482737..00000000000 --- a/src/cats/drop_ingres_database.in +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -# -# shell script to drop Bareos database(s) -# - -bindir=@INGRES_BINDIR@ -PATH="$bindir:$PATH" -db_name=${db_name:-@db_name@} -db_user=${db_user:-@db_user@} - -if destroydb -u${db_user} ${db_name} -then - echo "Drop of ${db_name} database succeeded." -else - echo "Drop of ${db_name} database failed." -fi -exit 0 diff --git a/src/cats/drop_mysql_database.in b/src/cats/drop_mysql_database.in deleted file mode 100644 index 61f24b9dccf..00000000000 --- a/src/cats/drop_mysql_database.in +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -# -# shell script to drop Bareos database(s) -# - -bindir=@MYSQL_BINDIR@ -db_name=@db_name@ - -$bindir/mysql $* -f < ${temp_sql_grants}; then + echo "Error creating privileges." + exit 1 +fi + +bindir=`get_database_utility_path ${db_type}` +if [ ! -z "${bindir}" ]; then + PATH="$bindir:$PATH" +fi + +case ${db_type} in + sqlite3) + # + # Nothing to do for SQLite3 + # + retval=0 + ;; + mysql) + mysql $* -f < ${temp_sql_grants} + + retval=$? + ;; + postgresql) + psql -f ${temp_sql_grants} -d ${db_name} $* + + retval=$? + ;; + ingres) + sql iidbdb $* << END-OF-DATA + CREATE USER ${db_user} ${pass} + \g +END-OF-DATA + + sql -u${db_user} $* ${db_name} < ${temp_sql_grants} + + retval=$? + ;; +esac + +rm -f ${temp_sql_grants} + +if [ ${retval} = 0 ]; then + echo "Privileges for user ${db_user} granted ON database ${db_name}." +else + echo "Error creating privileges." +fi + +exit ${retval} diff --git a/src/cats/grant_ingres_privileges.in b/src/cats/grant_ingres_privileges.in deleted file mode 100755 index 0135180ba19..00000000000 --- a/src/cats/grant_ingres_privileges.in +++ /dev/null @@ -1,74 +0,0 @@ -#!/bin/sh -# -# shell script TO GRANT privileges to the bareos database -# -bindir=@INGRES_BINDIR@ -PATH="$bindir:$PATH" -db_name=${db_name:-@db_name@} -db_user=${db_user:-@db_user@} - -sql iidbdb $* < ${temp_sql_schema}; then + echo "Failed to translate SQL definitions in ${sql_definitions}" + exit 1 + fi +fi + +bindir=`get_database_utility_path ${db_type}` +if [ ! -z "${bindir}" ]; then + PATH="$bindir:$PATH" +fi + +case ${db_type} in + sqlite3) + sqlite3 $* ${working_dir}/${db_name}.db < ${temp_sql_schema} + chmod 640 ${working_dir}/${db_name}.db + retval=0 + ;; + mysql) + mysql $* --database=${db_name} -f < ${temp_sql_schema} + retval=$? + if test $retval = 0; then + echo "Creation of Bareos MySQL tables succeeded." + else + echo "Creation of Bareos MySQL tables failed." + fi + ;; + postgresql) + psql -f ${temp_sql_schema} -d ${db_name} $* + retval=$? + if test $retval = 0; then + echo "Creation of Bareos PostgreSQL tables succeeded." + else + echo "Creation of Bareos PostgreSQL tables failed." + fi + ;; + ingres) + sql -u${db_user} $* ${db_name} < ${temp_sql_schema} + + retval=$? + if test $retval = 0; then + echo "Creation of Bareos Ingres tables succeeded." + else + echo "Creation of Bareos Ingres tables failed." + fi + ;; +esac + +rm -f ${temp_sql_schema} + +exit ${retval} diff --git a/src/cats/make_catalog_backup.in b/src/cats/make_catalog_backup.in index 33378b02a6e..d4e458592ba 100755 --- a/src/cats/make_catalog_backup.in +++ b/src/cats/make_catalog_backup.in @@ -1,5 +1,25 @@ #!/bin/sh # +# BAREOS® - Backup Archiving REcovery Open Sourced +# +# Copyright (C) 2000-2011 Free Software Foundation Europe e.V. +# Copyright (C) 2013-2014 Bareos GmbH & Co. KG +# +# This program is Free Software; you can redistribute it and/or +# modify it under the terms of version three of the GNU Affero General Public +# License as published by the Free Software Foundation and included +# in the file LICENSE. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. +# # This script dumps your Bareos catalog in ASCII format # It works for MySQL, SQLite, and PostgreSQL # @@ -17,15 +37,14 @@ # (default "") # $5 is the type of database # + +# +# Source the Bareos config functions. # -CFG="@sysconfdir@/bareos-dir.conf" -DBCHECK="@sbindir@/bareos-dbcheck" +. @scriptdir@/bareos-config-lib.sh -default_db_type=`${DBCHECK} -B -c ${CFG} | \ - grep 'db_type=' | \ - head -1 | \ - cut -d'=' -f2 | \ - tr '[A-Z]' '[a-z]'` +working_dir=`get_working_dir` +default_db_type=`get_database_driver_default` # # See if the fifth argument is a valid backend name. @@ -57,30 +76,32 @@ if [ -z "${db_type}" ]; then db_type="${default_db_type}" fi -cd @working_dir@ +cd ${working_dir} rm -f $1.sql +bindir=`get_database_utility_path ${db_type}` +if [ ! -z "${bindir}" ]; then + PATH="$bindir:$PATH" +fi + case ${db_type} in sqlite3) - BINDIR=@SQLITE_BINDIR@ - echo ".dump" | ${BINDIR}/sqlite3 $1.db >$1.sql + echo ".dump" | sqlite3 $1.db > $1.sql ;; mysql) - BINDIR=@MYSQL_BINDIR@ if test $# -gt 2; then - MYSQLPASSWORD=" --password=$3" + MYSQLPASSWORD=" --password=$3" else - MYSQLPASSWORD="" + MYSQLPASSWORD="" fi if test $# -gt 3; then - MYSQLHOST=" --host=$4" + MYSQLHOST=" --host=$4" else - MYSQLHOST="" + MYSQLHOST="" fi - ${BINDIR}/mysqldump -u ${2}${MYSQLPASSWORD}${MYSQLHOST} -f --opt $1 >$1.sql + mysqldump -u ${2}${MYSQLPASSWORD}${MYSQLHOST} -f --opt $1 > $1.sql ;; postgresql) - BINDIR=@POSTGRESQL_BINDIR@ if test $# -gt 2; then PGPASSWORD=$3 export PGPASSWORD @@ -91,7 +112,7 @@ case ${db_type} in PGHOST="" fi # you could also add --compress for compression. See man pg_dump - exec ${BINDIR}/pg_dump -c $PGHOST -U $2 $1 >$1.sql + pg_dump -c $PGHOST -U $2 $1 > $1.sql ;; esac # diff --git a/src/cats/make_ingres_catalog_backup.in b/src/cats/make_ingres_catalog_backup.in index 1ff616b68ac..5586803446d 100755 --- a/src/cats/make_ingres_catalog_backup.in +++ b/src/cats/make_ingres_catalog_backup.in @@ -1,19 +1,49 @@ #!/bin/sh # +# BAREOS® - Backup Archiving REcovery Open Sourced +# +# Copyright (C) 2000-2011 Free Software Foundation Europe e.V. +# Copyright (C) 2013-2014 Bareos GmbH & Co. KG +# +# This program is Free Software; you can redistribute it and/or +# modify it under the terms of version three of the GNU Affero General Public +# License as published by the Free Software Foundation and included +# in the file LICENSE. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. +# # shell script to make a dump of the bareos Ingres database using copydb and make # a base64 encoded tar of the content. # -bindir=@INGRES_BINDIR@ -PATH="$bindir:$PATH" -db_name=${db_name:-@db_name@} -db_user=${db_user:-@db_user@} -working_dir="@working_dir@" +# +# Source the Bareos config functions. +# +. @scriptdir@/bareos-config-lib.sh + +db_name="${db_name:-@db_name@}" +db_user="${db_user:-@db_user@}" +working_dir=`get_working_dir` + +bindir=`get_database_utility_path ${db_type}` +if [ ! -z "${bindir}" ]; then + PATH="$bindir:$PATH" +fi # # Source the Ingres settings when they exist. # -[ -f ${bindir}/../../.ingIIsh ] && . ${bindir}/../../.ingIIsh +if [ ! -z "${bindir}" ]; then + [ -f ${bindir}/../../.ingIIsh ] && . ${bindir}/../../.ingIIsh +fi # # See if the dumpdir exists. diff --git a/src/cats/mysql.in b/src/cats/mysql.in deleted file mode 100644 index e6abef0c777..00000000000 --- a/src/cats/mysql.in +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -# -# shell script to create Bareos MySQL tables -# -bindir=@MYSQL_BINDIR@ -db_name=@db_name@ - -$bindir/mysql $* ${db_name} diff --git a/src/cats/postgresql.in b/src/cats/postgresql.in deleted file mode 100644 index f55355fabae..00000000000 --- a/src/cats/postgresql.in +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -# -# shell script to create Bareos PostgreSQL tables -# -bindir=@POSTGRESQL_BINDIR@ -db_name=@db_name@ - -$bindir/psql $* ${db_name} diff --git a/src/cats/restore_ingres_catalog_backup.in b/src/cats/restore_ingres_catalog_backup.in index c5f5e0eb72c..fb8126826cb 100755 --- a/src/cats/restore_ingres_catalog_backup.in +++ b/src/cats/restore_ingres_catalog_backup.in @@ -1,19 +1,49 @@ #!/bin/sh # +# BAREOS® - Backup Archiving REcovery Open Sourced +# +# Copyright (C) 2000-2011 Free Software Foundation Europe e.V. +# Copyright (C) 2013-2014 Bareos GmbH & Co. KG +# +# This program is Free Software; you can redistribute it and/or +# modify it under the terms of version three of the GNU Affero General Public +# License as published by the Free Software Foundation and included +# in the file LICENSE. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. +# # shell script to restore a dump of the bareos Ingres database using # a base64 encoded tar of the content. # -bindir=@INGRES_BINDIR@ -PATH="$bindir:$PATH" -db_name=${db_name:-@db_name@} -db_user=${db_user:-@db_user@} -working_dir="@working_dir@" +# +# Source the Bareos config functions. +# +. @scriptdir@/bareos-config-lib.sh + +db_name="${db_name:-@db_name@}" +db_user="${db_user:-@db_user@}" +working_dir=`get_working_dir` + +bindir=`get_database_utility_path ${db_type}` +if [ ! -z "${bindir}" ]; then + PATH="$bindir:$PATH" +fi # # Source the Ingres settings when they exist. # -[ -f ${bindir}/../../.ingIIsh ] && . ${bindir}/../../.ingIIsh +if [ ! -z "${bindir}" ]; then + [ -f ${bindir}/../../.ingIIsh ] && . ${bindir}/../../.ingIIsh +fi # # See if the dumpdir exists. diff --git a/src/cats/sqlite.in b/src/cats/sqlite.in deleted file mode 100644 index e9329d09ad5..00000000000 --- a/src/cats/sqlite.in +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -# -# shell script to invoke SQLite on Bareos database - -bindir=@SQLITE_BINDIR@ -db_name=@db_name@ -$bindir/sqlite @working_dir@/${db_name}.db diff --git a/src/cats/update_bareos_tables.in b/src/cats/update_bareos_tables.in index 114eb28ae0d..f4e9aa96f30 100644 --- a/src/cats/update_bareos_tables.in +++ b/src/cats/update_bareos_tables.in @@ -1,10 +1,40 @@ #!/bin/sh # -# This routine alters the appropriately configured -# Bareos tables for PostgreSQL, Ingres, MySQL, or SQLite. +# BAREOS® - Backup Archiving REcovery Open Sourced +# +# Copyright (C) 2000-2011 Free Software Foundation Europe e.V. +# Copyright (C) 2013-2014 Bareos GmbH & Co. KG +# +# This program is Free Software; you can redistribute it and/or +# modify it under the terms of version three of the GNU Affero General Public +# License as published by the Free Software Foundation and included +# in the file LICENSE. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. +# +# This script will update a BAREOS database to the latest version. # -default_db_type=`@scriptdir@/bareos-config get_database_driver_default` +# +# Source the Bareos config functions. +# +. @scriptdir@/bareos-config-lib.sh + +db_name="${db_name:-@db_name@}" +db_user="${db_user:-@db_user@}" +db_version=`get_database_version` +bareos_sql_ddl=`get_database_ddl_dir` +temp_sql_schema="/tmp/tables.sql.$$" +default_db_type=`get_database_driver_default` +working_dir=`get_working_dir` # # See if the first argument is a valid backend name. @@ -12,24 +42,24 @@ default_db_type=`@scriptdir@/bareos-config get_database_driver_default` # if [ $# -gt 0 ]; then case $1 in - sqlite3) - db_type=$1 - shift - ;; - mysql) - db_type=$1 - shift - ;; - postgresql) - db_type=$1 - shift - ;; - ingres) - db_type=$1 - shift - ;; - *) - ;; + sqlite3) + db_type=$1 + shift + ;; + mysql) + db_type=$1 + shift + ;; + postgresql) + db_type=$1 + shift + ;; + ingres) + db_type=$1 + shift + ;; + *) + ;; esac fi @@ -40,5 +70,108 @@ if [ -z "${db_type}" ]; then db_type="${default_db_type}" fi -echo "Altering ${db_type} tables" -@scriptdir@/update_${db_type}_tables $* +echo "Updating ${db_type} tables" + +bindir=`get_database_utility_path ${db_type}` +if [ ! -z "${bindir}" ]; then + PATH="$bindir:$PATH" +fi + +while [ 1 ] +do + # + # Figure out what the current version of the database is. + # + case ${db_type} in + sqlite3) + DBVERSION=`echo 'SELECT MAX(VersionId) FROM Version;' | sqlite3 ${working_dir}/${db_name}.db` + ;; + mysql) + DBVERSION=`mysql -D ${db_name} $* -e "SELECT MAX(VersionId) FROM Version\G" | \ + sed -n -e 's/^.*VersionId.*: \(.*\)$/\1/p'` + ;; + postgresql) + DBVERSION=`psql -d ${db_name} -t --pset format=unaligned -c "SELECT MAX(VersionId) FROM Version;" $*` + ;; + ingres) + DBVERSION="@BDB_VERSION@" + ;; + *) + echo "Unknown database type ${db_type}" + exit 1 + ;; + esac + + if [ -z "${DBVERSION}" ]; then + echo "Unable to determine version of Bareos ${db_type} database" + exit 1 + fi + + if [ ${DBVERSION} = ${db_version} ]; then + echo "Finished upgrading database to version ${db_version}" + exit 0 + fi + + # + # See if its a known conversion. + # + found=0 + known_conversions=`ls ${bareos_sql_ddl}/updates/${db_type}* 2>/dev/null| \ + sed -e 's#.*/##' | \ + cut -d'.' -f2` + for conversion in ${known_conversions} + do + start_version=`echo ${conversion} | cut -d_ -f1` + end_version=`echo ${conversion} | cut -d_ -f2` + + if [ ${start_version} = ${DBVERSION} ]; then + found=1 + break + fi + done + + if [ ${found} = 0 ]; then + echo "Don't know how to upgrade from version ${DBVERSION} to ${db_version}" + exit 1 + fi + + sql_definitions="${bareos_sql_ddl}/updates/${db_type}.${conversion}.sql" + if [ ! -f ${sql_definitions} ]; then + echo "Unable to open database update definitions in file ${sql_definitions}" + exit 1 + fi + + sed -e "s/@DB_NAME@/${db_name}/" \ + -e "s/@DB_USER@/${db_user}/" \ + ${sql_definitions} > ${temp_sql_schema} + + echo "Upgrading database schema from version ${start_version} to ${end_version}" + + case ${db_type} in + sqlite3) + sqlite3 $* ${working_dir}/${db_name}.db < ${temp_sql_schema} + retval=0 + ;; + mysql) + mysql -D ${db_name} $* < ${temp_sql_schema} + retval=$? + ;; + postgresql) + psql -f ${temp_sql_schema} -d ${db_name} $* + retval=$? + ;; + ingres) + sql -u${db_user} $* ${db_name} < ${temp_sql_schema} + retval=$? + ;; + esac + + rm -f ${temp_sql_schema} + + if [ ${retval} != 0 ]; then + echo "Failed to upgrade database schema from version ${start_version} to ${end_version}" + break; + fi +done + +exit ${retval} diff --git a/src/cats/update_ingres_tables.in b/src/cats/update_ingres_tables.in deleted file mode 100755 index 49de2f4683a..00000000000 --- a/src/cats/update_ingres_tables.in +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh -# -# Shell script to update Ingres tables (without any function for now) -# -echo " " -echo "This script will update a Bareos Ingres database (if needed)" -echo " " -bindir=@INGRES_BINDIR@ -db_name=@db_name@ - -echo "Update of Bareos Ingres tables succeeded. (nothing to do)" -exit 0 diff --git a/src/cats/update_mysql_tables.in b/src/cats/update_mysql_tables.in deleted file mode 100644 index 00dcbbd9e07..00000000000 --- a/src/cats/update_mysql_tables.in +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/sh -echo " " -echo "This script will update a Bareos MySQL database from version 14 to @BDB_VERSION@" -echo " which is needed to convert from Bacula Community version 5.2.x to Bareos" -echo " " -bindir=@MYSQL_BINDIR@ -PATH="$bindir:$PATH" -db_name=${db_name:-@db_name@} - -mysql -D ${db_name} $* -e "select VersionId from Version\G" >/tmp/$$ -DBVERSION=`sed -n -e 's/^VersionId: \(.*\)$/\1/p' /tmp/$$` -if [ $DBVERSION != 14 ] ; then - echo " " - echo "The existing database is version $DBVERSION !!" - echo "This script can only update an existing version 14 database to version @BDB_VERSION@." - echo "Error. Cannot upgrade this database." - echo " " - exit 1 -fi - -if mysql -D ${db_name} $* -f <