Einzelnen Beitrag anzeigen

Benutzerbild von HeikoAdams
HeikoAdams

Registriert seit: 12. Jul 2004
Ort: Oberfranken
661 Beiträge
 
FreePascal / Lazarus
 
#3

Re: Volume Shadow Copy Service nutzen

  Alt 15. Feb 2010, 11:05
Ich habe mal die VSS-Dateien des Delphi Magazins, die ich verwende angehängt. Das einzige, was ich geändert habe, ist die Umstellung von static auf dynamic linking.

Folgende Routine dürfte ergo unter Vista/7 nicht durchlaufen
Delphi-Quellcode:
Const
  SYSTEM_WRITER_NAME = 'System Writer';
  SYSTEM_FILES_COMPONENT_NAME = 'System Files';

Var
  InstanceGUID : TGUID;
  WriterGUID : TGUID;
  BackupComps : IVssBackupComponents;
  SetGUID : TGUID;
  SnapGUID : TGUID;
  Async : IVssAsync;
  SnapProp : VSS_SNAPSHOT_PROP;
  SourceFile : String;
  Drive : String;
  ShadowPath : String;
  Path : String;
  DestPath : String;
  ErrorMsg : String;

begin
  BackupLogMemo.Clear();
  BackupLogMemo.Lines.Add('Starting backup operation.');
  BackupProgressBar.Position := 0;
  BackupProgressBar.Max := 10;
  GetVssGUIDsByName(SYSTEM_WRITER_NAME,InstanceGUID,WriterGUID);
  BackupLogMemo.Lines.Add('Writer name = "'+SYSTEM_WRITER_NAME+'".');
  BackupLogMemo.Lines.Add('Instance = '+GUIDToString(InstanceGUID)+'.');
  BackupLogMemo.Lines.Add('Writer = '+GUIDToString(WriterGUID)+'.');
  BackupProgressBar.StepIt();
  GetBackupComponentsAndInitialize(BackupComps,WriterGUID);
  BackupProgressBar.StepIt();
  OleCheck(BackupComps.SetBackupState(True,False,VSS_BT_COPY,False));
  BackupLogMemo.Lines.Add('Backup state has been set.');
  BackupProgressBar.StepIt();
  BackupLogMemo.Lines.Add('Starting a snapshot set...');
  OleCheck(BackupComps.StartSnapshotSet(SetGUID));
  BackupLogMemo.Lines.Add('Created backup set with ID '+GUIDToString(SetGUID)+'.');
  BackupProgressBar.StepIt();
  SourceFile := FileToBackUpEdit.Text;
  BackupLogMemo.Lines.Add('Source file = "'+SourceFile+'".');
  Drive := IncludeTrailingPathDelimiter(ExtractFileDrive(SourceFile));
  BackupLogMemo.Lines.Add('Drive to snapshot = '+Drive+'.');
  OleCheck(BackupComps.AddToSnapshotSet(Drive,GUID_NULL,SnapGUID)); // hier kommt immer der Integer overflow
  BackupLogMemo.Lines.Add('Drive '+Drive+' has been added to the set.');
  BackupLogMemo.Lines.Add('Snap GUID = '+GUIDToString(SnapGUID)+'.');
  BackupProgressBar.StepIt();
  OleCheck(BackupComps.AddComponent(InstanceGUID,WriterGUID,
           VSS_CT_FILEGROUP,nil,SYSTEM_FILES_COMPONENT_NAME));
  BackupLogMemo.Lines.Add('System component added.');
  BackupProgressBar.StepIt();
  Try
    OleCheck(BackupComps.PrepareForBackup(Async));
    Async.Wait(INFINITE);
    Async := nil;
    BackupLogMemo.Lines.Add('Preparations for backup finished.');
    BackupProgressBar.StepIt();
    OleCheck(BackupComps.DoSnapshotSet(Async));
    Async.Wait(INFINITE);
    Async := nil;
    BackupLogMemo.Lines.Add('Snapshot done.');
    BackupProgressBar.StepIt();
    { start the real backup }
    OleCheck(BackupComps.GetSnapshotProperties(SnapGUID,SnapProp));
    BackupLogMemo.Lines.Add('Got backup properties.');
    BackupProgressBar.StepIt();
    If ShowMetadataCheckBox.Checked Then Begin
      OutputBackupProperties(SnapProp);
    End;
    { copy files }
    Path := System.Copy(SourceFile,3,Length(SourceFile)-2); { remove drive ("C:") }
    ShadowPath := WideCharToString(SnapProp.m_pwszSnapshotDeviceObject);
    Path := ShadowPath+Path;
    DestPath := IncludeTrailingPathDelimiter(DestinationDirEdit.Text)+
                ExtractFileName(SourceFile);
    BackupLogMemo.Lines.Add('Shadow path (source) = "'+Path+'".');
    BackupLogMemo.Lines.Add('Destination file = "'+DestPath+'".');
    BackupProgressBar.StepIt();
    If (Not CopyFile(PAnsiChar(Path),PAnsiChar(DestPath),True)) Then Begin
      ErrorMsg := 'File backup failed: '+SysErrorMessage(GetLastError())+'.';
      BackupLogMemo.Lines.Add(ErrorMsg);
      ShowMessage(ErrorMsg);
    End
    Else Begin
      ErrorMsg := 'File backup OK!';
      BackupLogMemo.Lines.Add(ErrorMsg);
      ShowMessage(ErrorMsg);
    End;
    { clean up }
    BackupLogMemo.Lines.Add('Cleaning up resources...');
    VssFreeSnapshotProperties(@SnapProp);
    OleCheck(BackupComps.SetBackupSucceeded(InstanceGUID,WriterGUID,
             VSS_CT_FILEGROUP,nil,SYSTEM_FILES_COMPONENT_NAME,True));
  Finally
    BackupComps.BackupComplete(Async);
    Async.Wait(INFINITE);
    Async := nil;
    BackupLogMemo.Lines.Add('Backup is now complete.');
    BackupProgressBar.Position := 0;
  End;
Angehängte Dateien
Dateityp: pas vssutilities_134.pas (3,9 KB, 324x aufgerufen)
Dateityp: pas vssapi_192.pas (15,1 KB, 282x aufgerufen)
Jeder kann ein Held werden und Leben retten!
Einfach beim NKR oder der DKMS als Stammzellenspender registrieren! Also: worauf wartest Du noch?
  Mit Zitat antworten Zitat