-
Notifications
You must be signed in to change notification settings - Fork 300
Stream to Google Cloud Storage
David Volquartz Lebech edited this page Mar 12, 2024
·
2 revisions
An example of how to stream the mysqldump directly to a Google Cloud Storage file (with gzip compression).
<?php
# Install requirements:
# composer require ifsnop/mysqldump-php
# composer require google/cloud-storage
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/config.php';
use Ifsnop\Mysqldump as IMysqldump;
use Google\Cloud\Storage\StorageClient;
$database = DB_NAME;
$user = DB_USER;
$pass = DB_PASS;
$host = DB_HOST;
$bucket = GCS_BACKUP_BUCKET;
// GCS_KEY_FILE contains a string of the JSON contents of a Google Cloud Service Account
// Alternatively store the JSON file locally and read it with get file contents, etc.
$storage = new StorageClient([
'keyFile' => json_decode(GCS_KEY_FILE, true),
]);
// Register storage client for gs:// paths
$storage->registerStreamWrapper();
$objectName = "gs://$bucket/" . date('Y-m-d_His') . ".sql.gz";
try {
$dump = new IMysqldump\Mysqldump("mysql:host=$host;dbname=$database", $user, $pass, [
'compress' => IMysqldump\Mysqldump::GZIPSTREAM,
]);
$dump->start($objectName);
} catch (\Exception $e) {
echo 'mysqldump-php error: ' . $e->getMessage();
}
echo 'Backup complete' . PHP_EOL;
From here: https://gist.github.com/dlebech/991c892b0050bf57a643b19c97f0d2f4