Então… eu tenho trabalhado em um pequeno VBScript que pode:
- tirar instantâneos do VSS persistentes
- monte-os em uma pasta (a partir da qual você pode fazer o backup dos arquivos)
- desmontar instantâneos do VSS
Ele depende da vshadow.exe
( documentação ), parte do o SDK do Serviço de Cópias de Sombra de Volume 7.2 , conforme disponibilizado pela Microsoft. Eu tenho trabalhado com esta versão: " VSHADOW.EXE 2.2 - Cliente de exemplo de cópia de sombra de volume, Copyright (C) 2005 Microsoft Corporation. "
Basicamente, é um pequeno wrapper em torno desses quatro comandos vshadow:
vshadow.exe -q - List all shadow copies in the system vshadow.exe -p {volume list} - Manages persistent shadow copies vshadow.exe -el={SnapID},dir - Expose the shadow copy as a mount point vshadow.exe -ds={SnapID} - Deletes this shadow copy
Aqui está sua tela de ajuda:
VSS Snapshot Create/Mount Tool Usage: cscript /nologo VssSnapshot.vbs /target:path { /volume:X | /unmount } [/debug] /volume - drive letter of the volume to snapshot /target - the path (absolute or relative) to mount the snapshot to /debug - swich on debug output Examples: cscript /nologo VssSnapshot.vbs /target:C:\Backup\DriveD /volume:D cscript /nologo VssSnapshot.vbs /target:C:\Backup\DriveD /unmount Hint: No need to unmount before taking a new snapshot.
Aqui, um exemplo de saída:
C:\VssSnapshot>cscript /nologo VssSnapshot.vbs /target:MountPoints\E /volume:E 05/03/2010 17:13:04 preparing VSS mount point... 05/03/2010 17:13:04 mount point prepared at: C:\VssSnapshot\MountPoints\E 05/03/2010 17:13:04 creating VSS snapshot for volume: E 05/03/2010 17:13:08 snapshot created with ID: {4ed3a907-c66f-4b20-bda0-9dcda3b667ec} 05/03/2010 17:13:08 VSS snapshot mounted sucessfully 05/03/2010 17:13:08 finished C:\VssSnapshot>cscript /nologo VssSnapshot.vbs /target:MountPoints\E /unmount 05/03/2010 17:13:35 preparing VSS mount point... 05/03/2010 17:13:36 nothing else to do 05/03/2010 17:13:36 finished
E aqui está o script em si. A renúncia usual se aplica: O software é fornecido como é, eu não dou garantias, uso por sua conta e risco, se algo quebra o único a culpar é você mesmo. Eu testei bastante bem, embora e funciona bem para mim. Sinta-se livre para me avisar sobre quaisquer erros através dos comentários abaixo.
''# VssSnapshot.vbs
''# http://serverfault.com/questions/119120/how-to-use-a-volume-shadow-copy-to-make-backups/119592#119592
Option Explicit
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
''# -- MAIN SCRIPT -------------------------------------------
Dim args, snapshotId, targetPath, success
Set args = WScript.Arguments.Named
CheckEnvironment
Log "preparing VSS mount point..."
targetPath = PrepareVssMountPoint(args("target"))
If args.Exists("unmount") Then
Log "nothing else to do"
ElseIf targetPath <> vbEmpty Then
Log "mount point prepared at: " & targetPath
Log "creating VSS snapshot for volume: " & args("volume")
snapshotId = CreateVssSnapshot(args("volume"))
If snapshotId <> vbEmpty Then
Log "snapshot created with ID: " & snapshotId
success = MountVssSnapshot(snapshotId, targetPath)
If success Then
Log "VSS snapshot mounted sucessfully"
Else
Die "failed to mount snapshot"
End If
Else
Die "failed to create snapshot"
End If
Else
Die "failed to prepare mount point"
End If
Log "finished"
''# -- FUNCTIONS ---------------------------------------------
Function PrepareVssMountPoint(target) ''# As String
Dim cmd, result, outArray
Dim path, snapshot, snapshotId
Dim re, matches, match
PrepareVssMountPoint = VbEmpty
target = fso.GetAbsolutePathName(target)
If Not fso.FolderExists(fso.GetParentFolderName(target)) Then
Die "Invalid mount point: " & target
End If
''# create or unmount (=delete existing snapshot) mountpoint
If Not fso.FolderExists(target) Then
If Not args.Exists("unmount") Then fso.CreateFolder target
Else
Set re = New RegExp
re.MultiLine = False
re.Pattern = "- Exposed locally as: ([^\r\n]*)"
cmd = "vshadow -q"
result = RunCommand(cmd, false)
outarray = Split(result, "*")
For Each snapshot In outArray
snapshotId = ParseSnapshotId(snapshot)
If snapshotId <> vbEmpty Then
Set matches = re.Execute(snapshot)
If matches.Count = 1 Then
path = Trim(matches(0).SubMatches(0))
If fso.GetAbsolutePathName(path) = target Then
cmd = "vshadow -ds=" & snapshotId
RunCommand cmd, true
Exit For
End If
End If
End If
Next
If args.Exists("unmount") Then fso.DeleteFolder target
End If
PrepareVssMountPoint = target
End Function
Function CreateVssSnapshot(volume) ''# As String
Dim cmd, result
If Not fso.DriveExists(volume) Then
Die "Drive " & volume & " does not exist."
End If
cmd = "vshadow -p " & Replace(UCase(volume), ":", "") & ":"
result = RunCommand(cmd, false)
CreateVssSnapshot = ParseSnapshotId(result)
End Function
Function MountVssSnapshot(snapshotId, target) ''# As Boolean
Dim cmd, result
If fso.FolderExists(targetPath) Then
cmd = "vshadow -el=" & snapshotId & "," & targetPath
result = RunCommand(cmd, true)
Else
Die "Mountpoint does not exist: " & target
End If
MountVssSnapshot = (result = "0")
End Function
Function ParseSnapshotId(output) ''# As String
Dim re, matches, match
Set re = New RegExp
re.Pattern = "SNAPSHOT ID = (\{[^}]{36}\})"
Set matches = re.Execute(output)
If matches.Count = 1 Then
ParseSnapshotId = matches(0).SubMatches(0)
Else
ParseSnapshotId = vbEmpty
End If
End Function
Function RunCommand(cmd, exitCodeOnly) ''# As String
Dim shell, process, output
Dbg "Running: " & cmd
Set shell = CreateObject("WScript.Shell")
On Error Resume Next
Set process = Shell.Exec(cmd)
If Err.Number <> 0 Then
Die Hex(Err.Number) & " - " & Err.Description
End If
On Error GoTo 0
Do While process.Status = 0
WScript.Sleep 100
Loop
output = Process.StdOut.ReadAll
If process.ExitCode = 0 Then
Dbg "OK"
Dbg output
Else
Dbg "Failed with ERRORLEVEL " & process.ExitCode
Dbg output
If Not process.StdErr.AtEndOfStream Then
Dbg process.StdErr.ReadAll
End If
End If
If exitCodeOnly Then
Runcommand = process.ExitCode
Else
RunCommand = output
End If
End Function
Sub CheckEnvironment
Dim argsOk
If LCase(fso.GetFileName(WScript.FullName)) <> "cscript.exe" Then
Say "Please execute me on the command line via cscript.exe!"
Die ""
End If
argsOk = args.Exists("target")
argsOk = argsOk And (args.Exists("volume") Or args.Exists("unmount"))
If Not argsOk Then
Say "VSS Snapshot Create/Mount Tool" & vbNewLine & _
vbNewLine & _
"Usage: " & vbNewLine & _
"cscript /nologo " & fso.GetFileName(WScript.ScriptFullName) & _
" /target:path { /volume:X | /unmount } [/debug]" & _
vbNewLine & vbNewLine & _
"/volume - drive letter of the volume to snapshot" & _
vbNewLine & _
"/target - the path (absolute or relative) to mount the snapshot to" & _
vbNewLine & _
"/debug - swich on debug output" & _
vbNewLine & vbNewLine & _
"Examples: " & vbNewLine & _
"cscript /nologo " & fso.GetFileName(WScript.ScriptFullName) & _
" /target:C:\Backup\DriveD /volume:D" & vbNewLine & _
"cscript /nologo " & fso.GetFileName(WScript.ScriptFullName) & _
" /target:C:\Backup\DriveD /unmount" & _
vbNewLine & vbNewLine & _
"Hint: No need to unmount before taking a new snapshot." & vbNewLine
Die ""
End If
End Sub
Sub Say(message)
If message <> "" Then WScript.Echo message
End Sub
Sub Log(message)
Say FormatDateTime(Now()) & " " & message
End Sub
Sub Dbg(message)
If args.Exists("debug") Then
Say String(75, "-")
Say "DEBUG: " & message
End If
End Sub
Sub Die(message)
If message <> "" Then Say "FATAL ERROR: " & message
WScript.Quit 1
End Sub
Espero que isso ajude alguém. Sinta-se à vontade para usá-lo de acordo com cc-by-sa . Tudo o que peço é que você deixe o link intacto que aponta para cá.