Use DreamObjects to Backup Your MySQL Database (PHP Script)

Dev

Use DreamObjects to Backup Your MySQL Database (PHP Script)

by Javod Khalaj
//

DreamObjects is DreamHost’s public cloud storage offering. It’s analogous to Amazon’s S3 offering, but slightly cheaper. The nice thing about DreamObjects is that it is compatible with the S3 API. This basically makes DreamObjects a drop-in replacement – very convenient.

DreamObjects is ideal for backup storage.

This tutorial breaks down into four parts:

  1. Get the Amazon AWS SDK for PHP
  2. Create a backup of your database and gzip it
  3. Upload your file to DreamObjects
  4. Set up a cron to schedule the entire task

First, we want to download the AWS SDK for PHP; as of this writing the SDK is on version 2. You can install using Composer or Phar, if you’re familiar with that approach, or just download the .zip file. We will be using the .zip file for this tutorial.

Once you’ve downloaded and unzipped the .zip file, you should have an “aws” directory. Inside this directory, there is an “aws-autoloader.php” file. This is the file that we will need to include in our script. It will automatically set up and load the classes needed to communicate with DreamObjects.

Create a new PHP file outside of the “aws” directory named: “backup.php”

Our working folder should now have two objects:

  1. A file named “backup.php”
  2. A folder named “aws”

backup.php code:

// Here we require the aws-autoloader file
// this sets up the classes we need to work with DreamObjects
// Put in your information anywhere the "REPLACE_*" words appear
require('aws/aws-autoloader.php');


// Your AWS_KEY and AWS_SECRET_KEY can be retrieved from:
// https://panel.dreamhost.com/
// Log in DreamHost Panel > Cloud Services > DreamObjects
define('AWS_KEY', 'REPLACE_YOUR_KEY_GOES_HERE');
define('AWS_SECRET_KEY', 'REPLACE_YOUR_SECRET_GOES_HERE');
define('HOST', 'https://objects.dreamhost.com');

// Pull in the class we need
use AwsS3S3Client;

// Establish connection with DreamObjects with an S3 client.
$client = S3Client::factory(array(
    'base_url' => HOST,
    'key'      => AWS_KEY,
    'secret'   => AWS_SECRET_KEY
));

// Create a date time to use for a filename
$date = new DateTime('now');
$filetime = $date->format('Y-m-d.H:i:s');

// exec() function can sometimes be disabled, check your php.ini file if you run into issues
// Get a mysqldump of your database, pipe it to gzip and output the file
// Notice that there is no space between -p and the password
exec('mysqldump --host=REPLACE_DATABASE_HOST REPLACE_DATABASE_NAME -u"REPLACE_USERNAME" -p"REPLACE_PASSWORD" | gzip -c | cat > ./db-' . $filetime . '.sql.gz');

// Push it up to DreamObjects
// Notice that the $acl is set to private
$key         = 'db-' . $filetime . '.sql.gz';
$source_file = './db-' . $filetime . '.sql.gz';
$acl         = 'private';
$bucket      = 'REPLACE_YOUR_BUCKET_NAME';
$client->upload($bucket, $key, fopen($source_file, 'r'), $acl);

// Remove backup from local storage
exec('rm -f ./db-' . $filetime . '.sql.gz');

Now that we have our code set up, we need to schedule it to run.
For this, we set up a crontab. In this cron, I’m scheduling it to run once a day.
If you’re unfamiliar with crontab, here is a quick tutorial.

 0 0 * * * /path/to/php /path/to/backup.php 

And we’re done! We have scheduled an upload of your MySQL database to DreamObjects for safekeeping.

For more technical info on how to use the API with DreamObjects, visit: http://docs.dreamobjects.net/s3-examples/php2.html

More About the Author

Javod Khalaj

Experience Architect
Cohort Analysis in Tableau: User Retention Given Only Created and Last Seen Dates Cohorts have been the go-to analysis for user retention for a while now, and Tableau has a great article on how to go about creating ...
Use DreamObjects to Backup Your MySQL Database (PHP Script) DreamObjects is DreamHost’s public cloud storage offering. It’s analogous to Amazon’s S3 offering, but slightly cheaper. The nice thing ...

See more from this author →

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072

×

Love our blog? You should see our emails. Sign up for our newsletter!