javod.com
Jul
2014
02
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 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 Aws\S3\S3Client;

// 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

 
your name: 
url: 
comment: 
  required: copy the following code to post
test