User Tools

Site Tools


snapshot_backup

This is an old revision of the document!


Table of Contents

Backup

This snapshot backup enable you to have retention.
this manage my infrastructure backup on ec2.

Requirement :

  • perl
  • Net::Amazon::EC2
  • bash

Files :

  • config # store your amazon key here
  • db.pl # manage local DB for your snapshot / dates / volume
  • backup.sh # Cron this script
  • create_snap.pl # Create Snapshot
  • config_bash # Manage config of your retention / backup
  • snap_status.pl # Check status of your snapshot
  • delete_snap.pl # Delete Snapshot which is out of retention.

C0D3s

| config
#!/usr/bin/perl
 
$awsId = '<your AWS_ID>'; 
$awsKey = '<your AWS_KEY>';
$awsinstance = '<AWS_INSTANCES>'; ## right now it is not used anywhere
| db.pl
#!/usr/bin/perl
 
# developed by k2patel
# [email protected]
 
# This script create Volume of requested size and then return the vol-ID.
# Which can be mounted later.
 
# this Script Manage all Backup Record.
 
use Switch;
use DBI;
 
$filename = 'machine.db';
unless (-e $filename) {
	print "Creating Database...";
	$dbh = DBI->connect( "dbi:SQLite:machine.db" ) || die "Cannot connect: $DBI::errstr";
	$dbh->do( "CREATE TABLE TVR1 ( v_ID, s_ID, Dte )" );
	$dbh->disconnect;
} 
 
my($num_args) = $#ARGV + 1;
if ($num_args < 3) {
  print "\nUsage: db.pl <Operation> <Volume_ID> <Date> [<Snapshot ID>]\n";
  print "Operation : Allowed \"add / get / del\" \n";
  print "All three Value require\n";
  print "Snapshot ID must provided when the Operation is Add / Del\n";
  exit 1;
}
 
$dbh = DBI->connect( "dbi:SQLite:machine.db" ) || die "Cannot connect: $DBI::errstr";
 
switch( $ARGV[0] ){
	case "add"	{
		if ($ARGV[3] eq ''){
			print "You can not use ADD without Snapshot";
			exit 1;
		}else{
		$dbh->do( "INSERT INTO TVR1 VALUES ( '$ARGV[1]', '$ARGV[2]', '$ARGV[3]' )" );
		}
	}
	case "del"	{
		if ($ARGV[3] eq ''){
			print "You can not use DEL without Snapshot";
			exit 1;
		}else{
		$dbh->do( "DELETE FROM TVR1 WHERE s_ID = '$ARGV[2]' AND Dte = '$ARGV[3]'" );
		}
	}
	case "get"	{
		$res = $dbh->selectall_arrayref( "SELECT s_ID, Dte FROM TVR1 WHERE v_ID = '$ARGV[1]' AND Dte = '$ARGV[2]'" );
		foreach( @$res ) {
			#print "$_->[0] $_->[1] $_->[2]\n";
			print "$_->[0] $_->[1] $_->[2]\n"
			}
	}
	else
		{
		$res = $dbh->selectall_arrayref( "SELECT * FROM TVR1" );
		foreach( @$res ) {
			print "$_->[0] $_->[1] $_->[2]\n";
			}
		}
}
$dbh->disconnect;
snapshot_backup.1316550482.txt.gz · Last modified: 2011/09/20 20:28 by k2patel