Handle permissions and multiple workspaces correctly in restore
This commit is contained in:
@@ -71,7 +71,7 @@ CLI::taskArg('workspace', true);
|
|||||||
CLI::taskOpt("overwrite", "If a workspace already exists, overwrite it.", "o", "overwrite");
|
CLI::taskOpt("overwrite", "If a workspace already exists, overwrite it.", "o", "overwrite");
|
||||||
CLI::taskOpt("info", "Only shows information about a backup archive.", "i");
|
CLI::taskOpt("info", "Only shows information about a backup archive.", "i");
|
||||||
CLI::taskOpt("workspace", "Select which workspace to restore if multiple workspaces are present in the archive.",
|
CLI::taskOpt("workspace", "Select which workspace to restore if multiple workspaces are present in the archive.",
|
||||||
"w", "workspace");
|
"w:", "workspace=");
|
||||||
CLI::taskRun(run_workspace_restore);
|
CLI::taskRun(run_workspace_restore);
|
||||||
|
|
||||||
CLI::taskName('cacheview-repair');
|
CLI::taskName('cacheview-repair');
|
||||||
|
|||||||
@@ -215,8 +215,10 @@ EOT;
|
|||||||
}
|
}
|
||||||
list($options, $arguments) = $getopt;
|
list($options, $arguments) = $getopt;
|
||||||
foreach ($taskData['opt']['descriptions'] as $optName => $optDescription) {
|
foreach ($taskData['opt']['descriptions'] as $optName => $optDescription) {
|
||||||
$validOpts[$optDescription['short']] = $optName;
|
$short = str_replace(":", "", $optDescription['short']);
|
||||||
$validOpts[$optDescription['long']] = $optName;
|
$long = str_replace("=", "", $optDescription['long']);
|
||||||
|
$validOpts[$short] = $optName;
|
||||||
|
$validOpts[$long] = $optName;
|
||||||
}
|
}
|
||||||
$taskOpts = array();
|
$taskOpts = array();
|
||||||
try {
|
try {
|
||||||
@@ -229,7 +231,7 @@ EOT;
|
|||||||
if (strpos($optName, '--') === 0)
|
if (strpos($optName, '--') === 0)
|
||||||
$optName = substr($optName, 2);
|
$optName = substr($optName, 2);
|
||||||
if (!array_key_exists($optName, $validOpts))
|
if (!array_key_exists($optName, $validOpts))
|
||||||
throw new Exception("Invalid option: $optName");
|
throw new Exception("option not found: $optName");
|
||||||
if (array_key_exists($validOpts[$optName], $taskOpts))
|
if (array_key_exists($validOpts[$optName], $taskOpts))
|
||||||
throw new Exception("'$optName' specified more then once");
|
throw new Exception("'$optName' specified more then once");
|
||||||
$taskOpts[$validOpts[$optName]] = $optArg;
|
$taskOpts[$validOpts[$optName]] = $optArg;
|
||||||
@@ -255,6 +257,15 @@ EOT;
|
|||||||
return pakeColor::colorize($message, "INFO");
|
return pakeColor::colorize($message, "INFO");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a warning colorized version of the message.
|
||||||
|
*
|
||||||
|
* @param string $message the message to colorize
|
||||||
|
*/
|
||||||
|
public static function warning($message) {
|
||||||
|
return pakeColor::colorize($message, "COMMENT");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an error colorized version of the message.
|
* Returns an error colorized version of the message.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -145,7 +145,6 @@ class workspaceTools {
|
|||||||
$this->resetDBNames = $resetDBNames;
|
$this->resetDBNames = $resetDBNames;
|
||||||
$this->resetDBDiff = array();
|
$this->resetDBDiff = array();
|
||||||
|
|
||||||
CLI::logging("Resetting db info\n");
|
|
||||||
if (!$this->workspaceExists())
|
if (!$this->workspaceExists())
|
||||||
throw new Exception("Could not find db.php in the workspace");
|
throw new Exception("Could not find db.php in the workspace");
|
||||||
$sDbFile = file_get_contents($this->dbPath);
|
$sDbFile = file_get_contents($this->dbPath);
|
||||||
@@ -891,6 +890,19 @@ class workspaceTools {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static public function dirPerms($filename, $owner, $group, $perms) {
|
||||||
|
$chown = chown($filename, $owner);
|
||||||
|
$chgrp = chgrp($filename, $group);
|
||||||
|
$chmod = chmod($filename, $perms);
|
||||||
|
if ($chgrp === false || $chmod === false || $chown === false)
|
||||||
|
CLI::logging (CLI::error ("Failed to set permissions for $filename") . "\n");
|
||||||
|
if (is_dir($filename)) {
|
||||||
|
foreach (glob($filename . "/*") as $item) {
|
||||||
|
workspaceTools::dirPerms($item, $owner, $group, $perms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* restore an archive into a workspace
|
* restore an archive into a workspace
|
||||||
*
|
*
|
||||||
@@ -926,7 +938,7 @@ class workspaceTools {
|
|||||||
}
|
}
|
||||||
if (count($metaFiles) > 1 && (!isset($srcWorkspace)))
|
if (count($metaFiles) > 1 && (!isset($srcWorkspace)))
|
||||||
throw new Exception("Multiple workspaces in backup but no workspace specified to restore");
|
throw new Exception("Multiple workspaces in backup but no workspace specified to restore");
|
||||||
if (isset($srcWorkspace) && !in_array("$srcWorkspace.meta", $metaFiles))
|
if (isset($srcWorkspace) && !in_array("$srcWorkspace.meta", array_map(basename, $metaFiles)))
|
||||||
throw new Exception("Workspace $srcWorkspace not found in backup");
|
throw new Exception("Workspace $srcWorkspace not found in backup");
|
||||||
foreach ($metaFiles as $metaFile) {
|
foreach ($metaFiles as $metaFile) {
|
||||||
$metadata = G::json_decode(file_get_contents($metaFile));
|
$metadata = G::json_decode(file_get_contents($metaFile));
|
||||||
@@ -941,15 +953,15 @@ class workspaceTools {
|
|||||||
$createWorkspace = false;
|
$createWorkspace = false;
|
||||||
}
|
}
|
||||||
if (isset($srcWorkspace) && strcmp($metadata->WORKSPACE_NAME, $srcWorkspace) != 0) {
|
if (isset($srcWorkspace) && strcmp($metadata->WORKSPACE_NAME, $srcWorkspace) != 0) {
|
||||||
CLI::logging("Workspace $backupWorkspace found, but not restoring.\n");
|
CLI::logging(CLI::warning("> Workspace $backupWorkspace found, but not restoring.") . "\n");
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
CLI::logging("Restoring $backupWorkspace to $workspaceName\n");
|
CLI::logging("> Restoring " . CLI::info($backupWorkspace) . " to " . CLI::info($workspaceName) . "\n");
|
||||||
}
|
}
|
||||||
$workspace = new workspaceTools($workspaceName);
|
$workspace = new workspaceTools($workspaceName);
|
||||||
if ($workspace->workspaceExists())
|
if ($workspace->workspaceExists())
|
||||||
if ($overwrite)
|
if ($overwrite)
|
||||||
CLI::logging("Workspace $workspaceName already exists, overwriting!\n");
|
CLI::logging(CLI::warning("> Workspace $workspaceName already exists, overwriting!") . "\n");
|
||||||
else
|
else
|
||||||
throw new Exception("Destination workspace already exists (use -o to overwrite)");
|
throw new Exception("Destination workspace already exists (use -o to overwrite)");
|
||||||
|
|
||||||
@@ -957,16 +969,24 @@ class workspaceTools {
|
|||||||
G::rm_dir($workspace->path);
|
G::rm_dir($workspace->path);
|
||||||
|
|
||||||
foreach ($metadata->directories as $dir) {
|
foreach ($metadata->directories as $dir) {
|
||||||
CLI::logging("Restoring directory '$dir'\n");
|
CLI::logging("+> Restoring directory '$dir'\n");
|
||||||
|
|
||||||
if (!rename("$tempDirectory/$dir", $workspace->path)) {
|
if (!rename("$tempDirectory/$dir", $workspace->path)) {
|
||||||
throw new Exception("There was an error copying the backup files ($tempDirectory/$dir) to the workspace directory {$workspace->path}.");
|
throw new Exception("There was an error copying the backup files ($tempDirectory/$dir) to the workspace directory {$workspace->path}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CLI::logging("> Changing file permissions\n");
|
||||||
|
$shared_stat = stat(PATH_DATA);
|
||||||
|
if ($shared_stat !== false)
|
||||||
|
workspaceTools::dirPerms($workspace->path, $shared_stat['uid'], $shared_stat['gid'], $shared_stat['mode']);
|
||||||
|
else
|
||||||
|
CLI::logging(CLI::error ("Could not get the shared folder permissions, not changing workspace permissions") . "\n");
|
||||||
|
|
||||||
list($dbHost, $dbUser, $dbPass) = @explode(SYSTEM_HASH, G::decrypt(HASH_INSTALLATION, SYSTEM_HASH));
|
list($dbHost, $dbUser, $dbPass) = @explode(SYSTEM_HASH, G::decrypt(HASH_INSTALLATION, SYSTEM_HASH));
|
||||||
|
|
||||||
CLI::logging("Connecting to system database in '$dbHost'\n");
|
CLI::logging("> Connecting to system database in '$dbHost'\n");
|
||||||
$link = mysql_connect($dbHost, $dbUser, $dbPass);
|
$link = mysql_connect($dbHost, $dbUser, $dbPass);
|
||||||
if (!$link)
|
if (!$link)
|
||||||
throw new Exception('Could not connect to system database: ' . mysql_error());
|
throw new Exception('Could not connect to system database: ' . mysql_error());
|
||||||
@@ -975,7 +995,7 @@ class workspaceTools {
|
|||||||
|
|
||||||
foreach ($metadata->databases as $db) {
|
foreach ($metadata->databases as $db) {
|
||||||
$dbName = $newDBNames[$db->name];
|
$dbName = $newDBNames[$db->name];
|
||||||
CLI::logging("Restoring database {$db->name} to $dbName\n");
|
CLI::logging("+> Restoring database {$db->name} to $dbName\n");
|
||||||
$workspace->executeSQLScript($dbName, "$tempDirectory/{$db->name}.sql");
|
$workspace->executeSQLScript($dbName, "$tempDirectory/{$db->name}.sql");
|
||||||
$workspace->createDBUser($dbName, $db->pass, "localhost", $dbName);
|
$workspace->createDBUser($dbName, $db->pass, "localhost", $dbName);
|
||||||
$workspace->createDBUser($dbName, $db->pass, "%", $dbName);
|
$workspace->createDBUser($dbName, $db->pass, "%", $dbName);
|
||||||
@@ -984,8 +1004,12 @@ class workspaceTools {
|
|||||||
mysql_close($link);
|
mysql_close($link);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CLI::logging("Removing temporary files\n");
|
||||||
|
|
||||||
G::rm_dir($tempDirectory);
|
G::rm_dir($tempDirectory);
|
||||||
|
|
||||||
|
CLI::logging(CLI::info("Done restoring") . "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user