Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- # [2016] Infobl0x, Inc.
- # All Rights Reserved.
- # NOTICE: All information contained herein is, and remains the property of Infoblox, Inc. and its suppliers, if any.
- # The intellectual and technical concepts contained herein are proprietary to Infoblox, Inc and its suppliers and may
- # be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law.
- # Dissemination of this information or reproduction of this material is strictly forbidden unless prior written
- # permission is obtained from Infoblox, Inc.
- use strict;
- use warnings;
- use Data::Dumper;
- $Data::Dumper::Indent = 3;
- use Infoblox;
- my $login = (getpwuid $>);
- if ($login eq 'root') {
- print "This script must NOT be run as root, remove the 'sudo' command.\n";
- exit(66);
- }
- my $ibhost = "10.100.0.100";
- my $ibuser = "api";
- my $ibpass = "Infobl0x";
- # Setting up print styles
- my $error = "[\e[1;31m><\e[0m]";
- my $good = "[\e[0;32mOK\e[0m]";
- my $info = "[\e[1;37m--\e[0m]";
- my $warn = "[\e[1;33m!!\e[0m]";
- my $empty = " ";
- my $dbug = "[\e[0;35m??\e[0m]";
- # Functions that handle the print styles
- sub errorprint($) { print $error . " " . $_[0]; }
- sub goodprint($) { print $good . " " . $_[0]; }
- sub infoprint($) { print $info . " " . $_[0]; }
- sub warnprint($) { print $warn . " " . $_[0]; }
- sub emptyprint($) { print $empty . " " . $_[0]; }
- sub debugprint($) { if ((defined $ARGV[0]) && ($ARGV[0] eq "--debug")) { print $dbug . " " . $_[0];}}
- sub trim($) {
- my $string = $_[0];
- $string =~ s/^\s+//;
- $string =~ s/\s+$//;
- return $string;
- }
- my $SESSION = Infoblox::Session->new(
- 'master' => $ibhost,
- 'username' => $ibuser,
- 'password' => $ibpass,
- );
- infoprint "Connecting to the Infoblox Grid...\n";
- if ($SESSION->status_code()) {
- my $result = $SESSION->status_code();
- my $response = $SESSION->status_detail();
- errorprint "API connection failed (" . $ibuser . '@' . $ibhost . ")\n";
- errorprint $response . " (" . $result . ")\n";
- } else {
- goodprint "API connection established (" . $ibuser . '@' . $ibhost . ")\n";
- goodprint "Server Version: " . $SESSION->server_version() . "\n";
- print "\n";
- infoprint "Checking backup directory...\n";
- my $backupDir = "/home/training/Downloads/GridBackups";
- if (!-d $backupDir) {
- warnprint "Backup directory " . $backupDir . " does not exist\n";
- errorprint "Aborting.\n";
- exit(66);
- } else {
- goodprint "Backup directory " . $backupDir . " exists\n";
- }
- print "\n";
- infoprint "Listing Current Backups...\n";
- opendir(DIR, $backupDir) or die $!;
- my @files = grep { /tar\.gz$/ && -f "$backupDir/$_" } readdir(DIR);
- closedir(DIR);
- if (scalar(@files) > 0) {
- @files = sort @files;
- my $i = 0;
- foreach my $file (@files) {
- $i++;
- my $paddednumber = sprintf("% 3d", $i);
- print $paddednumber . ". " . $file . "\n";
- }
- } else {
- warnprint "No backup files found\n";
- errorprint "Aborting.\n";
- exit(66);
- }
- print "\n";
- infoprint "Select Backup...\n";
- my $validInput = 0;
- my $backupNumber = -1;
- while ($validInput == 0) {
- print "\nPlease enter the backup number to restore (q to exit): ";
- $backupNumber = <STDIN>;
- chomp $backupNumber;
- if (lc($backupNumber) eq "q") {
- warnprint "Exiting at user's request.\n";
- exit(99);
- }
- if ($backupNumber =~ /^\d+$/) {
- $backupNumber--;
- if (($backupNumber >= 0) && ($backupNumber < scalar(@files))) {
- $validInput = 1;
- } else {
- $backupNumber++;
- warnprint "'" . $backupNumber . "' is not a valid backup number\n";
- }
- } else {
- warnprint "'" . $backupNumber . "' is not a valid backup number\n";
- }
- }
- $validInput = 0;
- while ($validInput == 0) {
- print "\nPreparing to restore file " . $files[$backupNumber] . "\n";
- print " Do you want to continue (y/n): ";
- my $response = <STDIN>;
- chomp $response;
- if (lc($response) eq "n") {
- warnprint "Exiting at user's request.\n";
- exit(99);
- } elsif (lc($response) eq "y") {
- $validInput = 1;
- } else {
- warnprint "'" . $response . "' is not a valid response\n";
- }
- }
- print "\n";
- infoprint "Restoring Backup...\n";
- my $restoreStatus = $SESSION->import_data(
- 'type' => "backup",
- 'force' => "true",
- 'path' => $backupDir . "/" . $files[$backupNumber],
- );
- if ($restoreStatus) {
- goodprint "Restore from " . $files[$backupNumber] . " was successful\n";
- emptyprint "Please wait a few minutes while the Grid restarts\n";
- } else {
- my $result = $SESSION->status_code();
- my $response = $SESSION->status_detail();
- warnprint "Restore from " . $files[$backupNumber] . " failed\n";
- errorprint $response . " (" . $result . ")\n";
- exit(66);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement