在vc ++中使用VSS进行卷备份

时间:2012-08-16 13:48:06

标签: visual-c++ volume-shadow-service

有人可以建议我如何进行批量备份吗?

下面是我的代码。创建C:驱动器和tryinh的shapshot以备份并使用CopyFile按文件备份文件。他们以任何方式备份valume吗?

cout<<"=============Begin Initialize=========="<<endl;
         if(FAILED(CoInitializeEx(NULL,0)))
         {
              cout << "CoInitialize() failed\n";
              return(0);
         }

          if(FAILED(CoInitializeSecurity(
              NULL,
              -1,
              NULL,
              NULL,
              RPC_C_AUTHN_LEVEL_PKT_PRIVACY, 
              RPC_C_IMP_LEVEL_IDENTIFY,
              NULL,
              EOAC_NONE,
              NULL))) 
          {
              cout << "CoInitializeSecurity() failed\n";
              return(0);
          }

           if(FAILED(CreateVssBackupComponents(&m_pVssObject))) 
           {
               cout << "CreateVssBackupComponents() failed\n";
               return(0);
           }

           if(FAILED(m_pVssObject->InitializeForBackup())) 
           { 
               cout << "IVssBackupComponents->InitializeForBackup() failed\n";
               return(0);
           }

          // if(FAILED(m_pVssObject->SetContext(dwContext))) 
           if(FAILED(m_pVssObject->SetContext(VSS_CTX_BACKUP | VSS_CTX_APP_ROLLBACK))) 
           { 
               cout << "IVssBackupComponents->SetContext() failed\n";
               return(0);
           }

           if(FAILED(m_pVssObject->SetBackupState(true,true,VSS_BT_FULL,false)))
           {
               cout << "IVssBackupComponents->SetContext() failed\n";
               return(0);
           }         
            cout<<"=============End Initialize=========="<<endl;

            // Start the shadow set
            CHECK_COM(m_pVssObject->StartSnapshotSet(&m_latestSnapshotSetID))

            GetVolumePathNameW((LPCWSTR)wstrVolume.c_str(),wszVolumePathName, MAX_PATH);
            CHECK_COM(m_pVssObject->IsVolumeSupported(GUID_NULL, wszVolumePathName, &supported));

            // Add the specified volumes to the shadow set
            VSS_ID SnapshotID;
            CHECK_COM(m_pVssObject->AddToSnapshotSet(wszVolumePathName, GUID_NULL, &SnapShotId));
            m_latestSnapshotIdList.push_back(SnapshotID);

            cout<<"Prepare the shadow for backup\n";
            CHECK_COM(m_pVssObject->PrepareForBackup(&pPrepare));
                cout<<"Waiting for the asynchronous operation to finish..."<<endl;
                CHECK_COM(pPrepare->Wait());
                HRESULT hrReturned = S_OK;
                CHECK_COM(pPrepare->QueryStatus(&hrReturned, NULL));
                // Check if the async operation succeeded...
                if(FAILED(hrReturned))
                {
                    cout<<"Error during the last asynchronous operation."<<endl;
                }
                pPrepare->Release();

            cout<<"Creating the shadow (DoSnapshotSet) ... "<<endl;
            CHECK_COM(m_pVssObject->DoSnapshotSet(&pDoShadowCopy));
                cout<<"Waiting for the asynchronous operation to finish..."<<endl;
                CHECK_COM(pDoShadowCopy->Wait());
                hrReturned = S_OK;
                CHECK_COM(pDoShadowCopy->QueryStatus(&hrReturned, NULL));
                // Check if the async operation succeeded...
                if(FAILED(hrReturned))
                {
                    cout<<"Error during the last asynchronous operation."<<endl;
                }
                pDoShadowCopy->Release();

            HRESULT result;
            //CHECK_COM(m_pVssObject->GetSnapshotProperties(SnapShotId,&props));

            result=m_pVssObject->GetSnapshotProperties(SnapShotId,&props);

            if(result== S_OK)
            {
                _tprintf (_T(" Snapshot Id :")  WSTR_GUID_FMT _T("\n"), GUID_PRINTF_ARG( props.m_SnapshotId));                               
                _tprintf (_T(" Snapshot Set ID :")  WSTR_GUID_FMT _T("\n"), GUID_PRINTF_ARG( props.m_SnapshotSetId));
                _tprintf (_T(" Provider ID :")  WSTR_GUID_FMT _T("\n"), GUID_PRINTF_ARG( props.m_ProviderId));
                _tprintf (_T(" OriginalVolumeName : %ls\n"),props.m_pwszOriginalVolumeName);
                if(props.m_pwszExposedPath != NULL) _tprintf (_T(" Exposed Path  : %ls\n"),props.m_pwszExposedPath);
                if(props.m_pwszExposedName != NULL) _tprintf (_T(" Exposed Path Name : %ls\n"),props.m_pwszExposedName);
                if(props.m_pwszSnapshotDeviceObject != NULL) _tprintf (_T(" SnapShot device object: %ls\n"),props.m_pwszSnapshotDeviceObject);


                SYSTEMTIME stUTC, stLocal;
                FILETIME ftCreate;
                ftCreate.dwHighDateTime  =  HILONG(props.m_tsCreationTimestamp);
                ftCreate.dwLowDateTime   =  LOLONG(props.m_tsCreationTimestamp);

                FileTimeToSystemTime(&ftCreate, &stUTC);
                SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

                _tprintf (TEXT("Created : %02d/%02d/%d  %02d:%02d \n"),   stLocal.wMonth, stLocal.wDay, stLocal.wYear, stLocal.wHour, stLocal.wMinute );
                _tprintf (_T("\n"));
            }


            WCHAR sam_file[1024];


            wsprintf(sam_file,L"%s%s",props.m_pwszSnapshotDeviceObject,L"\\WINDOWS\\System32\\Config\\SAM");

            CopyFile(sam_file,L"c:\\temp\\dbg_sm_bkup",FALSE);

            VssFreeSnapshotProperties(&props);  
            m_pVssObject->Release();

先谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用vshadow工具进行卷影复制。源代码也可以在线使用sdks。请浏览以下链接,

http://msdn.microsoft.com/en-us/library/windows/desktop/bb530725(v=vs.85).aspx