使用php代码创建数据库到ftp服务器的自动备份

时间:2018-06-12 11:32:41

标签: php sql 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值存储。

可能是什么问题?。

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

ftp_connect有一个你可以设置的超时参数 这来自docs

  

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