How to read/process locked files?
-
Hi all, I'm working on a backup project in C++ for Windows XP and Server 2003. I want to be able to backup locked files like the registry and database files. I've looked into Volume Shadow Copy and it seems like the right direction to head, but I can't understand how to implement it in C++. Here is our current method for backing up a file. 1. FindFirstFile/FindNextFile 2. Send path to file to compression function 3. Compression function simply calls zip.exe with the proper arguments (path to file, path to zip, etc) 3a. zip.exe is Info-ZIP.org's zip program. My major issue with shadow volumes is that I want to directly access the files and not copy them. Since I'm trying to backup database files, it doesn't make sense to copy them (they are several GB in size sometimes). I'm curious to know if there are other methods outside of VSS to read/process locked files. The majority of enterprise backup solutions can do it without any issue or delay, so I know it's possible, I just don't know how. I've looked at Hobocopy and it seems to be a valid option, but I don't think it'll work with my current compression method. I tried creating a shadow copy and using the \\?\GLOBALROOT\... path with zip.exe, but it failed, of course. Thanks.
-
Hi all, I'm working on a backup project in C++ for Windows XP and Server 2003. I want to be able to backup locked files like the registry and database files. I've looked into Volume Shadow Copy and it seems like the right direction to head, but I can't understand how to implement it in C++. Here is our current method for backing up a file. 1. FindFirstFile/FindNextFile 2. Send path to file to compression function 3. Compression function simply calls zip.exe with the proper arguments (path to file, path to zip, etc) 3a. zip.exe is Info-ZIP.org's zip program. My major issue with shadow volumes is that I want to directly access the files and not copy them. Since I'm trying to backup database files, it doesn't make sense to copy them (they are several GB in size sometimes). I'm curious to know if there are other methods outside of VSS to read/process locked files. The majority of enterprise backup solutions can do it without any issue or delay, so I know it's possible, I just don't know how. I've looked at Hobocopy and it seems to be a valid option, but I don't think it'll work with my current compression method. I tried creating a shadow copy and using the \\?\GLOBALROOT\... path with zip.exe, but it failed, of course. Thanks.
What if you granted your code the
SE_BACKUP_NAME
privilege and then usedCreateFile()
withFILE_FLAG_BACKUP_SEMANTICS
?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
What if you granted your code the
SE_BACKUP_NAME
privilege and then usedCreateFile()
withFILE_FLAG_BACKUP_SEMANTICS
?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
Here's my sample code and output:
#include <windows.h>
#include <iostream>
#include <time.h>
#include <tchar.h>using namespace std;
BOOL SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);
HANDLE hFile;void main() {
BOOL ret = SetPrivilege(SE\_BACKUP\_NAME,TRUE); cout << "Ret: " << ret << endl; DWORD dwErr=0; hFile = CreateFile(L"c:\\\\windows\\\\system32\\\\config\\\\system", GENERIC\_READ, FILE\_SHARE\_READ | FILE\_SHARE\_WRITE, NULL, OPEN\_EXISTING, FILE\_FLAG\_BACKUP\_SEMANTICS, 0); cout << "Handle: " << hFile << endl; if (INVALID\_HANDLE\_VALUE == hFile) { dwErr = GetLastError(); CloseHandle(hFile); cout << "Could not open file: " << dwErr << endl; } CloseHandle(hFile); SetPrivilege(SE\_BACKUP\_NAME,FALSE);
}
BOOL SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
{
TOKEN_PRIVILEGES tp;
LUID luid;
HANDLE hToken;OpenProcessToken(GetCurrentProcess(), TOKEN\_ADJUST\_PRIVILEGES | TOKEN\_QUERY, &hToken); if ( !LookupPrivilegeValue(NULL, lpszPrivilege, &luid) ) return FALSE; tp.PrivilegeCount = 1; tp.Privileges\[0\].Luid = luid; if (bEnablePrivilege) tp.Privileges\[0\].Attributes = SE\_PRIVILEGE\_ENABLED; else tp.Privileges\[0\].Attributes = 0; AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN\_PRIVILEGES) NULL, 0); return ( (GetLastError()!=ERROR\_SUCCESS)?FALSE:TRUE);
}
Ret: 1 Handle: FFFFFFFF Could not open file: 32
As you can see, it gave an Error 32, which is: ERROR_SHARING_VIOLATION 32 (0x20) The process cannot access the file because it is being used by another process. I think I'm doing it properly... :mad: -
Hi all, I'm working on a backup project in C++ for Windows XP and Server 2003. I want to be able to backup locked files like the registry and database files. I've looked into Volume Shadow Copy and it seems like the right direction to head, but I can't understand how to implement it in C++. Here is our current method for backing up a file. 1. FindFirstFile/FindNextFile 2. Send path to file to compression function 3. Compression function simply calls zip.exe with the proper arguments (path to file, path to zip, etc) 3a. zip.exe is Info-ZIP.org's zip program. My major issue with shadow volumes is that I want to directly access the files and not copy them. Since I'm trying to backup database files, it doesn't make sense to copy them (they are several GB in size sometimes). I'm curious to know if there are other methods outside of VSS to read/process locked files. The majority of enterprise backup solutions can do it without any issue or delay, so I know it's possible, I just don't know how. I've looked at Hobocopy and it seems to be a valid option, but I don't think it'll work with my current compression method. I tried creating a shadow copy and using the \\?\GLOBALROOT\... path with zip.exe, but it failed, of course. Thanks.
You'll need to update your code (based on hobocopy) to use this API -- ExposeSnapshot() -- will cause the snapshot to be mounted to a drive letter. Letting you use a "normal path". http://msdn.microsoft.com/en-us/library/aa382662(VS.85).aspx[^]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
-
You'll need to update your code (based on hobocopy) to use this API -- ExposeSnapshot() -- will cause the snapshot to be mounted to a drive letter. Letting you use a "normal path". http://msdn.microsoft.com/en-us/library/aa382662(VS.85).aspx[^]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
Thanks for the hint, Peter, but when I added the following, the program crashes. I assume I'm missing something.
IVssBackupComponents *pReq;
VSS_ID snapID;
PWSTR wszExposed;CLSIDFromString(L"{3c8b7455-4f04-4ead-b666-578ddb608138}", &snapID); hres = pReq->ExposeSnapshot(snapID, NULL, VSS\_VOLSNAP\_ATTR\_EXPOSED\_LOCALLY, L"x:", &wszExposed);
I am using vshadow -wait c: on a test machine to generate the snapshot, so I'm not sure if that will cause any conflicts. I'll go ahead and tinker with HoboCopy and toss it in there to see what happens. Thanks.
-
Thanks for the hint, Peter, but when I added the following, the program crashes. I assume I'm missing something.
IVssBackupComponents *pReq;
VSS_ID snapID;
PWSTR wszExposed;CLSIDFromString(L"{3c8b7455-4f04-4ead-b666-578ddb608138}", &snapID); hres = pReq->ExposeSnapshot(snapID, NULL, VSS\_VOLSNAP\_ATTR\_EXPOSED\_LOCALLY, L"x:", &wszExposed);
I am using vshadow -wait c: on a test machine to generate the snapshot, so I'm not sure if that will cause any conflicts. I'll go ahead and tinker with HoboCopy and toss it in there to see what happens. Thanks.
Doing more research on ExposeSnapshot(), aside from my above implementation being incorrect (don't need the {}'s), I don't think it works on XP. The snapshot needs to be created with a specific context property, either VSS_CTX_APP_ROLLBACK or VSS_CTX_NAS_ROLLBACK. Sadly, XP does not support setting a different context other than the default, VSS_CTX_BACKUP. So, it seems that using ExposeSnapshot is not a viable option as it needs to work on XP and Server 2003. Thanks, though. I learned quite a bit more about VSS by looking through this.