Changeset 2904 for box/trunk/bin


Ignore:
Timestamp:
08/04/2011 21:01:01 (14 months ago)
Author:
chris
Message:

Actually create a VSS snapshot.

Clean up writer metadata and writer status after use.

Location:
box/trunk/bin/bbackupd
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/bin/bbackupd/BackupDaemon.cpp

    r2902 r2904  
    120120                { 
    121121                        char* pStr = ConvertFromWideString(buf, CP_UTF8); 
     122                         
     123                        if(pStr == NULL) 
     124                        { 
     125                                return "conversion failed"; 
     126                        } 
     127                         
    122128                        std::string result(pStr); 
    123129                        free(pStr); 
     
    132138                } 
    133139 
    134                 std::ostream& operator<< (std::ostream& o, const BSTR arg) 
     140                std::string BstrToString(const BSTR arg) 
    135141                { 
    136142                        if(arg == NULL) 
    137143                        { 
    138                                 o << "(null)"; 
     144                                return std::string("(null)"); 
    139145                        } 
    140146                        else 
    141147                        { 
    142148                                // Extract the *long* before where the arg points to 
    143                                 long len = ((long *)arg)[-1]; 
     149                                long len = ((long *)arg)[-1] / 2; 
    144150                                std::wstring wstr((WCHAR *)arg, len); 
    145151                                std::string str; 
     
    148154                                        throw std::exception("string conversion failed"); 
    149155                                } 
    150                                 o << str; 
    151                         } 
    152  
    153                         return o; 
     156                                return str; 
     157                        } 
    154158                } 
    155159#       endif 
     
    10691073} 
    10701074 
     1075#define CALL_MEMBER_FN(object, method) ((object).*(method)) 
     1076 
     1077bool BackupDaemon::CallAndWaitForAsync(AsyncMethod method, 
     1078        const std::string& description) 
     1079{ 
     1080        IVssAsync *pAsync; 
     1081        HRESULT result = CALL_MEMBER_FN(*mpVssBackupComponents, method)(&pAsync); 
     1082        if(result != S_OK) 
     1083        { 
     1084                BOX_ERROR("VSS: " << description << " failed: " << 
     1085                        GetMsgForHresult(result)); 
     1086                return false; 
     1087        } 
     1088 
     1089        return WaitForAsync(pAsync, description); 
     1090} 
     1091 
    10711092void BackupDaemon::CreateVssBackupComponents() 
    10721093{ 
     
    11311152        } 
    11321153 
    1133         { 
    1134                 IVssAsync *pAsync; 
    1135                 result = mpVssBackupComponents->GatherWriterMetadata(&pAsync); 
    1136                 if(result != S_OK) 
    1137                 { 
    1138                         BOX_ERROR("VSS: Failed to set backup state: " << 
    1139                                 GetMsgForHresult(result)); 
    1140                         goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
    1141                 } 
    1142  
    1143                 if(!WaitForAsync(pAsync, "GatherWriterMetadata()")) 
    1144                 { 
    1145                         goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
    1146                 } 
     1154        if(!CallAndWaitForAsync(&IVssBackupComponents::GatherWriterMetadata, 
     1155                "GatherWriterMetadata()")) 
     1156        { 
     1157                goto CreateVssBackupComponents_cleanup_WriterMetadata; 
    11471158        } 
    11481159 
     
    11531164                BOX_ERROR("VSS: Failed to get writer count: " << 
    11541165                        GetMsgForHresult(result)); 
    1155                 goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
     1166                goto CreateVssBackupComponents_cleanup_WriterMetadata; 
    11561167        } 
    11571168 
     
    11671178                        BOX_ERROR("Failed to get VSS metadata from writer " << iWriter << 
    11681179                                ": " << GetMsgForHresult(result)); 
    1169                         goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
     1180                        goto CreateVssBackupComponents_cleanup_WriterMetadata; 
    11701181                } 
    11711182 
     
    11751186                if(result != S_OK) 
    11761187                { 
    1177                         BOX_ERROR("Failed to get VSS metadata file counts from " 
     1188                        BOX_ERROR("VSS: Failed to get metadata file counts from " 
    11781189                                "writer " << iWriter << ": " <<  
    11791190                                GetMsgForHresult(result)); 
     
    11881199                        if(result != S_OK) 
    11891200                        { 
    1190                                 BOX_ERROR("Failed to get VSS metadata component " << 
     1201                                BOX_ERROR("VSS: Failed to get metadata component " << 
    11911202                                        iComponent << " from writer " << iWriter << ": " <<  
    11921203                                        GetMsgForHresult(result)); 
     
    11981209                        if(result != S_OK) 
    11991210                        { 
    1200                                 BOX_ERROR("Failed to get VSS metadata component " << 
     1211                                BOX_ERROR("VSS: Failed to get metadata component " << 
    12011212                                        iComponent << " info from writer " << iWriter << ": " <<  
    12021213                                        GetMsgForHresult(result)); 
     
    12171228 
    12181229                        BOX_INFO("VSS: logical path: " <<  
    1219                                 pComponentInfo->bstrLogicalPath); 
     1230                                BstrToString(pComponentInfo->bstrLogicalPath)); 
    12201231                        BOX_INFO("VSS: component name: " <<  
    1221                                 pComponentInfo->bstrComponentName); 
     1232                                BstrToString(pComponentInfo->bstrComponentName)); 
    12221233                        BOX_INFO("VSS: caption: " <<  
    1223                                 pComponentInfo->bstrCaption); 
     1234                                BstrToString(pComponentInfo->bstrCaption)); 
    12241235                        BOX_INFO("VSS: restore metadata: " <<  
    12251236                                pComponentInfo->bRestoreMetadata); 
     
    12541265                BOX_ERROR("VSS: Failed to start snapshot set: " << 
    12551266                        GetMsgForHresult(result)); 
    1256                 goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
     1267                goto CreateVssBackupComponents_cleanup_WriterMetadata; 
    12571268        } 
    12581269 
     
    12931304                                                volumeRoot << " to snapshot set: " << 
    12941305                                                GetMsgForHresult(result)); 
    1295                                         goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
     1306                                        goto CreateVssBackupComponents_cleanup_WriterMetadata; 
    12961307                                } 
    12971308                        } 
     
    13091320        } 
    13101321 
    1311         { 
    1312                 IVssAsync *pAsync; 
    1313                 result = mpVssBackupComponents->PrepareForBackup(&pAsync); 
     1322        if(!CallAndWaitForAsync(&IVssBackupComponents::PrepareForBackup, 
     1323                "PrepareForBackup()")) 
     1324        { 
     1325                goto CreateVssBackupComponents_cleanup_WriterMetadata; 
     1326        } 
     1327 
     1328        if(!CallAndWaitForAsync(&IVssBackupComponents::DoSnapshotSet, 
     1329                "DoSnapshotSet()")) 
     1330        { 
     1331                goto CreateVssBackupComponents_cleanup_WriterMetadata; 
     1332        } 
     1333 
     1334        if(!CallAndWaitForAsync(&IVssBackupComponents::GatherWriterStatus, 
     1335                "GatherWriterStatus()")) 
     1336        { 
     1337                goto CreateVssBackupComponents_cleanup_WriterStatus; 
     1338        } 
     1339 
     1340        result = mpVssBackupComponents->GetWriterStatusCount(&writerCount); 
     1341        if(result != S_OK) 
     1342        { 
     1343                BOX_ERROR("VSS: Failed to get writer status count: " <<  
     1344                        GetMsgForHresult(result)); 
     1345                goto CreateVssBackupComponents_cleanup_WriterStatus; 
     1346        } 
     1347 
     1348        for(UINT iWriter = 0; iWriter < writerCount; iWriter++) 
     1349        { 
     1350                VSS_ID instance, writer; 
     1351                BSTR writerNameBstr; 
     1352                VSS_WRITER_STATE writerState; 
     1353                HRESULT writerResult; 
     1354 
     1355                result = mpVssBackupComponents->GetWriterStatus(iWriter, 
     1356                        &instance, &writer, &writerNameBstr, &writerState, 
     1357                        &writerResult); 
    13141358                if(result != S_OK) 
    13151359                { 
    1316                         BOX_ERROR("VSS: Failed to prepare for backup: " << 
    1317                                 GetMsgForHresult(result)); 
    1318                         goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
    1319                 } 
    1320  
    1321                 if(!WaitForAsync(pAsync, "PrepareForBackup()")) 
    1322                 { 
    1323                         goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
    1324                 } 
     1360                        BOX_ERROR("VSS: Failed to query writer " << iWriter << 
     1361                                " status: " << GetMsgForHresult(result)); 
     1362                        goto CreateVssBackupComponents_cleanup_WriterStatus; 
     1363                } 
     1364 
     1365                std::string writerName = BstrToString(writerNameBstr); 
     1366                ::SysFreeString(writerNameBstr); 
     1367 
     1368                if(writerResult != S_OK) 
     1369                { 
     1370                        BOX_ERROR("VSS: Writer " << iWriter << " (" << 
     1371                                writerName << ") failed: " << 
     1372                                GetMsgForHresult(writerResult)); 
     1373                        continue; 
     1374                } 
     1375 
     1376                std::string stateName; 
     1377 
     1378                switch(writerState) 
     1379                { 
     1380#define WRITER_STATE(code) \ 
     1381                case code: stateName = #code; break; 
     1382                WRITER_STATE(VSS_WS_UNKNOWN); 
     1383                WRITER_STATE(VSS_WS_STABLE); 
     1384                WRITER_STATE(VSS_WS_WAITING_FOR_FREEZE); 
     1385                WRITER_STATE(VSS_WS_WAITING_FOR_THAW); 
     1386                WRITER_STATE(VSS_WS_WAITING_FOR_POST_SNAPSHOT); 
     1387                WRITER_STATE(VSS_WS_WAITING_FOR_BACKUP_COMPLETE); 
     1388                WRITER_STATE(VSS_WS_FAILED_AT_IDENTIFY); 
     1389                WRITER_STATE(VSS_WS_FAILED_AT_PREPARE_BACKUP); 
     1390                WRITER_STATE(VSS_WS_FAILED_AT_PREPARE_SNAPSHOT); 
     1391                WRITER_STATE(VSS_WS_FAILED_AT_FREEZE); 
     1392                WRITER_STATE(VSS_WS_FAILED_AT_THAW); 
     1393                WRITER_STATE(VSS_WS_FAILED_AT_POST_SNAPSHOT); 
     1394                WRITER_STATE(VSS_WS_FAILED_AT_BACKUP_COMPLETE); 
     1395                WRITER_STATE(VSS_WS_FAILED_AT_PRE_RESTORE); 
     1396                WRITER_STATE(VSS_WS_FAILED_AT_POST_RESTORE); 
     1397                WRITER_STATE(VSS_WS_FAILED_AT_BACKUPSHUTDOWN); 
     1398#undef WRITER_STATE 
     1399                default: 
     1400                        std::ostringstream o; 
     1401                        o << "unknown (" << writerState << ")"; 
     1402                        stateName = o.str(); 
     1403                } 
     1404 
     1405                BOX_INFO("VSS: Writer " << iWriter << " (" << 
     1406                        writerName << ") is in state " << stateName); 
    13251407        } 
    13261408 
     
    13301412        if(result != S_OK) 
    13311413        { 
    1332                 BOX_ERROR("Failed to query VSS snapshot list: " <<  
     1414                BOX_ERROR("VSS: Failed to query snapshot list: " <<  
    13331415                        GetMsgForHresult(result)); 
    1334                 goto CreateVssBackupComponents_cleanup_mpVssBackupComponents; 
     1416                goto CreateVssBackupComponents_cleanup_WriterStatus; 
    13351417        } 
    13361418 
     
    13431425                if(result != S_OK && result != S_FALSE) 
    13441426                { 
    1345                         BOX_ERROR("Failed to enumerate VSS snapshot: " <<  
     1427                        BOX_ERROR("VSS: Failed to enumerate snapshot: " <<  
    13461428                                GetMsgForHresult(result)); 
    13471429                } 
    13481430                else if(count != 1) 
    13491431                { 
    1350                         BOX_ERROR("Failed to enumerate VSS snapshot: " << 
     1432                        BOX_ERROR("VSS: Failed to enumerate snapshot: " << 
    13511433                                "Next() returned " << count << " objects instead of 1"); 
    13521434                } 
    13531435                else if(rgelt.Type != VSS_OBJECT_SNAPSHOT) 
    13541436                { 
    1355                         BOX_ERROR("Failed to enumerate VSS snapshot: " << 
     1437                        BOX_ERROR("VSS: Failed to enumerate snapshot: " << 
    13561438                                "Next() returned a type " << rgelt.Type << " object " 
    13571439                                "instead of VSS_OBJECT_SNAPSHOT"); 
     
    14221504 
    14231505        pEnum->Release(); 
     1506 
     1507CreateVssBackupComponents_cleanup_WriterStatus: 
     1508        result = mpVssBackupComponents->FreeWriterStatus(); 
     1509        if(result != S_OK) 
     1510        { 
     1511                BOX_ERROR("VSS: Failed to free writer status: " << 
     1512                        GetMsgForHresult(result)); 
     1513        } 
     1514 
     1515CreateVssBackupComponents_cleanup_WriterMetadata: 
     1516        result = mpVssBackupComponents->FreeWriterMetadata(); 
     1517        if(result != S_OK) 
     1518        { 
     1519                BOX_ERROR("VSS: Failed to free writer metadata: " << 
     1520                        GetMsgForHresult(result)); 
     1521        } 
    14241522 
    14251523CreateVssBackupComponents_cleanup_mpVssBackupComponents: 
  • box/trunk/bin/bbackupd/BackupDaemon.h

    r2901 r2904  
    533533        void CreateVssBackupComponents(); 
    534534        bool WaitForAsync(IVssAsync *pAsync, const std::string& description); 
     535        typedef HRESULT (__stdcall IVssBackupComponents::*AsyncMethod)(IVssAsync**); 
     536        bool CallAndWaitForAsync(AsyncMethod method, 
     537                const std::string& description); 
    535538#endif 
    536539}; 
Note: See TracChangeset for help on using the changeset viewer.