Skip to content

Instantly share code, notes, and snippets.

@kbariotis
Created June 29, 2014 19:56
Show Gist options
  • Save kbariotis/5aca7ecb71bc097dfed0 to your computer and use it in GitHub Desktop.
Save kbariotis/5aca7ecb71bc097dfed0 to your computer and use it in GitHub Desktop.
quick and dirt PHP script to dump/restore Magento DB and change Domain
<?php
/* IGNORE_TABLES */
$tablesToBeIgnored = array (
"dataflow_batch_export",
"dataflow_batch_import",
"log_customer",
"log_quote",
"log_summary",
"log_summary_type",
"log_url",
"log_url_info",
"log_visitor",
"log_visitor_info",
"log_visitor_online",
"report_event",
"index_event",
"enterprise_logging_event_changes",
"core_cache",
"core_cache_tag",
"core_session",
"core_cache_tag"
);
/* IGNORE_TABLES_AGGRESSIVE */
$tablesToBeIgnoredAggressively = array (
"report_compared_product_index",
"report_viewed_product_index",
"sales_flat_quote_address",
"sales_flat_quote_shipping_rate",
"enterprise_customer_sales_flat_quote",
"enterprise_customer_sales_flat_quote_address",
"sales_flat_quote",
);
/* TRUNCATE_TABLES */
$tablesToBeTruncated = array (
"dataflow_batch_export",
"dataflow_batch_import",
"log_customer",
"log_quote",
"log_summary",
"log_summary_type",
"log_url",
"log_url_info",
"log_visitor",
"log_visitor_info",
"log_visitor_online",
"report_viewed_product_index",
"report_compared_product_index",
"report_event",
"index_event",
"index_process_event"
);
$localxml = "./app/etc/local.xml";
$dumpsql = "./var/db.sql";
if(!is_file($localxml)) {
throw new Exception('This is not a Magento\'s root directory');
}
$xml = simplexml_load_file($localxml);
$host = (string)$xml->global->resources->default_setup->connection->host;
$user = (string)$xml->global->resources->default_setup->connection->username;
$pass = (string)$xml->global->resources->default_setup->connection->password;
$dbname = (string)$xml->global->resources->default_setup->connection->dbname;
$action = '';
if(!isset($_GET['action'])) {
throw new Exception('An action must be set.');
}
$action = $_GET['action'];
if($action == 'dump') {
$ignoreString = '';
foreach($tablesToBeIgnored as $table) {
$ignoreString .= " --ignore-table " . $dbname .".".$table;
}
$dumpCmd = "mysqldump -u" . $user . " -p" . $pass . " " . $dbname . " " . $ignoreString . "--no-create-db
--single-transaction > " . $dumpsql;
echo $dumpCmd;
} else if ($action == 'restore') {
$oldDomain = $_GET['olddomain'];
$newDomain = $_GET['newdomain'];
if(!isset($oldDomain) || !isset($newDomain)) {
throw new Exception('Old/New domain values must be set for restoration.');
}
$changeDomainsCmd = "sed -i 's/" . $oldDomain . "/" . $newDomain . "/g'" . $dumpsql;
echo $changeDomainsCmd;
$restoreCmd = "mysql -u" . $user . " -p" . $pass . " " . $dbname . " < " . $dumpsql;
echo $restoreCmd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment