PHP code example of julio101290 / mysql-backup

1. Go to this page and download the library: Download julio101290/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/ */

    

julio101290 / mysql-backup example snippets




use DatabaseBackupManager\MySQLBackup;

// Initialize PDO connection
$db = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password');

// Create an instance of MySQLBackup
$mysqlBackup = new MySQLBackup($db);

// Backs up all tables
$backup = $mysqlBackup->backup();

// Backs up the specified tables
$backup = $mysqlBackup->backup(['tablename1']);
$backup = $mysqlBackup->backup(['tablename1', 'tablename2']);

// Include table data in the backup or vice versa
$backup = $mysqlBackup->backup(null, true); // Default is true

// Archiving
$backup = $mysqlBackup->backup(null, true, false); // Default is false

// Send the backup file by email
$backup = $mysqlBackup->backup(null, true, true, '[email protected]'); // Default is null

if ($backup) {
    echo "Database backup created successfully.";
} else {
    echo "Database backup failed!";
}

// Restore a database
$backupFile = 'backup_wordpress-2024-05-09_214345.sql';
$restore = $mysqlBackup->restore($backupFile);

// Whether to drop existing tables before restoring data
$restore = $mysqlBackup->restore($backupFile, true); // Default is true

if ($restore) {
    echo "Database restored successfully.";
} else {
    echo "Database restoration failed!";
}