Backup mysql

happyhippo

Nuovo Utente
31 Ott 2016
7
0
1
48
Ciao,
sto sviluppando un sito per un'autorimessa ad un amico che mi ha chiesto di inserire un bottone che gli crei un backup del database da scaricare sul suo pc ogni tanto. Forse manca qualche virgola :)
Comunque il sito si trova su aruba e con phpmyadmin è abbastanza semplice ma lui vorrebbe automatizzare il backup con un semplice tasto direttamente online... anche perchè se entra su phpmyadmin credo che riuscirebbe a distruggere tutto :((
Avete qualche suggerimento?
Grazie
 

happyhippo

Nuovo Utente
31 Ott 2016
7
0
1
48
si scusate, avete ragione... incollo il codice php

PHP:
    //ENTER THE RELEVANT INFO BELOW
    $mysqlUserName      = "xxxxxx";
    $mysqlPassword      = "xxxxxx";
    $mysqlHostName      = "xxxxxx";
    $DbName             = "xxxxx";
    $backup_name        = "xxxxxxx";
    $tables             = "xxxxxx";


    Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );

    function Export_Database($host,$user,$pass,$name,  $tables=false, $backup_name=false )
    {
        $mysqli = new mysqli($host,$user,$pass,$name);
        $mysqli->select_db($name);
        $mysqli->query("SET NAMES 'utf8'");

        $queryTables    = $mysqli->query('SHOW TABLES');
        while($row = $queryTables->fetch_row())
        {
            $target_tables[] = $row[0];
        }  
        if($tables !== false)
        {
            $target_tables = array_intersect( $target_tables, $tables);
        }
        foreach($target_tables as $table)
        {
            $result         =   $mysqli->query('SELECT * FROM '.$table); 
            $fields_amount  =   $result->field_count; 
            $rows_num=$mysqli->affected_rows;    
            $res            =   $mysqli->query('SHOW CREATE TABLE '.$table);
            $TableMLine     =   $res->fetch_row();
            $content        = (!isset($content) ?  '' : $content) . "\n\n".$TableMLine[1].";\n\n";

            for ($i = 0, $st_counter = 0; $i < $fields_amount;   $i++, $st_counter=0)
            {
                while($row = $result->fetch_row()) 
                {
                    if ($st_counter%100 == 0 || $st_counter == 0 ) 
                    {
                            $content .= "\nINSERT INTO ".$table." VALUES";
                    }
                    $content .= "\n(";
                    for($j=0; $j<$fields_amount; $j++) 
                    {
                        $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) );
                        if (isset($row[$j]))
                        {
                            $content .= '"'.$row[$j].'"' ;
                        }
                        else
                        {  
                            $content .= '""';
                        }    
                        if ($j<($fields_amount-1))
                        {
                                $content.= ',';
                        }     
                    }
                    $content .=")";
                 
                    if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num)
                    {  
                        $content .= ";";
                    }
                    else
                    {
                        $content .= ",";
                    }
                    $st_counter=$st_counter+1;
                }
            } $content .="\n\n\n";
        }
      
        $backup_name = $backup_name ? $backup_name : $name."_".date('d-m-Y').".sql";
        header('Content-Type: application/octet-stream');  
        header("Content-Transfer-Encoding: Binary");
        header("Content-disposition: attachment; filename=\"".$backup_name."\""); 
        echo $content; exit;
    }[PHP]
 
Ultima modifica di un moderatore:
Discussioni simili
Autore Titolo Forum Risposte Data
F Backup incrementale di file e mysql su mio pc Server Dedicati e VPS 2
xone Come effettuare backup del database mysql senza exec PHP 0
neo996sps Script per backup DB MySQL automatizzato che non va. PHP 8
P Backup MySQL con PhpMyAdmin MySQL 0
L Backup db mysql MySQL 0
Valerio93 Importare database senza backup MySQL 0
G [PHP] backup manuale(o automatico) tabelle msql PHP 6
S Restore Full Backup Sito WordPress in Cpanel Web Server 1
Laskot Errore backup db Database 0
felino Backup: sincronizzare cartelle e sottocartelle Windows e Software 22
Aruba Cloud Proteggi i tuoi file con Aruba Cloud BACKUP! Cloud Computing e Cloud Server 5
andreas88 Problema HD - avvia processo di Backup Hardware 4
felino Client MAIL: backup email esistenti Mac e Software 1
M Cartella predefinita dove salvare i backup PHP 0
glm2006ITALY Wordpress: ripristinare un backup WordPress 0
A Non riesco a fare backup sito perchè Filezilla non scarica tutti i file! Web Server 4
E come si esegue un backup dati per passaggio a nuovo hosting? Hosting 5
F backup database WordPress 7
glm2006ITALY Wordpress: guida completa al backup integrale WordPress 1
M Spostare sito in locale con akeeba backup Joomla 1
F Programma PHP che esegue backup e ripristino di database SQL SERVER PHP 12
L Backup virtual machine VMWare ESXi Windows e Software 0
P Backup automatico Hosting 2
G script per fare un backup da server a server PHP 2
M backup delle email sul server di posta Windows e Software 1
M Eliminare backup automatico e unire partizioni Hardware 0
L Backup automatico PHP 4
S backup dal sito PHP 4
catellostefano File di backup Database 0
G Come mettere file backup sul web? Alternative? Hosting 0
E Importazione file sql (restore/backup) MySQL 0
Cammarotas Backup in CCleaner Windows e Software 0
N SQL Server 2000: cancellare un set di backup da Ripristina Database Database 0
peppoweb Windows Server 2003: attenzione al backup Windows e Software 0
K form Inserimento record mysql PHP 2
P Mysql lento a cancellare MySQL 1
P Codifica caratteri speciali mysql php PHP 0
N MAX() + ADD_DATE - per update su Mysql MySQL 0
F Applicazione PHP/MySQL per prenotazioni: limitare il numero massimo di posti prenotabili PHP 20
L tipo boolean non funzionante su mariadb (mysql). E codice php 7.4. PHP 0
M PHP/MySQL - Estrarre valori min e max di ogni gruppo PHP 5
W MySQL ciclo in SELECT MySQL 0
L Mysql gestionale multipiattaforma MySQL 0
W MySQL SELECT list dinamica MySQL 0
M utilizzo mysql in nodejs - crea createdAt e updateAt MySQL 1
T colonne di tabelle mysql ordinate MySQL 0
M Sintassi "personalizzata" per mysql workbench? MySQL 0
A Mysql MySQL 0
F Ricreare struttura php+mysql su Xampp Apache 0
M Array associativi php su 2 campi mysql PHP 10

Discussioni simili