diff --git a/workflow/engine/bin/tasks/cliCommon.php b/workflow/engine/bin/tasks/cliCommon.php index 4c0d4c22f..f7d461486 100644 --- a/workflow/engine/bin/tasks/cliCommon.php +++ b/workflow/engine/bin/tasks/cliCommon.php @@ -71,6 +71,52 @@ function logging($message, $filename = NULL) { } } +function progress($done, $total, $size=30) { + + static $start_time; + + // if we go over our bound, just ignore it + if($done > $total) return; + + if(empty($start_time)) $start_time=time(); + $now = time(); + + $perc=(double)($done/$total); + + $bar=floor($perc*$size); + + $status_bar="\r["; + $status_bar.=str_repeat("=", $bar); + if($bar<$size){ + $status_bar.=">"; + $status_bar.=str_repeat(" ", $size-$bar); + } else { + $status_bar.="="; + } + + $disp=number_format($perc*100, 0); + + $status_bar.="] $disp% $done/$total"; + + $rate = ($now-$start_time)/$done; + $left = $total - $done; + $eta = round($rate * $left, 2); + + $elapsed = $now - $start_time; + + $status_bar.= " remaining: ".number_format($eta)." sec. elapsed: ".number_format($elapsed)." sec."; + + echo "$status_bar "; + + flush(); + + // when done, send a newline + if($done == $total) { + echo "\n"; + } + +} + function get_workspaces_from_args($args, $includeAll = true) { $opts = parse_args($args); $workspaces = array(); diff --git a/workflow/engine/bin/tasks/cliWorkspaces.php b/workflow/engine/bin/tasks/cliWorkspaces.php index 38fc10699..14a3c97f5 100644 --- a/workflow/engine/bin/tasks/cliWorkspaces.php +++ b/workflow/engine/bin/tasks/cliWorkspaces.php @@ -4,6 +4,8 @@ pake_task('info'); pake_task('workspace-upgrade'); +pake_task('workspace-backup'); + pake_task('translation-upgrade'); pake_task('cacheview-upgrade'); @@ -77,7 +79,6 @@ function run_plugins_database_upgrade($command, $args) { } function run_database_export($command, $args) { - G::LoadSystem('dbMaintenance'); if (count($args) < 2) throw new Exception ("Please provide a workspace name and a directory for export"); $workspace = new workspaceTools($args[0]); @@ -105,7 +106,7 @@ function database_upgrade($command, $args) { else print_r("Upgrading database in ".pakeColor::colorize($workspace->name, "INFO")." "); try { - $changes = $workspace->repairSchema($checkOnly); + $changes = $workspace->upgradeDatabase($checkOnly); if ($changes != false) { if ($checkOnly) { echo "> ".pakeColor::colorize("Run upgrade", "INFO")."\n"; @@ -210,4 +211,13 @@ function run_drafts_clean($task, $args) { echo "\n"; } +function run_workspace_backup($task, $args) { + $workspace = new workspaceTools($args[0]); + if (isset($args[1])) + $filename = $args[1]; + else + $filename = PATH_DATA . "backups/" . $workspace->name . ".tar"; + $workspace->backup($filename); +} + ?> \ No newline at end of file diff --git a/workflow/engine/classes/class.wsTools.php b/workflow/engine/classes/class.wsTools.php index cd7d7aabc..1c6d550c0 100644 --- a/workflow/engine/classes/class.wsTools.php +++ b/workflow/engine/classes/class.wsTools.php @@ -6,6 +6,8 @@ * @author Alexandre Rosenfeld */ +G::LoadSystem('dbMaintenance'); + class workspaceTools { var $name = NULL; var $path = NULL; @@ -486,36 +488,6 @@ class workspaceTools { } } - public function dumpDatabase($dbname) { - $sql="show tables;"; - $result= mysql_query($sql); - if( $result) - { - while( $row= mysql_fetch_row($result)) - { - $table = $row[0]; - - echo "/* Table structure for table `$table` */\n"; - echo "DROP TABLE IF EXISTS `$table`;\n\n"; - $sql="show create table `$table`; "; - $result=mysql_query($sql); - if( $result) - { - if($row= mysql_fetch_assoc($result)) - { - echo $row['Create Table'].";\n\n"; - } - } - - } - } - else - { - echo "/* no tables in $mysql_database */\n"; - } - mysql_free_result($result); - } - public function exportDatabase($path) { $dbInfo = $this->getDBInfo(); $databases = array("wf", "rp", "rb"); @@ -524,7 +496,7 @@ class workspaceTools { $oDbMaintainer = new DataBaseMaintenance($dbInfo["host"], $dbInfo["user"], $dbInfo["pass"]); $oDbMaintainer->connect($dbInfo["name"]); - $oDbMaintainer->setTempDir($path . $dbInfo["name"] . "/"); + $oDbMaintainer->setTempDir($path . "/" . $dbInfo["name"] . "/"); $oDbMaintainer->backupDataBaseSchema($oDbMaintainer->getTempDir() . $dbInfo["name"] . ".sql"); $oDbMaintainer->backupSqlData(); } @@ -536,21 +508,38 @@ class workspaceTools { } else { foreach (glob($filename . "/*") as $item) { $this->addToBackup($backup, $item, $root); -} + } } } - public function backup($filename, $compress = true) { + static public function createBackup($filename, $compress = true) { G::LoadThirdParty('pear/Archive', 'Tar'); - $tar = new Archive_Tar($filename); + $backup = new Archive_Tar($filename); + return $backup; + } + + public function backup($filename, $compress = true) { + if (is_string($filename)) + $backup = $this->createBackup($filename); + else + $backup = $filename; //Get a temporary directory for database backup $tempDirectory = tempnam(__FILE__, ''); if (file_exists($tempDirectory)) { unlink($tempDirectory); } - $this->exportDatabase($tempDirectory); + mkdir($tempDirectory); + //$this->exportDatabase($tempDirectory); + //print_r(info("Database: $tempDirectory\n")); + $metaFilename = "$tempDirectory/{$this->name}.meta"; + file_put_contents($metaFilename, + str_replace(array(",", "{", "}"), array(",\n ", "{\n ", "\n}\n"), + G::json_encode($this->getMetadata()))); $this->addToBackup($backup, $tempDirectory, $tempDirectory); - $this->addToBackup($backup, $filename, $root); + $this->addToBackup($backup, $this->path, $this->path); + //print_r(file_get_contents($metaFilename)); + //print_r(G::json_decode(file_get_contents($metaFilename))); + //$this->addToBackup($backup, $filename, $root); } }