databases/scripts/dbSchema.php

60 lines
1.7 KiB
PHP
Raw Normal View History

2017-03-29 14:14:18 +02:00
<?php
$shortopts = '';
$longopts = array(
'host:',
'user:',
'pass:',
'path:',
'database::',
);
$options = getopt($shortopts, $longopts);
if ($options === false || count($options) == 0) {
echo "Oups ! options manquantes\n";
exit;
}
$link = new mysqli($options['host'], $options['user'], $options['pass']);
if ($link->connect_errno) {
echo "Echec lors de la connexion à MySQL : (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$databases = $tables = array();
// --- Uniquement database selectionné
if (array_key_exists('database', $options) && !empty($options['database']) ) {
$databases[] = $options['database'];
}
// --- Liste des bases de données
else {
$result = $link->query('SHOW DATABASES');
while ($row = $result->fetch_assoc()) {
$databases[] = $row['Database'];
}
}
$dbExcluded = array(
'mysql',
'information_schema',
);
// --- Sauvegarde des bases de données pour chaque table
foreach ($databases as $database) {
if ( !in_array($database, $dbExcluded) ) {
$link->query('USE '.$database);
$result = $link->query('SHOW TABLES');
while ($row = $result->fetch_array()) {
$table = $row[0];
echo date('Y/m/d - H:i:s') ." - Schema de '$database.$table'";
// --- Dump schema
exec(
'mysqldump -h' . $options['host'] .' -u' . $options['user'] .' -p' . $options['pass'] .
' --compact --no-data ' . $database . ' --tables ' . $table .
' | egrep -v "(^SET|^/\*\!)" | sed \'s/ AUTO_INCREMENT=[0-9]*\b//\' > ' .
$options['path'] . '/'.$database.'.'.$table.'.sql'
);
echo "\n";
}
}
}
// --- Fermeture du lien mysql
mysqli_close($link);