Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9268528
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类:

2012-12-24 09:40:27

PowerCLI: A Simple VM Backup Script 2012-05-28 11:14:47
标签:PowerCLI
Just lately I've been doing a lot of work in a Lab environment. Some of the work I'm doing is quite important to me so I decided to workout a way I can backup my VM's onto a backup device. To my surprise this was pretty simple to do.
Here is how I decided the script should function:
  1. Send myself an email telling me that the backup process has started
  2. Import the name of the VM to be backed up and the destination datastore from a CSV file
  3. Create a Snapshot of the VM I want to backup
  4. Create a Clone'd VM from the Snapshot
    • Place the backup onto my Backup Datastore
    • Name the backup -
    • Thin provision the backup
  5. Remove the Snapshot from the master VM
  6. Send myself an email telling me that the backup process has completed
So that's what I wanted to do, this is what I ended up with.
Backup Script
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
# Import Backup CSV
$backupinfo = Import-Csv C:\scripts\mybackups.csv
#Set Date format for clone names
$date = Get-Date -Format "yyyyMMdd"
#Set Date format for emails
$time = (Get-Date -f "HH:MM")
#Connect to vCenter
Connect-VIServer ""
foreach ($customer in $backupinfo)
{
$vm = Get-VM $customer.MasterVM
#Send Start Email
C:\scripts\backupstartedemail.ps1
# Create new snapshot for clone
$cloneSnap = $vm | New-Snapshot -Name "Clone Snapshot"
# Get managed object view
$vmView = $vm | Get-View
# Get folder managed object reference
$cloneFolder = $vmView.parent
# Build clone specification
$cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
$cloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot
# Make linked disk specification
$cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
$cloneSpec.Location.Datastore = (Get-Datastore -Name $customer.BackupDS | Get-View).MoRef
$cloneSpec.Location.Transform = [Vmware.Vim.VirtualMachineRelocateTransformation]::sparse
$cloneName = "$vm-$date"
# Create clone
$vmView.CloneVM( $cloneFolder, $cloneName, $cloneSpec )
# Write newly created VM to stdout as confirmation
Get-VM $cloneName
# Remove Snapshot created for clone
Get-Snapshot -VM (Get-VM -Name $customer.MasterVM) -Name $cloneSnap | Remove-Snapshot -confirm:$False
#Send Complete Email
C:\scripts\backupcompletedemail.ps1
}
#Disconnect from vCentre
Disconnect-VIServer -Confirm:$false
Send Started Email Script
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#Set Date format for emails
$timestart = (Get-Date -f "HH:MM")
$emailFrom = ""
$emailTo = ""
$subject = "[$vm - Backup Started]"
$body = "Backup Details
-------------
VM Name:",$vm,"
Clone Name:","$vm-$date","
Target Datastore:", $customer.BackupDS,"
Time Started:", $timestart
$smtpServer = ""
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom,$emailTo,$subject,$body)
Send Completed Email Script
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#Set Date format for emails
$timecomplete = (Get-Date -f "HH:MM")
$emailFrom = ""
$emailTo = ""
$subject = "[$vm - Backup Complete]"
$body = "Backup Details
-------------
VM Name:",$vm,"
Clone Name:","$vm-$date","
Target Datastore:", $customer.BackupDS,"
Time Started:", $timestart,"
Time Completed:", $timecomplete
$smtpServer = ""
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom,$emailTo,$subject,$body)
Content of CSV
The content of the csv file is very simple. This is what mine looks like:
MasterVM,BackupDS
VM1,BackupDataStore
VM2,BackupDataStore
Summary
So as you can see, it's simple, but very effective. Let me know your thoughts/Idea's on this could be improved.
 
阅读(1483) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~