Skip to content

Instantly share code, notes, and snippets.

@marvin
Last active November 3, 2022 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvin/5141575 to your computer and use it in GitHub Desktop.
Save marvin/5141575 to your computer and use it in GitHub Desktop.
esxi backup script - includes email notifications etc
#
#
#
use strict;
use warnings;
use MIME::Lite;
################-- VARIABLES --############################
my $host = "10.10.10.1"; # ESXI HOST! IP or DNS NAME
my $url = "https://".$host.":443/sdk/vimService";
my $username = "root";
my $password = "my-esxi-passwort";
my $snapshotname = "BackupSnap"; # Name of Snapshot
my $DSPath = "[datastore1]"; # Name of the Datastore
my @VMNames;
# ARRAY OF VIRTUAL Machines TO BACKUP
$VMNames[0] = "my-vm-1";
$VMNames[1] = "my-vm-2";
# $VMNames[2] = "";
# $VMNames[3] = "";
# $VMNames[4] = "";
my $RCLIPath = "C:/Progra~2/VMware/VMware~3"; # MUST BE WINDOWS SHORT PATH!
my $DestPath = "B:/"; # DESTINATION DRIVE
my @recipients = ('recipient1@mymail.com','recipient2@mymail.com');
my $backup_report_msg = "Folgende Server und Dateien wurden gesichert:\n\n";
###########################################################
################-- MAIN METHOD --############################
print actualtime();
print " ***** Script Start *************************\n\n";
print " ----- Sending Start Report Email -----\n\n";
&emailReportStart();
print actualtime();
print " ----- Create Snapshots of running VM's -----";
print "\n\n";
system("perl $RCLIPath/Perl/apps/vm/snapshotmanager.pl --url $url --username $username --password $password --operation create --powerstatus poweredOn --snapshotname $snapshotname");
print "\n\n";
print actualtime();
print " ----- Copy VM files to local storage -----";
print "\n\n";
my $i = 0;
foreach my $vm (@VMNames)
{
$backup_report_msg = $backup_report_msg . "-> " . $vm . "\n";
my @cache = `perl $RCLIPath/bin/vifs.pl --url $url --username $username --password $password --dir \"$DSPath $VMNames[$i]\"`;
#run as long the cache array has data and save the value everytime in $filename
foreach my $filename (@cache)
{
chomp($filename);
if($filename =~ /./ && $filename !~ /Content Listing/ && $filename !~ /------/ && $filename !~ /\.log/ && $filename !~ /.vswp/ && $filename !~ /.vmsn/ && $filename !~ /-delta/)
{
chomp($filename);
system("perl $RCLIPath/bin/vifs.pl --url $url --username $username --password $password --get \"$DSPath $VMNames[$i]/$filename\" \"$DestPath$VMNames[$i]/$filename\"");
print "\n";
$backup_report_msg = $backup_report_msg . "---> " . $filename . " stored in $DestPath$VMNames[$i]/$filename\n";
}
}
$i++;
}
print "\n\n";
print actualtime();
print " ----- Remove Snapshots of running VM's -----";
print "\n\n";
system("perl $RCLIPath/Perl/apps/vm/snapshotmanager.pl --url $url --username $username --password $password --operation remove --powerstatus poweredOn --snapshotname $snapshotname --children 1");
print "\n\n";
print " ----- Sending Start Report Email -----\n\n";
&emailReportEnd();
print actualtime();
print " ***** Script End ***************************";
###########################################################
################-- FUNCTIONS --############################
sub actualtime
{
my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat,
$Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
my $CTIME_String = localtime(time);
$Monat+=1;
$Jahrestag+=1;
$Monat = $Monat < 10 ? $Monat = "0".$Monat : $Monat;
$Monatstag = $Monatstag < 10 ? $Monatstag = "0".$Monatstag : $Monatstag;
$Stunden = $Stunden < 10 ? $Stunden = "0".$Stunden : $Stunden;
$Minuten = $Minuten < 10 ? $Minuten = "0".$Minuten : $Minuten;
$Sekunden = $Sekunden < 10 ? $Sekunden = "0".$Sekunden : $Sekunden;
$Jahr+=1900;
return "$Jahr-$Monat-$Monatstag $Stunden:$Minuten:$Sekunden";
}
sub emailReportStart
{
my $t = actualtime();
my $subject = "ESX Backup Started - " . $t;
my $body = "Die folgenden VM werden von " . $host . " gesichert:\n\n";
foreach my $vm (@VMNames) {
$body = $body . "\n" . "-> " . $vm
}
&sendEmail($subject, $body);
}
sub emailReportEnd
{
my $t = actualtime();
my $subject = "ESX Backup Completed - " . $t;
&sendEmail($subject, $backup_report_msg);
}
sub sendEmail
{
my $subject = $_[0];
my $body = $_[1];
foreach my $recp (@recipients) {
my $msg = MIME::Lite->new(
From => 'from@mymail.com',
To => $recp,
Subject => $subject,
Type => 'multipart/mixed'
);
$msg->attach (
Type => 'TEXT',
Data => $body
);
$msg->send('smtp','my-mail-server');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment