首页 > 解决方案 > 使用 php 代码创建数据库到 ftp 服务器的自动备份

问题描述

需要创建 sql 文件到 ftp 服务器的自动备份。

下面提到的代码我使用的是相同的

    function backup_db()
    {
    ini_set('memory_limit','100G');
        ini_set('max_input_time', 3000000);  
        ini_set('max_execution_time', 3000000);
        error_reporting(E_ALL);
        ini_set('display_errors', 1);

        //echo 'here';exit;  
        $this->load->dbutil();
        $this->load->helper(array('file', 'download')); 

        $prefs = array(
                'tables'      => array(),   // Array of tables to backup.
                'ignore'      => array('general_ledger'),           // List of tables to omit from the backup
                'format'      => 'zip',             // gzip, zip, txt
                'filename'    => 'mybackup.sql',    // File name - NEEDED ONLY WITH ZIP FILES
                'add_drop'    => TRUE,              // Whether to add DROP TABLE statements to backup file
                'add_insert'  => TRUE,              // Whether to add INSERT data to backup file
                'newline'     => "\n"               // Newline character used in backup file
              );


        //$this->db->save_queries = false;
        $backup = $this->dbutil->backup($prefs);
        //echo "in";exit();
        $filename = 'backup-' . date('d_m_Y_H_i_s') . ' .zip';

        write_file('/opt/backups/' . $filename, $backup);
        //force_download($filename, $backup); 



    try{
    $ftp_server = "xyz.xyz.xyz.xyz"; //server Ip
    $ftp_conn = ftp_connect($ftp_server);
    $ftp_username = "user";
    $ftp_userpass = "pass";

    $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

    // open file for reading
    $file = "/opt/backups/".$filename;
    $fp = fopen($file,"r");

    // upload file
    $success=ftp_fput($ftp_conn, "/Live_db_backups/Backup/$filename", $fp, FTP_ASCII);

    // close this connection and file handler
    ftp_close($ftp_conn);
    fclose($fp);
}catch (Exception $e) {
$this->session->set_flashdata('error',"FTP Error for Db backup File Download!!!!");
echo 'FTP Bachup Failed';
}

      echo 'here';  exit;
    }

这段代码中实际发生的只是存储了一半的数据库备份。

完整的数据库备份未保存到文件中。

尝试增加ini_set('memory_limit','100G'); ini_set('max_input_time', 3000000);
ini_set('max_execution_time', 3000000)

但仍然只有一半的 db 值正在存储。

可能是什么问题?

任何帮助表示赞赏。

标签: phpsqlftp

解决方案


ftp_connect 有一个可以设置的超时参数 这是来自文档

timeout 此参数指定所有后续网络操作的超时时间(以秒为单位)。如果省略,则默认值为 90 秒。可以随时使用 ftp_set_option() 和 ftp_get_option() 更改和查询超时。


推荐阅读