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;
48 static DEV_RECORD *rec;
49 static DEV_BLOCK *block;
54 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
55 " -b <file> specify a bootstrap file\n"
56 " -dnn set debug level to nn\n"
57 " -e <file> exclude list\n"
58 " -i <file> include list\n"
59 " -? print this message\n\n");
63 static void my_free_jcr(JCR *jcr)
69 int main (int argc, char *argv[])
76 my_name_is(argc, argv, "bextract");
77 init_msg(NULL, NULL); /* setup message handler */
79 memset(ff, 0, sizeof(FF_PKT));
80 init_include_exclude_files(ff);
82 while ((ch = getopt(argc, argv, "b:d:e:i:?")) != -1) {
84 case 'b': /* bootstrap file */
85 bsr = parse_bsr(NULL, optarg);
89 case 'd': /* debug level */
90 debug_level = atoi(optarg);
95 case 'e': /* exclude list */
96 if ((fd = fopen(optarg, "r")) == NULL) {
97 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
98 optarg, strerror(errno));
101 while (fgets(line, sizeof(line), fd) != NULL) {
102 strip_trailing_junk(line);
103 Dmsg1(900, "add_exclude %s\n", line);
104 add_fname_to_exclude_list(ff, line);
109 case 'i': /* include list */
110 if ((fd = fopen(optarg, "r")) == NULL) {
111 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
112 optarg, strerror(errno));
115 while (fgets(line, sizeof(line), fd) != NULL) {
116 strip_trailing_junk(line);
117 Dmsg1(900, "add_include %s\n", line);
118 add_fname_to_include_list(ff, 0, line);
134 Pmsg0(0, "Wrong number of arguments: \n");
137 if (!got_inc) { /* If no include file, */
138 add_fname_to_include_list(ff, 0, "/"); /* include everything */
141 jcr = new_jcr(sizeof(JCR), my_free_jcr);
142 jcr->VolSessionId = 1;
143 jcr->VolSessionTime = (uint32_t)time(NULL);
145 strcpy(jcr->Job, "bextract");
146 jcr->dev_name = get_pool_memory(PM_FNAME);
147 strcpy(jcr->dev_name, argv[0]);
149 do_extract(argv[0], argv[1]);
159 static void do_extract(char *devname, char *where)
166 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)) {
222 free_pool_memory(rec->data);
223 rec->data = get_memory(70000);
225 uint32_t compress_buf_size = 70000;
226 POOLMEM *compress_buf = get_memory(compress_buf_size);
229 if (!read_block_from_device(dev, block)) {
231 Dmsg1(500, "Main read record failed. rem=%d\n", rec->remainder);
232 if (dev->state & ST_EOT) {
233 if (!mount_next_read_volume(jcr, dev, block)) {
238 if (dev->state & ST_EOF) {
239 continue; /* try again */
241 if (dev->state & ST_SHORT) {
244 Pmsg0(0, "Read Record got a bad record\n");
245 status_dev(dev, &status);
246 Dmsg1(20, "Device status: %x\n", status);
248 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
249 else if (status & MT_EOT)
250 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
251 else if (status & MT_EOF)
252 Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
253 else if (status & MT_DR_OPEN)
254 Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
255 else if (!(status & MT_ONLINE))
256 Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
258 Emsg3(M_ERROR_TERM, 0, "Read error %d on Record Header %s: %s\n",
259 status, dev_name(dev), strerror(errno));
262 for (rec->state=0; !is_block_empty(rec); ) {
263 if (!read_record_from_block(block, rec)) {
267 /* This is no longer used */
268 if (rec->VolSessionId == 0 && rec->VolSessionTime == 0) {
269 Emsg0(M_ERROR, 0, "Zero header record. This shouldn't happen.\n");
270 break; /* END OF FILE */
274 * Check for Start or End of Session Record
277 if (rec->FileIndex < 0) {
279 memset(&sessrec, 0, sizeof(sessrec));
280 switch (rec->FileIndex) {
282 rtype = "Fresh Volume Label";
285 rtype = "Volume Label";
286 unser_volume_label(dev, rec);
289 rtype = "Begin Session";
290 unser_session_label(&sessrec, rec);
293 rtype = "End Session";
296 rtype = "End of Media";
302 if (debug_level > 0) {
303 printf("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
304 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
307 Dmsg1(40, "Got label = %d\n", rec->FileIndex);
308 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
309 Dmsg0(40, "Get EOM LABEL\n");
310 break; /* yes, get out */
312 continue; /* ignore other labels */
313 } /* end if label record */
315 /* Is this the file we want? */
316 if (bsr && !match_bsr(bsr, rec, &dev->VolHdr, &sessrec)) {
320 if (is_partial_record(rec)) {
324 /* File Attributes stream */
325 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
328 /* If extracting, it was from previous stream, so
329 * close the output file.
333 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
338 set_statp(jcr, fname, ofile, lname, type, &statp);
341 if (sizeof_pool_memory(fname) < rec->data_len) {
342 fname = realloc_pool_memory(fname, rec->data_len + 1);
344 if (sizeof_pool_memory(ofile) < sizeof_pool_memory(fname) + wherelen + 1) {
345 ofile = realloc_pool_memory(ofile, sizeof_pool_memory(fname) + wherelen + 1);
347 if (sizeof_pool_memory(lname) < rec->data_len) {
348 lname = realloc_pool_memory(lname, rec->data_len + 1);
354 * An Attributes record consists of:
359 * Link name (if file linked i.e. FT_LNK)
362 sscanf(rec->data, "%ld %d", &record_file_index, &type);
363 if (record_file_index != rec->FileIndex)
364 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
365 rec->FileIndex, record_file_index);
367 while (*ap++ != ' ') /* skip record file index */
369 while (*ap++ != ' ') /* skip type */
371 /* Save filename and position to attributes */
376 *fp = *ap++; /* terminate filename & point to attribs */
378 /* Skip to Link name */
379 if (type == FT_LNK) {
384 strcat(lname, lp); /* "save" link name */
390 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
392 decode_stat(ap, &statp);
394 * Prepend the where directory so that the
395 * files are put where the user wants.
397 * We do a little jig here to handle Win32 files with
399 * If where is null and we are running on a win32 client,
401 * Otherwise, if the second character of the filename is a
402 * colon (:), change it into a slash (/) -- this creates
403 * a reasonable pathname on most systems.
405 strcpy(ofile, where);
406 if (fname[1] == ':') {
408 strcat(ofile, fname);
411 strcat(ofile, fname);
413 /* Pmsg1(000, "Restoring: %s\n", ofile); */
415 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
418 print_ls_output(ofile, lname, type, &statp);
422 /* Data stream and extracting */
423 } else if (rec->Stream == STREAM_FILE_DATA) {
425 total += rec->data_len;
426 Dmsg2(8, "Write %ld bytes, total=%ld\n", rec->data_len, total);
427 if ((uint32_t)write(ofd, rec->data, rec->data_len) != rec->data_len) {
428 Emsg1(M_ERROR_TERM, 0, "Write error: %s\n", strerror(errno));
432 } else if (rec->Stream == STREAM_GZIP_DATA) {
437 compress_len = compress_buf_size;
438 if (uncompress((Bytef *)compress_buf, &compress_len,
439 (const Bytef *)rec->data, (uLong)rec->data_len) != Z_OK) {
440 Emsg0(M_ERROR_TERM, 0, _("Uncompression error.\n"));
443 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
444 if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
445 Pmsg0(0, "===Write error===\n");
446 Emsg2(M_ERROR_TERM, 0, "Write error on %s: %s\n", ofile, strerror(errno));
448 total += compress_len;
449 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
454 Emsg0(M_ERROR_TERM, 0, "GZIP data stream found, but GZIP not configured!\n");
459 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
460 } else if (extract) {
462 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
467 set_statp(jcr, fname, ofile, lname, type, &statp);
468 } else if (rec->Stream != STREAM_MD5_SIGNATURE) {
469 Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec->Stream, rec->data);
475 /* If output file is still open, it was the last one in the
476 * archive since we just hit an end of file, so close the file.
480 set_statp(jcr, fname, ofile, lname, type, &statp);
482 release_device(jcr, dev, block);
484 free_pool_memory(fname);
485 free_pool_memory(ofile);
486 free_pool_memory(lname);
487 free_pool_memory(compress_buf);
494 extern char *getuser(uid_t uid);
495 extern char *getgroup(gid_t gid);
497 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
504 p = encode_mode(statp->st_mode, buf);
505 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
507 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
509 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
511 p = encode_time(statp->st_ctime, p);
515 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
517 if (type == FT_LNK) {
523 for (f=link; *f && (p-buf) < (int)sizeof(buf); )
531 /* Dummies to replace askdir.c */
532 int dir_get_volume_info(JCR *jcr) { return 1;}
533 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
534 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
535 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
536 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
537 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
538 int dir_send_job_status(JCR *jcr) {return 1;}
541 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
543 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
544 jcr->VolumeName, dev_name(dev));