1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Simple working code

function database_backup()
{
    $this->load->dbutil();
    $prefs = array('format' => 'zip', 'filename' => 'Database-backup_' . date('Y-m-d_H-i'));
    $backup = $this->dbutil->backup($prefs);
    if (!write_file('./uploads/backup/BD-backup_' . date('Y-m-d_H-i') . '.zip', $backup)) {
    echo "Error while creating auto database backup!";
    }
    else {
            echo "Database backup has been successfully Created";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.