Volume backup using VSS in vc++

2.6k views Asked by At

Can anyone suggest me how to do volume backup?

below is my code. Creating shapshot of C: drive and tryinh to backed up and using CopyFile to backup file by file. Is their any way to backup 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();

Thanks in Advance

1

There are 1 answers

0
Rajesh Subramanian On

You can do volume shadow copy using vshadow tool. Source code also available online also with sdks. Go through the following link,

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