A very specific and awesome request!
Here's a PHP script that does exactly what you asked for:
Prerequisites:
- You have a Wasabi S3 account and a bucket named
siteDBBackupBucket.
- You have the Wasabi S3 access key ID and secret access key.
- You have PHP 7.2+ installed on your server (earlier versions might work too, but I haven't tested them).
- You have the
mysqli extension enabled in your PHP installation (it's usually enabled by default).
- You have the
aws/aws-sdk-php library installed via Composer (we'll get to that in a minute).
Step 1: Install the AWS SDK for PHP
Run the following command in your terminal (in the directory where your PHP script will live):
composer require aws/aws-sdk-php
This will install the AWS SDK for PHP, which we'll use to interact with Wasabi S3.
Step 2: Create the PHP script
Create a new file named backup-db-to-wasabi.php (or any name you like) and paste the following code:
<?php
// Configuration
$dbHost = 'your-mysql-host'; // e.g., localhost
$dbUsername = 'your-mysql-username';
$dbPassword = 'your-mysql-password';
$dbName = 'siteDB'; // the DB we want to backup
$wasabiAccessKeyId = 'YOUR_WASABI_ACCESS_KEY_ID';
$wasabiSecretAccessKey = 'YOUR_WASABI_SECRET_ACCESS_KEY';
$wasabiRegion = 'us-east-1'; // check Wasabi's docs for the correct region
$wasabiBucketName = 'siteDBBackupBucket';
// Load the AWS SDK
require 'vendor/autoload.php';
use Aws\S3\S3Client;
// Create an S3 client for Wasabi
$s3Client = new S3Client([
'version' => 'latest',
'region' => $wasabiRegion,
'endpoint' => 'https://s3.' . $wasabiRegion . '.wasabisys.com', // Wasabi's S3 endpoint
'credentials' => [
'key' => $wasabiAccessKeyId,
'secret' => $wasabiSecretAccessKey,
],
]);
// Function to backup the MySQL DB
function backupDatabase($host, $username, $password, $dbName) {
$dumpFile = $dbName . '_' . date('Y-m-d_H-i-s') . '.sql.gz';
$command = "mysqldump -h $host -u $username -p$password $dbName | gzip > $dumpFile";
exec($command);
return $dumpFile;
}
// Backup the DB
$backupFile = backupDatabase($dbHost, $dbUsername, $dbPassword, $dbName);
// Upload the backup to Wasabi S3
try {
$result = $s3Client->putObject([
'Bucket' => $wasabiBucketName,
'Key' => $backupFile,
'Body' => fopen($backupFile, 'rb'),
'ACL' => 'private', // keep the backup private
]);
echo "Backup uploaded successfully: " . $result['ObjectURL'] . "\n";
} catch (Aws\Exception\S3Exception $e) {
echo "Error uploading backup: " . $e->getMessage() . "\n";
}
// Clean up: delete the local backup file
unlink($backupFile);
What the script does:
- Configures the MySQL DB connection and Wasabi S3 credentials.
- Installs and loads the AWS SDK for PHP.
- Creates an S3 client for Wasabi using the provided credentials.
- Defines a
backupDatabase() function that:
- Uses
mysqldump to export the MySQL DB to a SQL file.
- Gzips the SQL file.
- Returns the filename of the gzipped backup.
- Calls
backupDatabase() to create a backup of siteDB.
- Uploads the backup file to Wasabi S3 using the
putObject() method.
- Catches any S3 upload errors and prints an error message.
- Deletes the local backup file after a successful upload.
Important:
- Replace the placeholders (
your-mysql-host, YOUR_WASABI_ACCESS_KEY_ID, etc.) with your actual values.
- Make sure the PHP script has the necessary permissions to write files (for the backup) and execute
mysqldump.
- Test the script by running it manually (e.g.,
php backup-db-to-wasabi.php) to ensure everything works as expected.
- Schedule the script to run periodically (e.g., daily) using a cron job (e.g.,
0 0 * * * php /path/to/backup-db-to-wasabi.php).
That's it! Your MySQL DB siteDB will now be backed up daily (or whenever you schedule it) and uploaded to your Wasabi S3 bucket siteDBBackupBucket.
Happy backups!