Created
August 29, 2016 14:34
-
-
Save ferdhie/d493a529caf298d60b70622b1898049a to your computer and use it in GitHub Desktop.
Backup EBS, retain last 7 days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
putenv('AWS_ACCESS_KEY=XXXXXXXXXXXXXXXXXX'); | |
putenv('AWS_SECRET_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); | |
putenv("JAVA_HOME=/usr/lib/jvm/jre"); | |
$ec2_bin="/opt/aws/bin"; | |
$my_cert="/home/ec2-user/cert-xxx.pem"; | |
$my_key="/home/ec2-user/pk-xxx.pem"; | |
$instance_id=file_get_contents('http://169.254.169.254/latest/meta-data/instance-id'); | |
$region="ap-southeast-1"; | |
$availability_zone = file_get_contents('http://169.254.169.254/latest/meta-data/placement/availability-zone'); | |
$EC2_REGION = preg_replace('~([0-9][0-9]*)[a-z]*$~', '\1', $availability_zone); | |
//lama backup dipertahankan | |
$past7days = strtotime('-7 days'); | |
echo "REGION = $EC2_REGION\n"; | |
echo "INSTANCE ID = $instance_id\n"; | |
putenv("EC2_HOME=/opt/aws/apitools/ec2"); | |
//Hapus backupan lama | |
$lines=array(); | |
exec("$ec2_bin/ec2-describe-snapshots --region $EC2_REGION", $lines); | |
foreach($lines as $line) | |
{ | |
$line = trim($line); | |
if (!$line) continue; | |
$words = preg_split('~\t~', $line, -1, PREG_SPLIT_NO_EMPTY); | |
if (!empty($words[0]) && $words[0] == 'SNAPSHOT') | |
{ | |
$snap_id = $words[1]; | |
$state = $words[3]; | |
$created = strtotime($words[4]); | |
$descr = $words[8]; | |
if ( $state != 'completed' || preg_match('~Created by CreateImage~', $descr) ) | |
continue; | |
//jika description dari snapshot mengandung kata-kata backup dan created_time kurang dari 7 hari terakhir, maka hapus | |
if (preg_match('~^backup~', $descr) && $created <= $past7days) | |
{ | |
echo "Delete snapshot $descr ($snap_id)\n"; | |
exec("$ec2_bin/ec2-delete-snapshot --region $EC2_REGION -C $my_cert -K $my_key $snap_id"); | |
} | |
} | |
} | |
$lines=array(); | |
exec("$ec2_bin/ec2-describe-volumes --region $EC2_REGION", $lines); | |
$volumes = array(); | |
foreach($lines as $line) | |
{ | |
$line = trim($line); | |
if (!$line) continue; | |
$words = preg_split('~\s+~', $line, -1, PREG_SPLIT_NO_EMPTY); | |
if (!empty($words[0]) && $words[0] == 'ATTACHMENT') | |
{ | |
$volume_id = $words[1]; | |
$instance_id = $words[2]; | |
$state = $words[4]; | |
echo "$volume_id,$instance_id,$state\n"; | |
if ($state=='attached') | |
{ | |
$volumes[$volume_id]=$instance_id; | |
} | |
} | |
} | |
$today = date('D,Y-m-d'); | |
foreach($volumes as $volume_id => $instance_id) | |
{ | |
$description="backup {$instance_id} {$today}"; | |
echo "Creating Snapshot for the volume: $volume_id with description: $description\n"; | |
exec("$ec2_bin/ec2-create-snapshot -C $my_cert -K $my_key --region $EC2_REGION -d \"$description\" $volume_id"); | |
} | |
echo "Done\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment