Permalink
Browse files

Issue #444: Command line interface for MySQL data backup

Closes #552
  • Loading branch information...
1 parent c67f088 commit 5452f507e23b1fcf9b605491e1eb5f5411007dc4 Mark Wilkie committed with ginatrapani Jan 7, 2011
@@ -58,6 +58,9 @@ public function import($zipfile) {
// rebuild db
$sql = file_get_contents($create_table);
+ if(getenv('BACKUP_VERBOSE')!==false) {
+ print " Creating tables...\n\n";
+ }
$stmt = $this->execute($sql);
$stmt->closeCursor();
unlink($create_table);
@@ -69,6 +72,9 @@ public function import($zipfile) {
$matches = array();
if(preg_match('#.*/(\w+).txt$#', $table, $matches)) {
$table = $matches[1];
+ if(getenv('BACKUP_VERBOSE')!==false) {
+ print " Restoring data for table: $table\n";
+ }
$q = "LOAD DATA INFILE '$infile' INTO TABLE $table";
$stmt = $this->execute($q);
if(! $stmt) {
@@ -86,20 +92,29 @@ public function import($zipfile) {
}
}
- public function export() {
+ public function export($backup_file = null) {
// get table names...
$q = "show tables";
$q2 = "show create table ";
$stmt = $this->execute($q);
$data = $this->getDataRowsAsArrays($stmt);
$create_tables = '';
$zip_file = THINKUP_WEBAPP_PATH . self::CACHE_DIR . '/thinkup_db_backup.zip';
+ if($backup_file) {
+ $zip_file = $backup_file;
+ }
$zip = new ZipArchive();
if(file_exists($zip_file)) {
unlink($zip_file);
}
- if ($zip->open($zip_file, ZIPARCHIVE::CREATE)!==TRUE) {
- throw new Exception("Unable to open backup for for exporting: $zip_file");
+ // make sure w can create this zip file, ZipArchive is a little funky and wont let us know its status
+ // until we call close
+ $zip_create_status = @touch($zip_file);
+ if($zip_create_status) {
+ unlink($zip_file);
+ }
+ if (! $zip_create_status || $zip->open($zip_file, ZIPARCHIVE::CREATE)!==TRUE) {
+ throw new Exception("Unable to open backup file for exporting: $zip_file");
}
// lock tables for writes...
@@ -108,7 +123,9 @@ public function export() {
$tmp_table_files = array();
foreach($data as $table) {
foreach($table as $key => $value) {
-
+ if(getenv('BACKUP_VERBOSE')!==false) {
+ print " Backing up data for table: $value\n";
+ }
$stmt = $this->execute($q2 . $value);
$create_tables .= "-- Create $value table statement\n";
$create_tables .= "DROP TABLE IF EXISTS $value;\n";
@@ -130,13 +147,18 @@ public function export() {
// unlock tables...
$stmt = $this->execute("unlock tables");
-
+ if(getenv('BACKUP_VERBOSE')!==false) {
+ print "\n Backing up create table statments\n";
+ }
$zip->addFromString("create_tables.sql", $create_tables);
- $zip->close();
+ $zip_close_status = $zip->close();
// clean up tmp table files
foreach($tmp_table_files as $tmp_file) {
unlink($tmp_file);
}
+ if($zip_close_status == false) {
+ throw new Exception("Unable to create backup file for exporting, bad file path?: $zip_file");
+ }
return $zip_file;
}
}
@@ -38,9 +38,10 @@
/**
* Export database to tmp dir
+ * @param $str Backup File (optional)
* @return $str Path to backup file
*/
- public function export();
+ public function export($backup_file = null);
/**
* Import database zip file
@@ -0,0 +1,83 @@
+<?php
+/**
+ *
+ * ThinkUp/webapp/install/cli/backup.php
+ *
+ * Copyright (c) 2009-2010 Mark Wilkie
+ *
+ * LICENSE:
+ *
+ * This file is part of ThinkUp (http://thinkupapp.com).
+ *
+ * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
+ * later version.
+ *
+ * ThinkUp 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 General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *
+ * Caommnd line interface for backing up thinkup data
+ *
+ * @license http://www.gnu.org/licenses/gpl.html
+ * @copyright 2009-2010 Mark Wilkie
+ * @author Mark Wilkie <mwilkie[at]gmail[dot]com>
+ */
+chdir('../..');
+require_once 'init.php';
+
+// don't run via the web...
+if(isset($_SERVER['SERVER_NAME'])) {
+ die("This script should only be run via the command line...");
+}
+
+try {
+ array_shift($argv);
+ if($argv[0] && preg_match('/^(\-h|\-\-help)$/i', $argv[0])) {
+ usage();
+ }
+
+
+ if (count($argv) != 2 || ! preg_match('/^(\-\-export|\-\-import)$/', $argv[0]) ) {
+ usage();
+ } else {
+ $filename = $argv[1];
+ if( ! preg_match('/\.zip$/', $filename) ) {
+ error_log("\nError: data file should end in .zip");
+ usage();
+ } else {
+
+ putenv('BACKUP_VERBOSE=true');
+ $backup_dao = DAOFactory::getDAO('BackupDAO');
+ if($argv[0] == '--export') {
+ print "\nExporting data to: $filename\n\n";
+ $backup_dao->export($filename);
+ print "\nBackup completed...\n\n";
+ } else {
+ if(! file_exists($filename)) {
+ error_log("\nError: data import file '$filename' not found");
+ usage();
+ }
+ print "\nImporting data from: $filename\n\n";
+ $backup_dao->import($filename);
+ print "\nImport completed...\n\n";
+
+ }
+ }
+ }
+} catch(Exception $e) {
+ error_log(" Error: " . $e->getMessage() . "\n");
+}
+
+function usage() {
+ print "\n Usage:\n\n";
+ print " php backup.php [--help] [--export|--import] filename.zip\n\n";
+ print " --export export_filename.zip - exports data to specified filename\n";
+ print " --import import_filename.zip - imports data from specified filename\n";
+ print " --help - usage help\n\n";
+ exit;
+}

0 comments on commit 5452f50

Please sign in to comment.