PHP code example of deagel1337 / backup-kit

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

    

deagel1337 / backup-kit example snippets




eagel1337\Backup\Kit\Application\Backup\BackupMariaDbApplication;
use Deagel1337\Backup\Kit\DatabaseBackup\Driver\MariaDbBackupDriver;
use Deagel1337\Backup\Kit\DatabaseBackup\Model\DatabaseConnection;
use Deagel1337\Backup\Kit\Step\Backup\BackupDatabaseStep;
use Deagel1337\Backup\Kit\Step\Backup\CheckDiskSpaceStep;

$connection = new DatabaseConnection(
    driver: 'mariadb',
    host: getenv('DB_HOST') ?: '127.0.0.1',
    port: (int) (getenv('DB_PORT') ?: 3306),
    database: getenv('DB_DATABASE') ?: '',
    username: getenv('DB_USERNAME') ?: '',
    password: getenv('DB_PASSWORD') ?: '',
);

$driver = new MariaDbBackupDriver($connection);
$application = BackupMariaDbApplication::create([
    new CheckDiskSpaceStep(),
    new BackupDatabaseStep($driver),
]);

$dump = $application->run(__DIR__ . '/backup.sql');
echo "Dump erstellt: {$dump->path}\n";

use Deagel1337\Backup\Kit\Application\Backup\BackupApplication;
use Deagel1337\Backup\Kit\Archive\Driver\BorgArchiveDriver;
use Deagel1337\Backup\Kit\Archive\Model\RetentionPolicy;
use Deagel1337\Backup\Kit\Services\ArchiveService;

$databaseDriver = new MariaDbBackupDriver($connection);
$archiveService = new ArchiveService(new BorgArchiveDriver(
    repository: getenv('BORG_REPOSITORY') ?: '',
    passphrase: getenv('BORG_PASSPHRASE') ?: '',
));

$result = (new BackupApplication($databaseDriver, $archiveService))->run(
    dumpPath: __DIR__ . '/backup.sql',
    archiveName: 'backup-' . date('Y-m-d-H-i-s'),
    files: [__DIR__ . '/data'],
    retention: new RetentionPolicy(keepDaily: 7, keepWeekly: 4),
);

echo "Backup erstellt: {$result->archive->path}\n";

use Deagel1337\Backup\Kit\Application\Restore\RestoreMariaDbApplication;
use Deagel1337\Backup\Kit\DatabaseBackup\Model\DatabaseDump;
use Deagel1337\Backup\Kit\Step\Restore\CreateDatabaseBackupStep;
use Deagel1337\Backup\Kit\Step\Restore\RestoreDatabaseStep;

$databaseDriver = new MariaDbBackupDriver($connection);
$dump = new DatabaseDump(__DIR__ . '/backup.sql', 'mariadb', 'sql');
$databaseDriver->validateRequirements();
$databaseDriver->validateDump($dump);

$restore = RestoreMariaDbApplication::create($databaseDriver, [
    new CreateDatabaseBackupStep($databaseDriver, __DIR__ . '/before-restore.sql'),
    new RestoreDatabaseStep($databaseDriver),
]);
$restore->run($dump);



eagel1337\Backup\Kit\Application\Archive\ArchiveApplication;
use Deagel1337\Backup\Kit\Archive\Driver\BorgArchiveDriver;
use Deagel1337\Backup\Kit\Services\ArchiveService;

$driver = new BorgArchiveDriver(
    repository: getenv('BORG_REPOSITORY') ?: '',
    passphrase: getenv('BORG_PASSPHRASE') ?: '',
    sshKeyPath: getenv('BORG_SSH_KEY') ?: null,
    sshPort: getenv('BORG_SSH_PORT') !== false
        ? (int) getenv('BORG_SSH_PORT')
        : null,
);

$archive = new ArchiveApplication(new ArchiveService($driver));
$created = $archive->run(
    paths: [__DIR__ . '/data'],
    name: 'backup-' . date('Y-m-d-H-i-s'),
);

echo "Archiv erstellt: {$created->path}\n";

$service = new ArchiveService($driver);
$service->prune(
    keepLast: 7,
    keepDaily: 7,
    keepWeekly: 4,
    keepMonthly: 12,
    keepYearly: 3,
);

$driver = new TarArchiveDriver(archiveDirectory: __DIR__ . '/backups');

use Deagel1337\Backup\Kit\Application\Archive\ArchiveApplication;
use Deagel1337\Backup\Kit\Archive\Driver\TarArchiveDriver;
use Deagel1337\Backup\Kit\Services\ArchiveService;

$archives = new ArchiveApplication(new ArchiveService(
    new TarArchiveDriver(archiveDirectory: __DIR__ . '/backups')
));
$archive = $archives->run([__DIR__ . '/data'], 'local-backup.tar.gz');
$entries = iterator_to_array($archives->list($archive));
$restoreDirectory = __DIR__ . '/restored';
if (! is_dir($restoreDirectory)
    && ! mkdir($restoreDirectory, 0700, true)
    && ! is_dir($restoreDirectory)) {
    throw new RuntimeException('Restore-Verzeichnis konnte nicht erstellt werden.');
}
$archives->extract($archive, $restoreDirectory);