我必须在一定的时间间隔内在实时服务器中备份我的数据库。为此我创建了一个批处理文件,并使用普通的Windows'Schedular安排它的执行。我在批处理文件中调用了一个php文件来执行备份功能。在php文件中,我使用普通的mysql查询连接到数据库并获取数据结构,但这总是引发错误说
警告:mysql_connect()[function.mysql-connect]:[2002]一个连接 尝试失败,因为连接方没有(尝试连接 通过tcp:// [远程数据库服务器]) 第xx行
上的[本地服务器上的文件]
请帮助我克服此错误,以便我可以在本地备份远程数据库。
答案 0 :(得分:2)
错误代码2002表示MySQL未在远程服务器上运行,或者您使用的是错误的端口号?您确定用于连接的端口号是打开的,而不是带有防火墙的阻塞物吗?
在MySQL参考中详细了解它,它们解释了2002年的错误:http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
答案 1 :(得分:1)
您可以使用的另一个选项是让实时服务器将JSON数据发送到本地服务器并以本地方式插入,您无需处理配置,并且可以轻松地为应用程序中的其他模块扩展相同的过程。其他可能需要相同数据而不重新开始工作的应用程序。
答案 2 :(得分:-2)
<?php
ini_set('display_errors', 1);
ini_set('max_execution_time', 0);
$notify_me = 'true';
$email = 'notify email';
$this_server = 'FQDN of local server';
$remote_server = 'FQDN of remote server';
$dbserver = "localhost";
$dbuser = "root";
$dbpass = "";
$time_of_day = '22';
$day_of_week = 'Sat';
$backup_user = 'backups';
$who_am_i = 'Name for email to go to';
$backup_server = 'remote ftp server to upload to';
$ftp_user = 'ftp user name';
$ftp_pass = 'ftp password';
$command="php -q /home/$backup_user/create_mysql_dumps.php";
$job="0 $time_of_day * * * $command";
echo "( crontab -l | grep -v \"$command\" ; echo \"$job\" ) | crontab -";
shell_exec("( crontab -l | grep -v \"$command\" ; echo \"$job\" ) | crontab -");
@shell_exec("adduser $backup_user");
mysql_connect($dbserver, $dbuser, $dbpass);
mysql_select_db("mysql");
function dirToArray($directory) {
$array_items = array();
$handle = @ opendir($directory);
while (false !== ($file = @ readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory)) {
$array_items[] = $file;
}
}
}
@ closedir($handle);
return $array_items;
}
$sql = mysql_query("show databases");
while($row = mysql_fetch_array($sql)){
@shell_exec("mkdir /home/$backup_user/data/");
shell_exec("chmod 777 /home/$backup_user/data/");
shell_exec("rm /home/$backup_user/data/$row[0].sql -f");
shell_exec("mysqldump -u $dbuser -p$dbpass $row[0] > /home/$backup_user/data/$row[0].sql");
echo "Dbase Backup created for $row[0]\n";
usleep(2000);
if(file_exists("/home/$backup_user/data/$row[0].sql")){
if($notify_me == 'true'){
mail($email, "Dbase Backup created for $row[0]", "Hello $who_am_i This is your backup script telling you that its working and saving a backup for $row[0] in /home/$backup_user/data/$row[0].sql", null, "-f root@$this_server");
}
}
}
@shell_exec("mkdir /home/$backup_user/data/");
shell_exec("chmod 777 /home/$backup_user/data/");
shell_exec("rm /home/$backup_user/data/all-databases.sql");
shell_exec("mysqldump -u $dbuser -p$dbpass --all-databases > /home/$backup_user/data/all-databases.sql");
echo "Dbase Backup created for All Databases\n";
if(file_exists("/home/$backup_user/data/$row[0].sql")){
if($notify_me == 'true'){
mail($email, "Dbase Backup created for All Databases", "Hello $who_am_i This is your backup script telling you that its working and saving a backup for ALL SERVERS HAS BEEN CREATED", null, "-f root@$this_server");
}
}
$homes = dirToArray("/home");
$exclude = array('backups', 'mail_admin', 'trials', 'vmail', 'lost+found', 'aquota.user', 'aquota.group');
foreach($homes as $home){
if(!in_array($home, $exclude) & is_dir("/home/$home")){
echo "File System Backup created for /home/$home\n";
@mkdir("/home/$backup_user/files/");
@mkdir("/home/$backup_user/files/$home");
@shell_exec("rm /home/$backup_user/files/$home/* -R -f");
shell_exec("cp /home/$home/* /home/$backup_user/files/$home/ -R -p");
if($notify_me == 'true'){
mail($email, "File System Backup created for /home/$home", "Hello $who_am_i This is your backup script telling you that its working and saving a backup for /home/$home in /home/$backup_user/files/$home", null, "-f root@$this_server");
}
usleep(2000);
}
}
if(date("D") == $day_of_week){
@shell_exec("rm /home/$backup_user/named/* -R -f");
@shell_exec("rm /home/$backup_user/httpd/* -R -f");
@shell_exec("rm /home/$backup_user/mysql/my.cnf -f");
@shell_exec("rm /home/$backup_user/php/php.ini -f");
@mkdir("/home/backups/named");
@mkdir("/home/backups/httpd");
@mkdir("/home/backups/named");
@mkdir("/home/backups/mysql");
@mkdir("/home/backups/php");
shell_exec("cp /var/named/* /home/$backup_user/named/ -R -p");
shell_exec("cp /etc/httpd/conf/* /home/$backup_user/httpd/ -R -p");
shell_exec("cp /etc/my.cnf /home/$backup_user/mysql/my.cnf -p");
shell_exec("cp /etc/php.ini /home/$backup_user/php/php.ini -p");
shell_exec("cp /etc/named.conf /home/$backup_user/named/named.conf -p");
@mkdir("/home/$backup_user/zip");
@shell_exec("rm /home/$backup_user/$this_server-backup-weekly.tar.gz -f");
shell_exec("tar -czvf /home/$backup_user/$this_server-backup-weekly.tar.gz /home/$backup_user --exclude=/home/$backup_user/$this_server-backup-weekly.tar.gz");
if(file_exists("/home/$backup_user/$this_server-backup-weekly.tar.gz")){
echo "Weekly Archive created for $this_server\n";
if($notify_me == 'true'){
mail($email, "Weekly Archive created for $this_server", "Hello $who_am_i This is your backup script telling you that its working and saving a weekly archive for $server at /home/$backup_user/$this_server-backup-weekly.tar.gz", null, "-f root@$this_server");
}
echo "sending backup to " . "http://$backup_server/backme_up.php\n";
$file = "/home/$backup_user/$this_server-backup-weekly.tar.gz";
$fp = fopen($file, 'r');
$conn_id = ftp_connect($backup_server) or die("Couldn't connect to $ftp_server");
echo "$conn_id, $ftp_user, $ftp_pass";
ftp_login($conn_id, $ftp_user, $ftp_pass);
ftp_chdir($conn_id, "/home/$ftp_user");
$contents = ftp_nlist($conn_id, ".");
if (ftp_delete($conn_id, "$this_server-backup-weekly.tar.gz")) {
echo "$this_server-backup-weekly.tar.gzdeleted successful\n";
} else {
echo "could not delete $file\n";
}
if (ftp_put($conn_id, "$this_server-backup-weekly.tar.gz", $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
ftp_close($conn_id);
fclose($fp);
}
}
die();
exit;