시스템 관리환경을 더할 수 있겠다.
From rhev-wiki
Note: This script was written on a RHEV 2.2 system.
This tutorial describes how to schedule a PowerShell script that will take a snapshot of a running virtual machine in RHEV. Note that we have to power down the virtual machine first, due to the fact that as of RHEV 2.2 the capability to create live snapshots of a running virtual machine does not yet exist.
All of these steps are performed on the Windows server that is acting as the RHEV Manager server.
1. Create a directory, such as E:\scripts, on your RHEV management server
2. Using Wordpad or Notepad, create a new file called "snapshot.ps1" in your scripts directory
We will now add the lines of the main script one at a time explaining each one:
3. The first line of the script authenticates the Active Directory user that has administrative rights to the RHEV environment. Note that the password for the account is stored in plain text here, so bear that in mind if you still want to go this route.
Login-User -UserName "rhevadmin" -Password "password" -Domain "example.com"
4. The next line in the script selects the virtual machine that we will be taking a snapshot of (we're using our RHEL6-beta vm as an example), and we're setting up a couple of other variables for the name of the snapshot that we will be creating
$vmName = "RHEL6-beta"
$vm = select-vm ("name=$vmName")
$date = get-date
$description = ($vmName + '_' + $date)
5. The next line will actually power down the virtual machine.
Shutdown-Vm -VmObject $vm
6. Next, we enter into a loop that constantly checks the Status of the virtual machine every two seconds. We don't break out of the loop until the status is Down (to give our virtual machine time to shut off).
do
{
Start-Sleep -seconds 2
$guest = Select-Vm ("name=$vmName")
$status = $guest.Status
} until ($status -eq "Down")
7. Next, we create our snapshot of the virtual machine
Create-Snapshot -VmId $vm.VmId -Description $description
8. Here, we enter another loop. We need to wait for the snapshot to finish before we attempt to restart the virtual machine. The virtual machine's status while the snapshot is being created is "Image Locked," so we need to wait for that status to change.
do
{
Start-Sleep -seconds 2
$guest = Select-Vm ("name=$vmName")
$status = $guest.Status
} until ($status -ne "Image Locked")
9. Now, we power the virtual machine back up
Start-Vm -VmObject $vm
10. Save and close the snapshot.ps1 script
11. Using Wordpad or Notepad, we will now create a script named run_snapshot.bat that Windows Scheduled Tasks can use to run the PowerShell script (since we can't run the PowerShell script directly from Scheduled Tasks itself). This script file contains just the one line below:
powershell -command "& 'E:\scripts\snapshot.ps1' "
Save and close this file in your scripts directory.
Now we'll create the Scheduled Task that will run our snapshot script.
12. On your RHEV management server, go to Start->All Programs->Accessories->System Tools->Scheduled Tasks
13. Double click on "Add Schedule Task." The Scheduled Task Wizard will now display.
14. Click Next
15. Click on the Browse button, and navigate to your scripts directory, and click on the run_snapshot.bat file then click Open.
16. Select how often you want the script to run, then click Next
17. Configure when you want the script to run, then click Next
18. Enter in the administrator's credentials, then click Next
19. Click Finish
20. You should now see the run_snapshot job in the Scheduled Tasks window. Repeat these steps for each virtual machine that you wish to automatically create snapshots of.
Getting Email Notifications from the Snapshot Script
We'll make some additions to the script here so that after the snapshot is created, we'll get an email notification and a listing of the snapshots for the VM itself.
Add the following lines below the Start-Vm -VmObject $vm line in the script (substituting your email addresses and mail server name)
$From = "rhevadmin@example.com"
$To = "rhevadmin@example.com"
$Subject = "VM snapshot notification for $vmName"
$Body = get-snapshot -VmObject $vm
$SMTPServer = "smtp.example.com"
$mail = new-object Net.Mail.SmtpClient($SMTPServer)
$mail.Send($From, $To, $Subject, $Body)
So now the full script should contain
Login-User -UserName "rhevadmin" -Password "password" -Domain "example.com"
$vmName = "RHEL6-beta"
$vm = select-vm ("name=$vmName")
$date = get-date
$description = ($vmName + '_' + $date)
Shutdown-Vm -VmObject $vm
do
{
Start-Sleep -seconds 2
$guest = Select-Vm ("name=$vmName")
$status = $guest.Status
} until ($status -eq "Down")
Create-Snapshot -VmId $vm.VmId -Description $description
do
{
Start-Sleep -seconds 2
$guest = Select-Vm ("name=$vmName")
$status = $guest.Status
} until ($status -ne "Image Locked")
Start-Vm -VmObject $vm
$From = "rhevadmin@example.com"
$To = "rhevadmin@example.com"
$Subject = "VM snapshot notification for $vmName"
$Body = get-snapshot -VmObject $vm
$SMTPServer = "smtp.example.com"
$mail = new-object Net.Mail.SmtpClient($SMTPServer)
$mail.Send($From, $To, $Subject, $Body)
After the snapshot has been created, you'll get an email notification while the vm boots back up.