snapshot_backup
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| snapshot_backup [2011/09/20 20:25] – [Backup] k2patel | snapshot_backup [2020/08/10 02:35] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| This snapshot backup enable you to have retention.\\ | This snapshot backup enable you to have retention.\\ | ||
| + | this manage my infrastructure backup on ec2. | ||
| ==== Requirement : ==== | ==== Requirement : ==== | ||
| Line 7: | Line 8: | ||
| * perl | * perl | ||
| * Net:: | * Net:: | ||
| + | * DBI:SQLite | ||
| * bash | * bash | ||
| Line 19: | Line 21: | ||
| * delete_snap.pl | * delete_snap.pl | ||
| - | ==== C0D3s ==== | + | ===== C0D3s ===== |
| + | ==== config ==== | ||
| <code bash | config > | <code bash | config > | ||
| Line 29: | Line 32: | ||
| $awsinstance = '< | $awsinstance = '< | ||
| </ | </ | ||
| + | |||
| + | ==== bash config ==== | ||
| + | <code bash | config_bash> | ||
| + | # this file contain what need to be backup. | ||
| + | # Follow the format of value | ||
| + | # instance< | ||
| + | |||
| + | instance1=" | ||
| + | instance2=" | ||
| + | </ | ||
| + | |||
| + | ==== db.pl ==== | ||
| + | |||
| + | <code perl | db.pl > | ||
| + | # | ||
| + | |||
| + | # developed by k2patel | ||
| + | # k2patel@hotmail.com | ||
| + | |||
| + | # this Script Manage all Backup Record. | ||
| + | |||
| + | use Switch; | ||
| + | use DBI; | ||
| + | |||
| + | $filename = ' | ||
| + | unless (-e $filename) { | ||
| + | print " | ||
| + | $dbh = DBI-> | ||
| + | $dbh-> | ||
| + | $dbh-> | ||
| + | } | ||
| + | |||
| + | my($num_args) = $#ARGV + 1; | ||
| + | if ($num_args < 3) { | ||
| + | print " | ||
| + | print " | ||
| + | print "All three Value require\n"; | ||
| + | print " | ||
| + | exit 1; | ||
| + | } | ||
| + | |||
| + | $dbh = DBI-> | ||
| + | |||
| + | switch( $ARGV[0] ){ | ||
| + | case " | ||
| + | if ($ARGV[3] eq '' | ||
| + | print "You can not use ADD without Snapshot"; | ||
| + | exit 1; | ||
| + | }else{ | ||
| + | $dbh-> | ||
| + | } | ||
| + | } | ||
| + | case " | ||
| + | if ($ARGV[3] eq '' | ||
| + | print "You can not use DEL without Snapshot"; | ||
| + | exit 1; | ||
| + | }else{ | ||
| + | $dbh-> | ||
| + | } | ||
| + | } | ||
| + | case " | ||
| + | $res = $dbh-> | ||
| + | foreach( @$res ) { | ||
| + | #print " | ||
| + | print " | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | $res = $dbh-> | ||
| + | foreach( @$res ) { | ||
| + | print " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | $dbh-> | ||
| + | </ | ||
| + | |||
| + | ==== backup.sh ==== | ||
| + | |||
| + | <code bash | backup.sh> | ||
| + | #!/bin/bash | ||
| + | |||
| + | # developed by k2patel | ||
| + | # k2patel@hotmail.com | ||
| + | |||
| + | ############# | ||
| + | # this Script will do the Backup and remove old copy. | ||
| + | # This script work based on existing DB Entry. | ||
| + | # When you initialize it only create one copy rest of the dummy entry need to be entered by you. | ||
| + | ##################################### | ||
| + | |||
| + | |||
| + | BASE=" | ||
| + | cd $BASE | ||
| + | |||
| + | source config_bash | ||
| + | |||
| + | DATE=$(date +%Y%m%d) | ||
| + | perl=$(which perl) | ||
| + | |||
| + | |||
| + | for inst in instance{1..10} | ||
| + | do | ||
| + | eval var1=\$$inst | ||
| + | arry=($var1) | ||
| + | if [[ $1 == " | ||
| + | if [ " | ||
| + | then | ||
| + | vol=${arry[0]} | ||
| + | echo " | ||
| + | newsnp=`$perl create_snap.pl $vol $DATE` | ||
| + | $perl db.pl add $vol $newsnp $DATE | ||
| + | echo " | ||
| + | $perl snap_status.pl $newsnp | ||
| + | echo " | ||
| + | fi | ||
| + | else | ||
| + | if [ " | ||
| + | then | ||
| + | freq=${arry[1]} | ||
| + | vol=${arry[0]} | ||
| + | iter=${arry[2]} | ||
| + | lookup=$(echo "$iter * $freq" | bc) | ||
| + | isit=`date -d "now -$lookup days" +%Y%m%d` | ||
| + | isit_nxt=`date -d "now -$freq days" +%Y%m%d` | ||
| + | lstnc=(`$perl $BASE/db.pl get " | ||
| + | if [ " | ||
| + | echo " | ||
| + | newsnp=`$perl create_snap.pl $vol $DATE` | ||
| + | $perl db.pl add $vol $newsnp $DATE | ||
| + | echo " | ||
| + | $perl snap_status.pl $newsnp | ||
| + | echo " | ||
| + | lstnx=(`$perl $BASE/db.pl get " | ||
| + | if [ " | ||
| + | echo " | ||
| + | else | ||
| + | if [ " | ||
| + | $perl delete_snap.pl ${lstnc[0]} | ||
| + | fi | ||
| + | fi | ||
| + | else | ||
| + | echo " | ||
| + | fi | ||
| + | fi | ||
| + | fi | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== create snapshot ==== | ||
| + | |||
| + | <code perl | create_snap.pl> | ||
| + | # | ||
| + | # Created / written by k2patel@hotmail.com | ||
| + | # this script create snapshot for given volume. | ||
| + | |||
| + | use Data:: | ||
| + | use strict; | ||
| + | #use Date::Calc qw( :all ); | ||
| + | |||
| + | use lib "/ | ||
| + | use Net:: | ||
| + | |||
| + | # Configuration. | ||
| + | require " | ||
| + | |||
| + | # our $ec2; | ||
| + | our $awsId; | ||
| + | our $awsKey; | ||
| + | our $awsinstance; | ||
| + | |||
| + | my($num_args) = $#ARGV + 1; | ||
| + | if ($num_args < 2) { | ||
| + | print " | ||
| + | print "This will create Snapshot for given Volume\n"; | ||
| + | print " | ||
| + | exit 1; | ||
| + | } | ||
| + | |||
| + | ## Define Access Keys for Amazon. | ||
| + | my $ec2 = Net:: | ||
| + | AWSAccessKeyId => $awsId, | ||
| + | SecretAccessKey => $awsKey | ||
| + | ); | ||
| + | |||
| + | my @result = $ec2-> | ||
| + | VolumeId => $ARGV[0], | ||
| + | Description => $ARGV[1] | ||
| + | ); | ||
| + | |||
| + | print $result[0]-> | ||
| + | #print Dumper \@result; | ||
| + | </ | ||
| + | |||
| + | ==== delete shnapshot ==== | ||
| + | <code perl | delete_snap.pl> | ||
| + | # This script delete snapshot of requested volume | ||
| + | # Author : k2patel < | ||
| + | # Please send me message for suggestion or update. | ||
| + | |||
| + | # this script delete snapshot | ||
| + | |||
| + | use Data:: | ||
| + | use strict; | ||
| + | #use Date::Calc qw( :all ); | ||
| + | |||
| + | use lib "/ | ||
| + | use Net:: | ||
| + | |||
| + | # Configuration. | ||
| + | require " | ||
| + | |||
| + | # our $ec2; | ||
| + | our $awsId; | ||
| + | our $awsKey; | ||
| + | our $awsinstance; | ||
| + | |||
| + | my($num_args) = $#ARGV + 1; | ||
| + | if ($num_args < 1) { | ||
| + | print " | ||
| + | print "This will Delete Snapshot\n"; | ||
| + | exit 1; | ||
| + | } | ||
| + | |||
| + | ## Define Access Keys for Amazon. | ||
| + | my $ec2 = Net:: | ||
| + | AWSAccessKeyId => $awsId, | ||
| + | SecretAccessKey => $awsKey | ||
| + | ); | ||
| + | |||
| + | my @result = $ec2-> | ||
| + | SnapshotId => $ARGV[0] | ||
| + | ); | ||
| + | |||
| + | |||
| + | if ( $result[0] eq 1 ){ | ||
| + | #print "Good to go \n"; | ||
| + | exit 0; | ||
| + | }else{ | ||
| + | my $err = Dumper \@result; | ||
| + | open F,"> | ||
| + | print F $err; | ||
| + | close F; | ||
| + | if(system ("/ | ||
| + | unlink " | ||
| + | } | ||
| + | exit 1; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Check Snapshot status ==== | ||
| + | <code perl | snap_status.pl > | ||
| + | # This script check status of given snapshot and wait for it get completed. | ||
| + | # Author : k2patel < | ||
| + | # Please send me message for suggestion or update. | ||
| + | |||
| + | |||
| + | use Data:: | ||
| + | use strict; | ||
| + | #use Date::Calc qw( :all ); | ||
| + | |||
| + | use lib "/ | ||
| + | use Net:: | ||
| + | |||
| + | # Configuration. | ||
| + | require " | ||
| + | |||
| + | # our $ec2; | ||
| + | our $awsId; | ||
| + | our $awsKey; | ||
| + | our $awsinstance; | ||
| + | |||
| + | my($num_args) = $#ARGV + 1; | ||
| + | if ($num_args < 1) { | ||
| + | print " | ||
| + | print "This will check Snapshot Status\n"; | ||
| + | exit 1; | ||
| + | } | ||
| + | |||
| + | ## Define Access Keys for Amazon. | ||
| + | my $ec2 = Net:: | ||
| + | AWSAccessKeyId => $awsId, | ||
| + | SecretAccessKey => $awsKey | ||
| + | ); | ||
| + | |||
| + | my @result = $ec2-> | ||
| + | SnapshotId => $ARGV[0] | ||
| + | ); | ||
| + | |||
| + | while ( $result[0][0]-> | ||
| + | sleep 10; | ||
| + | } | ||
| + | |||
| + | if ( $result[0][0]-> | ||
| + | my $tst = "Good to go \n"; | ||
| + | exit 0; | ||
| + | }else{ | ||
| + | my $err = Dumper \@result; | ||
| + | open F,"> | ||
| + | print F $err; | ||
| + | close F; | ||
| + | if(system ("/ | ||
| + | unlink " | ||
| + | } | ||
| + | exit 1; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Machine DB ==== | ||
| + | |||
| + | <code bash> | ||
| + | sqlite3 machine.db | ||
| + | </ | ||
| + | |||
| + | <code sql | machine.db> | ||
| + | PRAGMA foreign_keys=OFF; | ||
| + | BEGIN TRANSACTION; | ||
| + | CREATE TABLE DESK ( v_ID, s_ID, Dte ); | ||
| + | </ | ||
| + | |||
snapshot_backup.1316550342.txt.gz · Last modified: 2020/08/10 02:29 (external edit)
