Skip to content

Instantly share code, notes, and snippets.

Created April 5, 2012 03:36
Show Gist options
  • Save amacneil/2307758 to your computer and use it in GitHub Desktop.
Save amacneil/2307758 to your computer and use it in GitHub Desktop.
Extract a single ExpressionEngine site from a MSM install
* Instructions:
* First add your database connection details below.
* Then either upload this script to your server and visit the page,
* or run it using the command line: php no_more_msm.php
// database connection details
$server = '';
$database = 'your_database';
$username = 'your_username';
$password = 'your_password';
$table_prefix = 'exp_';
$site_id = '2'; // the MSM site id you want to keep!
// no need to edit below this line...
function show_error()
$error = mysql_error();
echo "Error: $error\n";
$db = mysql_connect($server, $username, $password) or show_error();
mysql_select_db($database) or show_error();
// check target site exists
$site_id = (int)$site_id;
$query = mysql_query("SELECT * FROM `{$table_prefix}sites` WHERE `site_id` = $site_id");
$row = mysql_fetch_assoc($query);
if ($row) {
echo "Found site: {$row['site_label']}\n\n";
} else {
echo "Can't find site ID $site_id!\n";
// update tables
$tables = array();
$query = mysql_query("SHOW TABLES LIKE '$table_prefix%'") or show_error();
while ($row = mysql_fetch_row($query)) {
$tables[] = $row[0];
foreach ($tables as $table) {
echo "Table $table:\n";
$query = mysql_query("SHOW COLUMNS FROM `$table` LIKE 'site_id'") or show_error();
$site_row = mysql_fetch_assoc($query);
if ($site_row) {
// delete all rows with unwanted site ids
mysql_query("DELETE FROM `$table` WHERE `site_id` != $site_id") or show_error();
echo " Deleted ".mysql_affected_rows()." unwanted rows.\n";
// migrate all remaining rows to site id 1
mysql_query("UPDATE `$table` SET `site_id` = 1") or show_error();
echo " Migrated ".mysql_affected_rows()." rows.\n";
// update target site name to "default_site"
mysql_query("UPDATE `{$table_prefix}sites` SET `site_name` = 'default_site'") or show_error();
echo <<<EOF
!!! Remember to remove \$config['multiple_sites_enabled'] = 'y'; from config.php!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment