Skip to content

Instantly share code, notes, and snippets.

@greg-1-anderson
Created July 22, 2015 23:29
Show Gist options
  • Save greg-1-anderson/811e56657f2cb111d109 to your computer and use it in GitHub Desktop.
Save greg-1-anderson/811e56657f2cb111d109 to your computer and use it in GitHub Desktop.
Proof-of-concept script to access Pantheon Database, e.g. for recovery. We need more info from Terminus before this is generally useful
<?php
$self=array_shift($argv);
$prefix="wp_";
while (count($argv) > 0) {
$arg = array_shift($argv);
if ($arg[0] == '-') {
switch ($arg) {
case "--port":
$db_port = array_shift($argv);
break;
case "--pw":
$db_password = array_shift($argv);
break;
case "--prefix":
$prefix = array_shift($argv);
break;
}
}
else {
$site = $arg;
}
}
if (empty($site) || empty($db_port) || empty($db_password)) {
print "Usage: $self --port 12345 --pw 'database password' SITE";
exit(1);
}
# Get the site UUID
$uuid = exec("terminus site info --site=$site --field=id");
# TODO: Are the database user and database name invariant on Pantheon?
$db_user="pantheon";
$db_name="pantheon";
$db_host="dbserver.dev.$uuid.drush.in";
# TODO: How do we get the database password?
# $db_password="6056133e857b46f58eb99fa32f439be8";
# TODO: Maybe we could use an sftp client to write the wp-native-php-sessions
# code to the site's repository, and commit it with terminus.
#
# GitHub:
# https://github.com/pantheon-systems/wp-native-php-sessions
#
# Release archive:
# https://github.com/pantheon-systems/wp-native-php-sessions/archive/0.1.zip
$sftp_port="2222";
$sftp_host="dev.$uuid@appserver.dev.$uuid.drush.in";
# sftp -o Port="$SFTP_PORT" "$SFTP_HOST"
// Make sure the site is awake
exec("terminus site wake --site=$site --env=dev");
// print("mysql --silent -u \"$db_user\" -p\"$db_password\" -h \"$db_host\" -P \"$db_port\" \"$db_name\" -e \"show tables;\"");
print("mysql --silent -u \"$db_user\" -p\"$db_password\" -h \"$db_host\" -P \"$db_port\" \"$db_name\" -e \"SELECT option_value FROM ${prefix}options WHERE option_name = 'active_plugins';\"");
$serialized_active_plugins = exec("mysql --silent -u \"$db_user\" -p\"$db_password\" -h \"$db_host\" -P \"$db_port\" \"$db_name\" -e \"SELECT option_value FROM ${prefix}options WHERE option_name = 'active_plugins';\"");
print "$serialized_active_plugins\n";
$active_plugins = unserialize($serialized_active_plugins);
print var_export($active_plugins);
print "\n";
// Next: enable wp-native-php-sessions
//array (
// 0 => 'wp-native-php-sessions/pantheon-sessions.php',
//)
if (!in_array('wp-native-php-sessions/pantheon-sessions.php', $active_plugins)) {
print "\nAdding wp-native-php-sessions plugin\n";
$active_plugins[] = 'wp-native-php-sessions/pantheon-sessions.php';
$serialized_active_plugins = serialize($active_plugins);
// Using the deprecated function; we would need to connect to the SQL database
// to use the recommended functions (e.g. mysqli_real_escape_string()).
$escaped_serialized_active_plugins = mysql_real_escape_string($serialized_active_plugins);
print("mysql --silent -u \"$db_user\" -p\"$db_password\" -h \"$db_host\" -P \"$db_port\" \"$db_name\" -e \"UPDATE ${prefix}options SET option_value='$escaped_serialized_active_plugins' WHERE option_name='active_plugins';\"");
exec("mysql --silent -u \"$db_user\" -p\"$db_password\" -h \"$db_host\" -P \"$db_port\" \"$db_name\" -e \"UPDATE ${prefix}options SET option_value='$escaped_serialized_active_plugins' WHERE option_name='active_plugins';\"");
}
else {
print "\nwp-native-php-sessions already enabled\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment