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($fields, 0, -2).")";
$values = substr($values, 0, -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($dumpfile, dump_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_remotefile, FTP_ASCII) or die_nice("FTP: Could not put $ftp_localfile");
ftp_chmod($ftp, 0666, $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_hostname, 80) 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($sock, 1024);
}
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) < 1) die_nice("HTTP: Request returned no data");
if (strcmp(substr($data, 0, 2), 'OK') != 0) die_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) > 1) mysql_query(substr($data, 0, -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()