Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* ----------------- BACKUP DEL DATABASE MYSQLi MACOS86.IT ------------------------------------------ */
- /* ------------------------------ By CIRO82 --------------------------------------------------------- */
- /* Report degli errori */
- error_reporting(E_ALL);
- /* Crea nuova istanza per la classe DatabaseBackup */
- $dbBackup = new DatabaseBackup();
- /* @Funzione chiamata - backupDatabase()
- * @parametri - 1st Param - Default : recupera tutte le tabelle
- Tabelle specifiche - array(table1,table2,table3...);
- * 2nd Param - (Cartella) Se non specificata ne crea una dove lo script risiede
- */
- $tables = '*';
- $dbBackup->backupDatabase($tables,'BackupLogs');
- /* -----------------------------------------LA SEGUENTE PARTE NON VA COMPILATA -------------------------------------------------> */
- class DatabaseBackup{
- private $hostname = ''; /* DB Hostname */
- private $username = ''; /* DB Username */
- private $password = ''; /* DB Password */
- private $database = ''; /* Database Name */
- private $characterSet = 'utf8'; /* Set di caratteri del DB */
- private $backupDirectory = 'BackupLogs'; /* Cartella backup */
- /* Connessione a Mysqli */
- private $link = '';
- /*----------------------------------------- LA SEGUENTE PARTE VA COMPILATA -----------------------------------------------------> */
- /* Classe del Constructor */
- function __construct(){
- /* Initializzazione delle variabili del DB */
- $this->hostname = 'Nome del tuo host DB';
- $this->username = 'nome utente DB';
- $this->password = 'password del tuo DB';
- $this->database = 'nome del tuo DB';
- /* Funzione di chiamata all'inizializzazione del DB */
- $this->initalizeDB();
- /* <----------------------------------------------------------------------------------------------------------------------------- */
- }
- /* Funzione per il collegamento a MySQL DB */
- private function initalizeDB(){
- $this->link = mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
- /* Messaggio se ci sono errori*/
- if(mysqli_connect_error()){
- die('Connection Error - '.mysqli_connect_errno().' : '.mysqli_connect_error());
- }
- /* Se non è definito un set caratteri imposta quello di default */
- if(!mysqli_character_set_name($this->link)){
- mysqli_set_charset($this->link,$this->characterSet);
- }
- }
- /* Funzione di backup del Database */
- public function backupDatabase($tables = '*',$backupDirectory = ''){
- /* Se sono necessarie tutte le tabelle: */
- if($tables == '*'){
- $tables = array();
- /* Recupera tutte le tabelle del database corrente */
- $result = mysqli_query($this->link,"SHOW TABLES");
- /* Passa attraverso tutte le righe e assegna l'array $tables */
- while($row = mysqli_fetch_row($result)){
- $tables[] = $row[0];
- }
- }else{
- /* Se $tables è un array, assegnare direttamente altrimenti esplodere la stringa */
- $tables = is_array($tables) ? $tables : explode(',',$tables);
- }
- /* Crea il database */
- $sql = 'SET FOREIGN_KEY_CHECKS = 0;'."\n".'CREATE DATABASE IF NOT EXISTS `'.$this->database."`;\n";
- /* Usa il database */
- $sql .= 'USE `'.$this->database.'`;';
- /* Passa attraverso tutti le $tables */
- foreach($tables as $table){
- /* Messaggio di output */
- echo 'Salvataggio : `'.$table.'` : ';
- /* Recupera i dettagli della tabella */
- $tableDetails = mysqli_query($this->link, "SELECT * FROM ".$table);
- /* Controlla il numero di colonne nella tabella */
- $totalCols = mysqli_num_fields($tableDetails);
- /* Se la tabella esiste, droppala */
- $sql .= "\n\nDROP TABLE IF EXISTS `".$table."`;\n";
- /* Crea la struttura dela tabella */
- $result1 = mysqli_fetch_row(mysqli_query($this->link,'SHOW CREATE TABLE '.$table));
- $sql .= $result1[1].";\n\n";
- while($row = mysqli_fetch_row($tableDetails)){
- $sql .= 'INSERT INTO `'.$table.'` VALUES(';
- for($j=0; $j<$totalCols; $j++){
- $row[$j] = preg_replace("/\n/","\\n",addslashes($row[$j]));
- if (isset($row[$j]))
- {
- $sql .= '"'.$row[$j].'"' ;
- }
- else
- {
- $sql.= '""';
- }
- if ($j < ($totalCols-1))
- {
- $sql .= ', ';
- }
- }
- $sql .= "); \n";
- }
- echo 'Completato <br/>';
- }
- $sql .= 'SET FOREIGN_KEY_CHECKS = 1;';
- /* Se il secondo parametro non è stato specificato, verrà usato quello di default */
- $backupDirectory = ($backupDirectory == '') ? $this->backupDirectory : $backupDirectory;
- if($this->logDatabase($sql,$backupDirectory)){
- echo '<h4>Database <span style="color:#7D0097">`'.$this->database.'`</span>esportato correttamente nella cartella - <span style="color:#1CAD7A"> `'.$backupDirectory.'`</span><h4>';exit;
- }else{
- echo '<h2>Errore esportazione database '.$this->database.'<h2>';exit;
- }
- }
- /* Funzione di salvataggio del database */
- private function logDatabase($sql,$backupDirectory = ''){
- if(!$sql){
- return false;
- }
- if(!file_exists($backupDirectory)){
- if(mkdir($backupDirectory)){
- $filename = 'log_'.$this->database.date('Y-m-d_H-i-s');
- $fileHandler = fopen($backupDirectory.'/'.$filename.'.sql','w+');
- fwrite($fileHandler,$sql);
- fclose($fileHandler);
- return true;
- }
- }else{
- $filename = 'log_'.$this->database.date('Y-m-d_H-i-s');
- $fileHandler = fopen($backupDirectory.'/'.$filename.'.sql','w+');
- fwrite($fileHandler,$sql);
- fclose($fileHandler);
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement