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 $hasissues = 0;
- sub timestamp {
- my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
- my $nice_timestamp = sprintf ( "%04d%02d%02d-%02d%02d%02d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
- return $nice_timestamp;
- }
- 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) {
- goodprint "Backup directory " . $backupDir . " exists\n";
- }
- elsif (-e $backupDir) {
- warnprint "File with name " . $backupDir . " exists but is not a directory\n";
- errorprint "Aborting.\n";
- exit(66);
- }
- else {
- if (mkdir($backupDir)) {
- goodprint "Backup directory " . $backupDir . " created\n";
- } else {
- warnprint "Unable to create backup directory " . $backupDir . "\n";
- errorprint "Aborting.\n";
- exit(66);
- }
- }
- print "\n";
- infoprint "Backing up the Infoblox Grid...\n";
- my $gridVersion = $SESSION->server_version();
- my $timeStamp = timestamp();
- my $fileName = "$ibhost" . "_$timeStamp" . "_$gridVersion" . ".tar.gz";
- my $backupStatus = $SESSION->export_data(
- 'type' => 'backup',
- 'path' => $backupDir . "/" . $fileName,
- );
- if ($backupStatus) {
- goodprint "Backup to " . $fileName . " was successful\n";
- } else {
- my $result = $SESSION->status_code();
- my $response = $SESSION->status_detail();
- warnprint "Backup to " . $fileName . " failed\n";
- errorprint $response . " (" . $result . ")\n";
- exit(66);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement