Advertisement
Guest User

Untitled

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