PHP code example of druidfi / mysqldump-php
1. Go to this page and download the library: Download druidfi/mysqldump-php 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/ */
druidfi / mysqldump-php example snippets
try {
$dump = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dump->start('storage/work/dump.sql');
} catch (\Druidfi\Mysqldump\Exception\MysqldumpException $e) {
echo 'mysqldump-php error: ' . $e->getMessage();
}
use Druidfi\Mysqldump\ConnectionInterface;
use Druidfi\Mysqldump\Mysqldump;
class ExistingPdoConnection implements ConnectionInterface
{
public function __construct(private readonly PDO $pdo) {}
public function connect(): PDO { return $this->pdo; }
public function getHost(): string { return 'app-db'; }
public function getDbName(): string { return 'testdb'; }
}
$dumper = new Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dumper->setConnector(new ExistingPdoConnection($pdo));
$dumper->start('storage/work/dump.sql');
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dumper->setTransformTableRowHook(function ($tableName, array $row) {
if ($tableName === 'customers') {
$row['social_security_number'] = (string) rand(1000000, 9999999);
}
return $row;
});
$dumper->start('storage/work/dump.sql');
use Druidfi\Mysqldump\Anonymizer;
$dumper->setTransformTableRowHook(Anonymizer::columnMap([
'customers' => [
'email' => Anonymizer::email(), // [email protected]
'phone' => Anonymizer::mask(3), // 040********
'social_security_number' => Anonymizer::fixed('REDACTED'),
],
'users' => [
'name' => Anonymizer::hash('my-secret-salt'),
// Any callable(mixed $value, array $row): mixed works alongside the helpers
'display_name' => fn ($value, array $row) => 'user-' . $row['id'],
],
]));
$dumper->setTransformTableRowHook(function ($tableName, array $row) {
// Exclude soft-deleted users from the dump
if ($tableName === 'users' && $row['deleted_at'] !== null) {
return null;
}
return $row;
});
$dumper->setInfoHook(function ($object, $info) {
if ($object === 'table') {
echo $info['name'], ': ', $info['rowCount'], PHP_EOL;
}
});
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dumper->setTableWheres([
'users' => 'date_registered > NOW() - INTERVAL 3 MONTH AND deleted=0',
'logs' => 'date_logged > NOW() - INTERVAL 1 DAY',
'posts' => 'isLive=1'
]);
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dumper->setTableLimits([
'users' => 300,
'logs' => 50,
'posts' => 10
]);
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dumper->setTableLimits([
'users' => [20, 10], // MySQL query equivalent "... LIMIT 20, 10", i.e. 10 rows starting from offset 20
]);
$storage = new Google\Cloud\Storage\StorageClient(['projectId' => 'my-project']);
$storage->registerStreamWrapper();
$dump = new \Druidfi\Mysqldump\Mysqldump($dsn, $user, $pass, ['compress' => 'Gzipstream']);
$dump->start('gs://my-bucket/backup.sql.gz');
$dumpSettings = ['compress' => 'Gzip', 'no-data' => true];
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password', $dumpSettings, $pdoOptions);
$dump = new \Druidfi\Mysqldump\Mysqldump(
sprintf('mysql:host=%s;dbname=%s', getenv('DB_HOST'), getenv('DB_NAME')),
getenv('DB_USER'),
getenv('DB_PASSWORD')
);