Permalink
Browse files

Added error handling for problems with the history.db file, and allow…

… mysqldump to be in non-standard locations
  • Loading branch information...
1 parent f66b307 commit bf389a79bfec25d3516b9c93e0cda90b2255f452 Eric Caron committed Nov 22, 2011
Showing with 30 additions and 9 deletions.
  1. +1 −0 config.ini.php.dist
  2. +29 −9 processor.php
View
@@ -1,4 +1,5 @@
<?php
+define('MYSQLDUMP_BIN', '/usr/bin/mysqldump');
define('ALERT_EMAIL', 'youremail@yourcompany.com'); //Default address to receive any alerts
define('ARCHIVE_DIR', 'PUT-FULL-PATH-HERE');
define('DEFAULT_DAILY_KEPT', 3);
View
@@ -1,20 +1,22 @@
<?php
/**
* Master controller for performing all database backups.
+ * NO MODIFICATIONS TO THIS FILE ARE NECESSARY!!!
*/
//Load the misc. global configuration settings
if (!file_exists(dirname(__FILE__).'/config.ini.php')) {
die("No config.ini.php file found. Exiting.\n");
}
include dirname(__FILE__).'/config.ini.php';
+
+//Make sure that the config file is setup correctly
if (!is_dir(ARCHIVE_DIR)) {
- die("Archive directory - " . ARCHIVE_DIR . " - does not exist.\n");
+ die("Archive directory - " . ARCHIVE_DIR . " - does not exist or access was denied.\n");
+}
+if (!file_exists(MYSQLDUMP_BIN)) {
+ die('Executable mysqldump not found at '.MYSQLDUMP_BIN."\n");
}
-
-/**
- * NO MODIFICATION REQUIRED PAST THIS POINT
- */
error_reporting(E_ALL);
$GLOBALS['errors'] = array();
@@ -44,18 +46,36 @@
);
//Access our self-contained database
-$db = new SQLite3(dirname(__FILE__).'/history.db');
+try {
+ $db = new SQLite3(dirname(__FILE__).'/history.db');
+} catch (Exception $e) {
+ if ($dbExists === false) {
+ die("Unable to create SQLite database, likely because history.db cannot be created in ".dirname(__FILE__)."\n");
+ } else {
+ die("Unable to load SQLite database: ".$e->getMessage()."\n");
+ }
+}
//Include all our specialized functions
include dirname(__FILE__).'/includes/functions.inc.php';
+//If we created the database, seed it with the appropriate tables
if ($dbExists === false) {
$db->query('CREATE TABLE db_history (db text, last_update int, PRIMARY KEY (db));');
$db->query('CREATE TABLE tbl_history (db text, tbl text, last_update int, PRIMARY KEY (db, tbl));');
+} else { //Verify that the two tables exist
+ $q = $db->query('SELECT name FROM sqlite_master WHERE type = "table";');
+ $tables = array();
+ while ($res = $q->fetchArray()) {
+ $tables[] = $res['name'];
+ }
+ if (!in_array('db_history', $tables) || !in_array('tbl_history', $tables)) {
+ die("'history.db' is not a valid SQLite database, or does not contain the valid tables.\n");
+ }
}
//Make sure the parent directory for the dumps exists
-$parentBackupDir = "/tmp/sql-".date("Ymd", START_TIME)."/";
+$parentBackupDir = sys_get_temp_dir()."/sql-".date("Ymd", START_TIME)."/";
if (!is_dir($parentBackupDir)) mkdir($parentBackupDir,0700);
//Loop through every configuration file and perform the backups
@@ -146,7 +166,7 @@
$databaseSuccess = true;
if ($dumpFullDatabase === true) {
echo "\tDumping database all at once...\n";
- $command = "/usr/bin/mysqldump -h $host -u $user --password=$pass -qe $flag ".$database." | gzip -f > ".$innerDir.$database.".sql.gz";
+ $command = MYSQLDUMP_BIN." -h $host -u $user --password=$pass -qe $flag ".$database." | gzip -f > ".$innerDir.$database.".sql.gz";
$process = proc_open($command, $descriptorSpec, $pipes);
if (is_resource($process)) {
//Never needed since we pipe all STDOUT to gzip, but run to close the stream
@@ -207,7 +227,7 @@
}
if ($needDump === true) {
$result = array();
- $command = "/usr/bin/mysqldump -h $host -u $user --password=$pass -qe $flag ".$database." ".$table." | gzip -f > ".$innerDir.$table.".sql.gz";
+ $command = MYSQLDUMP_BIN . " -h $host -u $user --password=$pass -qe $flag ".$database." ".$table." | gzip -f > ".$innerDir.$table.".sql.gz";
$process = proc_open($command, $descriptorSpec, $pipes);
if (is_resource($process)) {
//Never needed since we pipe all STDOUT to gzip, but run to close the stream

0 comments on commit bf389a7

Please sign in to comment.