3 * Dumb program to extract files from a Bacula backup.
11 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 #include "findlib/find.h"
41 static void do_extract(char *fname, char *prefix);
42 static void print_ls_output(char *fname, char *link, int type, struct stat *statp);
43 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
45 static DEVICE *dev = NULL;
50 static FF_PKT *ff = &my_ff;
52 static BSR *bsr = NULL;
54 static DEV_RECORD *rec;
55 static DEV_BLOCK *block;
60 "\nVersion: " VERSION " (" DATE ")\n\n"
61 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
62 " -b <file> specify a bootstrap file\n"
63 " -dnn set debug level to nn\n"
64 " -e <file> exclude list\n"
65 " -i <file> include list\n"
66 " -? print this message\n\n");
70 static void my_free_jcr(JCR *jcr)
76 int main (int argc, char *argv[])
83 my_name_is(argc, argv, "bextract");
84 init_msg(NULL, NULL); /* setup message handler */
86 memset(ff, 0, sizeof(FF_PKT));
87 init_include_exclude_files(ff);
89 while ((ch = getopt(argc, argv, "b:d:e:i:?")) != -1) {
91 case 'b': /* bootstrap file */
92 bsr = parse_bsr(NULL, optarg);
96 case 'd': /* debug level */
97 debug_level = atoi(optarg);
102 case 'e': /* exclude list */
103 if ((fd = fopen(optarg, "r")) == NULL) {
104 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
105 optarg, strerror(errno));
108 while (fgets(line, sizeof(line), fd) != NULL) {
109 strip_trailing_junk(line);
110 Dmsg1(900, "add_exclude %s\n", line);
111 add_fname_to_exclude_list(ff, line);
116 case 'i': /* include list */
117 if ((fd = fopen(optarg, "r")) == NULL) {
118 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
119 optarg, strerror(errno));
122 while (fgets(line, sizeof(line), fd) != NULL) {
123 strip_trailing_junk(line);
124 Dmsg1(900, "add_include %s\n", line);
125 add_fname_to_include_list(ff, 0, line);
141 Pmsg0(0, "Wrong number of arguments: \n");
144 if (!got_inc) { /* If no include file, */
145 add_fname_to_include_list(ff, 0, "/"); /* include everything */
148 jcr = new_jcr(sizeof(JCR), my_free_jcr);
149 jcr->VolSessionId = 1;
150 jcr->VolSessionTime = (uint32_t)time(NULL);
152 strcpy(jcr->Job, "bextract");
153 jcr->dev_name = get_pool_memory(PM_FNAME);
154 strcpy(jcr->dev_name, argv[0]);
156 do_extract(argv[0], argv[1]);
166 * Device got an error, attempt to analyse it
168 static void display_error_status()
172 Emsg0(M_ERROR, 0, dev->errmsg);
173 status_dev(dev, &status);
174 Dmsg1(20, "Device status: %x\n", status);
176 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
177 else if (status & MT_EOT)
178 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
179 else if (status & MT_EOF)
180 Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
181 else if (status & MT_DR_OPEN)
182 Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
183 else if (!(status & MT_ONLINE))
184 Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
186 Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
190 static void do_extract(char *devname, char *where)
197 long record_file_index;
199 POOLMEM *fname; /* original file name */
200 POOLMEM *ofile; /* output name with prefix */
201 POOLMEM *lname; /* link name */
202 int wherelen; /* prefix length */
203 SESSION_LABEL sessrec;
204 uint32_t num_files = 0;
207 if (strncmp(devname, "/dev/", 5) != 0) {
208 /* Try stripping file part */
209 p = devname + strlen(devname);
210 while (p >= devname && *p != '/') {
214 strcpy(VolName, p+1);
218 strcpy(jcr->VolumeName, VolName);
220 dev = init_dev(NULL, devname);
221 if (!dev || !open_device(dev)) {
222 Emsg1(M_ERROR_TERM, 0, "Cannot open %s\n", devname);
224 Dmsg0(90, "Device opened for read.\n");
226 if (stat(where, &statp) < 0) {
227 Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
228 where, strerror(errno));
230 if (!S_ISDIR(statp.st_mode)) {
231 Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
234 wherelen = strlen(where);
235 fname = get_pool_memory(PM_FNAME);
236 ofile = get_pool_memory(PM_FNAME);
237 lname = get_pool_memory(PM_FNAME);
239 block = new_block(dev);
241 create_vol_list(jcr);
243 Dmsg1(20, "Found %d volumes names to restore.\n", jcr->NumVolumes);
246 * Ready device for reading, and read records
248 if (!acquire_device_for_read(jcr, dev, block)) {
255 free_pool_memory(rec->data);
256 rec->data = get_memory(70000);
258 uint32_t compress_buf_size = 70000;
259 POOLMEM *compress_buf = get_memory(compress_buf_size);
262 if (!read_block_from_device(dev, block)) {
263 Dmsg1(500, "Main read record failed. rem=%d\n", rec->remainder);
264 if (dev->state & ST_EOT) {
266 if (!mount_next_read_volume(jcr, dev, block)) {
269 record = new_record();
270 read_block_from_device(dev, block);
271 read_record_from_block(block, record);
272 get_session_record(dev, record, &sessrec);
276 if (dev->state & ST_EOF) {
277 continue; /* try again */
279 if (dev->state & ST_SHORT) {
282 display_error_status();
286 for (rec->state=0; !is_block_empty(rec); ) {
287 if (!read_record_from_block(block, rec)) {
291 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
292 Dmsg0(40, "Get EOM LABEL\n");
294 break; /* yes, get out */
297 /* Some sort of label? */
298 if (rec->FileIndex < 0) {
299 get_session_record(dev, rec, &sessrec);
301 } /* end if label record */
303 /* Is this the file we want? */
304 if (bsr && !match_bsr(bsr, rec, &dev->VolHdr, &sessrec)) {
308 if (is_partial_record(rec)) {
312 /* File Attributes stream */
313 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
316 /* If extracting, it was from previous stream, so
317 * close the output file.
321 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
326 set_statp(jcr, fname, ofile, lname, type, &statp);
329 if (sizeof_pool_memory(fname) < rec->data_len) {
330 fname = realloc_pool_memory(fname, rec->data_len + 1);
332 if (sizeof_pool_memory(ofile) < rec->data_len + wherelen + 1) {
333 ofile = realloc_pool_memory(ofile, rec->data_len + wherelen + 1);
335 if (sizeof_pool_memory(lname) < rec->data_len) {
336 lname = realloc_pool_memory(lname, rec->data_len + wherelen + 1);
342 * An Attributes record consists of:
347 * Link name (if file linked i.e. FT_LNK)
350 sscanf(rec->data, "%ld %d", &record_file_index, &type);
351 if (record_file_index != rec->FileIndex)
352 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
353 rec->FileIndex, record_file_index);
355 while (*ap++ != ' ') /* skip record file index */
357 while (*ap++ != ' ') /* skip type */
359 /* Save filename and position to attributes */
364 *fp = *ap++; /* terminate filename & point to attribs */
366 /* Skip to Link name */
367 if (type == FT_LNK || type == FT_LNKSAVED) {
377 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
379 decode_stat(ap, &statp);
381 * Prepend the where directory so that the
382 * files are put where the user wants.
384 * We do a little jig here to handle Win32 files with
386 * If where is null and we are running on a win32 client,
388 * Otherwise, if the second character of the filename is a
389 * colon (:), change it into a slash (/) -- this creates
390 * a reasonable pathname on most systems.
392 if (where[0] == 0 && win32_client) {
393 strcpy(ofile, fname);
396 strcpy(ofile, where);
397 if (fname[1] == ':') {
399 strcat(ofile, fname);
402 strcat(ofile, fname);
404 /* Fixup link name */
405 if (type == FT_LNK || type == FT_LNKSAVED) {
406 if (lp[0] == '/') { /* if absolute path */
407 strcpy(lname, where);
409 /* ***FIXME**** we shouldn't have links on Windoz */
420 /* Pmsg1(000, "Restoring: %s\n", ofile); */
422 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
426 print_ls_output(ofile, lname, type, &statp);
430 /* Data stream and extracting */
431 } else if (rec->Stream == STREAM_FILE_DATA) {
433 total += rec->data_len;
434 Dmsg2(8, "Write %ld bytes, total=%ld\n", rec->data_len, total);
435 if ((uint32_t)write(ofd, rec->data, rec->data_len) != rec->data_len) {
436 Emsg1(M_ERROR_TERM, 0, "Write error: %s\n", strerror(errno));
440 } else if (rec->Stream == STREAM_GZIP_DATA) {
445 compress_len = compress_buf_size;
446 if (uncompress((Bytef *)compress_buf, &compress_len,
447 (const Bytef *)rec->data, (uLong)rec->data_len) != Z_OK) {
448 Emsg0(M_ERROR_TERM, 0, _("Uncompression error.\n"));
451 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
452 if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
453 Pmsg0(0, "===Write error===\n");
454 Emsg2(M_ERROR_TERM, 0, "Write error on %s: %s\n", ofile, strerror(errno));
456 total += compress_len;
457 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
462 Emsg0(M_ERROR_TERM, 0, "GZIP data stream found, but GZIP not configured!\n");
467 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
468 } else if (extract) {
470 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
475 set_statp(jcr, fname, ofile, lname, type, &statp);
476 } else if (rec->Stream != STREAM_MD5_SIGNATURE) {
477 Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec->Stream, rec->data);
483 /* If output file is still open, it was the last one in the
484 * archive since we just hit an end of file, so close the file.
488 set_statp(jcr, fname, ofile, lname, type, &statp);
490 release_device(jcr, dev, block);
492 free_pool_memory(fname);
493 free_pool_memory(ofile);
494 free_pool_memory(lname);
495 free_pool_memory(compress_buf);
499 printf("%u files restored.\n", num_files);
503 extern char *getuser(uid_t uid);
504 extern char *getgroup(gid_t gid);
506 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
513 p = encode_mode(statp->st_mode, buf);
514 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
516 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
518 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
520 p = encode_time(statp->st_ctime, p);
524 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
526 if (type == FT_LNK) {
532 for (f=link; *f && (p-buf) < (int)sizeof(buf); )
540 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
543 memset(sessrec, 0, sizeof(sessrec));
544 switch (rec->FileIndex) {
546 rtype = "Fresh Volume Label";
549 rtype = "Volume Label";
550 unser_volume_label(dev, rec);
553 rtype = "Begin Session";
554 unser_session_label(sessrec, rec);
557 rtype = "End Session";
560 rtype = "End of Media";
566 Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
567 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
570 /* Dummies to replace askdir.c */
571 int dir_get_volume_info(JCR *jcr) { return 1;}
572 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
573 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
574 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
575 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
576 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
577 int dir_send_job_status(JCR *jcr) {return 1;}
580 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
582 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
583 jcr->VolumeName, dev_name(dev));