Hyper-V virtual machine backup script…

Today my friend Marko Cepe sent me his VBS script that does great job to backup virtual machines that are running on Hyper-V.
This script does shut down virtual machine by sending shut down command to machine using integration services, then it waits virtual machine to enter stopped state, after that it does export of the machine and turns the machine back on.

Usage:

C:SKRIPTE> ExportVM.vbs VMName ExportDirectory

Example:

C:SKRIPTE> ExportVM.vbs TestVM X:BackupTestVM

Click, download and enjoy the script.

Comments appreciated…

13 thoughts on “Hyper-V virtual machine backup script…

  1. Aleksandar

    I would like to perform, with your script, export to a remote location. Where can I specify credentials? I am always getting an error “logon failure: unknown user or bad password”, line 156 of your script that reference creation of a folder. Thank you for the script !!

    Like

    Reply
  2. Marko Čepe

    Hello!

    This is a feature that will be added in the feature release.

    My current solution is a second BAT script. First it maps a remote folder using NET USE and the desired credentials, then it calls the script and at the end deletes the maped remote folder. A schedlued task executes the BAT script.

    Voila! Everything works just fine.

    EXAMPLE:
    ————
    IF EXIST “B:” NET USE B: /delete /yes
    NET USE B: \server_ipBackUp password /USER:username /YES
    C:SKRIPTEExportVM.vbs vm_name B:vm_name
    NET USE B: /delete /yes

    Note: Call the script using the drive letter of the maped remote folder.

    Like

    Reply
  3. Marko Čepe

    Recently i had some issues exporting our SBS2008 server to our NAS. The NAS was part of the domain, because we used it for some user shares. Users could connect to their shares with their AD credentials.

    What the issue was … when the SBS shutted down, the Hyper-V server (not in domain) failed to authenticate to the NAS and the export failed with error ‘The destination folder is unaccessible, unreachable or does not exist’.

    What I did is as follows … I removed the NAS from the domain and added it to a workgroup. Afther that I also added our Hyper-V servers to the same workgroup. On the NAS and on the Servers I created a local user with the same name (BackupAdmin). The user has to be in Administrators security group.

    In my previous post I wrote about a BAT script, that is being started with a task scheduler. This script maps a shared folder on the NAS using NET USE and some credentials. Use this local BackupAdmin user with NET USE.

    Now everything works fine. When the SBS is shutted down, the Hyper-V server authenticates it-self with the local user BackupAdmin in is granted acces to the shared folder on the NAS, because the same user exists also on the NAS.

    Thanks for this tip go to Luka Manojlovič. So, Luka, thank you. 😀

    NOTE: If you add a server to a workgroup or just rename the workgroup, you have to restart the server. So do this in afterhours.

    Like

    Reply
  4. Marko Čepe

    Hello!

    Sorry for the late resposnse …

    The log should be in the same directory as the script.

    So if you put the script into C:Sctipts, the log should be there to. The log filename should be ExportVirtualSystemLog.log.

    You can also specify custom path and log filename if you wish.
    – open the script with notepad or othe simple text editor
    – search for ‘ExportVirtualSystemLog.log’ (without ‘ ‘)
    – result should be in line 215. The line begins with ‘set fileStream …’
    – edit path in () – current value is (“.ExportVirtualSystemLog.log”, 8, true)

    The dot ‘.’ means same directory as the script is in. If you wish to set a different directory, just change the dot with something else, like ‘E:DataLogfiles’. You can also change the logs filename.
    The nuber 8 means ‘Open file for apeending’, you can also change this value to 1 (read only, unusable in this case, as you want to write the log, not read), or to value 2 (the contents are overwritten).

    The value ‘True’ opens the file in Unicode. You can also change this value to False (opens file in ASCII).

    Thats it … Hope it helps.

    Like

    Reply
  5. Marko Čepe

    Hello!

    I made some improvements regarding the job progress and the log file.

    In the previous version in case og exporting large VHDs it could happen that you get duplicated ‘In progress…’ entries in the log file.
    Like this:
    – In progress… 0% completed.
    – In progress… 1% completed.
    – In progress… 1% completed.
    – In progress… 1% completed.
    – In progress… 1% completed.
    – In progress… 1% completed.
    – In progress… 1% completed.
    – In progress… 2% completed.
    – In progress… 2% completed.
    – In progress… 2% completed.
    – In progress… 2% completed.

    The new vesrion of the script writes the ‘In progress…’ line only when the progress percentage really changes.

    I already sent the new version of the script to Luka Manjolovič. I hope he’ll uploads it as soon as possible.

    Like

    Reply
  6. Benjamim

    Luka,

    I can use this script on Windows Server Standard 2008? I could use on Windows 2008 Server R2 Standard, but I can’t use on this another version.

    Like

    Reply
  7. Manubz

    Hi,
    I tryied your script today but i have an error that i can’t explain..

    On line 159 char 5 : set objInParam = managementService.Methods_(“ExportVirtualSystemEx”).InParameters.SpawnInstance_()

    Have u already have this type of problem ?

    Thanks for reply.

    Manu.

    Like

    Reply
  8. Marko

    Hi Manubz.

    I don’t know what ‘non trouve’ means. I gues it means something like ‘not’ found’.

    Please try updateing your server with the latest service packs. Also try to update PowerShell to the latest version.

    I’m using this script on multiple servers – 2008 and 2008R2. They all have the latest service packs installed and I’m having no problems with this script.

    Let me know if it helped.

    Like

    Reply
  9. Marko

    Just open-up Hyper-V Manager, click on Action -> Import Virtual Machine and point the Location to the exportetd VM folder… A straight-forward procedure …

    Like

    Reply

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.