PHP code example of fin / user-backup-lib
1. Go to this page and download the library: Download fin/user-backup-lib 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/ */
fin / user-backup-lib example snippets
use App\Services\UserBackupService;
use App\ValueObjects\UserBackupCreateOptions;
$options = UserBackupCreateOptions::fromLegacy(
userId: 42,
accountIds: [101, 102],
activeIds: [501],
ignoredTables: ['temp_logs'],
connections: ['mysql', 'replica'],
);
$backup = UserBackupService::createFromOptions($options);
use App\Services\UserBackupService;
$backup = UserBackupService::create(
userId: 42,
accountIds: [101, 102],
activeIds: [501],
ignoredTables: ['temp_logs'],
connections: ['mysql', 'replica'],
);
$backup->fetchAllUserData();
$path = $backup->saveBackupToFile('/tmp/backup_42.json');
// вернется /tmp/backup_42.json.enc
use App\Services\UserBackupService;
use App\ValueObjects\FilterSubquery;
$backup = UserBackupService::create(
userId: 42,
accountIds: [101, 102],
activeIds: [501, 502], // используется как fallback, если подзапрос неприменим
ignoredTables: ['temp_logs'],
connections: ['mysql', 'replica'],
activeIdSubquery: new FilterSubquery(
table: 'actives', // откуда брать id
selectColumn: 'id',
whereColumn: 'user_id',
whereValue: 42,
),
);
$backup->fetchAllUserData();
$path = $backup->saveBackupToFile('/tmp/backup_42.json');
use App\Contracts\UserBackupServiceFactoryInterface;
use App\ValueObjects\UserDataScope;
$factory = app(UserBackupServiceFactoryInterface::class);
$backup = $factory->make(new UserDataScope(
userId: 42,
accountIds: [101, 102],
activeIds: [501],
ignoredTables: ['temp_logs'],
));
$backup->fetchAllUserData();
$path = $backup->saveBackupToFile(storage_path('app/backups/user-42.json'));
$backup = $factory->makeForUser(
userId: 42,
accountIds: [101, 102],
activeIds: [501],
ignoredTables: ['temp_logs'],
);
use App\Contracts\UserDataDeletionServiceInterface;
use App\ValueObjects\UserDataScope;
$cleaner = app(UserDataDeletionServiceInterface::class);
$cleaner->deleteScope(new UserDataScope(
userId: 42,
accountIds: [101, 102],
activeIds: [501],
ignoredTables: ['temp_logs'],
));
$cleaner->deleteUserData(
userId: 42,
accountIds: [101, 102],
activeIds: [501],
ignoredTables: ['temp_logs'],
);
use App\Services\FileStorageService;
$data = FileStorageService::decryptFile('/tmp/backup_42.json.enc');
use App\Services\FileStorageService;
$storage = new FileStorageService();
foreach ($storage->streamBackupData('/tmp/backup_42.json.enc') as $entry) {
$table = $entry['table'];
$row = $entry['row'];
}
return [
'connections' => ['mysql', 'replica'],
];
bash
php -d pcov.enabled=1 vendor/bin/phpunit --coverage-text --coverage-html build/coverage