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"
35 static void do_extract(char *fname, char *prefix);
36 static void print_ls_output(char *fname, char *link, int type, struct stat *statp);
39 static DEVICE *dev = NULL;
44 static FF_PKT *ff = &my_ff;
46 static BSR *bsr = NULL;
51 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
52 " -b <file> specify a bootstrap file\n"
53 " -dnn set debug level to nn\n"
54 " -e <file> exclude list\n"
55 " -i <file> include list\n"
56 " -? print this message\n\n");
60 static void my_free_jcr(JCR *jcr)
66 int main (int argc, char *argv[])
73 my_name_is(argc, argv, "bextract");
74 init_msg(NULL, NULL); /* setup message handler */
76 memset(ff, 0, sizeof(FF_PKT));
77 init_include_exclude_files(ff);
79 while ((ch = getopt(argc, argv, "b:d:e:i:?")) != -1) {
81 case 'b': /* bootstrap file */
82 bsr = parse_bsr(optarg);
86 case 'd': /* debug level */
87 debug_level = atoi(optarg);
92 case 'e': /* exclude list */
93 if ((fd = fopen(optarg, "r")) == NULL) {
94 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
95 optarg, strerror(errno));
98 while (fgets(line, sizeof(line), fd) != NULL) {
99 strip_trailing_junk(line);
100 Dmsg1(900, "add_exclude %s\n", line);
101 add_fname_to_exclude_list(ff, line);
106 case 'i': /* include list */
107 if ((fd = fopen(optarg, "r")) == NULL) {
108 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
109 optarg, strerror(errno));
112 while (fgets(line, sizeof(line), fd) != NULL) {
113 strip_trailing_junk(line);
114 Dmsg1(900, "add_include %s\n", line);
115 add_fname_to_include_list(ff, 0, line);
131 Pmsg0(0, "Wrong number of arguments: \n");
134 if (!got_inc) { /* If no include file, */
135 add_fname_to_include_list(ff, 0, "/"); /* include everything */
138 jcr = new_jcr(sizeof(JCR), my_free_jcr);
139 jcr->VolSessionId = 1;
140 jcr->VolSessionTime = (uint32_t)time(NULL);
142 strcpy(jcr->Job, "bextract");
143 jcr->dev_name = get_pool_memory(PM_FNAME);
144 strcpy(jcr->dev_name, argv[0]);
146 do_extract(argv[0], argv[1]);
156 static void do_extract(char *devname, char *where)
164 long record_file_index;
168 POOLMEM *fname; /* original file name */
169 POOLMEM *ofile; /* output name with prefix */
170 POOLMEM *lname; /* link name */
171 int wherelen; /* prefix length */
172 SESSION_LABEL sessrec;
174 if (strncmp(devname, "/dev/", 5) != 0) {
175 /* Try stripping file part */
176 p = devname + strlen(devname);
177 while (p >= devname && *p != '/') {
181 strcpy(VolName, p+1);
185 strcpy(jcr->VolumeName, VolName);
187 dev = init_dev(NULL, devname);
188 if (!dev || !open_device(dev)) {
189 Emsg1(M_ERROR_TERM, 0, "Cannot open %s\n", devname);
191 Dmsg0(90, "Device opened for read.\n");
193 if (stat(where, &statp) < 0) {
194 Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
195 where, strerror(errno));
197 if (!S_ISDIR(statp.st_mode)) {
198 Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
201 wherelen = strlen(where);
202 fname = get_pool_memory(PM_FNAME);
203 ofile = get_pool_memory(PM_FNAME);
204 lname = get_pool_memory(PM_FNAME);
206 block = new_block(dev);
208 create_vol_list(jcr);
210 Dmsg1(20, "Found %d volumes names to restore.\n", jcr->NumVolumes);
213 * Ready device for reading, and read records
215 if (!acquire_device_for_read(jcr, dev, block)) {
221 memset(&rec, 0, sizeof(rec));
222 rec.data = get_memory(70000);
224 uint32_t compress_buf_size = 70000;
225 POOLMEM *compress_buf = get_memory(compress_buf_size);
229 DEV_RECORD *record; /* for reading label of multi-volumes */
231 if (!read_record(dev, block, &rec)) {
233 Dmsg1(500, "Main read record failed. rem=%d\n", rec.remainder);
234 if (dev->state & ST_EOT) {
236 Dmsg0(500, "Not end of record.\n");
238 Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
240 * End Of Tape -- mount next Volume (if another specified)
242 if (jcr->NumVolumes > 1 && jcr->CurVolume < jcr->NumVolumes) {
243 VOL_LIST *vol = jcr->VolList;
244 /* Find next Volume */
246 for (int i=1; i<jcr->CurVolume; i++) {
249 strcpy(jcr->VolumeName, vol->VolumeName);
250 Dmsg1(400, "There is another volume %s.\n", jcr->VolumeName);
253 dev->state &= ~ST_READ;
254 if (!acquire_device_for_read(jcr, dev, block)) {
255 Emsg2(M_FATAL, 0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev),
260 record = new_record();
261 Dmsg1(500, "read record after new tape. rem=%d\n", record->remainder);
262 read_record(dev, block, record); /* read vol label */
263 dump_label_record(dev, record, 0);
267 Dmsg0(90, "End of Device reached.\n");
268 break; /* End of Tape */
270 if (dev->state & ST_EOF) {
271 continue; /* try again */
273 Pmsg0(0, "Read Record got a bad record\n");
274 status_dev(dev, &status);
275 Dmsg1(20, "Device status: %x\n", status);
277 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
278 else if (status & MT_EOT)
279 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
280 else if (status & MT_EOF)
281 Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
282 else if (status & MT_DR_OPEN)
283 Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
284 else if (!(status & MT_ONLINE))
285 Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
287 Emsg3(M_ERROR_TERM, 0, "Read error %d on Record Header %s: %s\n", n, dev_name(dev), strerror(errno));
291 /* This is no longer used */
292 if (rec.VolSessionId == 0 && rec.VolSessionTime == 0) {
293 Emsg0(M_ERROR, 0, "Zero header record. This shouldn't happen.\n");
294 break; /* END OF FILE */
298 * Check for Start or End of Session Record
301 if (rec.FileIndex < 0) {
303 memset(&sessrec, 0, sizeof(sessrec));
304 switch (rec.FileIndex) {
306 rtype = "Fresh Volume Label";
309 rtype = "Volume Label";
310 unser_volume_label(dev, &rec);
313 rtype = "Begin Session";
314 unser_session_label(&sessrec, &rec);
317 rtype = "End Session";
320 rtype = "End of Media";
326 if (debug_level > 0) {
327 printf("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
328 rtype, rec.VolSessionId, rec.VolSessionTime, rec.Stream, rec.data_len);
331 Dmsg1(40, "Got label = %d\n", rec.FileIndex);
332 if (rec.FileIndex == EOM_LABEL) { /* end of tape? */
333 Dmsg0(40, "Get EOM LABEL\n");
334 break; /* yes, get out */
336 continue; /* ignore other labels */
337 } /* end if label record */
339 /* Is this the file we want? */
340 if (bsr && !match_bsr(bsr, &rec, &dev->VolHdr, &sessrec)) {
344 /* File Attributes stream */
345 if (rec.Stream == STREAM_UNIX_ATTRIBUTES) {
348 /* If extracting, it was from previous stream, so
349 * close the output file.
353 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
358 set_statp(jcr, fname, ofile, lname, type, &statp);
361 if (sizeof_pool_memory(fname) < rec.data_len) {
362 fname = realloc_pool_memory(fname, rec.data_len + 1);
364 if (sizeof_pool_memory(ofile) < sizeof_pool_memory(fname) + wherelen + 1) {
365 ofile = realloc_pool_memory(ofile, sizeof_pool_memory(fname) + wherelen + 1);
367 if (sizeof_pool_memory(lname) < rec.data_len) {
368 ofile = realloc_pool_memory(ofile, rec.data_len + 1);
374 * An Attributes record consists of:
379 * Link name (if file linked i.e. FT_LNK)
382 sscanf(rec.data, "%ld %d", &record_file_index, &type);
383 if (record_file_index != rec.FileIndex)
384 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
385 rec.FileIndex, record_file_index);
387 while (*ap++ != ' ') /* skip record file index */
389 while (*ap++ != ' ') /* skip type */
391 /* Save filename and position to attributes */
396 *fp = *ap++; /* terminate filename & point to attribs */
398 /* Skip to Link name */
399 if (type == FT_LNK) {
404 strcat(lname, lp); /* "save" link name */
410 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
412 decode_stat(ap, &statp);
414 * Prepend the where directory so that the
415 * files are put where the user wants.
417 * We do a little jig here to handle Win32 files with
419 * If where is null and we are running on a win32 client,
421 * Otherwise, if the second character of the filename is a
422 * colon (:), change it into a slash (/) -- this creates
423 * a reasonable pathname on most systems.
425 strcpy(ofile, where);
426 if (fname[1] == ':') {
428 strcat(ofile, fname);
431 strcat(ofile, fname);
433 /* Pmsg1(000, "Restoring: %s\n", ofile); */
435 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
438 print_ls_output(ofile, lname, type, &statp);
442 /* Data stream and extracting */
443 } else if (rec.Stream == STREAM_FILE_DATA) {
445 total += rec.data_len;
446 Dmsg2(8, "Write %ld bytes, total=%ld\n", rec.data_len, total);
447 if ((uint32_t)write(ofd, rec.data, rec.data_len) != rec.data_len) {
448 Emsg1(M_ERROR_TERM, 0, "Write error: %s\n", strerror(errno));
452 } else if (rec.Stream == STREAM_GZIP_DATA) {
457 compress_len = compress_buf_size;
458 if (uncompress((Bytef *)compress_buf, &compress_len,
459 (const Bytef *)rec.data, (uLong)rec.data_len) != Z_OK) {
460 Emsg0(M_ERROR_TERM, 0, _("Uncompression error.\n"));
463 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
464 if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
465 Pmsg0(0, "===Write error===\n");
466 Emsg2(M_ERROR_TERM, 0, "Write error on %s: %s\n", ofile, strerror(errno));
468 total += compress_len;
469 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec.data_len,
474 Emsg0(M_ERROR_TERM, 0, "GZIP data stream found, but GZIP not configured!\n");
479 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
480 } else if (extract) {
482 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
487 set_statp(jcr, fname, ofile, lname, type, &statp);
488 } else if (rec.Stream != STREAM_MD5_SIGNATURE) {
489 Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec.Stream, rec.data);
493 /* If output file is still open, it was the last one in the
494 * archive since we just hit an end of file, so close the file.
498 set_statp(jcr, fname, ofile, lname, type, &statp);
500 release_device(jcr, dev, block);
502 free_pool_memory(fname);
503 free_pool_memory(ofile);
504 free_pool_memory(lname);
505 free_pool_memory(compress_buf);
511 extern char *getuser(uid_t uid);
512 extern char *getgroup(gid_t gid);
514 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
521 p = encode_mode(statp->st_mode, buf);
522 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
524 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
526 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
528 p = encode_time(statp->st_ctime, p);
532 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
534 if (type == FT_LNK) {
540 for (f=link; *f && (p-buf) < (int)sizeof(buf); )
548 /* Dummies to replace askdir.c */
549 int dir_get_volume_info(JCR *jcr) { return 1;}
550 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
551 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
552 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
553 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
554 int dir_send_job_status(JCR *jcr) {return 1;}
557 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
559 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
560 jcr->VolumeName, dev_name(dev));