Announcement

Collapse
No announcement yet.

Vbs : add mounting folder before backup

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Vbs : add mounting folder before backup

    hi everyone, here is my problem :

    I found a great script on the internet that let me backup my virtual machines (Microsoft VS 2005) every night (it put the machines into "saved state" then copy the files to a backup location).

    I would like to backup the files into my file server, the script works only if i have a folder mounted.

    How do I add to the script the command that mount to a drive the backup folder (on my file server) into the virtual server (I would like to have W: for \\fileserver) ?

    Here is the vb script :

    Code:
    ' backupvm.vbs
    ' Hasdou Update on 12/11/07
    ' Usage : backupvm.vbs <vm instance name> <backup location>
    ' e.g. cscript backupvm.vbs nameofthemachine d:\backup\
    ' Make sure that you place the \ at the end of the backup path or you'll get errors.
    '
    Option Explicit
    On Error Resume Next
    
    Dim objFSO, objVirtualServer, objVM, objSaveTask, objVHD
    
    'Connect to file system object.
    set objFSO=CreateObject("Scripting.FileSystemObject") 
    
    'Connect to Virtual Server.
    set objVirtualServer = CreateObject("VirtualServer.Application")
    
    'Get virtual machine from command-line parameter.
    set objVM = objVirtualServer.FindVirtualMachine(WScript.Arguments(0))
    
    'Save state of the virtual machine.
    set objSaveTask = objVM.Save
    
    'Loop waiting for task completion
    while not objSaveTask.isComplete
        WScript.Sleep 1000
    wend
    
    'Copy virtual hard disks and undo disks.
    for each objVHD in objVM.HardDiskConnections
        If objFSO.FileExists(objVHD.HardDisk.file) Then
            'Wscript.Echo objVHD.HardDisk.file & " " & WScript.Arguments(1)
            objFSO.CopyFile objVHD.HardDisk.file, WScript.Arguments(1)
        End If
        If objFSO.FileExists(objVHD.undoHardDisk.file) Then
            'Wscript.Echo objVHD.undoHardDisk.file & " " & WScript.Arguments(1)
            objFSO.CopyFile objVHD.undoHardDisk.file, WScript.Arguments(1)
        End If
    Next
    
    'Copy .vmc and .vsv files.
    objFSO.CopyFile objVM.File, WScript.Arguments(1)
    objFSO.CopyFile objVM.SavedStateFilePath, WScript.Arguments(1)
    
    'Start the virtual machine.
    objVM.Startup

  • #2
    Re: Vbs : add mounting folder before backup

    Please post the sourcelocation (url) of that script.
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"

    Comment


    • #3
      Re: Vbs : add mounting folder before backup

      What do you mean ? What URL ?

      Comment


      • #4
        Re: Vbs : add mounting folder before backup

        Marcel just wants to know if you wrote the script or modified it from one available on the Net. If it is a modified one, a link (URL) to the author is required to give them credit for their creation.
        1 1 was a racehorse.
        2 2 was 1 2.
        1 1 1 1 race 1 day,
        2 2 1 1 2

        Comment


        • #5
          Re: Vbs : add mounting folder before backup

          Hasdou it is not kind to re-publish a copied/pasted and even totally unchanged script to the public, where you added your name to the top and deleted the author's name.
          Originally posted by biggles77
          a link (URL) to the author is required to give them credit for their creation.
          Besites that, Virtualization is hot. This script in particular was part of a q&a discussion, so people here might be intressed in the method that is used, and like to read more about this script.

          Code:
          ' backupvm.vbs
          ' www.windowsitpro.com/Articles/ArticleID/49656/49656.html?Ad=1
          ' John Savill
          '
          '   Update on 30/Nov/07 ,by Rems
          '  Hardcoded a slash to the destination folders. 
          '
          '     script Usage :  
          '     cscript backupvm.vbs "savdalum01" "\\server\share\backup folder"
          '     (use quotes around parameters that contain spaces)
          '
          Option Explicit
          On Error Resume Next
          
          Dim objFSO, objVirtualServer, objVM, objSaveTask, objVHD
          
          'Connect to file system object.
          set objFSO=CreateObject("Scripting.FileSystemObject") 
          
          'Connect to Virtual Server.
          set objVirtualServer = CreateObject("VirtualServer.Application")
          
          'Get virtual machine from command-line parameter.
          set objVM = objVirtualServer.FindVirtualMachine(WScript.Arguments(0))
          
          'Save state of the virtual machine.
          set objSaveTask = objVM.Save
          
          'Loop waiting for task completion
          while not objSaveTask.isComplete
              WScript.Sleep 1000
          wend
          
          'Copy virtual hard disks and undo disks.
          for each objVHD in objVM.HardDiskConnections
              If objFSO.FileExists(objVHD.HardDisk.file) Then
                  'Wscript.Echo objVHD.HardDisk.file & " " & WScript.Arguments(1)
                  objFSO.CopyFile objVHD.HardDisk.file, WScript.Arguments(1) & "\",True
              End If
              If objFSO.FileExists(objVHD.undoHardDisk.file) Then
                  'Wscript.Echo objVHD.undoHardDisk.file & " " & WScript.Arguments(1)
                  objFSO.CopyFile objVHD.undoHardDisk.file, WScript.Arguments(1) & "\",True
              End If
          Next
          
          'Copy .vmc and .vsv files.
          objFSO.CopyFile objVM.File, WScript.Arguments(1)
          objFSO.CopyFile objVM.SavedStateFilePath, WScript.Arguments(1)
          
          'Start the virtual machine.
          objVM.Startup
          • Did you mind the note in the original script?;
            "Make sure that you place the \ at the end of the backup path or you'll get errors."
          • Do not make use of mapped drives if you planned to schedule (or 'runas') this script.
            And check the access permissions for this secondary logged-on user.
          • And, tell us what error message you get, after you where trying to do what?

          More about the .CopyFile function;
          - http://www.google.com/search?hl=nl&q=.CopyFile+function

          More on VirtualServer scripting;
          - http://www.google.com/search?q=Creat...lication%22%29


          \Rems
          Last edited by Rems; 30th November 2007, 11:18.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: Vbs : add mounting folder before backup

            please don't think that.

            The only reason I wrote my name instead of the original one is because the script is used on a internal (very big) network.

            My boss is ok about scripts from the internet if we write into them the name of the sys admin that found it and the date of the last update.

            I am glad to say that I found the script here :

            http://www.windowsitpro.com/Articles...9656.html?Ad=1

            Comment


            • #7
              Re: Vbs : add mounting folder before backup

              My boss is ok about scripts from the internet if we write into them the name of the sys admin that found it and the date of the last update.
              And I must say, that is an absolute smart policy!

              The intensions of my first remark was actually not for accusing someone of something. As far as I know you, you are OK hasdou. With that remark I was reflecting how it appears on a public website.


              For professionals among each other it is always nice to 'give credit where credit is due'. This not only inspirers contributers to spend more time on a subject, or for others to start contributing, in some cases it can also link readers to the source of a possible solution, so - what is published 'for us knowledge-hungry people', can then any time be discussed, enhanced, corrected or improved when nessery.
              (No worries when you forget, administrators and moderators of this forum often ask for these details when it could be relevant or can contribute more or less to the quality of the forum).
              For myself I add an url in every script that I copied/past from the internet. It enables the possibility to check for revisions. And it can help to read more about the method that was used incases the script has to go back to the drawing board. I find this very enriching for my scripts repository. I can use it as a reference too when writing new script.



              How do I add to the script the command that mount to a drive the backup folder (on my file server) into the virtual server (I would like to have W: for \\fileserver) ?
              I am not sure what you mean by 'into the virtual server'. The disk files, saved memory, and configuration of the virtual instance will be copied during a suspention of this virtual instance.

              When you schedule the script as a task, it then will be executed in an different security context as the currently logged user. And no drive mappings will initially be loaded. You can use a UNC path instead, but if you for some reason realy need a mapped drive, then you must choose a driveletter that is available any time the script runs.

              The next script select automatically the first available driveletter, then connect the drive, but does not write it to the users registry;
              Code:
              <cut>
              set objFSO = wscript.CreateObject("Scripting.FileSystemObject") 
              <cut>
              Dim sDrive
              sDrive = ConnectDrive(WScript.Arguments(1))
              <cut>
              objFSO.CopyFile objVHD.HardDisk.file, sDrive & "\",True
              <cut>
              objFSO.CopyFile objVHD.undoHardDisk.file, sDrive & "\",True
              <cut>
              
              Function ConnectDrive(strRemoteName)
                Dim i
                For i = Asc("D") To Asc("Z") 
                  If Not objFSO.DriveExists(Chr(i)) Then 
                    ConnectDrive = Chr(i) & ":" 
                    Exit For 
                  End If 
                Next 
                With CreateObject("WScript.network")
                  .MapNetworkDrive ConnectDrive, strRemoteName, False
                End With
                ConnectDrive = ConnectDrive 
              End Function
              More about the .MapNetworkDrive method;
              - http://www.google.com/search?q=MapNetworkDrive+Method


              \Rems

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment


              • #8
                Re: Vbs : add mounting folder before backup

                Originally posted by Rems
                For myself I add an url in every script that I copied/past from the internet. It enables the possibility to check for revisions. And it can help to read more about the method that was used incases the script has to go back to the drawing board. I find this very enriching for my scripts repository. I can use it as a reference too when writing new script.
                That is clever and a really useful tip if you have complex scripts that you are using. Thanks Rems.
                1 1 was a racehorse.
                2 2 was 1 2.
                1 1 1 1 race 1 day,
                2 2 1 1 2

                Comment


                • #9
                  Re: Vbs : add mounting folder before backup

                  Thank you Rems, we are clear . Can I add your code to my script ? and if it's possible ... where exactly ?

                  Comment


                  • #10
                    Re: Vbs : add mounting folder before backup

                    You can add the code to your script, but I see no reason for doing so.

                    In the code sample there is at the end a part that starts with
                    Function ConnectDrive(strRemoteName) and end with End Function
                    This function map a drive letter to the UNC (WScript.Arguments(1)) that you provided as the 2nd parameter on the script command line.
                    The function select automatically the first available drive letter. But if you want a fixed driveletter [W:], then replace
                    Code:
                      Dim i
                      For i = Asc("D") To Asc("Z") 
                        If Not objFSO.DriveExists(Chr(i)) Then 
                          ConnectDrive = Chr(i) & ":" 
                          Exit For 
                        End If 
                      Next
                    With ConnectDrive = "W:"
                    You can add Functions, like this function, and subroutines to the end of a script.

                    This function can be called anywhere from the main part of the script with the line:
                    sDrive = ConnectDrive(WScript.Arguments(1))
                    and returns the drive letter that is now is mapped. After that you can use the drive letter ("sDrive") in the script.

                    Because the 'ConnectDrive' function relies on a instance created from of the "FileSystemObject", and this object was already created in the original script and can be used global, there is no need to create another object. Just make sure that you do not call the 'ConnectDrive' before the line: set objFSO=CreateObject("Scripting.FileSystemObject") that already exists in the original script.

                    Because the script stated Option Explicit, you must declare all variables before you can use them. I declared sDrive with the line DIM sDrive, but you can add also sDrive to the already existing DIM ...-line. You can declear a varialbe only one time.

                    Finally;
                    Modify the two existing lines,
                    objFSO.CopyFile objVHD.HardDisk.file, sDrive & "\",True
                    objFSO.CopyFile objVHD.undoHardDisk.file, sDrive & "\",True

                    Now the files will be copied to a drive letter and not to a UNC.


                    \Rems

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment


                    • #11
                      Re: Vbs : add mounting folder before backup

                      hi Rems and thanks once more for the help.
                      But your explaination is Jibrish for me

                      I am not a script pro and I guess all my tests based on your solution failed because I don't get it x2

                      Can you just correct my script ... I guess that if not ... I'll be searching for a new job soon. My boss is trying to catch me all day long ... I am hiding in the toilets until' you'll answer me x3

                      Have a good night and bring me food soon x4

                      Comment

                      Working...
                      X