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;
206 if (strncmp(devname, "/dev/", 5) != 0) {
207 /* Try stripping file part */
208 p = devname + strlen(devname);
209 while (p >= devname && *p != '/') {
213 strcpy(VolName, p+1);
217 strcpy(jcr->VolumeName, VolName);
219 dev = init_dev(NULL, devname);
220 if (!dev || !open_device(dev)) {
221 Emsg1(M_ERROR_TERM, 0, "Cannot open %s\n", devname);
223 Dmsg0(90, "Device opened for read.\n");
225 if (stat(where, &statp) < 0) {
226 Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
227 where, strerror(errno));
229 if (!S_ISDIR(statp.st_mode)) {
230 Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
233 wherelen = strlen(where);
234 fname = get_pool_memory(PM_FNAME);
235 ofile = get_pool_memory(PM_FNAME);
236 lname = get_pool_memory(PM_FNAME);
238 block = new_block(dev);
240 create_vol_list(jcr);
242 Dmsg1(20, "Found %d volumes names to restore.\n", jcr->NumVolumes);
245 * Ready device for reading, and read records
247 if (!acquire_device_for_read(jcr, dev, block)) {
254 free_pool_memory(rec->data);
255 rec->data = get_memory(70000);
257 uint32_t compress_buf_size = 70000;
258 POOLMEM *compress_buf = get_memory(compress_buf_size);
261 if (!read_block_from_device(dev, block)) {
262 Dmsg1(500, "Main read record failed. rem=%d\n", rec->remainder);
263 if (dev->state & ST_EOT) {
265 if (!mount_next_read_volume(jcr, dev, block)) {
268 record = new_record();
269 read_block_from_device(dev, block);
270 read_record_from_block(block, record);
271 get_session_record(dev, record, &sessrec);
275 if (dev->state & ST_EOF) {
276 continue; /* try again */
278 if (dev->state & ST_SHORT) {
281 display_error_status();
285 for (rec->state=0; !is_block_empty(rec); ) {
286 if (!read_record_from_block(block, rec)) {
290 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
291 Dmsg0(40, "Get EOM LABEL\n");
293 break; /* yes, get out */
296 /* Some sort of label? */
297 if (rec->FileIndex < 0) {
298 get_session_record(dev, rec, &sessrec);
300 } /* end if label record */
302 /* Is this the file we want? */
303 if (bsr && !match_bsr(bsr, rec, &dev->VolHdr, &sessrec)) {
307 if (is_partial_record(rec)) {
311 /* File Attributes stream */
312 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
315 /* If extracting, it was from previous stream, so
316 * close the output file.
320 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
325 set_statp(jcr, fname, ofile, lname, type, &statp);
328 if (sizeof_pool_memory(fname) < rec->data_len) {
329 fname = realloc_pool_memory(fname, rec->data_len + 1);
331 if (sizeof_pool_memory(ofile) < rec->data_len + wherelen + 1) {
332 ofile = realloc_pool_memory(ofile, rec->data_len + wherelen + 1);
334 if (sizeof_pool_memory(lname) < rec->data_len) {
335 lname = realloc_pool_memory(lname, rec->data_len + wherelen + 1);
341 * An Attributes record consists of:
346 * Link name (if file linked i.e. FT_LNK)
349 sscanf(rec->data, "%ld %d", &record_file_index, &type);
350 if (record_file_index != rec->FileIndex)
351 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
352 rec->FileIndex, record_file_index);
354 while (*ap++ != ' ') /* skip record file index */
356 while (*ap++ != ' ') /* skip type */
358 /* Save filename and position to attributes */
363 *fp = *ap++; /* terminate filename & point to attribs */
365 /* Skip to Link name */
366 if (type == FT_LNK || type == FT_LNKSAVED) {
376 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
378 decode_stat(ap, &statp);
380 * Prepend the where directory so that the
381 * files are put where the user wants.
383 * We do a little jig here to handle Win32 files with
385 * If where is null and we are running on a win32 client,
387 * Otherwise, if the second character of the filename is a
388 * colon (:), change it into a slash (/) -- this creates
389 * a reasonable pathname on most systems.
391 if (where[0] == 0 && win32_client) {
392 strcpy(ofile, fname);
395 strcpy(ofile, where);
396 if (fname[1] == ':') {
398 strcat(ofile, fname);
401 strcat(ofile, fname);
403 /* Fixup link name */
404 if (type == FT_LNK || type == FT_LNKSAVED) {
405 if (lp[0] == '/') { /* if absolute path */
406 strcpy(lname, where);
408 /* ***FIXME**** we shouldn't have links on Windoz */
419 /* Pmsg1(000, "Restoring: %s\n", ofile); */
421 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
425 print_ls_output(ofile, lname, type, &statp);
429 /* Data stream and extracting */
430 } else if (rec->Stream == STREAM_FILE_DATA) {
432 total += rec->data_len;
433 Dmsg2(8, "Write %ld bytes, total=%ld\n", rec->data_len, total);
434 if ((uint32_t)write(ofd, rec->data, rec->data_len) != rec->data_len) {
435 Emsg1(M_ERROR_TERM, 0, "Write error: %s\n", strerror(errno));
439 } else if (rec->Stream == STREAM_GZIP_DATA) {
444 compress_len = compress_buf_size;
445 if (uncompress((Bytef *)compress_buf, &compress_len,
446 (const Bytef *)rec->data, (uLong)rec->data_len) != Z_OK) {
447 Emsg0(M_ERROR_TERM, 0, _("Uncompression error.\n"));
450 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
451 if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
452 Pmsg0(0, "===Write error===\n");
453 Emsg2(M_ERROR_TERM, 0, "Write error on %s: %s\n", ofile, strerror(errno));
455 total += compress_len;
456 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
461 Emsg0(M_ERROR_TERM, 0, "GZIP data stream found, but GZIP not configured!\n");
466 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
467 } else if (extract) {
469 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
474 set_statp(jcr, fname, ofile, lname, type, &statp);
475 } else if (rec->Stream != STREAM_MD5_SIGNATURE) {
476 Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec->Stream, rec->data);
482 /* If output file is still open, it was the last one in the
483 * archive since we just hit an end of file, so close the file.
487 set_statp(jcr, fname, ofile, lname, type, &statp);
489 release_device(jcr, dev, block);
491 free_pool_memory(fname);
492 free_pool_memory(ofile);
493 free_pool_memory(lname);
494 free_pool_memory(compress_buf);
498 printf("%u files restored.\n", num_files);
502 extern char *getuser(uid_t uid);
503 extern char *getgroup(gid_t gid);
505 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
512 p = encode_mode(statp->st_mode, buf);
513 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
515 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
517 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
519 p = encode_time(statp->st_ctime, p);
523 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
525 if (type == FT_LNK) {
531 for (f=link; *f && (p-buf) < (int)sizeof(buf); )
539 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
542 memset(sessrec, 0, sizeof(sessrec));
543 switch (rec->FileIndex) {
545 rtype = "Fresh Volume Label";
548 rtype = "Volume Label";
549 unser_volume_label(dev, rec);
552 rtype = "Begin Session";
553 unser_session_label(sessrec, rec);
556 rtype = "End Session";
559 rtype = "End of Media";
565 Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
566 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
569 /* Dummies to replace askdir.c */
570 int dir_get_volume_info(JCR *jcr) { return 1;}
571 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
572 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
573 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
574 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
575 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
576 int dir_send_job_status(JCR *jcr) {return 1;}
579 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
581 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
582 jcr->VolumeName, dev_name(dev));