如何使用Codeigniter备份默认数据库以外的数据库

时间:2019-12-09 05:59:39

标签: php codeigniter database-connection database-backups

$this->db=$this->load->database('anotherdb',TRUE);
        $dbname = $this->db->database;
        // Load the DB utility class
        $this->load->dbutil();
        //$this->dbutil = $this->load->dbutil($dbname, TRUE);
        $filename = $dbname.date('d-m-Y').".sql";
        $zipname = $dbname.date('d-m-Y').".zip";

        //insert into table... 
        $date = date('Y-m-d');
        $qry = $this->db->query("select * from backup where crdate = '$date'");
        if($qry->num_rows() == 0){
            $details = array('name'=>$zipname,'crdate'=>date('Y-m-d'));
            $this->db->insert('backup',$details);
        }

        $prefs = array(
                'tables'      => array(),  // Array of tables to backup.
                'ignore'      => array(),           // List of tables to omit from the backup
                'format'      => 'zip',             // gzip, zip, txt
                'filename'    => $filename,    // File name - NEEDED ONLY WITH ZIP FILES
                'newline'     => "\n"               // Newline character used in backup file
            );

        $backup = $this->dbutil->backup($prefs);
        // Backup your entire database and assign it to a variable
        //$backup =& $this->dbutil->backup();

        // Load the file helper and write the file to your server
        $this->load->helper('file');
        if(write_file('./backup/'.$zipname, $backup,'w')){
            // Load the download helper and send the file to your desktop
            $this->load->helper('download');
            force_download($zipname, $backup);
            // $dbname = 'testproject';
            // include(base_url().'/backuptest/backup.php');
        }

我将这段代码用于备份'anotherdb',但是我没有得到此备份。仅默认数据库正在备份。这样任何人都可以提供帮助。谢谢

0 个答案:

没有答案