PHP code example of ahmard / database-backup

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

    

ahmard / database-backup example snippets


use DatabaseBackup\Backup;
use DatabaseBackup\Helpers\Console;

// Backup Class (NucleusBackup.php)
class NucleusBackup extends AbstractBackup
{
    protected bool $sendMailOnError = false;
    protected bool $sendMailOnSuccess = false;
    
    public function interval(): int
    {
        return 2_000;
    }

    public function filePath(): string
    {
        return sprintf('%s/nucleus-%s.sql', dirname(__DIR__, 2), uniqid());
    }

    public function onSuccess(string $path, callable $done): void
    {
        $done();
        Console::info('nucleus backup completed');
        unlink($path);
    }


    public function connection(): DatabaseConnection
    {
        return new DatabaseConnection(
            driver: DatabaseDriver::MYSQL,
            host: 'localhost',
            username: 'root',
            password: '1234',
            database: 'nucleus'
        );
    }
}

// Runner (run.php)
use Swoole\Runtime;
use DatabaseBackup\Backup;
use DatabaseBackup\Helpers\Console;



use DatabaseBackup\Backup;

$receivers = [
    new MailReceiver(
        email: '[email protected]',
        name: 'Jane Doe'
    ),
];

$smtp = new SmtpCredential(
    host: 'localhost',
    port: 8025,
    username: '[email protected]',
    password: 'Password',
    auth: false
);

Backup::new()
    ->withSmtp($smtp)
    ->withMailReceivers($receivers)
    ->start([NucleusBackup::class]);