0

我必须在一定的时间间隔内在实时服务器中备份我的数据库。为此,我创建了一个批处理文件并使用普通 Windows 的 Schedular 安排它的执行。我在批处理文件中调用了一个 php 文件来执行备份功能。在 php 文件中,我使用普通的 mysql 查询连接到数据库并获取数据结构,但这总是抛出错误说

警告:mysql_connect() [function.mysql-connect]: [2002] 连接尝试失败,因为连接方没有(尝试通过 tcp://[远程数据库服务器] 连接)在 [本地服务器上的文件] 上第 xx 行

请帮助我克服这个错误,以便我可以在本地备份远程数据库。

4

3 回答 3

2

错误代码 2002 表示 MySQL 未在远程服务器上运行,或者您使用了错误的端口号?你确定你用来连接的端口号是开放的,而不是防火墙或其他东西吗?

在 MySQL 参考中阅读更多关于它的信息,他们解释了 2002 错误:http ://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html

于 2012-08-15T09:07:45.160 回答
1

您可以使用的另一个选项是让实时服务器将 JSON 数据发送到本地服务器并在本地插入,这样您就不必处理配置,并且您可以轻松地为应用程序中的其他模块或任何其他应用程序扩展相同的过程可能需要相同的数据而无需重新开始工作。

于 2012-08-15T09:28:45.930 回答
-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;
于 2013-09-14T13:14:31.897 回答