Skip to content

Instantly share code, notes, and snippets.

@pserrano
Forked from Serhioromano/backup.php
Last active August 29, 2015 14:07

Revisions

  1. pserrano revised this gist Oct 14, 2014. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    This is based of https://gist.github.com/Serhioromano/9738785 gist but my gist give more information of snapshot description of all machines than you make snapshot
    This is based of https://gist.github.com/Serhioromano/9738785 gist but my fork gist give more information of snapshot description of all machines than you want make snapshot it.

    You need php-cli and php-curl installed on your system for execute all.

    How to create daily HDD/EBS snapshot of you AWS EC2 instance
    ---
    @@ -15,7 +16,7 @@ It looks like a very required and trivial task. But there is not outof the box s

    3. Clon this gist

    $ git clone https://gist.github.com/9738785.git ec2
    $ git clone https://gist.github.com/bd4e889af66e6d31c13c.git ec2

    4. Go to that folder

    @@ -27,7 +28,7 @@ It looks like a very required and trivial task. But there is not outof the box s
    6. Open [releases](https://github.com/aws/aws-sdk-php/releases) of the AWS PHP SDK github project and copy URL of `aws.zip` button. Now download it into your server.

    $ wget https://github.com/aws/aws-sdk-php/releases/download/2.6.0/aws.zip
    $ wget https://github.com/aws/aws-sdk-php/releases/download/2.7.0/aws.zip

    7. Unzip this file into `aws` directory.

    @@ -52,4 +53,4 @@ It looks like a very required and trivial task. But there is not outof the box s

    10. If everything is ok just add cronjob.

    * 23 * * * /usr/local/ec2/backup.php
    * 23 * * * /usr/local/ec2/backup.php
  2. pserrano revised this gist Oct 14, 2014. 2 changed files with 16 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion backup.php
    Original file line number Diff line number Diff line change
    @@ -59,12 +59,24 @@
    {
    continue;
    }

    $volinfo = $client->describeVolumes(
    array(
    'VolumeIds' => array ($volume),
    )
    );

    $description = $snap_descr . ': ' . $volume;

    if (isset($volinfo['Volumes'][0]['Tags'][0]['Value'])){
    $description .= ' | Machine: ' . $volinfo['Volumes'][0]['Tags'][0]['Value'];
    }

    $result = $client->createSnapshot(
    array(
    'DryRun' => $dryrun,
    'VolumeId' => $volume,
    'Description' => $snap_descr,
    'Description' => $description,
    )
    );

    3 changes: 3 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    This is based of https://gist.github.com/Serhioromano/9738785 gist but my gist give more information of snapshot description of all machines than you make snapshot


    How to create daily HDD/EBS snapshot of you AWS EC2 instance
    ---

  3. @Serhioromano Serhioromano revised this gist Mar 26, 2014. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -8,42 +8,42 @@ It looks like a very required and trivial task. But there is not outof the box s
    1. Opent SSH connection to your server.
    2. Navigate to folder

    $ cd /usr/local/
    $ cd /usr/local/

    3. Clon this gist

    $ git clone https://gist.github.com/9738785.git ec2
    $ git clone https://gist.github.com/9738785.git ec2

    4. Go to that folder

    $ cd ec2
    $ cd ec2

    5. Make `backup.php` executable

    $ chmod +x backup.php
    $ chmod +x backup.php
    6. Open [releases](https://github.com/aws/aws-sdk-php/releases) of the AWS PHP SDK github project and copy URL of `aws.zip` button. Now download it into your server.

    $ wget https://github.com/aws/aws-sdk-php/releases/download/2.6.0/aws.zip
    $ wget https://github.com/aws/aws-sdk-php/releases/download/2.6.0/aws.zip

    7. Unzip this file into `aws` directory.

    $ unzip aws.zip -d aws
    $ unzip aws.zip -d aws

    8. Edit `backup.php` php file and set all settings in line `5-12`

    $dryrun = FALSE;
    $interval = '24 hours';
    $keep_for = '10 Days';
    $volumes = array('vol-********');
    $api_key = '*********************';
    $api_secret = '****************************************';
    $ec2_region = 'us-east-1';
    $snap_descr = "Daily backup";
    $dryrun = FALSE;
    $interval = '24 hours';
    $keep_for = '10 Days';
    $volumes = array('vol-********');
    $api_key = '*********************';
    $api_secret = '****************************************';
    $ec2_region = 'us-east-1';
    $snap_descr = "Daily backup";

    9. Test it. Run this script

    $ ./backup.php
    $ ./backup.php

    Test is snapshot was created.

  4. @Serhioromano Serhioromano revised this gist Mar 26, 2014. 2 changed files with 54 additions and 14 deletions.
    16 changes: 8 additions & 8 deletions backup.php
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,14 @@
    <?php
    date_default_timezone_set('UCT');

    $dryrun = FALSE;
    $interval = '24 hours';
    $keep_for = '10 Days';
    $volumes = array('vol-********');
    $api_key = 'AKIAIXXXXXXXXXXXXXXX';
    $api_secret = 'IzMni.........................emQKct';
    $ec2_region = 'us-east-1';
    $snap_descr = "Daily backup";
    $dryrun = FALSE;
    $interval = '24 hours';
    $keep_for = '10 Days';
    $volumes = array('vol-********');
    $api_key = 'AKIAIXXXXXXXXXXXXXXX';
    $api_secret = 'IzMni.........................emQKct';
    $ec2_region = 'us-east-1';
    $snap_descr = "Daily backup";

    require 'aws/aws-autoloader.php';

    52 changes: 46 additions & 6 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,52 @@
    How to create daily HDD snapshot of you AWS EC2 instance
    How to create daily HDD/EBS snapshot of you AWS EC2 instance
    ---

    It looks like a very required and trivial task. But there is not outof the box solution in AWS. But fortunately it is easy to setup with few steps.

    ### How to setup

    1. Copy `backup.php` and `db.json` files into your server folder `/usr/local/ec2/'.
    2. Set parameter in lines 5-11.
    3. Download [AWS-PHP-SDK](https://github.com/aws/aws-sdk-php/releases) and put into foder `/usr/local/ec2/`. Put it so that `require 'aws/aws-autoloader.php';` points to correct file.
    4. Make `backup.php` file executable with `chmod +x backup.php` and make sure PHP can write into `db.json`.
    5. Now try to run file in command line `./backup.php`. Now you can add this command to cron.
    1. Opent SSH connection to your server.
    2. Navigate to folder

    $ cd /usr/local/

    3. Clon this gist

    $ git clone https://gist.github.com/9738785.git ec2

    4. Go to that folder

    $ cd ec2

    5. Make `backup.php` executable

    $ chmod +x backup.php
    6. Open [releases](https://github.com/aws/aws-sdk-php/releases) of the AWS PHP SDK github project and copy URL of `aws.zip` button. Now download it into your server.

    $ wget https://github.com/aws/aws-sdk-php/releases/download/2.6.0/aws.zip

    7. Unzip this file into `aws` directory.

    $ unzip aws.zip -d aws

    8. Edit `backup.php` php file and set all settings in line `5-12`

    $dryrun = FALSE;
    $interval = '24 hours';
    $keep_for = '10 Days';
    $volumes = array('vol-********');
    $api_key = '*********************';
    $api_secret = '****************************************';
    $ec2_region = 'us-east-1';
    $snap_descr = "Daily backup";

    9. Test it. Run this script

    $ ./backup.php

    Test is snapshot was created.

    10. If everything is ok just add cronjob.

    * 23 * * * /usr/local/ec2/backup.php
  5. @Serhioromano Serhioromano revised this gist Mar 24, 2014. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion backup.php
    Original file line number Diff line number Diff line change
    @@ -75,6 +75,6 @@
    );
    }

    file_put_contents('/usr/local/ec2/db.json', json_encode($db));
    file_put_contents(__DIR__.'/db.json', json_encode($db));

    return;
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -8,5 +8,5 @@ It looks like a very required and trivial task. But there is not outof the box s
    1. Copy `backup.php` and `db.json` files into your server folder `/usr/local/ec2/'.
    2. Set parameter in lines 5-11.
    3. Download [AWS-PHP-SDK](https://github.com/aws/aws-sdk-php/releases) and put into foder `/usr/local/ec2/`. Put it so that `require 'aws/aws-autoloader.php';` points to correct file.
    4. Make `backup.php` file executable with `chmod +x backup.php`
    4. Make `backup.php` file executable with `chmod +x backup.php` and make sure PHP can write into `db.json`.
    5. Now try to run file in command line `./backup.php`. Now you can add this command to cron.
  6. @Serhioromano Serhioromano revised this gist Mar 24, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion backup.php
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@
    $api_key = 'AKIAIXXXXXXXXXXXXXXX';
    $api_secret = 'IzMni.........................emQKct';
    $ec2_region = 'us-east-1';
    $snap_descr = "Daily backup";

    require 'aws/aws-autoloader.php';

    @@ -63,7 +64,7 @@
    array(
    'DryRun' => $dryrun,
    'VolumeId' => $volume,
    'Description' => 'Daily backup',
    'Description' => $snap_descr,
    )
    );

  7. @Serhioromano Serhioromano revised this gist Mar 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup.php
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@
    )
    );

    $db = json_decode(file_get_contents('/usr/local/ec2/db.json'), TRUE);
    $db = json_decode(file_get_contents(__DIR__.'/db.json'), TRUE);

    $snapshots = array();
    foreach($db AS $key => $snapshot)
  8. @Serhioromano Serhioromano created this gist Mar 24, 2014.
    79 changes: 79 additions & 0 deletions backup.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    #!/usr/bin/php -q
    <?php
    date_default_timezone_set('UCT');

    $dryrun = FALSE;
    $interval = '24 hours';
    $keep_for = '10 Days';
    $volumes = array('vol-********');
    $api_key = 'AKIAIXXXXXXXXXXXXXXX';
    $api_secret = 'IzMni.........................emQKct';
    $ec2_region = 'us-east-1';

    require 'aws/aws-autoloader.php';

    use Aws\Ec2\Ec2Client;

    $client = Ec2Client::factory(
    array(
    'key' => $api_key,
    'secret' => $api_secret,
    'region' => $ec2_region
    )
    );

    $db = json_decode(file_get_contents('/usr/local/ec2/db.json'), TRUE);

    $snapshots = array();
    foreach($db AS $key => $snapshot)
    {
    if(!empty($snapshots[$snapshot['volume']]))
    {
    if($snapshot['time'] > $snapshots[$snapshot['volume']]['time'])
    {
    $snapshots[$snapshot['volume']] = $snapshot;
    }
    }
    else
    {
    $snapshots[$snapshot['volume']] = $snapshot;
    }

    if($snapshot['time'] < strtotime('- ' . $keep_for))
    {
    $client->deleteSnapshot(
    array(
    'DryRun' => $dryrun,
    'SnapshotId' => $snapshot['id'],
    )
    );

    unset($db[$key]);
    }
    }

    foreach($volumes As $volume)
    {
    if((!empty($snapshots[$volume])) && ($snapshots[$volume]['time'] > strtotime('-' . $interval)))
    {
    continue;
    }

    $result = $client->createSnapshot(
    array(
    'DryRun' => $dryrun,
    'VolumeId' => $volume,
    'Description' => 'Daily backup',
    )
    );

    $db[] = array(
    'volume' => $volume,
    'time' => strtotime($result['StartTime']),
    'id' => $result['SnapshotId']
    );
    }

    file_put_contents('/usr/local/ec2/db.json', json_encode($db));

    return;
    1 change: 1 addition & 0 deletions db.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    []
    12 changes: 12 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    How to create daily HDD snapshot of you AWS EC2 instance
    ---

    It looks like a very required and trivial task. But there is not outof the box solution in AWS. But fortunately it is easy to setup with few steps.

    ### How to setup

    1. Copy `backup.php` and `db.json` files into your server folder `/usr/local/ec2/'.
    2. Set parameter in lines 5-11.
    3. Download [AWS-PHP-SDK](https://github.com/aws/aws-sdk-php/releases) and put into foder `/usr/local/ec2/`. Put it so that `require 'aws/aws-autoloader.php';` points to correct file.
    4. Make `backup.php` file executable with `chmod +x backup.php`
    5. Now try to run file in command line `./backup.php`. Now you can add this command to cron.