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 */
57 static int wherelen; /* prefix length */
58 static uint32_t num_files = 0;
59 static struct stat statp;
60 static uint32_t compress_buf_size = 70000;
61 static POOLMEM *compress_buf;
67 "\nVersion: " VERSION " (" DATE ")\n\n"
68 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
69 " -b <file> specify a bootstrap file\n"
70 " -dnn set debug level to nn\n"
71 " -e <file> exclude list\n"
72 " -i <file> include list\n"
73 " -? print this message\n\n");
78 int main (int argc, char *argv[])
85 working_directory = "/tmp";
86 my_name_is(argc, argv, "bextract");
87 init_msg(NULL, NULL); /* setup message handler */
89 memset(ff, 0, sizeof(FF_PKT));
90 init_include_exclude_files(ff);
92 while ((ch = getopt(argc, argv, "b:d:e:i:?")) != -1) {
94 case 'b': /* bootstrap file */
95 bsr = parse_bsr(NULL, optarg);
99 case 'd': /* debug level */
100 debug_level = atoi(optarg);
101 if (debug_level <= 0)
105 case 'e': /* exclude list */
106 if ((fd = fopen(optarg, "r")) == NULL) {
107 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
108 optarg, strerror(errno));
111 while (fgets(line, sizeof(line), fd) != NULL) {
112 strip_trailing_junk(line);
113 Dmsg1(900, "add_exclude %s\n", line);
114 add_fname_to_exclude_list(ff, line);
119 case 'i': /* include list */
120 if ((fd = fopen(optarg, "r")) == NULL) {
121 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
122 optarg, strerror(errno));
125 while (fgets(line, sizeof(line), fd) != NULL) {
126 strip_trailing_junk(line);
127 Dmsg1(900, "add_include %s\n", line);
128 add_fname_to_include_list(ff, 0, line);
144 Pmsg0(0, "Wrong number of arguments: \n");
147 if (!got_inc) { /* If no include file, */
148 add_fname_to_include_list(ff, 0, "/"); /* include everything */
160 static void do_extract(char *devname)
163 jcr = setup_jcr("bextract", devname, bsr);
164 dev = setup_to_read_device(jcr);
169 /* Make sure where directory exists and that it is a directory */
170 if (stat(where, &statp) < 0) {
171 Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
172 where, strerror(errno));
174 if (!S_ISDIR(statp.st_mode)) {
175 Emsg1(M_ERROR_TERM, 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);
186 compress_buf = get_memory(compress_buf_size);
188 read_records(jcr, dev, record_cb, mount_next_read_volume);
189 /* If output file is still open, it was the last one in the
190 * archive since we just hit an end of file, so close the file.
194 set_statp(jcr, fname, ofile, lname, type, &statp);
196 release_device(jcr, dev);
198 free_pool_memory(fname);
199 free_pool_memory(ofile);
200 free_pool_memory(lname);
201 free_pool_memory(compress_buf);
204 printf("%u files restored.\n", num_files);
209 * Called here for each record from read_records()
211 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
213 if (rec->FileIndex < 0) {
214 return; /* we don't want labels */
217 /* File Attributes stream */
218 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
221 /* If extracting, it was from previous stream, so
222 * close the output file.
226 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
231 set_statp(jcr, fname, ofile, lname, type, &statp);
234 if (sizeof_pool_memory(fname) < rec->data_len) {
235 fname = realloc_pool_memory(fname, rec->data_len + 1);
237 if (sizeof_pool_memory(ofile) < rec->data_len + wherelen + 1) {
238 ofile = realloc_pool_memory(ofile, rec->data_len + wherelen + 1);
240 if (sizeof_pool_memory(lname) < rec->data_len) {
241 lname = realloc_pool_memory(lname, rec->data_len + wherelen + 1);
247 * An Attributes record consists of:
252 * Link name (if file linked i.e. FT_LNK)
255 sscanf(rec->data, "%ld %d", &record_file_index, &type);
256 if (record_file_index != rec->FileIndex)
257 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
258 rec->FileIndex, record_file_index);
260 while (*ap++ != ' ') /* skip record file index */
262 while (*ap++ != ' ') /* skip type */
264 /* Save filename and position to attributes */
269 *fp = *ap++; /* terminate filename & point to attribs */
271 /* Skip to Link name */
272 if (type == FT_LNK || type == FT_LNKSAVED) {
282 if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
284 decode_stat(ap, &statp);
286 * Prepend the where directory so that the
287 * files are put where the user wants.
289 * We do a little jig here to handle Win32 files with
291 * If where is null and we are running on a win32 client,
293 * Otherwise, if the second character of the filename is a
294 * colon (:), change it into a slash (/) -- this creates
295 * a reasonable pathname on most systems.
297 if (where[0] == 0 && win32_client) {
298 strcpy(ofile, fname);
301 strcpy(ofile, where);
302 if (fname[1] == ':') {
304 strcat(ofile, fname);
307 strcat(ofile, fname);
309 /* Fixup link name */
310 if (type == FT_LNK || type == FT_LNKSAVED) {
311 if (lp[0] == '/') { /* if absolute path */
312 strcpy(lname, where);
314 /* ***FIXME**** we shouldn't have links on Windoz */
325 /* Pmsg1(000, "Restoring: %s\n", ofile); */
327 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
331 print_ls_output(ofile, lname, type, &statp);
335 /* Data stream and extracting */
336 } else if (rec->Stream == STREAM_FILE_DATA) {
338 total += rec->data_len;
339 Dmsg2(8, "Write %ld bytes, total=%ld\n", rec->data_len, total);
340 if ((uint32_t)write(ofd, rec->data, rec->data_len) != rec->data_len) {
341 Emsg1(M_ERROR_TERM, 0, "Write error: %s\n", strerror(errno));
345 } else if (rec->Stream == STREAM_GZIP_DATA) {
350 compress_len = compress_buf_size;
351 if (uncompress((Bytef *)compress_buf, &compress_len,
352 (const Bytef *)rec->data, (uLong)rec->data_len) != Z_OK) {
353 Emsg0(M_ERROR_TERM, 0, _("Uncompression error.\n"));
356 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
357 if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
358 Pmsg0(0, "===Write error===\n");
359 Emsg2(M_ERROR_TERM, 0, "Write error on %s: %s\n", ofile, strerror(errno));
361 total += compress_len;
362 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
367 Emsg0(M_ERROR_TERM, 0, "GZIP data stream found, but GZIP not configured!\n");
372 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
373 } else if (extract) {
375 Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
380 set_statp(jcr, fname, ofile, lname, type, &statp);
381 } else if (rec->Stream != STREAM_MD5_SIGNATURE) {
382 Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec->Stream, rec->data);
389 /* Dummies to replace askdir.c */
390 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
391 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
392 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
393 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
394 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
395 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
396 int dir_send_job_status(JCR *jcr) {return 1;}
399 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
401 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
402 jcr->VolumeName, dev_name(dev));