Advertisement
Guest User

Infobl0x backupGrid.pl

a guest
Oct 27th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.63 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. my $hasissues = 0;
  49.  
  50. sub timestamp {
  51.     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
  52.     my $nice_timestamp = sprintf ( "%04d%02d%02d-%02d%02d%02d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
  53.     return $nice_timestamp;
  54. }
  55.  
  56. my $SESSION = Infoblox::Session->new(
  57.     'master'   => $ibhost,
  58.     'username' => $ibuser,
  59.     'password' => $ibpass,
  60. );
  61.  
  62. infoprint "Connecting to the Infoblox Grid...\n";
  63.  
  64. if ($SESSION->status_code()) {
  65.  
  66.     my $result = $SESSION->status_code();
  67.     my $response = $SESSION->status_detail();
  68.  
  69.     errorprint "API connection failed (" . $ibuser . '@' . $ibhost . ")\n";
  70.     errorprint $response . " (" . $result . ")\n";
  71.  
  72. } else {
  73.  
  74.     goodprint "API connection established (" . $ibuser . '@' . $ibhost . ")\n";
  75.     goodprint "Server Version: " . $SESSION->server_version() . "\n";
  76.  
  77.     print "\n";
  78.     infoprint "Checking backup directory...\n";
  79.  
  80.     my $backupDir = "/home/training/Downloads/GridBackups";
  81.  
  82.     if (-d $backupDir) {
  83.         goodprint "Backup directory " . $backupDir . " exists\n";
  84.     }
  85.     elsif (-e $backupDir) {
  86.         warnprint "File with name " . $backupDir . " exists but is not a directory\n";
  87.         errorprint "Aborting.\n";
  88.         exit(66);
  89.     }
  90.     else {
  91.  
  92.         if (mkdir($backupDir)) {
  93.             goodprint "Backup directory " . $backupDir . " created\n";
  94.         } else {
  95.             warnprint "Unable to create backup directory " . $backupDir . "\n";
  96.             errorprint "Aborting.\n";
  97.             exit(66);
  98.         }
  99.     }
  100.  
  101.     print "\n";
  102.     infoprint "Backing up the Infoblox Grid...\n";
  103.  
  104.     my $gridVersion = $SESSION->server_version();
  105.  
  106.     my $timeStamp = timestamp();
  107.     my $fileName = "$ibhost" . "_$timeStamp" . "_$gridVersion" . ".tar.gz";
  108.  
  109.     my $backupStatus = $SESSION->export_data(
  110.         'type' => 'backup',
  111.         'path' => $backupDir . "/" . $fileName,
  112.     );
  113.     if ($backupStatus) {
  114.  
  115.         goodprint "Backup to " . $fileName . " was successful\n";
  116.  
  117.     } else {
  118.  
  119.         my $result = $SESSION->status_code();
  120.         my $response = $SESSION->status_detail();
  121.  
  122.         warnprint "Backup to " . $fileName . " failed\n";
  123.         errorprint $response . " (" . $result . ")\n";
  124.         exit(66);
  125.  
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement