3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 Kern Sibbald
8 * The main author of Baculum is Marcin Haba.
9 * The original author of Bacula is Kern Sibbald, with contributions
10 * from many others, a complete list can be found in the file AUTHORS.
12 * You may use this file and others of this release according to the
13 * license defined in the LICENSE file, which includes the Affero General
14 * Public License, v3.0 ("AGPLv3") and some additional permissions and
15 * terms pursuant to its AGPLv3 Section 7.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
23 Prado::using('System.interfaces');
24 Prado::using('Application.Common.Class.Errors');
25 Prado::using('Application.API.Class.BException');
26 Prado::using('Application.API.Class.APIModule');
27 Prado::using('Application.API.Class.APIDbModule');
29 class Database extends APIModule {
34 * Supported database types
36 const PGSQL_TYPE = 'pgsql';
37 const MYSQL_TYPE = 'mysql';
38 const SQLITE_TYPE = 'sqlite';
41 * Check/test connection to database.
44 * @param array $db_params params to database connection
45 * @return array true if test passed, otherwise false
46 * @throws BCatalogException if problem with connection to database
48 public function testDbConnection(array $db_params) {
49 $is_connection = false;
50 $tables_format = null;
53 $connection = APIDbModule::getAPIDbConnection($db_params, true);
54 $connection->setActive(true);
55 $tables_format = $this->getTablesFormat($connection);
56 $is_connection = (is_numeric($tables_format) === true && $tables_format > 0);
57 } catch (TDbException $e) {
58 throw new BCatalogException(
59 DatabaseError::MSG_ERROR_DB_CONNECTION_PROBLEM . ' ' . $e->getErrorMessage(),
60 DatabaseError::ERROR_DB_CONNECTION_PROBLEM
64 if(array_key_exists('password', $db_params)) {
65 // mask database password.
66 $db_params['password'] = preg_replace('/.{1}/', '*', $db_params['password']);
69 $logmsg = 'DBParams=%s, Connection=%s, TablesFormat=%s';
70 $msg = sprintf($logmsg, print_r($db_params, true), var_export($is_connection, true), var_export($tables_format, true));
71 $this->getModule('logging')->log(
74 Logging::CATEGORY_APPLICATION,
78 return $is_connection;
82 * Test connection to the Catalog using params from config.
85 * @return bool true if connection established, otherwise false
87 public function testCatalog() {
89 $api_config = $this->getModule('api_config')->getConfig();
90 if (array_key_exists('db', $api_config)) {
91 $result = $this->testDbConnection($api_config['db']);
93 throw new BCatalogException(
94 DatabaseError::MSG_ERROR_DATABASE_ACCESS_NOT_SUPPORTED,
95 DatabaseError::ERROR_DATABASE_ACCESS_NOT_SUPPORTED
102 * Get Catalog database tables format
105 * @param TDBConnection $connection handler to database connection
106 * @return mixed Catalog database tables format or null
108 private function getTablesFormat(TDBConnection $connection) {
109 $query = 'SELECT versionid FROM Version';
110 $command = $connection->createCommand($query);
111 $row = $command->queryRow();
112 $tables_format = array_key_exists('versionid', $row) ? $row['versionid'] : null;
113 return $tables_format;
116 public function getDatabaseSize() {
117 $db_params = $this->getModule('api_config')->getConfig('db');
119 $connection = APIDbModule::getAPIDbConnection($db_params);
120 $connection->setActive(true);
121 $pdo = $connection->getPdoInstance();
124 switch ($db_params['type']) {
125 case self::PGSQL_TYPE: {
126 $sql = "SELECT pg_database_size('{$db_params['name']}') AS dbsize";
127 $result = $pdo->query($sql);
128 $row = $result->fetch();
129 $size = $row['dbsize'];
132 case self::MYSQL_TYPE: {
133 $sql = "SELECT Sum(data_length + index_length) AS dbsize FROM information_schema.tables";
134 $result = $pdo->query($sql);
135 $row = $result->fetch();
136 $size = $row['dbsize'];
139 case self::SQLITE_TYPE: {
140 $sql = "PRAGMA page_count";
141 $result = $pdo->query($sql);
142 $page_count = $result->fetch();
143 $sql = "PRAGMA page_size";
144 $result = $pdo->query($sql);
145 $page_size = $result->fetch();
146 $size = ($page_count['page_count'] * $page_size['page_size']);
150 $dbsize = array('dbsize' => $size, 'dbtype' => $db_params['type']);