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)) {
139 static void print_findex(UAContext *ua, RBSR_FINDEX *fi)
141 ua->send_msg("fi=0x%lx\n", fi);
142 for ( ; fi; fi=fi->next) {
143 if (fi->findex == fi->findex2) {
144 ua->send_msg("FileIndex=%d\n", fi->findex);
145 Dmsg1(1000, "FileIndex=%d\n", fi->findex);
147 ua->send_msg("FileIndex=%d-%d\n", fi->findex, fi->findex2);
148 Dmsg2(1000, "FileIndex=%d-%d\n", fi->findex, fi->findex2);
153 /* Create a new bootstrap record */
156 RBSR *bsr = (RBSR *)bmalloc(sizeof(RBSR));
157 memset(bsr, 0, sizeof(RBSR));
161 /* Free the entire BSR */
162 void free_bsr(RBSR *bsr)
165 for ( ; bsr; bsr=next) {
166 free_findex(bsr->fi);
167 if (bsr->VolParams) {
168 free(bsr->VolParams);
176 * Complete the BSR by filling in the VolumeName and
177 * VolSessionId and VolSessionTime using the JobId
179 bool complete_bsr(UAContext *ua, RBSR *bsr)
181 for ( ; bsr; bsr=bsr->next) {
183 memset(&jr, 0, sizeof(jr));
184 jr.JobId = bsr->JobId;
185 if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
186 ua->error_msg(_("Unable to get Job record. ERR=%s\n"), db_strerror(ua->db));
189 bsr->VolSessionId = jr.VolSessionId;
190 bsr->VolSessionTime = jr.VolSessionTime;
191 if ((bsr->VolCount=db_get_job_volume_parameters(ua->jcr, ua->db, bsr->JobId,
192 &(bsr->VolParams))) == 0) {
193 ua->error_msg(_("Unable to get Job Volume Parameters. ERR=%s\n"), db_strerror(ua->db));
194 if (bsr->VolParams) {
195 free(bsr->VolParams);
196 bsr->VolParams = NULL;
204 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
205 static uint32_t uniq = 0;
207 static void make_unique_restore_filename(UAContext *ua, POOL_MEM &fname)
210 int i = find_arg_with_value(ua, "bootstrap");
212 Mmsg(fname, "%s", ua->argv[i]);
213 jcr->unlink_bsr = false;
218 Mmsg(fname, "%s/%s.restore.%u.bsr", working_directory, my_name, uniq);
219 jcr->unlink_bsr = true;
221 if (jcr->RestoreBootstrap) {
222 free(jcr->RestoreBootstrap);
224 jcr->RestoreBootstrap = bstrdup(fname.c_str());
228 * Write the bootstrap records to file
230 uint32_t write_bsr_file(UAContext *ua, RESTORE_CTX &rx)
233 POOL_MEM fname(PM_MESSAGE);
237 make_unique_restore_filename(ua, fname);
238 fd = fopen(fname.c_str(), "w+b");
241 ua->error_msg(_("Unable to create bootstrap file %s. ERR=%s\n"),
242 fname.c_str(), be.bstrerror());
245 /* Write them to file */
246 count = write_bsr(ua, rx, fd);
250 ua->info_msg(_("No files found to read. No bootstrap file written.\n"));
254 ua->error_msg(_("Error writing bsr file.\n"));
259 ua->send_msg(_("Bootstrap records written to %s\n"), fname.c_str());
265 void display_bsr_info(UAContext *ua, RESTORE_CTX &rx)
268 POOL_MEM volmsg(PM_MESSAGE);
270 char Device[MAX_NAME_LENGTH];
272 /* Tell the user what he will need to mount */
274 ua->send_msg(_("The job will require the following\n"
275 " Volume(s) Storage(s) SD Device(s)\n"
276 "===========================================================================\n"));
277 /* Create Unique list of Volumes using prompt list */
278 start_prompt(ua, "");
280 /* Ensure that the volumes are printed in JobId order */
281 for (p=rx.JobIds; get_next_jobid_from_list(&p, &JobId) > 0; ) {
282 for (RBSR *nbsr=rx.bsr; nbsr; nbsr=nbsr->next) {
283 if (JobId != nbsr->JobId) {
286 for (int i=0; i < nbsr->VolCount; i++) {
287 if (nbsr->VolParams[i].VolumeName[0]) {
288 if (!get_storage_device(Device, nbsr->VolParams[i].Storage)) {
291 Mmsg(volmsg, "%-25.25s %-25.25s %-25.25s",
292 nbsr->VolParams[i].VolumeName,
293 nbsr->VolParams[i].Storage, Device);
294 add_prompt(ua, volmsg.c_str());
300 /* Print Volumes in any order */
301 for (RBSR *nbsr=rx.bsr; nbsr; nbsr=nbsr->next) {
302 for (int i=0; i < nbsr->VolCount; i++) {
303 if (nbsr->VolParams[i].VolumeName[0]) {
304 if (!get_storage_device(Device, nbsr->VolParams[i].Storage)) {
307 Mmsg(volmsg, "%-25.25s %-25.25s %-25.25s",
308 nbsr->VolParams[i].VolumeName,
309 nbsr->VolParams[i].Storage, Device);
310 add_prompt(ua, volmsg.c_str());
315 for (int i=0; i < ua->num_prompts; i++) {
316 ua->send_msg(" %s\n", ua->prompt[i]);
319 if (ua->num_prompts == 0) {
320 ua->send_msg(_("No Volumes found to restore.\n"));
329 * Here we actually write out the details of the bsr file.
330 * Note, there is one bsr for each JobId, but the bsr may
331 * have multiple volumes, which have been entered in the
332 * order they were written.
333 * The bsrs must be written out in the order the JobIds
334 * are found in the jobid list.
336 static uint32_t write_bsr(UAContext *ua, RESTORE_CTX &rx, FILE *fd)
339 uint32_t total_count = 0;
340 uint32_t LastIndex = 0;
344 char device[MAX_NAME_LENGTH];
346 if (*rx.JobIds == 0) {
347 for (bsr=rx.bsr; bsr; bsr=bsr->next) {
349 * For a given volume, loop over all the JobMedia records.
350 * VolCount is the number of JobMedia records.
352 for (int i=0; i < bsr->VolCount; i++) {
353 if (!is_volume_selected(bsr->fi, bsr->VolParams[i].FirstIndex,
354 bsr->VolParams[i].LastIndex)) {
355 bsr->VolParams[i].VolumeName[0] = 0; /* zap VolumeName */
359 find_storage_resource(ua, rx, bsr->VolParams[i].Storage,
360 bsr->VolParams[i].MediaType);
362 fprintf(fd, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
363 fprintf(fd, "MediaType=\"%s\"\n", bsr->VolParams[i].MediaType);
364 if (get_storage_device(device, bsr->VolParams[i].Storage)) {
365 fprintf(fd, "Device=\"%s\"\n", device);
367 if (bsr->VolParams[i].Slot > 0) {
368 fprintf(fd, "Slot=%d\n", bsr->VolParams[i].Slot);
370 fprintf(fd, "VolSessionId=%u\n", bsr->VolSessionId);
371 fprintf(fd, "VolSessionTime=%u\n", bsr->VolSessionTime);
372 if (bsr->VolParams[i].StartFile == bsr->VolParams[i].EndFile) {
373 fprintf(fd, "VolFile=%u\n", bsr->VolParams[i].StartFile);
375 fprintf(fd, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
376 bsr->VolParams[i].EndFile);
378 if (bsr->VolParams[i].StartBlock == bsr->VolParams[i].EndBlock) {
379 fprintf(fd, "VolBlock=%u\n", bsr->VolParams[i].StartBlock);
381 fprintf(fd, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
382 bsr->VolParams[i].EndBlock);
384 // Dmsg2(100, "bsr VolParam FI=%u LI=%u\n",
385 // bsr->VolParams[i].FirstIndex, bsr->VolParams[i].LastIndex);
387 count = write_findex(bsr->fi, bsr->VolParams[i].FirstIndex,
388 bsr->VolParams[i].LastIndex, fd);
390 fprintf(fd, "Count=%u\n", count);
392 total_count += count;
393 /* If the same file is present on two tapes or in two files
394 * on a tape, it is a continuation, and should not be treated
395 * twice in the totals.
397 if (!first && LastIndex == bsr->VolParams[i].FirstIndex) {
401 LastIndex = bsr->VolParams[i].LastIndex;
406 for (p=rx.JobIds; get_next_jobid_from_list(&p, &JobId) > 0; ) {
407 for (bsr=rx.bsr; bsr; bsr=bsr->next) {
408 if (JobId != bsr->JobId) {
412 * For a given volume, loop over all the JobMedia records.
413 * VolCount is the number of JobMedia records.
415 for (int i=0; i < bsr->VolCount; i++) {
416 if (!is_volume_selected(bsr->fi, bsr->VolParams[i].FirstIndex,
417 bsr->VolParams[i].LastIndex)) {
418 bsr->VolParams[i].VolumeName[0] = 0; /* zap VolumeName */
422 find_storage_resource(ua, rx, bsr->VolParams[i].Storage,
423 bsr->VolParams[i].MediaType);
425 fprintf(fd, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
426 fprintf(fd, "MediaType=\"%s\"\n", bsr->VolParams[i].MediaType);
427 if (get_storage_device(device, bsr->VolParams[i].Storage)) {
428 fprintf(fd, "Device=\"%s\"\n", device);
430 if (bsr->VolParams[i].Slot > 0) {
431 fprintf(fd, "Slot=%d\n", bsr->VolParams[i].Slot);
433 fprintf(fd, "VolSessionId=%u\n", bsr->VolSessionId);
434 fprintf(fd, "VolSessionTime=%u\n", bsr->VolSessionTime);
435 if (bsr->VolParams[i].StartFile == bsr->VolParams[i].EndFile) {
436 fprintf(fd, "VolFile=%u\n", bsr->VolParams[i].StartFile);
438 fprintf(fd, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
439 bsr->VolParams[i].EndFile);
441 if (bsr->VolParams[i].StartBlock == bsr->VolParams[i].EndBlock) {
442 fprintf(fd, "VolBlock=%u\n", bsr->VolParams[i].StartBlock);
444 fprintf(fd, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
445 bsr->VolParams[i].EndBlock);
447 // Dmsg2(100, "bsr VolParam FI=%u LI=%u\n",
448 // bsr->VolParams[i].FirstIndex, bsr->VolParams[i].LastIndex);
450 count = write_findex(bsr->fi, bsr->VolParams[i].FirstIndex,
451 bsr->VolParams[i].LastIndex, fd);
453 fprintf(fd, "Count=%u\n", count);
455 total_count += count;
456 /* If the same file is present on two tapes or in two files
457 * on a tape, it is a continuation, and should not be treated
458 * twice in the totals.
460 if (!first && LastIndex == bsr->VolParams[i].FirstIndex) {
464 LastIndex = bsr->VolParams[i].LastIndex;
471 void print_bsr(UAContext *ua, RBSR *bsr)
473 for ( ; bsr; bsr=bsr->next) {
474 for (int i=0; i < bsr->VolCount; i++) {
475 ua->send_msg("Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
476 ua->send_msg("MediaType\"%s\"\n", bsr->VolParams[i].MediaType);
477 ua->send_msg("VolSessionId=%u\n", bsr->VolSessionId);
478 ua->send_msg("VolSessionTime=%u\n", bsr->VolSessionTime);
479 ua->send_msg("VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
480 bsr->VolParams[i].EndFile);
481 ua->send_msg("VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
482 bsr->VolParams[i].EndBlock);
483 print_findex(ua, bsr->fi);
485 print_bsr(ua, bsr->next);
493 * Add a FileIndex to the list of BootStrap records.
494 * Here we are only dealing with JobId's and the FileIndexes
495 * associated with those JobIds.
496 * We expect that JobId, FileIndex are sorted ascending.
498 void add_findex(RBSR *bsr, uint32_t JobId, int32_t findex)
501 RBSR_FINDEX *fi, *lfi;
504 return; /* probably a dummy directory */
507 if (bsr->fi == NULL) { /* if no FI add one */
508 /* This is the first FileIndex item in the chain */
509 bsr->fi = new_findex();
511 bsr->fi->findex = findex;
512 bsr->fi->findex2 = findex;
515 /* Walk down list of bsrs until we find the JobId */
516 if (bsr->JobId != JobId) {
517 for (nbsr=bsr->next; nbsr; nbsr=nbsr->next) {
518 if (nbsr->JobId == JobId) {
524 if (!nbsr) { /* Must add new JobId */
525 /* Add new JobId at end of chain */
526 for (nbsr=bsr; nbsr->next; nbsr=nbsr->next)
528 nbsr->next = new_bsr();
529 nbsr->next->JobId = JobId;
530 nbsr->next->fi = new_findex();
531 nbsr->next->fi->findex = findex;
532 nbsr->next->fi->findex2 = findex;
538 * At this point, bsr points to bsr containing this JobId,
539 * and we are sure that there is at least one fi record.
542 /* Check if this findex is smaller than first item */
543 if (findex < fi->findex) {
544 if ((findex+1) == fi->findex) {
545 fi->findex = findex; /* extend down */
548 fi = new_findex(); /* yes, insert before first item */
550 fi->findex2 = findex;
555 /* Walk down fi chain and find where to insert insert new FileIndex */
556 for ( ; fi; fi=fi->next) {
557 if (findex == (fi->findex2 + 1)) { /* extend up */
559 fi->findex2 = findex;
561 * If the following record contains one higher, merge its
562 * file index by extending it up.
564 if (fi->next && ((findex+1) == fi->next->findex)) {
566 fi->findex2 = nfi->findex2;
567 fi->next = nfi->next;
572 if (findex < fi->findex) { /* add before */
573 if ((findex+1) == fi->findex) {
581 /* Add to last place found */
584 fi->findex2 = findex;
585 fi->next = lfi->next;
591 * Add all possible FileIndexes to the list of BootStrap records.
592 * Here we are only dealing with JobId's and the FileIndexes
593 * associated with those JobIds.
595 void add_findex_all(RBSR *bsr, uint32_t JobId)
600 if (bsr->fi == NULL) { /* if no FI add one */
601 /* This is the first FileIndex item in the chain */
602 bsr->fi = new_findex();
605 bsr->fi->findex2 = INT32_MAX;
608 /* Walk down list of bsrs until we find the JobId */
609 if (bsr->JobId != JobId) {
610 for (nbsr=bsr->next; nbsr; nbsr=nbsr->next) {
611 if (nbsr->JobId == JobId) {
617 if (!nbsr) { /* Must add new JobId */
618 /* Add new JobId at end of chain */
619 for (nbsr=bsr; nbsr->next; nbsr=nbsr->next)
621 nbsr->next = new_bsr();
622 nbsr->next->JobId = JobId;
623 nbsr->next->fi = new_findex();
624 nbsr->next->fi->findex = 1;
625 nbsr->next->fi->findex2 = INT32_MAX;
631 * At this point, bsr points to bsr containing this JobId,
632 * and we are sure that there is at least one fi record.
636 fi->findex2 = INT32_MAX;