Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.08 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use REST::Client;
  6. use MIME::Base64;
  7. use JSON;
  8. use Data::Dumper;
  9. use Getopt::Std;
  10.  
  11. # NOTE: THIS SCRIPT REQUIRES XTREMIO VERSION 4.0.2 is later
  12. #
  13. # The snapshot set name is renamed as a part of the refresh operation, so
  14. # a temporary name is used, and then the set is renamed back to the
  15. # original name.
  16. #
  17. # Scott Howard, scott.howard@emc.com
  18. # Daniel Pum, daniel.pum@emc.com
  19.  
  20. my %opts;
  21.  
  22. getopts('v', \%opts);
  23.  
  24. my $verbose=$opts{v};
  25.  
  26. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #
  27. my $CLUSTER="cluster";
  28. my @VGS=("prdvg", "prdjrnvg", "prdinstvg");
  29. # Assumption is that each CG is VG-cg
  30. # Assumption is that each SS is VG-ssro and VG-ssrw
  31.  
  32. my $XMSUSER="username";
  33. my $XMSPASS="password";
  34. my $XMS="hostname-or-IP";
  35.  
  36. my $DELETEDELAY=60;
  37. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #
  38.  
  39. #
  40. # Check the response from all API calls, and exit if they fail
  41. #
  42. sub checkerr($) {
  43.     my ($cl) = @_;
  44.  
  45.     my $respcode=$cl->responseCode();
  46.     return 0 if ($respcode>=200 && $respcode <300);
  47.  
  48.     # Error message is normally JSON, but sometimes it isnt...
  49.     my $msg = $cl->responseContent();
  50.     if ($msg =~ /^{/) {
  51.         $msg = from_json($msg)->{message};
  52.     }
  53.     print STDERR "Error - $msg (response code $respcode)\n";
  54.     exit(2);
  55. }
  56.  
  57. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #
  58.  
  59.  
  60. open(my $fh, '>', 'epic-snapshot-script.log') or die "Could not write to epic-snapshot-script.log\n";
  61.  
  62. ########
  63. # Setup the REST API connection to XMS
  64. ########
  65. $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
  66. my $client = REST::Client->new();
  67. my $headers = {Authorization => "Basic ".encode_base64($XMSUSER.":".$XMSPASS), "Content-Type" => 'application/json'};
  68.  
  69.  
  70. ########
  71. # Freeze Cache
  72. ########
  73. print $fh "Freeze Cache\n";
  74. #my $result = `ssh EPICUSER@EPICHOST /epic/prd/bin/instfreeze`;
  75. my $result = "\n";
  76. print $fh $result . "\n\n";
  77.  
  78.  
  79. ########
  80. # Freeze filesystems (only for AIX)
  81. ########
  82. print $fh "Freeze JFS filesystems\n";
  83. #$result = `ssh EPICUSER@EPICHOST /epic/prd/bin/jfs-freeze-script`;
  84. print $fh $result . "\n\n";
  85.  
  86.  
  87. ########
  88. # Refresh the RO snapshot set, WITHOUT the no-backup option
  89. # Loop through all the volume groups - one consistency group per host volume group
  90. ########
  91.  
  92. foreach ( @VGS ) {
  93.   my $CGNAME = $_ . "-cg";
  94.   my $SSRO = $_ . "-ssro";
  95.   my $SSRONEW = $_ . "-ssro-new";
  96.   my $SSROOLD = $_ . "-ssro-old";
  97.  
  98.   print $fh "Refresh RO snapshot set $SSRO from consistency group $CGNAME\n";
  99.   my %body= ('cluster-id' => $CLUSTER,
  100.       'from-consistency-group-id' => $CGNAME,
  101.       'to-snapshot-set-id' => $SSRO,
  102.       'snapshot-set-name' => $SSRONEW,
  103.   );
  104.  
  105.   $client->POST("https://$XMS/api/json/v2/types/snapshots", encode_json(\%body), $headers);
  106.   checkerr($client);
  107.   my $resp = from_json($client->responseContent());
  108.   print $fh Dumper($resp) . "\n\n";
  109. }
  110.  
  111.  
  112. ########
  113. # Thaw filesystems (only for AIX)
  114. ########
  115. print $fh "Thaw JFS filesystems\n";
  116. #$result = `ssh EPICUSER@EPICHOST /epic/prd/bin/jfs-thaw-script`;
  117. print $fh $result . "\n\n";
  118.  
  119.  
  120. ########
  121. # Thaw Cache
  122. ########
  123. print $fh "Thaw Cache\n";
  124. #$result = `ssh EPICUSER@EPICHOST /epic/prd/bin/instfreeze`;
  125. print $fh $result . "\n\n";
  126.  
  127.  
  128. ########
  129. # Rotate RO snapshots sets. Current becomes old, new becomes current
  130. ########
  131. foreach ( @VGS ) {
  132.   my $CGNAME = $_ . "-cg";
  133.   my $SSRO = $_ . "-ssro";
  134.   my $SSRONEW = $_ . "-ssro-new";
  135.   my $SSROOLD = $_ . "-ssro-old";
  136.  
  137.   print $fh "Rename snapshot set $SSRO to $SSROOLD\n";
  138.   my %body= ('new-name' => $SSROOLD,);
  139.  
  140.   $client->PUT("https://$XMS/api/json/v2/types/snapshot-sets?name=$SSRO&cluster-name=$CLUSTER", encode_json(\%body), $headers);
  141.   checkerr($client);
  142.  
  143.   print $fh "Rename snapshot set $SSRONEW to $SSRO\n";
  144.   %body= ('new-name' => $SSRO,);
  145.  
  146.   $client->PUT("https://$XMS/api/json/v2/types/snapshot-sets?name=$SSRONEW&cluster-name=$CLUSTER", encode_json(\%body), $headers);
  147.   checkerr($client);
  148. }
  149.  
  150.  
  151. ########
  152. # Refresh the RW snapshot set from the RO snapshot set, WITHOUT the no-backup option
  153. ########
  154. foreach ( @VGS ) {
  155.   my $CGNAME = $_ . "-cg";
  156.   my $SSRO = $_ . "-ssro";
  157.   my $SSRW = $_ . "-ssrw";
  158.   my $SSRWNEW = $_ . "-ssrw-new";
  159.   my $SSRWOLD = $_ . "-ssrw-old";
  160.  
  161.   print $fh "Refresh RW snapshot set $SSRW from snapshot set $SSRO\n";
  162.   my %body= ('cluster-id' => $CLUSTER,
  163.       'from-snapshot-set-id' => $SSRO,
  164.       'to-snapshot-set-id' => $SSRW,
  165.       'snapshot-set-name' => $SSRWNEW,
  166.   );
  167.  
  168.   $client->POST("https://$XMS/api/json/v2/types/snapshots", encode_json(\%body), $headers);
  169.   checkerr($client);
  170.   my $resp = from_json($client->responseContent());
  171.   print $fh Dumper($resp) . "\n\n";
  172. }
  173.  
  174.  
  175. ########
  176. # Rotate RW snapshots sets. Current becomes old, new becomes current
  177. ########
  178. foreach ( @VGS ) {
  179.   my $CGNAME = $_ . "-cg";
  180.   my $SSRW = $_ . "-ssrw";
  181.   my $SSRWNEW = $_ . "-ssrw-new";
  182.   my $SSRWOLD = $_ . "-ssrw-old";
  183.  
  184.   print $fh "Rename snapshot set $SSRW to $SSRWOLD\n";
  185.   my %body= ('new-name' => $SSRWOLD,);
  186.  
  187.   $client->PUT("https://$XMS/api/json/v2/types/snapshot-sets?name=$SSRW&cluster-name=$CLUSTER", encode_json(\%body), $headers);
  188.   checkerr($client);
  189.  
  190.   print $fh "Rename snapshot set $SSRWNEW to $SSRW\n";
  191.   %body= ('new-name' => $SSRW,);
  192.  
  193.   $client->PUT("https://$XMS/api/json/v2/types/snapshot-sets?name=$SSRWNEW&cluster-name=$CLUSTER", encode_json(\%body), $headers);
  194.   checkerr($client);
  195. }
  196.  
  197.  
  198. ########
  199. # Get a list of RO snapshot volumes to delete
  200. ########
  201. foreach ( @VGS ) {
  202.   my $CGNAME = $_ . "-cg";
  203.   my $SSRO = $_ . "-ssro";
  204.   my $SSRONEW = $_ . "-ssro-new";
  205.   my $SSROOLD = $_ . "-ssro-old";
  206.  
  207.   print $fh "Get list of RO snapshot volumes to delete\n";
  208.   $client->GET("https://${XMS}/api/json/v2/types/snapshot-sets?name=$SSROOLD&cluster-name=$CLUSTER&prop=vol-list", $headers);
  209.   checkerr($client);
  210.   my $resp = from_json($client->responseContent());
  211.   print $fh Dumper($resp) . "\n\n";
  212.  
  213.   my $volumes = $resp->{'content'}{'vol-list'};
  214.   foreach my $volume (@$volumes){
  215.       my ($id, $name, $number) = @$volume;
  216.       print $fh "Deleting snapshot volume $name\n";
  217.  
  218.       $client->DELETE("https://$XMS/api/json/v2/types/volumes?name=$name&cluster-name=$CLUSTER", $headers);
  219.       checkerr($client);
  220.  
  221.       sleep $DELETEDELAY;
  222.   }
  223. }
  224.  
  225.  
  226. ########
  227. # Get a list of RW snapshot volumes to delete
  228. ########
  229. foreach ( @VGS ) {
  230.   my $CGNAME = $_ . "-cg";
  231.   my $SSRW = $_ . "-ssrw";
  232.   my $SSRWNEW = $_ . "-ssrw-new";
  233.   my $SSRWOLD = $_ . "-ssrw-old";
  234.  
  235.   print $fh "Get list of RW snapshot volumes to delete\n";
  236.   $client->GET("https://${XMS}/api/json/v2/types/snapshot-sets?name=$SSRWOLD&cluster-name=$CLUSTER&prop=vol-list", $headers);
  237.   checkerr($client);
  238.   my $resp = from_json($client->responseContent());
  239.   print $fh Dumper($resp) . "\n\n";
  240.  
  241.   my $volumes = $resp->{'content'}{'vol-list'};
  242.   foreach my $volume (@$volumes){
  243.       my ($id, $name, $number) = @$volume;
  244.       print $fh "Deleting snapshot volume $name\n";
  245.  
  246.       $client->DELETE("https://$XMS/api/json/v2/types/volumes?name=$name&cluster-name=$CLUSTER", $headers);
  247.       checkerr($client);
  248.  
  249.       sleep $DELETEDELAY;
  250.   }
  251. }
  252.  
  253.  
  254. close $fh;
  255. exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement