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[])
67 my_name_is(argc, argv, "bextract");
68 init_msg(NULL, NULL); /* setup message handler */
70 memset(ff, 0, sizeof(FF_PKT));
71 init_include_exclude_files(ff);
73 while ((ch = getopt(argc, argv, "d:e:i:?")) != -1) {
75 case 'd': /* debug level */
76 debug_level = atoi(optarg);
81 case 'e': /* exclude list */
82 if ((fd = fopen(optarg, "r")) == NULL) {
83 Dmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
84 optarg, strerror(errno));
87 while (fgets(line, sizeof(line), fd) != NULL) {
88 strip_trailing_junk(line);
89 Dmsg1(900, "add_exclude %s\n", line);
90 add_fname_to_exclude_list(ff, line);
95 case 'i': /* include list */
96 if ((fd = fopen(optarg, "r")) == NULL) {
97 Dmsg2(0, "Could not open include 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_include %s\n", line);
104 add_fname_to_include_list(ff, 0, line);
119 Dmsg0(0, "Wrong number of arguments: \n");
123 jcr = new_jcr(sizeof(JCR), my_free_jcr);
124 jcr->VolSessionId = 1;
125 jcr->VolSessionTime = (uint32_t)time(NULL);
128 do_extract(argv[0], argv[1]);
135 static void do_extract(char *devname, char *where)
143 long record_file_index;
147 POOLMEM *fname; /* original file name */
148 POOLMEM *ofile; /* output name with prefix */
149 POOLMEM *lname; /* link name */
150 int wherelen; /* prefix length */
152 if (strncmp(devname, "/dev/", 5) != 0) {
153 /* Try stripping file part */
154 p = devname + strlen(devname);
155 while (p >= devname && *p != '/') {
159 strcpy(VolName, p+1);
164 dev = init_dev(NULL, devname);
165 if (!dev || !open_device(dev)) {
166 Emsg1(M_ABORT, 0, "Cannot open %s\n", devname);
168 Dmsg0(90, "Device opened for read.\n");
170 if (stat(where, &statp) < 0) {
171 Emsg2(M_ABORT, 0, "Cannot stat %s. It must exist. ERR=%s\n",
172 where, strerror(errno));
174 if (!S_ISDIR(statp.st_mode)) {
175 Emsg1(M_ABORT, 0, "%s must be a directory.\n", where);
178 wherelen = strlen(where);
179 fname = get_pool_memory(PM_FNAME);
180 ofile = get_pool_memory(PM_FNAME);
181 lname = get_pool_memory(PM_FNAME);
183 block = new_block(dev);
185 strcpy(jcr->VolumeName, VolName);
186 Dmsg1(100, "Volume=%s\n", jcr->VolumeName);
188 if (!acquire_device_for_read(jcr, dev, block)) {
189 Emsg1(M_ABORT, 0, "Cannot open %s\n", devname);
192 memset(&rec, 0, sizeof(rec));
193 rec.data = get_memory(70000);
195 uint32_t compress_buf_size = 70000;
196 POOLMEM *compress_buf = get_memory(compress_buf_size);
200 if (!read_record(dev, block, &rec)) {
202 if (dev->state & ST_EOT) {
205 if (dev->state & ST_EOF) {
206 continue; /* try again */
208 Dmsg0(0, "Read Record got a bad record\n");
209 status_dev(dev, &status);
210 Dmsg1(20, "Device status: %x\n", status);
212 Emsg0(M_ABORT, 0, "Unexpected End of Data\n");
213 else if (status & MT_EOT)
214 Emsg0(M_ABORT, 0, "Unexpected End of Tape\n");
215 else if (status & MT_EOF)
216 Emsg0(M_ABORT, 0, "Unexpected End of File\n");
217 else if (status & MT_DR_OPEN)
218 Emsg0(M_ABORT, 0, "Tape Door is Open\n");
219 else if (!(status & MT_ONLINE))
220 Emsg0(M_ABORT, 0, "Unexpected Tape is Off-line\n");
222 Emsg3(M_ABORT, 0, "Read error %d on Record Header %s: %s\n", n, dev_name(dev), strerror(errno));
226 /* This is no longer used */
227 if (rec.VolSessionId == 0 && rec.VolSessionTime == 0) {
228 Emsg0(M_ERROR, 0, "Zero header record. This shouldn't happen.\n");
229 break; /* END OF FILE */
233 * Check for Start or End of Session Record
236 if (rec.FileIndex < 0) {
238 switch (rec.FileIndex) {
240 rtype = "Fresh Volume Label";
243 rtype = "Volume Label";
246 rtype = "Begin Session";
249 rtype = "End Session";
252 rtype = "End of Media";
258 if (debug_level > 0) {
259 printf("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
260 rtype, rec.VolSessionId, rec.VolSessionTime, rec.Stream, rec.data_len);
265 /* File Attributes stream */
266 if (rec.Stream == STREAM_UNIX_ATTRIBUTES) {
269 /* If extracting, it was from previous stream, so
270 * close the output file.
274 Emsg0(M_ABORT, 0, "Logic error output file should be open\n");
279 set_statp(jcr, fname, ofile, lname, type, &statp);
282 if (sizeof_pool_memory(fname) < rec.data_len) {
283 fname = realloc_pool_memory(fname, rec.data_len + 1);
285 if (sizeof_pool_memory(ofile) < sizeof_pool_memory(fname) + wherelen + 1) {
286 ofile = realloc_pool_memory(ofile, sizeof_pool_memory(fname) + wherelen + 1);
288 if (sizeof_pool_memory(lname) < rec.data_len) {
289 ofile = realloc_pool_memory(ofile, rec.data_len + 1);
295 * An Attributes record consists of:
300 * Link name (if file linked i.e. FT_LNK)
303 sscanf(rec.data, "%ld %d %s", &record_file_index, &type, fname);
304 if (record_file_index != rec.FileIndex)
305 Emsg2(M_ABORT, 0, "Record header file index %ld not equal record index %ld\n",
306 rec.FileIndex, record_file_index);
308 /* Skip to attributes */
311 /* Skip to Link name */
312 if (type == FT_LNK) {
317 strcat(lname, lp); /* "save" link name */
322 /* Is this the file we want? */
323 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
325 decode_stat(ap, &statp);
327 * Prepend the where directory so that the
328 * files are put where the user wants.
330 * We do a little jig here to handle Win32 files with
332 * If where is null and we are running on a win32 client,
334 * Otherwise, if the second character of the filename is a
335 * colon (:), change it into a slash (/) -- this creates
336 * a reasonable pathname on most systems.
338 strcpy(ofile, where);
339 if (fname[1] == ':') {
341 strcat(ofile, fname);
344 strcat(ofile, fname);
346 /* Dmsg1(000, "Restoring: %s\n", ofile); */
348 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
351 print_ls_output(ofile, &statp);
355 /* Data stream and extracting */
356 } else if (rec.Stream == STREAM_FILE_DATA) {
358 total += rec.data_len;
359 Dmsg2(8, "Write %ld bytes, total=%ld\n", rec.data_len, total);
360 if ((uint32_t)write(ofd, rec.data, rec.data_len) != rec.data_len) {
361 Emsg1(M_ABORT, 0, "Write error: %s\n", strerror(errno));
365 } else if (rec.Stream == STREAM_GZIP_DATA) {
370 compress_len = compress_buf_size;
371 if (uncompress((Bytef *)compress_buf, &compress_len,
372 (const Bytef *)rec.data, (uLong)rec.data_len) != Z_OK) {
373 Emsg0(M_ABORT, 0, _("Uncompression error.\n"));
376 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
377 if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
378 Dmsg0(0, "===Write error===\n");
379 Emsg2(M_ABORT, 0, "Write error on %s: %s\n", ofile, strerror(errno));
381 total += compress_len;
382 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec.data_len,
387 Emsg0(M_ABORT, 0, "GZIP data stream found, but GZIP not configured!\n");
392 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
393 } else if (extract) {
395 Emsg0(M_ABORT, 0, "Logic error output file should be open\n");
400 set_statp(jcr, fname, ofile, lname, type, &statp);
401 } else if (rec.Stream != STREAM_MD5_SIGNATURE) {
402 Dmsg2(0, "None of above!!! stream=%d data=%s\n", rec.Stream, rec.data);
406 /* If output file is still open, it was the last one in the
407 * archive since we just hit an end of file, so close the file.
411 set_statp(jcr, fname, ofile, lname, type, &statp);
413 release_device(jcr, dev, block);
415 free_pool_memory(fname);
416 free_pool_memory(ofile);
417 free_pool_memory(lname);
418 free_pool_memory(compress_buf);
424 extern char *getuser(uid_t uid);
425 extern char *getgroup(gid_t gid);
427 static void print_ls_output(char *fname, struct stat *statp)
433 p = encode_mode(statp->st_mode, buf);
434 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
436 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
438 n = sprintf(p, "%8lld ", (uint64_t)statp->st_size);
440 p = encode_time(statp->st_ctime, p);