'Database backup with codeigniter 4

I want to take a backup of the database in CI 4.

$model = new class extends \CodeIgniter\Model {
        protected $table      = 'form';
        protected $primaryKey = 'id';
    };
    $db = \Closure::bind(function ($model) {
        return $model->db;
    }, null, $model)($model);
    
    $util = (new \CodeIgniter\Database\Database())->loadUtils($db);

now I want to save the table in SQL format. Thanks in advance.



Solution 1:[1]

public function backup_db(){
$model = new class extends \CodeIgniter\Model {
protected $database      = 'options';
protected $primaryKey = 'option_id';
    };
    $db = \Closure::bind(function ($model) {
    return $model->db;
    }, null, $model)($model);

    $util = (new \CodeIgniter\Database\Database())->loadUtils($db);
    echo $util->getXMLFromResult($model->get());
}

Solution 2:[2]

Currently CI4 doesn't directly support full database backup. You can do it for each of your models (one for each table) and use this from docs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user16025552
Solution 2 Tsefo