php.de

Zurück   php.de > php.de Intern > Wiki Diskussionsforum > Tutorials

Tutorials Hier findest Du Tutorials, welche nach und nach ein fertiges Script ergeben. Sehen, lernen & verstehen!

Thema geschlossen
 
LinkBack (2) Themen-Optionen Thema bewerten
Alt 18.02.2005, 17:38  
Erfahrener Benutzer
 
Registriert seit: 14.01.2004
Beiträge: 2.543
fantast
fantast eine Nachricht über ICQ schicken
Standard MySQL: Backup

Das Tutorial wurde geschrieben von: fantast

So, aufgrund wiederholter Anfragen hier mein kleines Script zum Synchronisieren zweier Datenbanken. Wie ueblich ohne Gewaehr, das ist nicht sonderlich eingaengig getestet. Ich weiss auch nich, ob hinter dem ganzen ein schwerwiegender Designfehler lauert. Aber schauts Euch an:

sql_front.php
PHP-Code:
<?php
# which ftp host to connect to
$ftp_hostname "";
# which ftp username to use
$ftp_username "";
# password for the ftp user
$ftp_password "";
# name of the local dumpfile; not important
$ftp_localfile "";
# name of the remote dumpfile; must be identical to $filename in sql_back.php
$ftp_remotefile "";

# which http host to connect to
$http_hostname "";
# secret to be sent via GET; must be identical to $secret in sql_back.php
$http_secret "";

# which mysql host to connect to; must be running 3.23.20 or newer
$mysql_hostname "";
# which mysql username to use
$mysql_username "";
# password for the mysql user
$mysql_password "";
# which database to synchronize
$mysql_database "";

// echoes a proper html page with the error message before dying
function die_nice($string) {
  die(
'<html><head><title>Error</title></head><body>'.
    
'<p style="color: red; font-size: large; text-align: center;">'.
    
$string.
    
'</p></body></html>');
}

// returns the table structure and data as a string
function dump_table($table) {
  
mysql_query("LOCK TABLE $table WRITE") or die_nice("DUMP_TABLE: Could not lock table $table - ".mysql_error());

  
$temp mysql_fetch_assoc(mysql_query("SHOW CREATE TABLE $table")) or die_nice("DUMP_TABLE: SHOW CREATE TABLE $table failed - ".mysql_error());
  
$dump "DROP TABLE IF EXISTS $table;\n".str_replace("\n"""$temp['Create Table']).";\n\n";
  
  
$result mysql_query("SELECT * FROM $table") or die_nice("DUMP_TABLE: SELECT * FROM $table failed - ".mysql_error());

  while (
$row mysql_fetch_assoc($result)) {
    
$fields "(";
    
$values "(";
    
    foreach (
$row as $key => $value) {
      
$fields .= mysql_escape_string($key).", ";
      
$values .= "'".mysql_escape_string($value)."', "// what about NULL ?
    
}
    
    
$fields substr($fields0, -2).")";
    
$values substr($values0, -2).")";

    
$dump .= "INSERT INTO $table $fields VALUES $values;\n";
  }
  
  
mysql_query("UNLOCK TABLES") or die_nice("DUMP_TABLE: Could not unlock table $table - ".mysql_error());
  return 
$dump."\n";
}

set_time_limit(300);

// generate mysqldump
$dumpfile fopen($ftp_localfile'w') or die_nice("DUMP: Could not open $ftp_localfile");

$mysql mysql_connect($mysql_hostname$mysql_username$mysql_password) or die_nice("DUMP: Could not connect to $mysql_hostname - ".mysql_error());
mysql_select_db($mysql_database) or die_nice("DUMP: Could not select database $mysql_database - ".mysql_error());

$result_a mysql_query("SHOW TABLES") or die_nice("DUMP: Could not list tables from $mysql_database - ".mysql_error());
while (
$row_a mysql_fetch_row($result_a)) {
  
fwrite($dumpfiledump_table($row_a[0])) or die_nice("DUMP: Could not write to file $ftp_localfile");
}
fclose($dumpfile) or die_nice("DUMP: Could not close $ftp_localfile");

// connect to server via ftp, upload the file
$ftp ftp_connect($ftp_hostname) or die_nice("FTP: Could not connect to $ftp_hostname");
ftp_login($ftp$ftp_username$ftp_password) or die_nice("FTP: Could not login as $ftp_username");
ftp_put($ftp$ftp_localfile$ftp_remotefileFTP_ASCII) or die_nice("FTP: Could not put $ftp_localfile");
ftp_chmod($ftp0666$ftp_remotefile) or die_nice("FTP: Could not chmod $ftp_remote_file"); // ftp_chmod() doesnt exist in php4
ftp_site($ftp"CHMOD 0666 ".$ftp_remotefile) or die_nice("FTP: Could not chmod $ftp_remotefile");
ftp_close($ftp) or die_nice("FTP: Could not disconnect from $ftp_hostname");

// remove the local file
unlink($ftp_localfile) or die_nice("FTP: Could not delete $ftp_localfile");

// trigger the script on the server
$cmd "GET /sql_back.php?secret=$http_secret HTTP/1.1\r\nHost: $http_hostname\r\nConnection: Close\r\n\r\n";
$data "";

$sock fsockopen($http_hostname80) or die_nice("HTTP: Could not connect to $http_hostname");
fwrite($sock$cmd) or die_nice("HTTP: Could not write to server");

while (!
feof($sock)) {
  
$data .= fread($sock1024);
}

fclose($sock) or die_nice("HTTP: Could not disconnect from $http_hostname");

# if there is no direct connection to the internet, use wget
# $data = shell_exec("wget $http_hostname/sql_back.php?secret=$http_secret -q -s -O -");

$data explode("\n"$data);
$data trim($data[count($data) - 1]);

if (
strlen($data) < 1die_nice("HTTP: Request returned no data");
if (
strcmp(substr($data02), 'OK') != 0die_nice("HTTP: Server said: $data");
?>

<html><head><title>Success</title></head><body>
<p style="color: green; font-size: large; text-align: center;">
Everything went fine, you may close this window !
</p></body></html>
sql_back.php
PHP-Code:
<?php
# which mysql host to connect to
$mysql_hostname "";
# which mysql username to use
$mysql_username "";
# password for the mysql user
$mysql_password "";
# which database to synchronize; the database must already exist
$mysql_database "";

# secret that is expected to be sent via GET; must be identical to $http_secret in sql_front.php
$secret "";
# name of the dumpfile; must be identical to $ftp_remotefile in sql_front.php
$filename "";

set_time_limit(300);

// look for file
if (!isset($_GET['secret']) || strcmp($_GET['secret'], $secret) != 0) die("Secret missing or wrong");
if (!
is_readable($filename)) die("$filename not found");

// read file into database
$mysql mysql_connect($mysql_hostname$mysql_username$mysql_password) or die("Could not connect to $mysql_hostname - ".mysql_error());
mysql_select_db($mysql_database) or die("Could not select database $mysql_database - ".mysql_error());

$dump file($filename) or die("Could not read file $filename");
foreach (
$dump as $data) {
  if (
strlen($data) > 1mysql_query(substr($data0, -1)) or die("Could not exec $data - ".mysql_error());
}

// delete file
unlink($filename) or die("Could not delete $filename");

die(
"OK");
?>
Benutzung is klar hoffe ich. Bei Fragen halt fragen. Wie schon erwaehnt, keine Gewaehr, Support, soweit ich kann.

Hier noch einige Links, ich bin ja nich der erste, der sowas schreibt:
http://www.dbcf.de/mysql-backup-tool/
http://www.0php.com/MySQL-Backup.php
http://www.hotscripts.com/PHP/Script...ols/index.html

Viel Spaß
fantast

Verwendete Funktionen:
die()
mysql_query()
mysql_fetch_assoc()
str_replace()
mysql_escape_string()
substr()
set_time_limit()
fopen()
mysql_connect()
mysql_select_db()
mysql_fetch_row()
fwrite()
fclose()
ftp_connect()
ftp_login()
ftp_put()
ftp_chmod()
unlink()
fsockopen()
explode()
trim()
strlen()
strcmp()
isset()
is_readable()
file()
__________________
Was ist validität?
fantast ist offline  
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 18.02.2005, 18:34  
Tobias
Gast
 
Beiträge: n/a
Standard

Der Beitrag wurde verschoben, wegen...
... Tutorial.

Bemerkung:
Bei weiteren Fragen bitte an mich wenden.

moved to Tutorials

ich hab den beitrag mal um eine funktionsliste erweitert..
 
Alt 26.04.2005, 21:00  
Gast
 
Beiträge: n/a
Standard

guter beitrag hilft mir auch thx!
 
Alt 20.05.2008, 08:50  
Erfahrener Benutzer
 
Registriert seit: 03.11.2004
Beiträge: 289
rbs_phoenix
rbs_phoenix eine Nachricht über ICQ schicken
Standard

Hier ist noch eine Möglichkeit, ein Backup durchzuführen:

PHP-Code:
$date date("d.m.Y");
$dateiname $date."_backup";
$db_user "xxx";
$db_pw "xxx";
$db_tab "xxx";               #Tabellen-Name

exec('mysqldump --password='.$db_pw.' --user='.$db_user.' --skip-opt '.$db_tab.' > backup/'.$dateiname.'.sql'); 
Bei manchen Providern sind die exec(); ähnlichen Funktionen deaktiviert, deshalb muss man vorher mal gucken, ob es geht bzw. ob man die Rechte dafür hat.

Für nähere Funktionsbenutzung schau HIER nach.
__________________
Homepage: http://www.rbs-page.de
rbs_phoenix ist offline  
Alt 20.07.2008, 00:59  
Erfahrener Benutzer
 
Registriert seit: 13.05.2006
Beiträge: 434
Curanai ist zur Zeit noch ein unbeschriebenes Blatt
Standard

Ergänzend zu rbs_phoenix:

Code:
<?php 
$dateiname = $date."_backup.sql";
$db_user = "xxx";
$db_pw = "xxx";
$db_tab = "xxx";
$path = "./[da_liegt_mein_backup]/";
exec('mysqwl --password='.$db_pw.' --user='.$db_user.' '.$db_tab.' < $path.$dateiname.');
?>
So drückst Du es wieder rein!
__________________
Manche Menschen sind wie Schnitzel - nicht zäh, aber beidseitig bekloppt!
Curanai ist offline  
Alt 20.07.2008, 01:18  
Erfahrener Benutzer
 
Registriert seit: 13.05.2006
Beiträge: 434
Curanai ist zur Zeit noch ein unbeschriebenes Blatt
Standard

Code:
exec('mysql --password='.$db_pw.' --user='.$db_user.' '.$db_tab.' < $path.$dateiname.');
Sorry, ohne das "w" natürlich.
__________________
Manche Menschen sind wie Schnitzel - nicht zäh, aber beidseitig bekloppt!
Curanai ist offline  
Alt 18.05.2011, 11:27  
Neuer Benutzer
 
Registriert seit: 18.05.2011
Beiträge: 8
PHP-Kenntnisse:
Anfänger
erdbeereis6 ist zur Zeit noch ein unbeschriebenes Blatt
Standard

Ich benutze HeidiSQL, damit kopiere ich die Datenbank meist hin und her. Ist es Sinnvoll die Datenbank mit einem extra script zu synchronisieren? Mein Problem ist, das bei HeidSQL da so lange dauert.

Geändert von Asipak (24.05.2011 um 17:17 Uhr). Grund: Signatur entfernt (unerwünschte Werbung)
erdbeereis6 ist offline  
Alt 18.05.2011, 11:38  
¯\_(ツ)_/¯
 
Benutzerbild von Flor1an
 
Registriert seit: 18.06.2008
Beiträge: 8.814
PHP-Kenntnisse:
Fortgeschritten
Flor1an ist ein wunderbarer AnblickFlor1an ist ein wunderbarer AnblickFlor1an ist ein wunderbarer AnblickFlor1an ist ein wunderbarer AnblickFlor1an ist ein wunderbarer AnblickFlor1an ist ein wunderbarer AnblickFlor1an ist ein wunderbarer Anblick
Standard

Das ist jetzt heute schon der dritte Beitrag der älter als 3 Jahre ist und wiederbelebt wurde. Bitte lasst doch solch alte Threads einfach liegen.
__________________
▇█▓▒░◕‿‿◕░▒▓█▇
Flor1an ist offline  
Thema geschlossen


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

LinkBacks (?)
LinkBack to this Thread: http://www.php.de/tutorials/18160-mysql-backup.html
Erstellt von For Type Datum
Full-Backup per Cronjob? This thread Refback 19.01.2012 09:31
Metacrawler - Die Metasuchmaschine. This thread Refback 31.05.2009 17:10

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
[Erledigt] MySQL backup automatisiert über cronjob Datenbanken 7 21.09.2010 19:00
Mysql Backup im richtigem Format rbs_phoenix Datenbanken 10 13.05.2008 22:22
Backup einer MySql Datenbank dh1sbg Beitragsarchiv 1 13.04.2008 20:45
[Erledigt] MySQL - ERROR 1044 bei erstellen einer Datenbank _youngenterpriser_ Datenbanken 2 05.02.2008 17:56
Mysql Server Einstellunen Optimieren pchero Datenbanken 3 01.05.2007 19:50
Export aus MySQL will nicht... madSoul PHP Tipps 2006 0 01.02.2006 13:01
Mysql 4.1.x unter php 4.3.9 Datenbanken 3 15.11.2005 13:49
Mysql Backup Datenbanken 2 05.11.2005 21:32
[Erledigt] Schnittstelle zwischen PHP und MySQL klappt net !!! Datenbanken 16 16.10.2005 14:24
[Erledigt] not allowed to connect to this MySQL server PHP Tipps 2005-2 2 23.09.2005 18:34
Suche Tipps für Persormance-Steigerung (Geld für Nützliches) Beitragsarchiv 18 16.08.2005 10:57
MYSQL läuft nur wenn /tmp auf 777 Datenbanken 5 06.07.2005 08:38
mysql root passwort vergessen Datenbanken 1 29.05.2005 11:33
Backup MySQL DB PHP Tipps 2005 12 18.02.2005 20:41
PHP5 &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; MySQL Datenbanken 5 01.08.2004 05:47

Besucher kamen über folgende Suchanfragen bei Google auf diese Seite
php mysql backup, mysql backup php, php mysql backup script, mysql backup script php, mysql backup php script, mysqlbackup, php mysql dump script, php script mysql dump, mysql backup mit php, mysql php backup, php script mysql backup, php backup mysql, mysql dump php script, php mysqldump script, mysql php backup script, backup mysql php, mysql backup per php, mysql backup script, php sql backup script, mysqldump php script

Alle Zeitangaben in WEZ +1. Es ist jetzt 01:28 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum

Creative Commons License
Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.