PHP code example of aksoyhlc / databasebackup

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

    

aksoyhlc / databasebackup example snippets




use Aksoyhlc\Databasebackup\DatabaseBackupService;

// Basic configuration
$dbConfig = [
    'host' => 'localhost',      // Database server
    'dbname' => 'database',     // Database name
    'user' => 'root',           // Username
    'pass' => 'password',       // Password
    'charset' => 'utf8mb4',     // Character set (optional)
    'port' => 3306              // Port (optional)
];

// Backup directory
$backupPath = __DIR__ . '/backups';

// Initialize backup service
$backupService = new DatabaseBackupService($dbConfig, $backupPath);

// Create backup
$result = $backupService->createBackup();

if ($result['success']) {
    echo "Backup successful: " . $result['fileName'];
} else {
    echo "Error: " . $result['message'];
}



use Aksoyhlc\Databasebackup\DatabaseBackupService;

$dbConfig = [
    'host' => 'localhost',      // Database server
    'dbname' => 'database',     // Database name
    'user' => 'root',           // Username
    'pass' => 'password',       // Password
    'charset' => 'utf8mb4',     // Character set
    'port' => 3306              // Port number
];

$backupPath = __DIR__ . '/backups';

// Advanced configuration options
$options = [
    // ---- Cache Settings ----
    'cacheTime' => 3600,        // Cache duration (seconds)
    
    // ---- Backup File Cleanup Settings ----
    'maxBackupCount' => 10,     // Maximum number of backups (excess will be deleted automatically)
    'maxBackupAgeDays' => 30,   // Maximum backup age (days, older ones will be deleted automatically)
    
    // ---- Content Filtering Settings ----
    'excludedTables' => [       // Tables to exclude from backup
        'log_table', 
        'temp_data'
    ],
    'tableModes' => [           // Table backup modes
        // 'full': Table structure and data (default)
        // 'structure_only': Only table structure
        // 'data_only': Only table data
        'large_table' => 'structure_only',
        'settings' => 'full'
    ],
    
    // ---- Backup Format Settings ----
    'compressOutput' => true,   // Compress the backup file? (gzip)
    'removeDefiners' => true,   // Remove SQL DEFINER statements?
    
    // ---- Progress Tracking ----
    'progressCallback' => function($status, $current, $total) {
        // Callback function for progress status
        echo "{$status}: {$current}/{$total}\n";
    },
    
    // ---- FTP Settings ----
    'ftpConfig' => [
        'enabled' => true,      // Is FTP backup active?
        'host' => 'ftp.example.com',  // FTP server address
        'username' => 'ftpuser',      // FTP username
        'password' => 'ftppass',      // FTP password
        'port' => 21,                 // FTP port number
        'path' => '/backups',         // Remote directory path
        'ssl' => false,               // Use SSL?
        'passive' => true             // Use passive mode?
    ]
];

$backupService = new DatabaseBackupService($dbConfig, $backupPath, $options);

// Create backup
$result = $backupService->createBackup();

// List backups
$backups = $backupService->listBackups();
foreach ($backups as $backup) {
    echo "File: {$backup['file_name']}, Size: {$backup['size']}, Date: {$backup['date']}\n";
}

// Download a backup
$downloadInfo = $backupService->prepareDownload('backup_database_2023-01-01_12-00-00.sql.gz');
if ($downloadInfo['success']) {
    // Information for serving the file to the user
    $filePath = $downloadInfo['filePath'];
    $fileName = $downloadInfo['fileName'];
    $mimeType = $downloadInfo['mimeType'];
}

// Delete a backup
$deleteResult = $backupService->deleteBackup('backup_database_2023-01-01_12-00-00.sql.gz');

// Upload a backup to FTP
$uploadResult = $backupService->uploadBackupToFtp('backup_database_2023-01-01_12-00-00.sql.gz');

// Clean old backups
$backupService->cleanOldBackups();

// Record messages with different log levels
$backupService->logMessage("Custom information message", "INFO");
$backupService->error("An error occurred");
$backupService->info("Information message");
$backupService->debug("Detailed debugging information");

// Test database connection
if ($backupService->testDatabaseConnection()) {
    echo "Database connection successful";
}

// Test FTP connection
if ($backupService->testFtpConnection()) {
    echo "FTP connection successful";
}

// Get database version
echo "Database version: " . $backupService->getDatabaseVersion();