2 Bacula® - The Network Backup Solution
4 Copyright (C) 2008-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation, which is
11 listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Written by James Harper, October 2008
32 #include "exchange-fd.h"
34 storage_group_node_t::storage_group_node_t(char *name, node_t *parent_node) : node_t(name, NODE_TYPE_STORAGE_GROUP, parent_node)
39 restore_environment = NULL;
40 saved_log_path = NULL;
44 storage_group_node_t::~storage_group_node_t()
50 if (file_node != NULL)
56 storage_group_node_t::startBackupFile(exchange_fd_context_t *context, struct save_pkt *sp)
60 WCHAR *tmp_logfiles, *tmp_logfile_ptr;
65 _DebugMessage(100, "startBackupNode_STORAGE_GROUP state = %d, name = %s\n", state, name);
72 if (context->job_level == 'F')
74 _DebugMessage(100, "Calling HrESEBackupSetup (BACKUP_TYPE_FULL)\n");
75 result = HrESEBackupSetup(hccx, ibi->hInstanceId, BACKUP_TYPE_FULL);
80 _DebugMessage(100, "Calling HrESEBackupSetup (BACKUP_TYPE_LOGS_ONLY)\n");
81 result = HrESEBackupSetup(hccx, ibi->hInstanceId, BACKUP_TYPE_LOGS_ONLY);
82 if (context->accurate)
89 _JobMessage(M_FATAL, "HrESEBackupSetup failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
95 if (context->path_bits[level + 1] == NULL)
97 _DebugMessage(100, "No specific database specified - backing them all up\n");
98 DATABASE_BACKUP_INFO *dbi = &ibi->rgDatabase[current_dbi];
99 char *tmp = new char[wcslen(dbi->wszDatabaseDisplayName) + 1];
100 wcstombs(tmp, dbi->wszDatabaseDisplayName, wcslen(dbi->wszDatabaseDisplayName) + 1);
101 store_node = new store_node_t(tmp, this);
102 store_node->dbi = dbi;
103 store_node->hccx = hccx;
104 context->current_node = store_node;
108 DATABASE_BACKUP_INFO *dbi = NULL;
110 for (current_dbi = 0; current_dbi < ibi->cDatabase; current_dbi++)
112 dbi = &ibi->rgDatabase[current_dbi];
113 char *tmp = new char[wcslen(dbi->wszDatabaseDisplayName) + 1];
114 wcstombs(tmp, dbi->wszDatabaseDisplayName, wcslen(dbi->wszDatabaseDisplayName) + 1);
115 if (stricmp(tmp, context->path_bits[level + 1]) == 0)
119 if (current_dbi == ibi->cDatabase)
121 _JobMessage(M_FATAL, "Invalid Database '%s'\n", context->path_bits[level + 1]);
124 store_node = new store_node_t(tmp, this);
125 _DebugMessage(100, "Database name = %s\n", store_node->name);
127 store_node->hccx = hccx;
128 store_node->dbi = dbi;
129 context->current_node = store_node;
133 _DebugMessage(100, "Calling HrESEBackupGetLogAndPatchFiles\n");
134 result = HrESEBackupGetLogAndPatchFiles(hccx, &tmp_logfiles);
137 _JobMessage(M_FATAL, "HrESEBackupGetLogAndPatchFiles failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
140 for (len = 0, tmp_logfile_ptr = tmp_logfiles; *tmp_logfile_ptr != 0; tmp_logfile_ptr += wcslen(tmp_logfile_ptr) + 1)
142 len += wcslen(tmp_logfile_ptr) + 1;
144 logfiles = new WCHAR[len + 1];
145 logfile_ptr = logfiles;
146 for (tmp_logfile_ptr = tmp_logfiles; *tmp_logfile_ptr != 0; tmp_logfile_ptr += wcslen(tmp_logfile_ptr) + 1)
148 // check file modification date
150 FILETIME modified_time;
154 include_file = false;
155 handle = INVALID_HANDLE_VALUE;
156 if (context->job_since == 0)
160 handle = CreateFileW(tmp_logfile_ptr, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
161 if (handle == INVALID_HANDLE_VALUE)
163 //_JobMessage(M_WARNING, "Could not open '%S' to check last modified date (0x%08x), including anyway\n", tmp_logfile_ptr, GetLastError());
169 if (GetFileTime(handle, NULL, NULL, &modified_time) == 0)
171 //_JobMessage(M_WARNING, "Could check last modified date for '%S' (0x%08x), including anyway\n", tmp_logfile_ptr, GetLastError());
177 tmp_time = (((int64_t)modified_time.dwHighDateTime) << 32) | modified_time.dwLowDateTime;
178 tmp_time -= 116444736000000000LL;
179 tmp_time /= 10000000;
180 if (tmp_time > context->job_since)
187 memcpy(logfile_ptr, tmp_logfile_ptr, (wcslen(tmp_logfile_ptr) + 1) * 2);
188 logfile_ptr += wcslen(logfile_ptr) + 1;
189 //_DebugMessage(100, "Including file %S\n", logfile_ptr);
193 if (context->accurate) {
194 tmp = new char[strlen(full_path) + wcslen(tmp_logfile_ptr) + 1];
195 strcpy(tmp, full_path);
196 wcstombs(tmp + strlen(full_path), tmp_logfile_ptr, wcslen(tmp_logfile_ptr) + 1);
197 bfuncs->setBaculaValue(context->bpContext, bVarFileSeen, (void *)tmp);
202 if (handle != INVALID_HANDLE_VALUE)
207 logfile_ptr = logfiles;
211 tmp = new char[wcslen(logfile_ptr) + 1];
212 wcstombs(tmp, logfile_ptr, wcslen(logfile_ptr) + 1);
213 file_node = new file_node_t(tmp, this);
215 file_node->hccx = hccx;
216 file_node->filename = logfile_ptr;
217 context->current_node = file_node;
220 time_t now = time(NULL);
221 sp->fname = full_path;
222 sp->link = full_path;
223 _DebugMessage(100, "fname = %s\n", sp->fname);
224 sp->statp.st_mode = 0700 | S_IFDIR;
225 sp->statp.st_ctime = now;
226 sp->statp.st_mtime = now;
227 sp->statp.st_atime = now;
228 sp->statp.st_size = 0;
229 //sp->statp.st_blocks = 0;
230 sp->type = FT_DIREND;
237 storage_group_node_t::endBackupFile(exchange_fd_context_t *context)
240 bRC retval = bRC_Error;
242 _DebugMessage(100, "endBackupNode_STORAGE_GROUP state = %d\n", state);
247 // should never happen
250 // free node->storage_group_node
251 if (context->path_bits[level + 1] == NULL)
254 if (current_dbi == ibi->cDatabase)
262 // should never happen
266 logfile_ptr += wcslen(logfile_ptr) + 1;
267 if (*logfile_ptr == 0)
272 if (context->truncate_logs)
274 _DebugMessage(100, "Calling HrESEBackupTruncateLogs\n");
275 result = HrESEBackupTruncateLogs(hccx);
278 _JobMessage(M_FATAL, "HrESEBackupTruncateLogs failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
282 _JobMessage(M_INFO, "Truncated database logs for Storage Group %s\n", name);
287 _JobMessage(M_INFO, "Did NOT truncate database logs for Storage Group %s\n", name);
289 _DebugMessage(100, "Calling HrESEBackupInstanceEnd\n");
290 result = HrESEBackupInstanceEnd(hccx, ESE_BACKUP_INSTANCE_END_SUCCESS);
293 _JobMessage(M_FATAL, "HrESEBackupInstanceEnd failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
297 context->current_node = parent;
304 storage_group_node_t::createFile(exchange_fd_context_t *context, struct restore_pkt *rp)
309 _DebugMessage(0, "createFile_STORAGE_GROUP state = %d\n", state);
311 if (strcmp(context->path_bits[level], name) != 0)
313 _DebugMessage(0, "Different storage group - switching back to parent\n", state);
314 saved_log_path = new WCHAR[wcslen(restore_environment->m_wszRestoreLogPath) + 1];
315 wcscpy(saved_log_path, restore_environment->m_wszRestoreLogPath);
316 _DebugMessage(100, "Calling HrESERestoreSaveEnvironment\n");
317 result = HrESERestoreSaveEnvironment(hccx);
320 _JobMessage(M_FATAL, "HrESERestoreSaveEnvironment failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
322 rp->create_status = CF_CREATED;
325 _DebugMessage(100, "Calling HrESERestoreClose\n");
326 result = HrESERestoreClose(hccx, RESTORE_CLOSE_NORMAL);
329 _JobMessage(M_FATAL, "HrESERestoreClose failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
331 rp->create_status = CF_CREATED;
334 context->current_node = parent;
337 if (saved_log_path != NULL)
339 _DebugMessage(0, "Calling HrESERestoreReopen\n");
340 result = HrESERestoreReopen(context->computer_name, service_name, saved_log_path, &hccx);
343 _JobMessage(M_FATAL, "HrESERestoreReopen failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
345 saved_log_path = NULL;
346 rp->create_status = CF_CREATED;
349 _DebugMessage(0, "Calling HrESERestoreGetEnvironment\n");
350 result = HrESERestoreGetEnvironment(hccx, &restore_environment);
353 _JobMessage(M_FATAL, "HrESERestoreGetEnvironment failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
355 saved_log_path = NULL;
356 rp->create_status = CF_CREATED;
359 saved_log_path = NULL;
367 if (context->path_bits[level + 2] == NULL)
369 _JobMessage(M_ERROR, "Unexpected log file '%s%s' - expecting database\n", full_path, context->path_bits[level + 1]);
373 service_name = new WCHAR[strlen(parent->name) + 1];
374 storage_group_name = new WCHAR[strlen(name) + 1];
375 mbstowcs(service_name, parent->name, strlen(parent->name) + 1);
376 mbstowcs(storage_group_name, name, strlen(name) + 1);
377 _DebugMessage(0, "Calling HrESERestoreOpen\n");
378 result = HrESERestoreOpen(context->computer_name, service_name, storage_group_name, NULL, &hccx);
381 _JobMessage(M_FATAL, "HrESERestoreOpen failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
385 _DebugMessage(0, "Calling HrESERestoreGetEnvironment\n");
386 result = HrESERestoreGetEnvironment(hccx, &restore_environment);
389 _JobMessage(M_FATAL, "HrESERestoreGetEnvironment failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
396 if (context->path_bits[level + 2] == NULL)
401 store_node = new store_node_t(bstrdup(context->path_bits[level + 1]), this);
402 store_node->hccx = hccx;
403 context->current_node = store_node;
406 if (context->path_bits[level + 2] != NULL)
408 _JobMessage(M_ERROR, "Unexpected file '%s'\n", full_path);
412 if (context->path_bits[level + 1] == NULL)
418 file_node = new file_node_t(bstrdup(context->path_bits[level + 1]), this);
419 file_node->hccx = hccx;
421 for (i = strlen(file_node->name) - 1; i >= 0; i--)
423 if (file_node->name[i] == '\\')
429 len = wcslen(restore_environment->m_wszRestoreLogPath) + strlen(file_node->name + i) + 1 + 1;
430 file_node->filename = new WCHAR[len];
431 wcscpy(file_node->filename, restore_environment->m_wszRestoreLogPath);
432 wcscat(file_node->filename, L"\\");
433 mbstowcs(&file_node->filename[wcslen(file_node->filename)], file_node->name + i, strlen(file_node->name + i) + 1);
434 context->current_node = file_node;
437 if (rp->type != FT_DIREND)
439 _JobMessage(M_ERROR, "Unexpected file '%s'\n", full_path);
443 // must be the storage group node
444 _DebugMessage(100, "Calling HrESERestoreSaveEnvironment\n");
445 result = HrESERestoreSaveEnvironment(hccx);
448 _JobMessage(M_FATAL, "HrESERestoreSaveEnvironment failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
453 _DebugMessage(100, "Calling HrESERestoreComplete\n");
454 result = HrESERestoreComplete(hccx, restore_environment->m_wszRestoreLogPath,
455 restore_environment->m_wszRestoreLogPath, storage_group_name, ESE_RESTORE_COMPLETE_ATTACH_DBS);
458 _JobMessage(M_FATAL, "HrESERestoreComplete failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
459 _DebugMessage(100, "Calling HrESERestoreClose\n");
460 result = HrESERestoreClose(hccx, RESTORE_CLOSE_NORMAL);
466 _JobMessage(M_INFO, "Storage Group '%s' restored successfully\n", name);
469 _DebugMessage(100, "Calling HrESERestoreClose\n");
470 result = HrESERestoreClose(hccx, RESTORE_CLOSE_NORMAL);
473 _JobMessage(M_FATAL, "HrESERestoreClose failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
478 rp->create_status = CF_CREATED;
481 rp->create_status = CF_CREATED;
488 storage_group_node_t::endRestoreFile(exchange_fd_context_t *context)
490 _DebugMessage(0, "endRestoreFile_STORAGE_GROUP state = %d\n", state);
500 context->current_node = parent;
503 context->current_node = parent;