// Get all available options and does some validity checking try { $opts->parse(); } catch (Exception $e) { Ot_Cli_Output::error($opts->getUsageMessage()); } if (!isset($opts->environment) || !in_array($opts->environment, $possibleEnvironments)) { Ot_Cli_Output::error('Environment not sepecified or not available' . "\n\n" . $opts->getUsageMessage()); } if (!isset($opts->cmd) || !in_array($opts->cmd, $possibleCommands)) { Ot_Cli_Output::error('Command not sepecified or not available' . "\n\n" . $opts->getUsageMessage()); } // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', $opts->environment); // Create application, bootstrap, and run $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini'); $application->bootstrap('db'); // Get the database configs $tablePrefix = $application->getOption('tablePrefix'); $dbAdapter = $application->getBootstrap()->getPluginResource('db')->getDbAdapter(); // Run the migrator try { $migration = new Ot_Migrate($dbAdapter, $pathToMigrateFiles, $tablePrefix); $result = $migration->migrate($opts->cmd, $opts->version); } catch (Exception $e) { Ot_Cli_Output::error($e->getMessage()); } // Display the results Ot_Cli_Output::success($result); // Exit without any errors exit;
} catch (Exception $e) { Ot_Cli_Output::error($opts->getUsageMessage()); } if (!isset($opts->source) || !in_array($opts->source, $possibleSources)) { Ot_Cli_Output::error('Source not sepecified or not available' . "\n\n" . $opts->getUsageMessage()); } if (!isset($opts->destination) || !in_array($opts->destination, $possibleDestinations)) { Ot_Cli_Output::error('Destination not sepecified or not available' . "\n\n" . $opts->getUsageMessage()); } if ($opts->source == $opts->destination) { Ot_Cli_Output::error('Source and Destination cannot be the same' . "\n\n" . $opts->getUsageMessage()); } $source = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', $opts->source); $destination = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', $opts->destination); if (!isset($source->resources->db)) { Ot_Cli_Output::error('No DB information found in source' . "\n\n" . $opts->getUsageMessage()); } if (!isset($destination->resources->db)) { Ot_Cli_Output::error('No DB information found in destination' . "\n\n" . $opts->getUsageMessage()); } if ($source->resources->db->adapter != 'PDO_MYSQL' || $destination->resources->db->adapter != 'PDO_MYSQL') { Ot_Cli_Output::error('Source or Destination is not MYSQL' . "\n\n" . $opts->getUsageMessage()); } // Drops all the tables in the destination DB $dropCommand = "mysqldump -u " . escapeshellarg($destination->resources->db->params->username) . " -p" . escapeshellarg($destination->resources->db->params->password) . " -h " . escapeshellarg($destination->resources->db->params->host) . " --add-drop-table --no-data" . " " . escapeshellarg($destination->resources->db->params->dbname) . " | " . " grep ^DROP " . " | " . " mysql -u " . escapeshellarg($destination->resources->db->params->username) . " -p" . escapeshellarg($destination->resources->db->params->password) . " -h " . escapeshellarg($destination->resources->db->params->host) . " " . escapeshellarg($destination->resources->db->params->dbname); $copyCommand = "mysqldump -u " . escapeshellarg($source->resources->db->params->username) . " -p" . escapeshellarg($source->resources->db->params->password) . " -h " . escapeshellarg($source->resources->db->params->host) . " " . escapeshellarg($source->resources->db->params->dbname) . " | " . " mysql -u " . escapeshellarg($destination->resources->db->params->username) . " -p" . escapeshellarg($destination->resources->db->params->password) . " -h " . escapeshellarg($destination->resources->db->params->host) . " " . escapeshellarg($destination->resources->db->params->dbname); system($dropCommand); system($copyCommand); Ot_Cli_Output::success('Copy Complete'); // Exit without any errors exit;
$opts = new Zend_Console_Getopt(array('environment|e=s' => 'DB Environment (' . implode($possibleSources, ', ') . ')', 'path|p=s' => 'Absolute file path to write the file to.')); // Get all available options and does some validity checking try { $opts->parse(); } catch (Exception $e) { Ot_Cli_Output::error($opts->getUsageMessage()); } if (!isset($opts->environment) || !in_array($opts->environment, $possibleSources)) { Ot_Cli_Output::error('Environment not sepecified or not available' . "\n\n" . $opts->getUsageMessage()); } if (!isset($opts->path)) { Ot_Cli_Output::error('Path not sepecified or not available' . "\n\n" . $opts->getUsageMessage()); } if (!is_writable($opts->path)) { Ot_Cli_Output::error('Path is not writable' . "\n\n" . $opts->getUsageMessage()); } $ini = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', $opts->environment); if (!isset($ini->resources->db)) { Ot_Cli_Output::error('No DB information found in environment' . "\n\n" . $opts->getUsageMessage()); } if ($ini->resources->db->adapter != 'PDO_MYSQL') { Ot_Cli_Output::error('DB is not MYSQL' . "\n\n" . $opts->getUsageMessage()); } date_default_timezone_set(isset($ini->phpSettings->date->timezone) ? $ini->phpSettings->date->timezone : 'America/New_York'); $filename = preg_replace('/\\/$/', '', $opts->path) . '/' . preg_replace('/[^a-z0-9]+/i', '_', $ini->resources->db->params->host . ':' . $ini->resources->db->params->dbname . ':' . date('r')) . '.sql'; // Drops all the tables in the destination DB $dumpCommand = "mysqldump -u " . escapeshellarg($ini->resources->db->params->username) . " -p" . escapeshellarg($ini->resources->db->params->password) . " -h " . escapeshellarg($ini->resources->db->params->host) . " " . escapeshellarg($ini->resources->db->params->dbname) . " > " . $filename; system($dumpCommand); Ot_Cli_Output::success('Backup Complete'); // Exit without any errors exit;