PHP code example of zaiblab / mysql-backup

1. Go to this page and download the library: Download zaiblab/mysql-backup library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zaiblab / mysql-backup example snippets


use DatabaseBackupManager\MySQLBackup;

$db = db_connect(); // CI4's database connection
$backup = new MySQLBackup($db, WRITEPATH . 'backups');

// Back up everything
$info = $backup->backup();

// Back up only specific tables
$info = $backup->backup(['users', 'orders']);

// Only save table structure (no data)
$info = $backup->backup(null, false);

// Create a zipped version of the backup
$info = $backup->backup(null, true, true);

if ($info) {
    echo "Backup file created: " . $info['file_name'] . "\n";
    echo "File size: " . $info['file_size'] . " bytes\n";
}

// Path to your backup file
$path = WRITEPATH . 'backups/backup_mydb-2025-06-21_081400.sql';

// Restore the backup
$restored = $backup->restore($path);

// Optionally: remove old tables before restoring
$restored = $backup->restore($path, true);

if ($restored) {
    echo "Database restored successfully!";
}
bash
composer dump-autoload