Install PHP 5.3 or higher

git clone git://github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP

cd AWSSDKForPHP

cp config.inc-sample.php  config.inc.php

vi config.inc.php

'key' => 'development-key',
'secret' => 'development-secret',

above two variable mention AWS managment conclose login key and password in encrypted format which information you get from securites IAM user.

Download phpmailer also

Now make one backup script  like below i got from google search



/**************************************************************************
|
|   Script to Automate EBS Backups
|   Run this script with CRON or whatever every X period of time to take
|   automatic snapshots of your EBS Volumes.  Script will delete old
|   snapshot after Y period of time
|
|   Version 1.01 updated 2012-08-02
|
|   Copyright 2012 Caleb Lloyd
|   http://www.caleblloyd.com/
|
|   I offer no warrant or guarentee on this code - use at your own risk
|   You are free to modify and redistribute this code as you please
|
|   Requires AWS PHP SDK be configured for your AWS Account:
|       http://aws.amazon.com/sdkforphp/
|
|   Optional PHPMailer Support to email results to yourself
|       http://phpmailer.worxware.com/
|
|   Stores snapshot information in "./snapshot_information.json"
|       Make sure PHP can write this file
|
**************************************************************************/


/**************************************************************************
|   Begin Configuration
**************************************************************************/

//Declare the volumes that you want to backup
//The Volume ID's are the keys of the array, you can store any custom information you
//want in value array, or just keep it blank.  Make sure you keep it as a blank array
//because the script will fillthis up with values...
$volumes=array( '111111111'=>array(),
//                'vol-22222222'=>array()
);

//Do not take a snapshot more than every X hours/minutes/days, etc. (uses strtotime)
//This prevents the script from running out of control and producing tons of snapshots
$snapshot_limit = '23 hours';

//Keep snapshots for this amount of time (also uses strtotime)
$keep_snapshots = '7 days 12 hours';

//Your path to the Amazon AWS PHP SDK
require_once 'path of  aws sdk/sdk.class.php';
//EC2 Region, view path of  aws sdk/services/services/ec2.class.php for definitions
$region='region which get in above file';

//Your path to PHP Mailer (if you don't want to eamil yourself the results, you can get rid of this)
require_once('php mailer path/class.phpmailer.php');
//Go to bottom of script to configure PHP Mailer settings


/**************************************************************************
|   End Configuration
**************************************************************************/

function snapshot_info($s)
{
    $info='';
    $info.='Volume: '.$s['volume'].'
';
    $info.=(!empty($s['volume_name'])?'Volume Name: '.$s['volume_name'].'
':'');
    $info.=(!empty($s['snapshot'])?'Snapshot: '.$s['snapshot'].'
':'');
    $info.=(!empty($s['instance'])?'EC2 Instance: '.$s['instance'].'
':'');
    $info.=(!empty($s['device'])?'Device: '.$s['device'].'
':'');
    $info.=(!empty($s['error'])?'Error: '.$s['error'].'
':'');
    $info.=(!empty($s['datetime'])?'Date/Time: '.$s['datetime'].'
':'');
    $info.='
';
    return $info;
}

$success=array();
$failure=array();
$preserve=array();
$success_delte=array();
$failure_delete=array();

$ec2 = new AmazonEC2();
$ec2 = $ec2->set_region($region);

$latest_snapshot=array();

if (file_exists('snapshot_information.json'))
    $json=file_get_contents('snapshot_information.json');
else
    $json='[]';
$snapshots=json_decode($json,TRUE);
foreach ($snapshots as $s)
{
    if (!empty($lastest_snapshot[$s['volume']]))
    {
        if ($s['timestamp']>$lastest_snapshot[$s['volume']]['timestamp'])
        {
            $lastest_snapshot[$s['volume']]=$s;
        }
    }
    else
    {
        $lastest_snapshot[$s['volume']]=$s;
    }
}

foreach ($volumes as $volume => $v)
{
    $v['volume']=$volume;
    $v['instance']='Not Attached to an Instance';

    $volume_information = $ec2->describe_volumes(array('VolumeId' => $volume));
    $v['volume_name'] = '(volume has no tags)';
    if (!empty($volume_information->body->volumeSet->item->tagSet->item->value))
    {
        $v['volume_name'] = (string)$volume_information->body->volumeSet->item->tagSet->item->value;
    }
    $description = 'Volume '.$volume.(empty($v['volume_name'])?'':' ('.$v['volume_name'].')');
   
    if (!empty($volume_information->body->volumeSet->item->attachmentSet->item->status))
    {
        if ($volume_information->body->volumeSet->item->attachmentSet->item->status == "attached")
        {
            $v['device'] = (string)$volume_information->body->volumeSet->item->attachmentSet->item->device;
            $v['instance'] = (string)$volume_information->body->volumeSet->item->attachmentSet->item->instanceId;
            $description.=' attached to '.$v['instance'].' as '.$v['device'];
        }
    }
    else
    {
        $description.= ' ('.$v['instance'].')';
    }
   
    if ((!empty($lastest_snapshot[$volume]))&&($lastest_snapshot[$volume]['timestamp']>strtotime('-'.$snapshot_limit)))
    {
        $error=TRUE;
        $v['datetime']=date('Y-m-d H:i:s');
        $v['timestamp']=time();
        $v['error']='An Automatic Snapshot Already Exists for that volume in the past '.$snapshot_limit;
        $failure[]=$v;
    }
    else
    {
        $response = $ec2->create_snapshot($volume, array('Description'=>$description));
        if ($response->isOK())
        {
            $v['datetime']=date('Y-m-d H:i:s');
            $v['timestamp']=time();
            $v['snapshot']=(string)$response->body->snapshotId;
            $success[$v['snapshot']]=$v;
 // adding the tag :
            $responseTag = $ec2->create_tags ( $v['snapshot'], array ( 'Key'=>'Name', 'Value'=>$v['volume_name'] ) );
        }
        else
        {
            $error=TRUE;
            $v['datetime']=date('Y-m-d H:i:s');
            $v['timestamp']=time();
            $v['error']=(string)$response->body->Errors->Error->Message;
            $failure[]=$v;
        }
    }
}

if (!empty($snapshots))
{
    foreach ($snapshots as $snapshot => $s)
    {
        $s['snapshot']=$snapshot;
        if ($s['timestamp']        {
            $response = $ec2->delete_snapshot($snapshot);
            if ($response->isOK())
            {
                $success_delete[$snapshot]=$s;
            }
            else
            {
                $error=TRUE;
                $s['error']=(string)$response->body->Errors->Error->Message;
                $failure_delete[$snapshot]=$s;
            }
        }
        else
        {
            $preserve[$snapshot]=$s;
        }
    }
    $snapshots_json=json_encode(array_merge($success,$preserve));
}
else
{
    $snapshots_json=json_encode($success);
}
file_put_contents('snapshot_information.json',$snapshots_json);

$message='';

if (!empty($success))
{
    $message.='The following Snapshots Succeeded:
';
    foreach ($success as $v)
    {
        $message.=snapshot_info($v);
    }
}

if (!empty($failure))
{
    $message.='The following Snapshots Failed and had Errors:
';
    foreach ($failure as $v)
    {
        $message.=snapshot_info($v);
    }
}

if (!empty($success_delete))
{
    $message.='The following old Snapshots were removed:
';
    foreach ($success_delete as $v)
    {
        $message.=snapshot_info($v);
    }
}

if (!empty($failure_delete))
{
    $message.='The following old Snapshots had errors while trying to remove:
';
    foreach ($failure_delete as $v)
    {
        $message.=snapshot_info($v);
    }
}

if (!empty($preserve))
{
    $message.='The following Snapshots were preserved:
';
    foreach ($preserve as $v)
    {
        $message.=snapshot_info($v);
    }
}
?>



0 comments:

Post a Comment