Sunday, June 28, 2009

VMware Backup Script

#############################################################################
#Backup VM Images
#06-05-2009
#
#Important:
# When restoring VM, ensure you restart the VM Machine
#
# *****Script IS CASE SENSITIVE*******
#############################################################################

my $url = "https://ipaddress:443/sdk/vimService "; #URL to your ESX Host
my $username = "root"; #Username
my $password = "PASSWORD"; #User password
my $snapshotname = "BackupSnap"; #Name of your Snapshot
my $DSPath = "[Store]"; #Datastore name on ESX Host, example [Store]
my @VMNames;
$VMNames[0] = "SERVERNAME"; #List your VM's - CASE SENSITIVE ***
$VMNames[1] = "SERVERNAME2";
$VMNames[2] = "SERVERNAME3";
#$VMNames[3] = "";
#$VMNames[4] = "";
#$VMNames[5] = "";
#$VMNames[6] = "";
#$VMNames[7] = "";
#$VMNames[8] = "";
#$VMNames[9] = "";
my $RCLIPath = "C:/Progra~1/VMware/VMWARE~1"; #VI Remote CLI Path (Windows: Use ONLY Short Folder Names!!!!)
my $DestPath = "X:/"; #Destination Path you like to copy to (Windows: Use ONLY Short Folder Names!!!!)

#IMPORTANT!!! -- Under DestPath must exist the VMNames Folder
#(For Example if your VMNames[0] = "ServerA" and your DestPath = "D:/": D:/ServerA/)
#--------------------------------------------------------

#call the sub function (at the bottom)
&actualtime();
print " ***** Script Start *************************\n\n";

&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";

&actualtime();
print " ----- Copy VM files to local storage -----";
print "\n\n";
my $i = 0;
#special loop for arrays. run as long the array has data
foreach (@VMNames)
{
#read all available files and save filenames in the cache-array
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)
{
#exclude uninterresting files from backup to save backup space
if($filename !~ /.log/ && $filename !~ /.vswp/ && $filename !~ /.vmsn/ && $filename !~ /-delta/)
{
#remove the "\n" at the end of $filename to prevent a error massage in log
chomp($filename)
&actualtime();
print " ----- Copy File: ";
print $filename;
#get files from VM Datastore to a local Storage
system("perl $RCLIPath/bin/vifs.pl --url $url --username $username --password $password --get \"$DSPath $VMNames[$i]/$filename\" \"$DestPath$VMNames[$i]/$filename\"");
print "\n";
}
}
$i++;
}
print "\n\n";

&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";

&actualtime();
print " ***** Script End ***************************";

#sub function to print the actual time in the log
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;

print "$Jahr-$Monat-$Monatstag $Stunden:$Minuten:$Sekunden";
}

No comments:

Post a Comment

Followers