Advertisement
Guest User

Infobl0x - restoreGrid.pl

a guest
Oct 27th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.68 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. # [2016] Infobl0x, Inc.
  4. # All Rights Reserved.
  5. # NOTICE: All information contained herein is, and remains the property of Infoblox, Inc. and its suppliers, if any.
  6. # The intellectual and technical concepts contained herein are proprietary to Infoblox, Inc and its suppliers and may
  7. # be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law.
  8. # Dissemination of this information or reproduction of this material is strictly forbidden unless prior written
  9. # permission is obtained from Infoblox, Inc.
  10.  
  11. use strict;
  12. use warnings;
  13. use Data::Dumper;
  14. $Data::Dumper::Indent = 3;
  15. use Infoblox;
  16.  
  17. my $login = (getpwuid $>);
  18. if ($login eq 'root') {
  19.     print "This script must NOT be run as root, remove the 'sudo' command.\n";
  20.     exit(66);
  21. }
  22.  
  23. my $ibhost = "10.100.0.100";
  24. my $ibuser = "api";
  25. my $ibpass = "Infobl0x";
  26.  
  27. # Setting up print styles
  28. my $error = "[\e[1;31m><\e[0m]";
  29. my $good = "[\e[0;32mOK\e[0m]";
  30. my $info = "[\e[1;37m--\e[0m]";
  31. my $warn = "[\e[1;33m!!\e[0m]";
  32. my $empty = "    ";
  33. my $dbug = "[\e[0;35m??\e[0m]";
  34.  
  35. # Functions that handle the print styles
  36. sub errorprint($) { print $error . " " . $_[0]; }
  37. sub goodprint($) { print $good . " " . $_[0]; }
  38. sub infoprint($) { print $info . " " . $_[0]; }
  39. sub warnprint($) { print $warn . " " . $_[0]; }
  40. sub emptyprint($) { print $empty . " " . $_[0]; }
  41. sub debugprint($) { if ((defined $ARGV[0]) && ($ARGV[0] eq "--debug")) { print $dbug . " " . $_[0];}}
  42. sub trim($) {
  43.     my $string = $_[0];
  44.     $string =~ s/^\s+//;
  45.     $string =~ s/\s+$//;
  46.     return $string;
  47. }
  48.  
  49. my $SESSION = Infoblox::Session->new(
  50.     'master'   => $ibhost,
  51.     'username' => $ibuser,
  52.     'password' => $ibpass,
  53. );
  54.  
  55. infoprint "Connecting to the Infoblox Grid...\n";
  56.  
  57. if ($SESSION->status_code()) {
  58.  
  59.     my $result = $SESSION->status_code();
  60.     my $response = $SESSION->status_detail();
  61.  
  62.     errorprint "API connection failed (" . $ibuser . '@' . $ibhost . ")\n";
  63.     errorprint $response . " (" . $result . ")\n";
  64.  
  65. } else {
  66.  
  67.     goodprint "API connection established (" . $ibuser . '@' . $ibhost . ")\n";
  68.     goodprint "Server Version: " . $SESSION->server_version() . "\n";
  69.  
  70.     print "\n";
  71.     infoprint "Checking backup directory...\n";
  72.  
  73.     my $backupDir = "/home/training/Downloads/GridBackups";
  74.  
  75.     if (!-d $backupDir) {
  76.         warnprint "Backup directory " . $backupDir . " does not exist\n";
  77.         errorprint "Aborting.\n";
  78.         exit(66);
  79.     } else {
  80.         goodprint "Backup directory " . $backupDir . " exists\n";
  81.     }
  82.  
  83.     print "\n";
  84.     infoprint "Listing Current Backups...\n";
  85.  
  86.     opendir(DIR, $backupDir) or die $!;
  87.     my @files = grep { /tar\.gz$/ && -f "$backupDir/$_" } readdir(DIR);
  88.     closedir(DIR);
  89.  
  90.     if (scalar(@files) > 0) {
  91.         @files = sort @files;
  92.         my $i = 0;
  93.         foreach my $file (@files) {
  94.             $i++;
  95.             my $paddednumber = sprintf("% 3d", $i);
  96.             print $paddednumber . ". " . $file . "\n";
  97.         }
  98.     } else {
  99.         warnprint "No backup files found\n";
  100.         errorprint "Aborting.\n";
  101.         exit(66);
  102.     }
  103.  
  104.     print "\n";
  105.     infoprint "Select Backup...\n";
  106.  
  107.     my $validInput = 0;
  108.     my $backupNumber = -1;
  109.     while ($validInput == 0) {
  110.         print "\nPlease enter the backup number to restore (q to exit): ";
  111.         $backupNumber = <STDIN>;
  112.         chomp $backupNumber;
  113.         if (lc($backupNumber) eq "q") {
  114.             warnprint "Exiting at user's request.\n";
  115.             exit(99);
  116.         }
  117.         if ($backupNumber =~ /^\d+$/) {
  118.             $backupNumber--;
  119.             if (($backupNumber >= 0) && ($backupNumber < scalar(@files))) {
  120.                 $validInput = 1;
  121.             } else {
  122.                 $backupNumber++;
  123.                 warnprint "'" . $backupNumber . "' is not a valid backup number\n";
  124.             }
  125.         } else {
  126.             warnprint "'" . $backupNumber . "' is not a valid backup number\n";
  127.         }
  128.     }
  129.  
  130.     $validInput = 0;
  131.     while ($validInput == 0) {
  132.         print "\nPreparing to restore file " . $files[$backupNumber] . "\n";
  133.         print "    Do you want to continue (y/n): ";
  134.         my $response = <STDIN>;
  135.         chomp $response;
  136.         if (lc($response) eq "n") {
  137.             warnprint "Exiting at user's request.\n";
  138.             exit(99);
  139.         } elsif (lc($response) eq "y") {
  140.             $validInput = 1;
  141.         } else {
  142.             warnprint "'" . $response . "' is not a valid response\n";
  143.         }
  144.     }
  145.  
  146.     print "\n";
  147.     infoprint "Restoring Backup...\n";
  148.  
  149.     my $restoreStatus = $SESSION->import_data(
  150.         'type'  => "backup",
  151.         'force' => "true",
  152.         'path'  => $backupDir . "/" . $files[$backupNumber],
  153.     );
  154.     if ($restoreStatus) {
  155.  
  156.         goodprint "Restore from " . $files[$backupNumber] . " was successful\n";
  157.         emptyprint "Please wait a few minutes while the Grid restarts\n";
  158.  
  159.     } else {
  160.  
  161.         my $result = $SESSION->status_code();
  162.         my $response = $SESSION->status_detail();
  163.  
  164.         warnprint "Restore from " . $files[$backupNumber] . " failed\n";
  165.         errorprint $response . " (" . $result . ")\n";
  166.         exit(66);
  167.  
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement