<?php /*************************************************************************** **************** XVweb Project ************************* **************** Name : SQL Dump/Export ************************* **************** License : GNU ************************* **************** Authors : Krzysztof Bednrczyk ************************* *************************XVweb Team***************************************** ***************************************************************************/ DEFINE('BdServer', 'LOCALHOST', true); // MySQL server / Serwer MySQL DEFINE('BdServer_User', 'USER', true); // MySQL user / Użytkownik MySQL DEFINE('BdServer_Password', 'PASSWORD', true); // MySQL Password / Hasło MySQL DEFINE('BdServer_Base', 'DATABASE', true); // MySQL DataBase / Baza danych DEFINE('Dump_File', 'dump.sql', true); // Name generated file / Nazwa wygenerowanego pliku $SQLDump = '/*'.chr(13); // Header for file / Nagłówek dla pliku $SQLDump .= '# SQL DUMP: '.(BdServer_Base).chr(13); // Name database / Nazwa bazy danych $SQLDump .= '# GENERATED: '.date("d.m.Y H:i:s").chr(13); //Date generated / Data wygenerowania $SQLDump .= '*/'.chr(13); // END Header try { //Try / próbój $dbh = new PDO('mysql:host='.BdServer.';dbname='.BdServer_Base, BdServer_User, BdServer_Password); // Connecting with database /Łączenie się z bazą danych foreach ($dbh->query('SHOW TABLES;') as $Table) { // Get tables / Pobieranie tabel $TableStructure = $dbh->query('SHOW CREATE TABLE `'.$Table[0].'`')->fetch(); // Get table structure / Pobieranie struktury tabeli $SQLDump .= '/*TABLE STRUCTURE FOR `'.$Table[0].' */ '.chr(13).chr(13); //Header for table structure / Nagłówek dla struktury tabeli $SQLDump .= $TableStructure[1].';'.chr(13); //Adding table structure to variable / Dodawanie struktury tabeli do zmiennej $SQLDump .= chr(13).'/*SQL TABLE RECORDS FOR `'.$Table[0].' */ '.chr(13).chr(13); // Header for table records / Nagłówek dla rekordów tabeli foreach($dbh->query('SELECT * FROM `'.$Table[0].'`;')->fetchAll(PDO::FETCH_ASSOC) as $SelectRow) // Foreach loop with records / Pętla z reordami tabeli $SQLDump .= sprintf('INSERT INTO `'.$Table[0].'` (%s) VALUES (%s);', implode(', ', array_map(function($n){ return '`'.$n.'`';} ,array_keys($SelectRow))), implode(', ',array_map(array($dbh, 'quote'), $SelectRow))).chr(13); //Generate sql query with records / Generowanie zapytań SQL z rekordami $SQLDump .= chr(13).chr(13); //Two empty lines for aesthetics / Dwie puste linie dla estetyki } } catch (PDOException $e) { //Catching errors / Łapanie błędów exit(sprintf("SQL error: %s", ($e->getMessage()))); // If error, then print message and quit / Jeśli błąd, wyświetl komunikat i zakończ pracę } file_put_contents(Dump_File, $SQLDump); //Save to file / Zapisz do pliku echo "Done!"; //Done / Zakończenie ?>

