2 Bacula® - The Network Backup Solution
4 Copyright (C) 2002-2008 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 two of the GNU General Public
10 License as published by the Free Software Foundation and included
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 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.
30 * Bacula Director -- Bootstrap Record routines.
32 * BSR (bootstrap record) handling routines split from
33 * ua_restore.c July MMIII
35 * Kern Sibbald, July MMII
43 /* Forward referenced functions */
44 static uint32_t write_bsr(UAContext *ua, RESTORE_CTX &rx, FILE *fd);
47 * Create new FileIndex entry for BSR
49 RBSR_FINDEX *new_findex()
51 RBSR_FINDEX *fi = (RBSR_FINDEX *)bmalloc(sizeof(RBSR_FINDEX));
52 memset(fi, 0, sizeof(RBSR_FINDEX));
56 /* Free all BSR FileIndex entries */
57 static void free_findex(RBSR_FINDEX *fi)
60 for ( ; fi; fi=next) {
67 * Get storage device name from Storage resource
69 static bool get_storage_device(char *device, char *storage)
72 if (storage[0] == 0) {
75 store = (STORE *)GetResWithName(R_STORAGE, storage);
79 DEVICE *dev = (DEVICE *)(store->device->first());
83 bstrncpy(device, dev->hdr.name, MAX_NAME_LENGTH);
88 * Our data structures were not designed completely
89 * correctly, so the file indexes cover the full
90 * range regardless of volume. The FirstIndex and LastIndex
91 * passed in here are for the current volume, so when
92 * writing out the fi, constrain them to those values.
94 * We are called here once for each JobMedia record
97 static uint32_t write_findex(RBSR_FINDEX *fi,
98 int32_t FirstIndex, int32_t LastIndex, FILE *fd)
101 for ( ; fi; fi=fi->next) {
102 int32_t findex, findex2;
103 if ((fi->findex >= FirstIndex && fi->findex <= LastIndex) ||
104 (fi->findex2 >= FirstIndex && fi->findex2 <= LastIndex) ||
105 (fi->findex < FirstIndex && fi->findex2 > LastIndex)) {
106 findex = fi->findex < FirstIndex ? FirstIndex : fi->findex;
107 findex2 = fi->findex2 > LastIndex ? LastIndex : fi->findex2;
108 if (findex == findex2) {
109 fprintf(fd, "FileIndex=%d\n", findex);
112 fprintf(fd, "FileIndex=%d-%d\n", findex, findex2);
113 count += findex2 - findex + 1;
121 * Find out if Volume defined with FirstIndex and LastIndex
122 * falls within the range of selected files in the bsr.
124 static bool is_volume_selected(RBSR_FINDEX *fi,
125 int32_t FirstIndex, int32_t LastIndex)
127 for ( ; fi; fi=fi->next) {
128 if ((fi->findex >= FirstIndex && fi->findex <= LastIndex) ||
129 (fi->findex2 >= FirstIndex && fi->findex2 <= LastIndex) ||
130 (fi->findex < FirstIndex && fi->findex2 > LastIndex)) {
138 /* Create a new bootstrap record */
141 RBSR *bsr = (RBSR *)bmalloc(sizeof(RBSR));
142 memset(bsr, 0, sizeof(RBSR));
146 /* Free the entire BSR */
147 void free_bsr(RBSR *bsr)
150 for ( ; bsr; bsr=next) {
151 free_findex(bsr->fi);
152 if (bsr->VolParams) {
153 free(bsr->VolParams);
161 * Complete the BSR by filling in the VolumeName and
162 * VolSessionId and VolSessionTime using the JobId
164 bool complete_bsr(UAContext *ua, RBSR *bsr)
166 for ( ; bsr; bsr=bsr->next) {
168 memset(&jr, 0, sizeof(jr));
169 jr.JobId = bsr->JobId;
170 if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
171 ua->error_msg(_("Unable to get Job record. ERR=%s\n"), db_strerror(ua->db));
174 bsr->VolSessionId = jr.VolSessionId;
175 bsr->VolSessionTime = jr.VolSessionTime;
176 if ((bsr->VolCount=db_get_job_volume_parameters(ua->jcr, ua->db, bsr->JobId,
177 &(bsr->VolParams))) == 0) {
178 ua->error_msg(_("Unable to get Job Volume Parameters. ERR=%s\n"), db_strerror(ua->db));
179 if (bsr->VolParams) {
180 free(bsr->VolParams);
181 bsr->VolParams = NULL;
189 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
190 static uint32_t uniq = 0;
192 static void make_unique_restore_filename(UAContext *ua, POOL_MEM &fname)
195 int i = find_arg_with_value(ua, "bootstrap");
197 Mmsg(fname, "%s", ua->argv[i]);
198 jcr->unlink_bsr = false;
203 Mmsg(fname, "%s/%s.restore.%u.bsr", working_directory, my_name, uniq);
204 jcr->unlink_bsr = true;
206 if (jcr->RestoreBootstrap) {
207 free(jcr->RestoreBootstrap);
209 jcr->RestoreBootstrap = bstrdup(fname.c_str());
213 * Write the bootstrap records to file
215 uint32_t write_bsr_file(UAContext *ua, RESTORE_CTX &rx)
218 POOL_MEM fname(PM_MESSAGE);
222 make_unique_restore_filename(ua, fname);
223 fd = fopen(fname.c_str(), "w+b");
226 ua->error_msg(_("Unable to create bootstrap file %s. ERR=%s\n"),
227 fname.c_str(), be.bstrerror());
230 /* Write them to file */
231 count = write_bsr(ua, rx, fd);
235 ua->info_msg(_("No files found to read. No bootstrap file written.\n"));
239 ua->error_msg(_("Error writing bsr file.\n"));
244 ua->send_msg(_("Bootstrap records written to %s\n"), fname.c_str());
246 if (debug_level >= 10) {
254 void display_bsr_info(UAContext *ua, RESTORE_CTX &rx)
257 POOL_MEM volmsg(PM_MESSAGE);
259 char Device[MAX_NAME_LENGTH];
262 /* Tell the user what he will need to mount */
264 ua->send_msg(_("The job will require the following\n"
265 " Volume(s) Storage(s) SD Device(s)\n"
266 "===========================================================================\n"));
267 /* Create Unique list of Volumes using prompt list */
268 start_prompt(ua, "");
269 if (*rx.JobIds == 0) {
270 /* Print Volumes in any order */
271 for (bsr=rx.bsr; bsr; bsr=bsr->next) {
272 for (int i=0; i < bsr->VolCount; i++) {
273 if (bsr->VolParams[i].VolumeName[0]) {
274 if (!get_storage_device(Device, bsr->VolParams[i].Storage)) {
277 Mmsg(volmsg, "%-25.25s %-25.25s %-25.25s",
278 bsr->VolParams[i].VolumeName,
279 bsr->VolParams[i].Storage, Device);
280 add_prompt(ua, volmsg.c_str());
285 /* Ensure that the volumes are printed in JobId order */
286 for (p=rx.JobIds; get_next_jobid_from_list(&p, &JobId) > 0; ) {
287 for (bsr=rx.bsr; bsr; bsr=bsr->next) {
288 if (JobId != bsr->JobId) {
291 for (int i=0; i < bsr->VolCount; i++) {
292 if (bsr->VolParams[i].VolumeName[0]) {
293 if (!get_storage_device(Device, bsr->VolParams[i].Storage)) {
296 Mmsg(volmsg, "%-25.25s %-25.25s %-25.25s",
297 bsr->VolParams[i].VolumeName,
298 bsr->VolParams[i].Storage, Device);
299 add_prompt(ua, volmsg.c_str());
305 for (int i=0; i < ua->num_prompts; i++) {
306 ua->send_msg(" %s\n", ua->prompt[i]);
309 if (ua->num_prompts == 0) {
310 ua->send_msg(_("No Volumes found to restore.\n"));
319 * Here we actually write out the details of the bsr file.
320 * Note, there is one bsr for each JobId, but the bsr may
321 * have multiple volumes, which have been entered in the
322 * order they were written.
323 * The bsrs must be written out in the order the JobIds
324 * are found in the jobid list.
326 static uint32_t write_bsr(UAContext *ua, RESTORE_CTX &rx, FILE *fd)
329 uint32_t total_count = 0;
330 uint32_t LastIndex = 0;
334 char device[MAX_NAME_LENGTH];
336 if (*rx.JobIds == 0) {
337 for (bsr=rx.bsr; bsr; bsr=bsr->next) {
339 * For a given volume, loop over all the JobMedia records.
340 * VolCount is the number of JobMedia records.
342 for (int i=0; i < bsr->VolCount; i++) {
343 if (!is_volume_selected(bsr->fi, bsr->VolParams[i].FirstIndex,
344 bsr->VolParams[i].LastIndex)) {
345 bsr->VolParams[i].VolumeName[0] = 0; /* zap VolumeName */
349 find_storage_resource(ua, rx, bsr->VolParams[i].Storage,
350 bsr->VolParams[i].MediaType);
352 fprintf(fd, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
353 fprintf(fd, "MediaType=\"%s\"\n", bsr->VolParams[i].MediaType);
354 if (get_storage_device(device, bsr->VolParams[i].Storage)) {
355 fprintf(fd, "Device=\"%s\"\n", device);
357 if (bsr->VolParams[i].Slot > 0) {
358 fprintf(fd, "Slot=%d\n", bsr->VolParams[i].Slot);
360 fprintf(fd, "VolSessionId=%u\n", bsr->VolSessionId);
361 fprintf(fd, "VolSessionTime=%u\n", bsr->VolSessionTime);
362 if (bsr->VolParams[i].StartFile == bsr->VolParams[i].EndFile) {
363 fprintf(fd, "VolFile=%u\n", bsr->VolParams[i].StartFile);
365 fprintf(fd, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
366 bsr->VolParams[i].EndFile);
368 if (bsr->VolParams[i].StartBlock == bsr->VolParams[i].EndBlock) {
369 fprintf(fd, "VolBlock=%u\n", bsr->VolParams[i].StartBlock);
371 fprintf(fd, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
372 bsr->VolParams[i].EndBlock);
374 // Dmsg2(100, "bsr VolParam FI=%u LI=%u\n",
375 // bsr->VolParams[i].FirstIndex, bsr->VolParams[i].LastIndex);
377 count = write_findex(bsr->fi, bsr->VolParams[i].FirstIndex,
378 bsr->VolParams[i].LastIndex, fd);
380 fprintf(fd, "Count=%u\n", count);
382 total_count += count;
383 /* If the same file is present on two tapes or in two files
384 * on a tape, it is a continuation, and should not be treated
385 * twice in the totals.
387 if (!first && LastIndex == bsr->VolParams[i].FirstIndex) {
391 LastIndex = bsr->VolParams[i].LastIndex;
396 for (p=rx.JobIds; get_next_jobid_from_list(&p, &JobId) > 0; ) {
397 for (bsr=rx.bsr; bsr; bsr=bsr->next) {
398 if (JobId != bsr->JobId) {
402 * For a given volume, loop over all the JobMedia records.
403 * VolCount is the number of JobMedia records.
405 for (int i=0; i < bsr->VolCount; i++) {
406 if (!is_volume_selected(bsr->fi, bsr->VolParams[i].FirstIndex,
407 bsr->VolParams[i].LastIndex)) {
408 bsr->VolParams[i].VolumeName[0] = 0; /* zap VolumeName */
412 find_storage_resource(ua, rx, bsr->VolParams[i].Storage,
413 bsr->VolParams[i].MediaType);
415 fprintf(fd, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
416 fprintf(fd, "MediaType=\"%s\"\n", bsr->VolParams[i].MediaType);
417 if (get_storage_device(device, bsr->VolParams[i].Storage)) {
418 fprintf(fd, "Device=\"%s\"\n", device);
420 if (bsr->VolParams[i].Slot > 0) {
421 fprintf(fd, "Slot=%d\n", bsr->VolParams[i].Slot);
423 fprintf(fd, "VolSessionId=%u\n", bsr->VolSessionId);
424 fprintf(fd, "VolSessionTime=%u\n", bsr->VolSessionTime);
425 if (bsr->VolParams[i].StartFile == bsr->VolParams[i].EndFile) {
426 fprintf(fd, "VolFile=%u\n", bsr->VolParams[i].StartFile);
428 fprintf(fd, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
429 bsr->VolParams[i].EndFile);
431 if (bsr->VolParams[i].StartBlock == bsr->VolParams[i].EndBlock) {
432 fprintf(fd, "VolBlock=%u\n", bsr->VolParams[i].StartBlock);
434 fprintf(fd, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
435 bsr->VolParams[i].EndBlock);
437 // Dmsg2(100, "bsr VolParam FI=%u LI=%u\n",
438 // bsr->VolParams[i].FirstIndex, bsr->VolParams[i].LastIndex);
440 count = write_findex(bsr->fi, bsr->VolParams[i].FirstIndex,
441 bsr->VolParams[i].LastIndex, fd);
443 fprintf(fd, "Count=%u\n", count);
445 total_count += count;
446 /* If the same file is present on two tapes or in two files
447 * on a tape, it is a continuation, and should not be treated
448 * twice in the totals.
450 if (!first && LastIndex == bsr->VolParams[i].FirstIndex) {
454 LastIndex = bsr->VolParams[i].LastIndex;
461 void print_bsr(UAContext *ua, RESTORE_CTX &rx)
463 write_bsr(ua, rx, stdout);
470 * Add a FileIndex to the list of BootStrap records.
471 * Here we are only dealing with JobId's and the FileIndexes
472 * associated with those JobIds.
473 * We expect that JobId, FileIndex are sorted ascending.
475 void add_findex(RBSR *bsr, uint32_t JobId, int32_t findex)
478 RBSR_FINDEX *fi, *lfi;
481 return; /* probably a dummy directory */
484 if (bsr->fi == NULL) { /* if no FI add one */
485 /* This is the first FileIndex item in the chain */
486 bsr->fi = new_findex();
488 bsr->fi->findex = findex;
489 bsr->fi->findex2 = findex;
492 /* Walk down list of bsrs until we find the JobId */
493 if (bsr->JobId != JobId) {
494 for (nbsr=bsr->next; nbsr; nbsr=nbsr->next) {
495 if (nbsr->JobId == JobId) {
501 if (!nbsr) { /* Must add new JobId */
502 /* Add new JobId at end of chain */
503 for (nbsr=bsr; nbsr->next; nbsr=nbsr->next)
505 nbsr->next = new_bsr();
506 nbsr->next->JobId = JobId;
507 nbsr->next->fi = new_findex();
508 nbsr->next->fi->findex = findex;
509 nbsr->next->fi->findex2 = findex;
515 * At this point, bsr points to bsr containing this JobId,
516 * and we are sure that there is at least one fi record.
519 /* Check if this findex is smaller than first item */
520 if (findex < fi->findex) {
521 if ((findex+1) == fi->findex) {
522 fi->findex = findex; /* extend down */
525 fi = new_findex(); /* yes, insert before first item */
527 fi->findex2 = findex;
532 /* Walk down fi chain and find where to insert insert new FileIndex */
533 for ( ; fi; fi=fi->next) {
534 if (findex == (fi->findex2 + 1)) { /* extend up */
536 fi->findex2 = findex;
538 * If the following record contains one higher, merge its
539 * file index by extending it up.
541 if (fi->next && ((findex+1) == fi->next->findex)) {
543 fi->findex2 = nfi->findex2;
544 fi->next = nfi->next;
549 if (findex < fi->findex) { /* add before */
550 if ((findex+1) == fi->findex) {
558 /* Add to last place found */
561 fi->findex2 = findex;
562 fi->next = lfi->next;
568 * Add all possible FileIndexes to the list of BootStrap records.
569 * Here we are only dealing with JobId's and the FileIndexes
570 * associated with those JobIds.
572 void add_findex_all(RBSR *bsr, uint32_t JobId)
577 if (bsr->fi == NULL) { /* if no FI add one */
578 /* This is the first FileIndex item in the chain */
579 bsr->fi = new_findex();
582 bsr->fi->findex2 = INT32_MAX;
585 /* Walk down list of bsrs until we find the JobId */
586 if (bsr->JobId != JobId) {
587 for (nbsr=bsr->next; nbsr; nbsr=nbsr->next) {
588 if (nbsr->JobId == JobId) {
594 if (!nbsr) { /* Must add new JobId */
595 /* Add new JobId at end of chain */
596 for (nbsr=bsr; nbsr->next; nbsr=nbsr->next)
598 nbsr->next = new_bsr();
599 nbsr->next->JobId = JobId;
600 nbsr->next->fi = new_findex();
601 nbsr->next->fi->findex = 1;
602 nbsr->next->fi->findex2 = INT32_MAX;
608 * At this point, bsr points to bsr containing this JobId,
609 * and we are sure that there is at least one fi record.
613 fi->findex2 = INT32_MAX;