Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Humpty-dump - dump WP or Magneto 1.x database from PHP
<?php
// dump database - either using WordPress config from same directory or locally configured parameters
// ... or Magento's local.xml
// v 1.3 (2015-03-18) Peeter Marvet, http://tehnokratt.net
// v 1.4 (2017-11-14) added autorenamer
if ( basename( __FILE__, '.php' ) === 'humpty-dump' ) {
$new_name = 'humpty-dump_' . substr( md5( rand() ), 0, 8 ) . '.php';
rename( basename( __FILE__ ), $new_name );
header( "HTTP/1.0 404 Not Found" );
echo "<pre>I should not be available on predictable address - so I renamed myself, new name is: <a href='$new_name'>$new_name</a> :-)</pre>";
die();
}
if ( is_file( dirname( __FILE__ ) . '/wp-config.php' ) ) {
include( dirname( __FILE__ ) . '/wp-config.php' );
} elseif ( file_exists( dirname( __FILE__ ) . '/app/etc/local.xml' ) ) {
// Load in the local.xml and retrieve the database settings
$xml = simplexml_load_file( dirname( __FILE__ ) . '/app/etc/local.xml' );
if ( isset( $xml->global->resources->default_setup->connection ) ) {
$connection = $xml->global->resources->default_setup->connection;
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', (string) $connection->dbname );
/** MySQL database username */
define( 'DB_USER', (string) $connection->username );
/** MySQL database password */
define( 'DB_PASSWORD', (string) $connection->password );
/** MySQL hostname */
define( 'DB_HOST', (string) $connection->host );
}
} else {
define( 'DB_NAME', 'name' );
define( 'DB_USER', 'user' );
define( 'DB_PASSWORD', 'pass' );
define( 'DB_HOST', 'localhost' );
}
// define('DB_CHARSET', 'utf8'); // NB! use latin1 on legacy systems that tend to produce unreadable dumps from phpmyadmin!
$backupFile = DB_NAME . "_" . date( "Y-m-d-H-i-s" ) . ".sql";
$command = "mysqldump --opt ";
if ( defined( 'DB_CHARSET' ) ) {
$command .= "--default-character-set=" . DB_CHARSET . " ";
$backupFile .= "_" . DB_CHARSET;
}
$backupFile .= '.gz';
$command .= "--host=" . DB_HOST . " --user=" . DB_USER . " --password=" . DB_PASSWORD . " " . DB_NAME . " | gzip > $backupFile";
echo "Dumping <strong>" . DB_NAME . "</strong> on <strong>" . DB_HOST . "</strong>... ";
echo system( $command );
echo 'Done! Grab it before it rots: <a href="http://' . $_SERVER['SERVER_NAME'] . '/' . $backupFile . '">' . $backupFile . '</a>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.