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);
37 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
39 static DEVICE *dev = NULL;
44 static FF_PKT *ff = &my_ff;
46 static BSR *bsr = NULL;
48 static DEV_RECORD *rec;
49 static DEV_BLOCK *block;
54 "\nVersion: " VERSION " (" DATE ")\n\n"
55 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
56 " -b <file> specify a bootstrap file\n"
57 " -dnn set debug level to nn\n"
58 " -e <file> exclude list\n"
59 " -i <file> include list\n"
60 " -? print this message\n\n");
64 static void my_free_jcr(JCR *jcr)
70 int main (int argc, char *argv[])
77 my_name_is(argc, argv, "bextract");
78 init_msg(NULL, NULL); /* setup message handler */
80 memset(ff, 0, sizeof(FF_PKT));
81 init_include_exclude_files(ff);
83 while ((ch = getopt(argc, argv, "b:d:e:i:?")) != -1) {
85 case 'b': /* bootstrap file */
86 bsr = parse_bsr(NULL, optarg);
90 case 'd': /* debug level */
91 debug_level = atoi(optarg);
96 case 'e': /* exclude list */
97 if ((fd = fopen(optarg, "r")) == NULL) {
98 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
99 optarg, strerror(errno));
102 while (fgets(line, sizeof(line), fd) != NULL) {
103 strip_trailing_junk(line);
104 Dmsg1(900, "add_exclude %s\n", line);
105 add_fname_to_exclude_list(ff, line);
110 case 'i': /* include list */
111 if ((fd = fopen(optarg, "r")) == NULL) {
112 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
113 optarg, strerror(errno));
116 while (fgets(line, sizeof(line), fd) != NULL) {
117 strip_trailing_junk(line);
118 Dmsg1(900, "add_include %s\n", line);
119 add_fname_to_include_list(ff, 0, line);
135 Pmsg0(0, "Wrong number of arguments: \n");
138 if (!got_inc) { /* If no include file, */
139 add_fname_to_include_list(ff, 0, "/"); /* include everything */
142 jcr = new_jcr(sizeof(JCR), my_free_jcr);
143 jcr->VolSessionId = 1;
144 jcr->VolSessionTime = (uint32_t)time(NULL);
146 strcpy(jcr->Job, "bextract");
147 jcr->dev_name = get_pool_memory(PM_FNAME);
148 strcpy(jcr->dev_name, argv[0]);
150 do_extract(argv[0], argv[1]);
160 * Device got an error, attempt to analyse it
162 static void display_error_status()
166 Emsg0(M_ERROR, 0, dev->errmsg);
167 status_dev(dev, &status);
168 Dmsg1(20, "Device status: %x\n", status);
170 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
171 else if (status & MT_EOT)
172 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
173 else if (status & MT_EOF)
174 Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
175 else if (status & MT_DR_OPEN)
176 Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
177 else if (!(status & MT_ONLINE))
178 Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
180 Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
184 static void do_extract(char *devname, char *where)
191 long record_file_index;
193 POOLMEM *fname; /* original file name */
194 POOLMEM *ofile; /* output name with prefix */
195 POOLMEM *lname; /* link name */
196 int wherelen; /* prefix length */
197 SESSION_LABEL sessrec;
199 if (strncmp(devname, "/dev/", 5) != 0) {
200 /* Try stripping file part */
201 p = devname + strlen(devname);
202 while (p >= devname && *p != '/') {
206 strcpy(VolName, p+1);
210 strcpy(jcr->VolumeName, VolName);
212 dev = init_dev(NULL, devname);
213 if (!dev || !open_device(dev)) {
214 Emsg1(M_ERROR_TERM, 0, "Cannot open %s\n", devname);
216 Dmsg0(90, "Device opened for read.\n");
218 if (stat(where, &statp) < 0) {
219 Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
220 where, strerror(errno));
222 if (!S_ISDIR(statp.st_mode)) {
223 Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
226 wherelen = strlen(where);
227 fname = get_pool_memory(PM_FNAME);
228 ofile = get_pool_memory(PM_FNAME);
229 lname = get_pool_memory(PM_FNAME);
231 block = new_block(dev);
233 create_vol_list(jcr);
235 Dmsg1(20, "Found %d volumes names to restore.\n", jcr->NumVolumes);
238 * Ready device for reading, and read records
240 if (!acquire_device_for_read(jcr, dev, block)) {
247 free_pool_memory(rec->data);
248 rec->data = get_memory(70000);
250 uint32_t compress_buf_size = 70000;
251 POOLMEM *compress_buf = get_memory(compress_buf_size);
254 if (!read_block_from_device(dev, block)) {
255 Dmsg1(500, "Main read record failed. rem=%d\n", rec->remainder);
256 if (dev->state & ST_EOT) {
258 if (!mount_next_read_volume(jcr, dev, block)) {
261 record = new_record();
262 read_block_from_device(dev, block);
263 read_record_from_block(block, record);
264 get_session_record(dev, record, &sessrec);
268 if (dev->state & ST_EOF) {
269 continue; /* try again */
271 if (dev->state & ST_SHORT) {
274 display_error_status();
278 for (rec->state=0; !is_block_empty(rec); ) {
279 if (!read_record_from_block(block, rec)) {
283 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
284 Dmsg0(40, "Get EOM LABEL\n");
286 break; /* yes, get out */
289 /* Some sort of label? */
290 if (rec->FileIndex < 0) {
291 get_session_record(dev, rec, &sessrec);
293 } /* end if label record */
295 /* Is this the file we want? */
296 if (bsr && !match_bsr(bsr, rec, &dev->VolHdr, &sessrec)) {
300 if (is_partial_record(rec)) {
304 /* File Attributes stream */
305 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
308 /* If extracting, it was from previous stream, so
309 * close the output file.
313 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
318 set_statp(jcr, fname, ofile, lname, type, &statp);
321 if (sizeof_pool_memory(fname) < rec->data_len) {
322 fname = realloc_pool_memory(fname, rec->data_len + 1);
324 if (sizeof_pool_memory(ofile) < sizeof_pool_memory(fname) + wherelen + 1) {
325 ofile = realloc_pool_memory(ofile, sizeof_pool_memory(fname) + wherelen + 1);
327 if (sizeof_pool_memory(lname) < rec->data_len) {
328 lname = realloc_pool_memory(lname, rec->data_len + 1);
334 * An Attributes record consists of:
339 * Link name (if file linked i.e. FT_LNK)
342 sscanf(rec->data, "%ld %d", &record_file_index, &type);
343 if (record_file_index != rec->FileIndex)
344 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
345 rec->FileIndex, record_file_index);
347 while (*ap++ != ' ') /* skip record file index */
349 while (*ap++ != ' ') /* skip type */
351 /* Save filename and position to attributes */
356 *fp = *ap++; /* terminate filename & point to attribs */
358 /* Skip to Link name */
359 if (type == FT_LNK) {
364 strcat(lname, lp); /* "save" link name */
370 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
372 decode_stat(ap, &statp);
374 * Prepend the where directory so that the
375 * files are put where the user wants.
377 * We do a little jig here to handle Win32 files with
379 * If where is null and we are running on a win32 client,
381 * Otherwise, if the second character of the filename is a
382 * colon (:), change it into a slash (/) -- this creates
383 * a reasonable pathname on most systems.
385 strcpy(ofile, where);
386 if (fname[1] == ':') {
388 strcat(ofile, fname);
391 strcat(ofile, fname);
393 /* Pmsg1(000, "Restoring: %s\n", ofile); */
395 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
398 print_ls_output(ofile, lname, type, &statp);
402 /* Data stream and extracting */
403 } else if (rec->Stream == STREAM_FILE_DATA) {
405 total += rec->data_len;
406 Dmsg2(8, "Write %ld bytes, total=%ld\n", rec->data_len, total);
407 if ((uint32_t)write(ofd, rec->data, rec->data_len) != rec->data_len) {
408 Emsg1(M_ERROR_TERM, 0, "Write error: %s\n", strerror(errno));
412 } else if (rec->Stream == STREAM_GZIP_DATA) {
417 compress_len = compress_buf_size;
418 if (uncompress((Bytef *)compress_buf, &compress_len,
419 (const Bytef *)rec->data, (uLong)rec->data_len) != Z_OK) {
420 Emsg0(M_ERROR_TERM, 0, _("Uncompression error.\n"));
423 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
424 if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
425 Pmsg0(0, "===Write error===\n");
426 Emsg2(M_ERROR_TERM, 0, "Write error on %s: %s\n", ofile, strerror(errno));
428 total += compress_len;
429 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
434 Emsg0(M_ERROR_TERM, 0, "GZIP data stream found, but GZIP not configured!\n");
439 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
440 } else if (extract) {
442 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
447 set_statp(jcr, fname, ofile, lname, type, &statp);
448 } else if (rec->Stream != STREAM_MD5_SIGNATURE) {
449 Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec->Stream, rec->data);
455 /* If output file is still open, it was the last one in the
456 * archive since we just hit an end of file, so close the file.
460 set_statp(jcr, fname, ofile, lname, type, &statp);
462 release_device(jcr, dev, block);
464 free_pool_memory(fname);
465 free_pool_memory(ofile);
466 free_pool_memory(lname);
467 free_pool_memory(compress_buf);
474 extern char *getuser(uid_t uid);
475 extern char *getgroup(gid_t gid);
477 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
484 p = encode_mode(statp->st_mode, buf);
485 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
487 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
489 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
491 p = encode_time(statp->st_ctime, p);
495 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
497 if (type == FT_LNK) {
503 for (f=link; *f && (p-buf) < (int)sizeof(buf); )
511 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
514 memset(sessrec, 0, sizeof(sessrec));
515 switch (rec->FileIndex) {
517 rtype = "Fresh Volume Label";
520 rtype = "Volume Label";
521 unser_volume_label(dev, rec);
524 rtype = "Begin Session";
525 unser_session_label(sessrec, rec);
528 rtype = "End Session";
531 rtype = "End of Media";
537 Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
538 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
541 /* Dummies to replace askdir.c */
542 int dir_get_volume_info(JCR *jcr) { return 1;}
543 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
544 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
545 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
546 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
547 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
548 int dir_send_job_status(JCR *jcr) {return 1;}
551 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
553 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
554 jcr->VolumeName, dev_name(dev));