Project

General

Profile

Feature #25739 » dump.php

Xavier Perseguers, 2011-04-07 22:29

 
<?php
define('PATH_typo3conf', realpath(dirname(__FILE__) . '/../') . '/');
$configFile = PATH_typo3conf . 'export/config.yaml';
$output = array();

function makeDumpInstallToolCompatible($file) {
$newFileLines = array();

if (is_file($file)) {
$lines = file($file);
$inCreateTableDefinition = FALSE;

foreach ($lines as $index => $line) {
if (preg_match("/^\/\*![0-9]{1,} /", $line) ||
substr($line, 0, 4) === 'SET ' ||
substr($line, 0, 10) === 'DROP TABLE'
) {
continue;
}
if (substr($line, 0, 12) === 'CREATE TABLE') {
$inCreateTableDefinition = TRUE;

preg_match("/CREATE TABLE ([^\ ]*)/", $line, $tableMatch);
if (!empty($tableMatch[1])) {
$newFileLines[] = 'DROP TABLE ' . $tableMatch[1] . ";\n";
}
}
if (substr($line, 0, 2) === ');' && $inCreateTableDefinition === TRUE) {
$inCreateTableDefinition = FALSE;
}

if ($inCreateTableDefinition === TRUE) {
$line = str_replace('`', NULL, $line);
}

if (preg_replace("/(\n|\t|\r)/", NULL, $line) !== '') {
$newFileLines[] = $line;
}
}
}

$fhandle = fopen($file, 'w');
fwrite($fhandle, implode('', $newFileLines));
fclose($fhandle);
}

/**
* Check if there's an configuration file for the export
*/
if (!is_file($configFile)) {
die($configFile . ' not found');
}

/**
* Parse the configuration file into the output array
*
* Format of the file:
*
* sqlfile1: table1, table2
* sqlfile2: table1, table2
*/
$config = file(PATH_typo3conf . 'export/config.yaml');
$table = null;

foreach ($config as $line) {
if (trim($line) !== '' && substr(trim($line), 0, 1) !== '#') {
if (strpos($line, ':') == false) {
continue;
}

$configRule = preg_replace("/(\n|\r|\t| )/", '', $line);
$configRule = explode(':', $configRule);
$output[$configRule[0]] = str_replace(',', ' ', $configRule[1]);
}
}

/**
* Load database config
*/
require_once(PATH_typo3conf . 'localconf.php');

if (!isset($_SERVER['OTAP'])) {
$typo_db_host = str_replace('dev.mysql.database.local', '91.209.192.236', $typo_db_host);
}

/**
* Create and execute the actual commands
*/
foreach ($output as $filename => $tables) {

if (count($tables) > 0) {
$file = PATH_typo3conf . $filename;

$cmd = sprintf("mysqldump --compatible=mysql40 --complete-insert --skip-opt --skip-quote-names --skip-comments -u%s -p%s -h%s %s %s > %s",
$typo_db_username,
$typo_db_password,
$typo_db_host,
$typo_db,
$tables,
$file);

echo str_replace($typo_db_password, '*****', $cmd) . "\n";
shell_exec($cmd);

makeDumpInstallToolCompatible($file);
}
}

?>
(2-2/2)