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"
34 static void do_extract(char *fname, char *prefix);
35 static void print_ls_output(char *fname, struct stat *statp);
38 static DEVICE *dev = NULL;
43 static FF_PKT *ff = &my_ff;
48 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
49 " -dnn set debug level to nn\n"
50 " -e <file> exclude list\n"
51 " -i <file> include list\n"
52 " -? print this message\n\n");
56 static void my_free_jcr(JCR *jcr)
61 int main (int argc, char *argv[])
68 my_name_is(argc, argv, "bextract");
69 init_msg(NULL, NULL); /* setup message handler */
71 memset(ff, 0, sizeof(FF_PKT));
72 init_include_exclude_files(ff);
74 while ((ch = getopt(argc, argv, "d:e:i:?")) != -1) {
76 case 'd': /* debug level */
77 debug_level = atoi(optarg);
82 case 'e': /* exclude list */
83 if ((fd = fopen(optarg, "r")) == NULL) {
84 Dmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
85 optarg, strerror(errno));
88 while (fgets(line, sizeof(line), fd) != NULL) {
89 strip_trailing_junk(line);
90 Dmsg1(900, "add_exclude %s\n", line);
91 add_fname_to_exclude_list(ff, line);
96 case 'i': /* include list */
97 if ((fd = fopen(optarg, "r")) == NULL) {
98 Dmsg2(0, "Could not open include 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_include %s\n", line);
105 add_fname_to_include_list(ff, 0, line);
121 Dmsg0(0, "Wrong number of arguments: \n");
124 if (!got_inc) { /* If no include file, */
125 add_fname_to_include_list(ff, 0, "/"); /* include everything */
128 jcr = new_jcr(sizeof(JCR), my_free_jcr);
129 jcr->VolSessionId = 1;
130 jcr->VolSessionTime = (uint32_t)time(NULL);
133 do_extract(argv[0], argv[1]);
140 static void do_extract(char *devname, char *where)
148 long record_file_index;
152 POOLMEM *fname; /* original file name */
153 POOLMEM *ofile; /* output name with prefix */
154 POOLMEM *lname; /* link name */
155 int wherelen; /* prefix length */
157 if (strncmp(devname, "/dev/", 5) != 0) {
158 /* Try stripping file part */
159 p = devname + strlen(devname);
160 while (p >= devname && *p != '/') {
164 strcpy(VolName, p+1);
169 dev = init_dev(NULL, devname);
170 if (!dev || !open_device(dev)) {
171 Emsg1(M_ABORT, 0, "Cannot open %s\n", devname);
173 Dmsg0(90, "Device opened for read.\n");
175 if (stat(where, &statp) < 0) {
176 Emsg2(M_ABORT, 0, "Cannot stat %s. It must exist. ERR=%s\n",
177 where, strerror(errno));
179 if (!S_ISDIR(statp.st_mode)) {
180 Emsg1(M_ABORT, 0, "%s must be a directory.\n", where);
183 wherelen = strlen(where);
184 fname = get_pool_memory(PM_FNAME);
185 ofile = get_pool_memory(PM_FNAME);
186 lname = get_pool_memory(PM_FNAME);
188 block = new_block(dev);
190 strcpy(jcr->VolumeName, VolName);
191 Dmsg1(100, "Volume=%s\n", jcr->VolumeName);
193 if (!acquire_device_for_read(jcr, dev, block)) {
194 Emsg1(M_ABORT, 0, "Cannot open %s\n", devname);
197 memset(&rec, 0, sizeof(rec));
198 rec.data = get_memory(70000);
200 uint32_t compress_buf_size = 70000;
201 POOLMEM *compress_buf = get_memory(compress_buf_size);
205 if (!read_record(dev, block, &rec)) {
207 if (dev->state & ST_EOT) {
210 if (dev->state & ST_EOF) {
211 continue; /* try again */
213 Dmsg0(0, "Read Record got a bad record\n");
214 status_dev(dev, &status);
215 Dmsg1(20, "Device status: %x\n", status);
217 Emsg0(M_ABORT, 0, "Unexpected End of Data\n");
218 else if (status & MT_EOT)
219 Emsg0(M_ABORT, 0, "Unexpected End of Tape\n");
220 else if (status & MT_EOF)
221 Emsg0(M_ABORT, 0, "Unexpected End of File\n");
222 else if (status & MT_DR_OPEN)
223 Emsg0(M_ABORT, 0, "Tape Door is Open\n");
224 else if (!(status & MT_ONLINE))
225 Emsg0(M_ABORT, 0, "Unexpected Tape is Off-line\n");
227 Emsg3(M_ABORT, 0, "Read error %d on Record Header %s: %s\n", n, dev_name(dev), strerror(errno));
231 /* This is no longer used */
232 if (rec.VolSessionId == 0 && rec.VolSessionTime == 0) {
233 Emsg0(M_ERROR, 0, "Zero header record. This shouldn't happen.\n");
234 break; /* END OF FILE */
238 * Check for Start or End of Session Record
241 if (rec.FileIndex < 0) {
243 switch (rec.FileIndex) {
245 rtype = "Fresh Volume Label";
248 rtype = "Volume Label";
251 rtype = "Begin Session";
254 rtype = "End Session";
257 rtype = "End of Media";
263 if (debug_level > 0) {
264 printf("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
265 rtype, rec.VolSessionId, rec.VolSessionTime, rec.Stream, rec.data_len);
270 /* File Attributes stream */
271 if (rec.Stream == STREAM_UNIX_ATTRIBUTES) {
274 /* If extracting, it was from previous stream, so
275 * close the output file.
279 Emsg0(M_ABORT, 0, "Logic error output file should be open\n");
284 set_statp(jcr, fname, ofile, lname, type, &statp);
287 if (sizeof_pool_memory(fname) < rec.data_len) {
288 fname = realloc_pool_memory(fname, rec.data_len + 1);
290 if (sizeof_pool_memory(ofile) < sizeof_pool_memory(fname) + wherelen + 1) {
291 ofile = realloc_pool_memory(ofile, sizeof_pool_memory(fname) + wherelen + 1);
293 if (sizeof_pool_memory(lname) < rec.data_len) {
294 ofile = realloc_pool_memory(ofile, rec.data_len + 1);
300 * An Attributes record consists of:
305 * Link name (if file linked i.e. FT_LNK)
308 sscanf(rec.data, "%ld %d", &record_file_index, &type);
309 if (record_file_index != rec.FileIndex)
310 Emsg2(M_ABORT, 0, "Record header file index %ld not equal record index %ld\n",
311 rec.FileIndex, record_file_index);
313 while (*ap++ != ' ') /* skip record file index */
315 while (*ap++ != ' ') /* skip type */
317 /* Save filename and position to attributes */
322 *fp = *ap++; /* terminate filename & point to attribs */
324 /* Skip to Link name */
325 if (type == FT_LNK) {
330 strcat(lname, lp); /* "save" link name */
335 /* Is this the file we want? */
336 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
338 decode_stat(ap, &statp);
340 * Prepend the where directory so that the
341 * files are put where the user wants.
343 * We do a little jig here to handle Win32 files with
345 * If where is null and we are running on a win32 client,
347 * Otherwise, if the second character of the filename is a
348 * colon (:), change it into a slash (/) -- this creates
349 * a reasonable pathname on most systems.
351 strcpy(ofile, where);
352 if (fname[1] == ':') {
354 strcat(ofile, fname);
357 strcat(ofile, fname);
359 /* Dmsg1(000, "Restoring: %s\n", ofile); */
361 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
364 print_ls_output(ofile, &statp);
368 /* Data stream and extracting */
369 } else if (rec.Stream == STREAM_FILE_DATA) {
371 total += rec.data_len;
372 Dmsg2(8, "Write %ld bytes, total=%ld\n", rec.data_len, total);
373 if ((uint32_t)write(ofd, rec.data, rec.data_len) != rec.data_len) {
374 Emsg1(M_ABORT, 0, "Write error: %s\n", strerror(errno));
378 } else if (rec.Stream == STREAM_GZIP_DATA) {
383 compress_len = compress_buf_size;
384 if (uncompress((Bytef *)compress_buf, &compress_len,
385 (const Bytef *)rec.data, (uLong)rec.data_len) != Z_OK) {
386 Emsg0(M_ABORT, 0, _("Uncompression error.\n"));
389 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
390 if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
391 Dmsg0(0, "===Write error===\n");
392 Emsg2(M_ABORT, 0, "Write error on %s: %s\n", ofile, strerror(errno));
394 total += compress_len;
395 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec.data_len,
400 Emsg0(M_ABORT, 0, "GZIP data stream found, but GZIP not configured!\n");
405 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
406 } else if (extract) {
408 Emsg0(M_ABORT, 0, "Logic error output file should be open\n");
413 set_statp(jcr, fname, ofile, lname, type, &statp);
414 } else if (rec.Stream != STREAM_MD5_SIGNATURE) {
415 Dmsg2(0, "None of above!!! stream=%d data=%s\n", rec.Stream, rec.data);
419 /* If output file is still open, it was the last one in the
420 * archive since we just hit an end of file, so close the file.
424 set_statp(jcr, fname, ofile, lname, type, &statp);
426 release_device(jcr, dev, block);
428 free_pool_memory(fname);
429 free_pool_memory(ofile);
430 free_pool_memory(lname);
431 free_pool_memory(compress_buf);
437 extern char *getuser(uid_t uid);
438 extern char *getgroup(gid_t gid);
440 static void print_ls_output(char *fname, struct stat *statp)
446 p = encode_mode(statp->st_mode, buf);
447 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
449 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
451 n = sprintf(p, "%8lld ", (uint64_t)statp->st_size);
453 p = encode_time(statp->st_ctime, p);