Search code examples
phpmysqlcodeigniter-3database-backups

Backup MySQL database with OHO Codeigniter


i have a problem to determine the destination where the zip backup file should be downloaded, and the name of the zip file are always generated randomly like this one i got 0ca26f32-b90c-4198-b078-ed2778a23c0b.zip but the sql file inside the zipped folder is taking the given name backup_2019-01-24-22-33-03.sql

PHP backup function

The below function is downloading a zipped folder contains the database sql file in the default download folder in windows.

public function backup_get(){
        $prefs = array(
            'tables' => array('convt', 'invoice', 'item', 'order_inv', 'person', 'return_details'), // Array of tables to backup.
            'ignore' => array(), // List of tables to omit from the backup
            'format' => 'zip', // gzip, zip, txt
            'filename' => 'backup_' . date("Y-m-d-H-i-s") . '.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
        );

        $backup = $this->dbutil->backup($prefs);
        $db_name = 'backup-on-' . date("Y-m-d-H-i-s") . '.zip';
        $save = './backup/' . $db_name;
        $this->load->helper('file');
        write_file($save, $backup);


        http_response_code(200);
        header('Content-Length: ' . filesize($save));
        header("Content-Type: application/zip");
        header('Content-Disposition: attachment; filename="backup.zip"');
        readfile($save);
        exit;
    }

Problem

i want to change the default destination form downloads folder to a one i manually/auto generated, and be able to give a name to the zip file.

any help is appreciated


Solution

  • Try to modify the backup_get() function like this :

    public function backup_get(){
        $prefs = array(
            'tables' => array('convt', 'invoice', 'item', 'order_inv', 'person', 'return_details'), // Array of tables to backup.
            'ignore' => array(), // List of tables to omit from the backup
            'format' => 'zip', // gzip, zip, txt
            'filename' => 'backup_' . date("Y-m-d-H-i-s") . '.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
        );
    
        $backup = $this->dbutil->backup($prefs);
        $db_name = 'backup-on-' . date("Y-m-d-H-i-s") . '.zip';
        $backup_path = './backup/'; // this is the destination directory name
        if (!file_exists($backup_path)) {
            mkdir($backup_path, 0755, true);
        }
        $save = $backup_path . $db_name;
        $this->load->helper('file');
        write_file($save, $backup);
    }
    

    The $backup_path is the custom directory that you want to set.