Sign in to follow this  
Followers 0
SnArF

Copy Virtual Machine using ShadowCopy

3 posts in this topic

#1 ·  Posted (edited)

This script copy's a running Virtual Machine from MS Virtual Server 2005 using ShadowCopy on a Windows 2003 server

I use ShadowCopy to minimize the downtime.

1. the VM is set to savestate

2. a shadowcopy is created

3. resume the VM

(this process takes about 1 or 2 minutes, depending on your harware)

4. create the copy

5. delete the shadowcopy

The only thing i need is a progressbar

; Copy Virtual Machine using ShadowCopy 
;#AutoIt3Wrapper_UseX64=y ;enable on x64 #include<constants.au3>
#include<array.au3> Global $ID, $Volume 

; Find the Virtual Machine
$svr = ObjCreate("Virtualserver.application", "localhost")
$vm = $svr.FindVirtualMachine("server01") 

; Setting the Virtual Machine in Savestate
$vm.save() 

; Waiting until Virtual Machine is in SaveState
Do
$state = $vm.state
Sleep(2000)
until $state = 2 

; Create a Shadowcopy
_shadowcopy() 

; resume Virtual Machine
$vm.startup() 

; Waiting until Virtual Machine is running
Do
$state = $vm.state
Sleep(2000)
Until $state = 5 

; Create a copy using SchadowCopy
DirCopy($Volume & '\virtual machines\server01', "d:\tst") 

; Removing the ShadowCopy
Run(@ComSpec & " /c vssadmin delete shadows /shadow=" & $ID & " /quiet", @WorkingDir, @SW_HIDE) 

Func _shadowcopy()
Global $StdOut, $StdErr

; Create the ShadowCopy
$commandline = "vssadmin create shadow /for=d:"
$init = Run(@ComSpec & " /c " & $commandline, @WorkingDir, @SW_HIDE, $STDOUT_CHILD)

While 1
  $StdOut &= StdoutRead($init)
  If @error Then ExitLoop
WEnd  $StdOutRecs = StringSplit($StdOut, @CRLF, 1)

; Create ShadowID and ShadowVolume
$searchID = _ArraySearch($StdOutRecs, "Shadow Copy ID:", 0, 0, 0, 1)
$searchVol = _ArraySearch($StdOutRecs, "Shadow Copy Volume Name:", 0, 0, 0, 1)
$shadowID = stringSplit($StdOutRecs[$searchID],':')
$shadowVol = StringSplit($StdOutRecs[$searchVol],':')

; removing Whitespaces
$ID = StringStripWS($shadowID[2], 8)
$Volume= StringStripWS($shadowVol[2], 8)
EndFunc
Edited by SnArF

My scripts: _ConsoleWriteLog | _FileArray2D

 

 

 

Share this post


Link to post
Share on other sites



Hi there,

I think you request very useful and interesting.

Do you want a progress bar for whole the process or only por the file copy?

Cheers


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

Only for the FileCopy.

I have a script that creates a progressbar for filecopy using kernel32.dll

Is it possible to modify this script to copy from the shadowvolume i created and showing de progressbar for the dir copy?

Hi there,

I think you request very useful and interesting.

Do you want a progress bar for whole the process or only por the file copy?

Cheers

Copy with progressbar script

$FileCopy = _CopyWithProgress("c:\file.txt", "d:\file.txt")
 ; Check return code to see if file copied correctly
If $FileCopy = 0 Then
    MsgBox(0,"Copy Complete", "File Copy Successful.")
Else
    MsgBox(16,"Copy Complete", "File Copy Failed.")
EndIf 
Func _CopyWithProgress($inSource, $inDest) ; Create dll hooks for Progress Update
    If Not IsDeclared("callback") Then Local $callback = DllCallbackRegister('__Progress', 'int', 'uint64;uint64;uint64;uint64;dword;dword;ptr;ptr;str')
    If Not IsDeclared("ptr") Then Local $ptr = DllCallbackGetPtr($callback) 
; Check that file open was successful
    If Not FileExists($inSource) Then
        MsgBox(16, "Error", "Unable to open source file: " & $inSource)
        Exit
    EndIf ; Get Destination Directory - without filename
    $DestDir = StringLeft($inDest, StringInStr($inDest, "\", "", -1)) 
; Check destination directory exists and create it if it doesn't try to create it
    If Not FileExists($DestDir) Then
        $DirCreate = DirCreate($DestDir)
        If $DirCreate = 0 Then MsgBox(16, "Error", "Could not create destination directory: " & $DestDir)
    EndIf ; Show the progress bar
    ProgressOn("File Copy", "Copying...", "Source: " & $inSource & @LF & "Dest:  " & $inDest)   $ret = DllCall('kernel32.dll', 'int', 'CopyFileExA', 'str', $inSource, 'str', $inDest, 'ptr', $ptr, 'str', '', 'int', 0, 'int', 0) 
; Hide the progress bar
    ProgressOff()   If $ret[0] <> 0 Then
    ; Success
        Return 0
    Else
    ; Fail
        Return 1
    EndIf EndFunc 
Func __Progress($FileSize, $BytesTransferred, $StreamSize, $StreamBytesTransferred, $dwStreamNumber, $dwCallbackReason, $hSourceFile, $hDestinationFile, $lpData)   ProgressSet(Round($BytesTransferred / $FileSize * 100, 0)) EndFunc

My scripts: _ConsoleWriteLog | _FileArray2D

 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0