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);
42 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
44 static DEVICE *dev = NULL;
48 static FF_PKT *ff = &my_ff;
49 static BSR *bsr = NULL;
50 static int extract = FALSE;
51 static long record_file_index;
52 static long total = 0;
53 static POOLMEM *fname; /* original file name */
54 static POOLMEM *ofile; /* output name with prefix */
55 static POOLMEM *lname; /* link name */
56 static POOLMEM *attribsEx; /* extended attributes (Win32) */
58 static int wherelen; /* prefix length */
59 static uint32_t num_files = 0;
60 static struct stat statp;
61 static uint32_t compress_buf_size = 70000;
62 static POOLMEM *compress_buf;
65 static int prog_name_msg = 0;
66 static char *VolumeName = NULL;
68 static char *wbuf; /* write buffer address */
69 static uint32_t wsize; /* write size */
70 static uint64_t fileAddr = 0; /* file write address */
72 #define CONFIG_FILE "bacula-sd.conf"
79 "\nVersion: " VERSION " (" BDATE ")\n\n"
80 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
81 " -b <file> specify a bootstrap file\n"
82 " -c <file> specify a configuration file\n"
83 " -dnn set debug level to nn\n"
84 " -e <file> exclude list\n"
85 " -i <file> include list\n"
86 " -V specify Volume names (separated by |)\n"
87 " -? print this message\n\n");
92 int main (int argc, char *argv[])
99 working_directory = "/tmp";
100 my_name_is(argc, argv, "bextract");
101 init_msg(NULL, NULL); /* setup message handler */
103 memset(ff, 0, sizeof(FF_PKT));
104 init_include_exclude_files(ff);
107 while ((ch = getopt(argc, argv, "b:c:d:e:i:?")) != -1) {
109 case 'b': /* bootstrap file */
110 bsr = parse_bsr(NULL, optarg);
114 case 'c': /* specify config file */
115 if (configfile != NULL) {
118 configfile = bstrdup(optarg);
121 case 'd': /* debug level */
122 debug_level = atoi(optarg);
123 if (debug_level <= 0)
127 case 'e': /* exclude list */
128 if ((fd = fopen(optarg, "r")) == NULL) {
129 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
130 optarg, strerror(errno));
133 while (fgets(line, sizeof(line), fd) != NULL) {
134 strip_trailing_junk(line);
135 Dmsg1(900, "add_exclude %s\n", line);
136 add_fname_to_exclude_list(ff, line);
141 case 'i': /* include list */
142 if ((fd = fopen(optarg, "r")) == NULL) {
143 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
144 optarg, strerror(errno));
147 while (fgets(line, sizeof(line), fd) != NULL) {
148 strip_trailing_junk(line);
149 Dmsg1(900, "add_include %s\n", line);
150 add_fname_to_include_list(ff, 0, line);
156 case 'V': /* Volume name */
170 Pmsg0(0, "Wrong number of arguments: \n");
174 if (configfile == NULL) {
175 configfile = bstrdup(CONFIG_FILE);
178 parse_config(configfile);
180 if (!got_inc) { /* If no include file, */
181 add_fname_to_include_list(ff, 0, "/"); /* include everything */
193 static void do_extract(char *devname)
196 jcr = setup_jcr("bextract", devname, bsr, VolumeName);
197 dev = setup_to_access_device(jcr, 1); /* acquire for read */
202 /* Make sure where directory exists and that it is a directory */
203 if (stat(where, &statp) < 0) {
204 Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
205 where, strerror(errno));
207 if (!S_ISDIR(statp.st_mode)) {
208 Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
211 wherelen = strlen(where);
212 fname = get_pool_memory(PM_FNAME);
213 ofile = get_pool_memory(PM_FNAME);
214 lname = get_pool_memory(PM_FNAME);
215 attribsEx = get_pool_memory(PM_FNAME);
217 compress_buf = get_memory(compress_buf_size);
219 read_records(jcr, dev, record_cb, mount_next_read_volume);
220 /* If output file is still open, it was the last one in the
221 * archive since we just hit an end of file, so close the file.
223 if (is_bopen(&bfd)) {
224 set_attributes(jcr, fname, ofile, lname, type, stream, &statp,
227 release_device(jcr, dev);
229 free_pool_memory(fname);
230 free_pool_memory(ofile);
231 free_pool_memory(lname);
232 free_pool_memory(compress_buf);
235 printf("%u files restored.\n", num_files);
240 * Called here for each record from read_records()
242 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
246 if (rec->FileIndex < 0) {
247 return; /* we don't want labels */
250 /* File Attributes stream */
251 if (rec->Stream == STREAM_UNIX_ATTRIBUTES || rec->Stream == STREAM_WIN32_ATTRIBUTES) {
252 char *ap, *lp, *fp, *apex;
254 stream = rec->Stream;
256 /* If extracting, it was from previous stream, so
257 * close the output file.
260 if (!is_bopen(&bfd)) {
261 Emsg0(M_ERROR, 0, "Logic error output file should be open but is not.\n");
264 set_attributes(jcr, fname, ofile, lname, type, stream, &statp,
268 if (sizeof_pool_memory(fname) < rec->data_len) {
269 fname = realloc_pool_memory(fname, rec->data_len + 1);
271 if (sizeof_pool_memory(ofile) < rec->data_len + wherelen + 1) {
272 ofile = realloc_pool_memory(ofile, rec->data_len + wherelen + 1);
274 if (sizeof_pool_memory(lname) < rec->data_len) {
275 lname = realloc_pool_memory(lname, rec->data_len + wherelen + 1);
281 * An Attributes record consists of:
286 * Link name (if file linked i.e. FT_LNK)
287 * Extended Attributes (Win32)
290 sscanf(rec->data, "%ld %d", &record_file_index, &type);
291 if (record_file_index != rec->FileIndex)
292 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
293 rec->FileIndex, record_file_index);
295 while (*ap++ != ' ') /* skip record file index */
297 while (*ap++ != ' ') /* skip type */
299 /* Save filename and position to attributes */
304 *fp = *ap++; /* terminate filename & point to attribs */
306 /* Skip to Link name */
307 if (type == FT_LNK || type == FT_LNKSAVED) {
316 if (rec->Stream == STREAM_WIN32_ATTRIBUTES) {
317 apex = ap; /* start at attributes */
318 while (*apex++ != 0) { /* skip attributes */
321 while (*apex++ != 0) { /* skip link name */
324 pm_strcpy(&attribsEx, apex); /* make a copy of Extended attributes */
326 *attribsEx = 0; /* no extended attributes */
330 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
333 decode_stat(ap, &statp, &LinkFI);
335 * Prepend the where directory so that the
336 * files are put where the user wants.
338 * We do a little jig here to handle Win32 files with
339 * a drive letter -- we simply strip the drive: from
340 * every filename if a prefix is supplied.
343 strcpy(ofile, fname);
347 strcpy(ofile, where);
348 if (win32_client && fname[1] == ':') {
349 fn = fname+2; /* skip over drive: */
351 fn = fname; /* take whole name */
353 /* Ensure where is terminated with a slash */
354 if (where[wherelen-1] != '/' && fn[0] != '/') {
357 strcat(ofile, fn); /* copy rest of name */
358 /* Fixup link name for hard links, but not for
361 if (type == FT_LNKSAVED) {
362 if (lp[0] == '/') { /* if absolute path */
363 strcpy(lname, where);
365 if (win32_client && lp[1] == ':') {
366 strcat(lname, lp+2); /* copy rest of name */
368 strcat(lname, lp); /* On Unix systems we take everything */
373 /* Pmsg1(000, "Restoring: %s\n", ofile); */
376 stat = create_file(jcr, fname, ofile, lname, type, stream,
377 &statp, attribsEx, &bfd, REPLACE_ALWAYS);
384 print_ls_output(ofile, lname, type, &statp);
389 set_attributes(jcr, fname, ofile, lname, type, stream, &statp,
391 print_ls_output(ofile, lname, type, &statp);
398 /* Data stream and extracting */
399 } else if (rec->Stream == STREAM_FILE_DATA || rec->Stream == STREAM_SPARSE_DATA) {
401 if (rec->Stream == STREAM_SPARSE_DATA) {
404 wbuf = rec->data + SPARSE_FADDR_SIZE;
405 wsize = rec->data_len - SPARSE_FADDR_SIZE;
406 ser_begin(rec->data, SPARSE_FADDR_SIZE);
408 if (fileAddr != faddr) {
410 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
411 Emsg2(M_ERROR_TERM, 0, _("Seek error on %s: %s\n"), ofile, strerror(errno));
416 wsize = rec->data_len;
419 Dmsg2(8, "Write %u bytes, total=%u\n", wsize, total);
420 if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
421 Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
426 } else if (rec->Stream == STREAM_GZIP_DATA || rec->Stream == STREAM_SPARSE_GZIP_DATA) {
432 if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
435 wbuf = rec->data + SPARSE_FADDR_SIZE;
436 wsize = rec->data_len - SPARSE_FADDR_SIZE;
437 ser_begin(rec->data, SPARSE_FADDR_SIZE);
439 if (fileAddr != faddr) {
441 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
442 Emsg2(M_ERROR, 0, _("Seek error on %s: %s\n"), ofile, strerror(errno));
447 wsize = rec->data_len;
449 compress_len = compress_buf_size;
450 if ((stat=uncompress((Bytef *)compress_buf, &compress_len,
451 (const Bytef *)wbuf, (uLong)wsize) != Z_OK)) {
452 Emsg1(M_ERROR_TERM, 0, _("Uncompression error. ERR=%d\n"), stat);
455 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
456 if ((uLongf)bwrite(&bfd, compress_buf, (size_t)compress_len) != compress_len) {
457 Pmsg0(0, "===Write error===\n");
458 Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
460 total += compress_len;
461 fileAddr += compress_len;
462 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
467 Emsg0(M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
472 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
473 } else if (extract) {
474 if (!is_bopen(&bfd)) {
475 Emsg0(M_ERROR, 0, "Logic error output file should be open but is not.\n");
478 set_attributes(jcr, fname, ofile, lname, type, stream, &statp,
480 } else if (rec->Stream == STREAM_PROGRAM_NAMES || rec->Stream == STREAM_PROGRAM_DATA) {
481 if (!prog_name_msg) {
482 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
485 } else if (!(rec->Stream == STREAM_MD5_SIGNATURE ||
486 rec->Stream == STREAM_SHA1_SIGNATURE)) {
487 Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec->Stream, rec->data);
494 /* Dummies to replace askdir.c */
495 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
496 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
497 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
498 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
499 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
500 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
501 int dir_send_job_status(JCR *jcr) {return 1;}
504 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
506 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
507 jcr->VolumeName, dev_name(dev));