3 * Dumb program to extract files from a Bacula backup.
5 * Kern E. Sibbald, 2000
11 Copyright (C) 2000-2004 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 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
40 static void do_extract(char *fname);
41 static bool record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
43 static DEVICE *dev = NULL;
48 static FF_PKT *ff = &my_ff;
49 static BSR *bsr = NULL;
50 static bool extract = false;
51 static int non_support_data = 0;
52 static long total = 0;
55 static uint32_t num_files = 0;
56 static uint32_t compress_buf_size = 70000;
57 static POOLMEM *compress_buf;
58 static int prog_name_msg = 0;
59 static int win32_data_msg = 0;
60 static char *VolumeName = NULL;
62 static char *wbuf; /* write buffer address */
63 static uint32_t wsize; /* write size */
64 static uint64_t fileAddr = 0; /* file write address */
66 #define CONFIG_FILE "bacula-sd.conf"
68 bool forge_on = false;
73 "\nVersion: " VERSION " (" BDATE ")\n\n"
74 "Usage: bextract <options> <bacula-archive-device-name> <directory-to-store-files>\n"
75 " -b <file> specify a bootstrap file\n"
76 " -c <file> specify a configuration file\n"
77 " -d <nn> set debug level to nn\n"
78 " -e <file> exclude list\n"
79 " -i <file> include list\n"
80 " -p proceed inspite of I/O errors\n"
81 " -V <volumes> specify Volume names (separated by |)\n"
82 " -? print this message\n\n");
87 int main (int argc, char *argv[])
94 working_directory = "/tmp";
95 my_name_is(argc, argv, "bextract");
96 init_msg(NULL, NULL); /* setup message handler */
98 memset(ff, 0, sizeof(FF_PKT));
99 init_include_exclude_files(ff);
102 while ((ch = getopt(argc, argv, "b:c:d:e:i:pV:?")) != -1) {
104 case 'b': /* bootstrap file */
105 bsr = parse_bsr(NULL, optarg);
106 // dump_bsr(bsr, true);
109 case 'c': /* specify config file */
110 if (configfile != NULL) {
113 configfile = bstrdup(optarg);
116 case 'd': /* debug level */
117 debug_level = atoi(optarg);
118 if (debug_level <= 0)
122 case 'e': /* exclude list */
123 if ((fd = fopen(optarg, "r")) == NULL) {
124 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
125 optarg, strerror(errno));
128 while (fgets(line, sizeof(line), fd) != NULL) {
129 strip_trailing_junk(line);
130 Dmsg1(900, "add_exclude %s\n", line);
131 add_fname_to_exclude_list(ff, line);
136 case 'i': /* include list */
137 if ((fd = fopen(optarg, "r")) == NULL) {
138 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
139 optarg, strerror(errno));
142 while (fgets(line, sizeof(line), fd) != NULL) {
143 strip_trailing_junk(line);
144 Dmsg1(900, "add_include %s\n", line);
145 add_fname_to_include_list(ff, 0, line);
155 case 'V': /* Volume name */
169 Pmsg0(0, "Wrong number of arguments: \n");
173 if (configfile == NULL) {
174 configfile = bstrdup(CONFIG_FILE);
177 parse_config(configfile);
179 if (!got_inc) { /* If no include file, */
180 add_fname_to_include_list(ff, 0, "/"); /* include everything */
190 Pmsg1(000, "%d Program Name and/or Program Data Stream records ignored.\n",
193 if (win32_data_msg) {
194 Pmsg1(000, "%d Win32 data or Win32 gzip data stream records. Ignored.\n",
200 static void do_extract(char *devname)
203 jcr = setup_jcr("bextract", devname, bsr, VolumeName);
204 dev = setup_to_access_device(jcr, 1); /* acquire for read */
210 /* Make sure where directory exists and that it is a directory */
211 if (stat(where, &statp) < 0) {
212 Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
213 where, strerror(errno));
215 if (!S_ISDIR(statp.st_mode)) {
216 Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
220 jcr->where = bstrdup(where);
223 compress_buf = get_memory(compress_buf_size);
225 read_records(dcr, record_cb, mount_next_read_volume);
226 /* If output file is still open, it was the last one in the
227 * archive since we just hit an end of file, so close the file.
229 if (is_bopen(&bfd)) {
230 set_attributes(jcr, attr, &bfd);
237 printf("%u files restored.\n", num_files);
242 * Called here for each record from read_records()
244 static bool record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
248 if (rec->FileIndex < 0) {
249 return true; /* we don't want labels */
252 /* File Attributes stream */
254 switch (rec->Stream) {
255 case STREAM_UNIX_ATTRIBUTES:
256 case STREAM_UNIX_ATTRIBUTES_EX:
258 /* If extracting, it was from previous stream, so
259 * close the output file.
262 if (!is_bopen(&bfd)) {
263 Emsg0(M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
265 set_attributes(jcr, attr, &bfd);
269 if (!unpack_attributes_record(jcr, rec->Stream, rec->data, attr)) {
270 Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
273 if (attr->file_index != rec->FileIndex) {
274 Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
275 rec->FileIndex, attr->file_index);
278 if (file_is_included(ff, attr->fname) && !file_is_excluded(ff, attr->fname)) {
280 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
281 if (!is_stream_supported(attr->data_stream)) {
282 if (!non_support_data++) {
283 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
284 stream_to_ascii(attr->data_stream));
291 build_attr_output_fnames(jcr, attr);
294 stat = create_file(jcr, attr, &bfd, REPLACE_ALWAYS);
301 print_ls_output(jcr, attr);
306 set_attributes(jcr, attr, &bfd);
307 print_ls_output(jcr, attr);
315 /* Data stream and extracting */
316 case STREAM_FILE_DATA:
317 case STREAM_SPARSE_DATA:
318 case STREAM_WIN32_DATA:
321 if (rec->Stream == STREAM_SPARSE_DATA) {
324 wbuf = rec->data + SPARSE_FADDR_SIZE;
325 wsize = rec->data_len - SPARSE_FADDR_SIZE;
326 ser_begin(rec->data, SPARSE_FADDR_SIZE);
328 if (fileAddr != faddr) {
330 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
331 Emsg2(M_ERROR_TERM, 0, _("Seek error on %s: %s\n"),
332 attr->ofname, strerror(errno));
337 wsize = rec->data_len;
340 Dmsg2(8, "Write %u bytes, total=%u\n", wsize, total);
341 if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
342 Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"),
343 attr->ofname, strerror(errno));
349 /* GZIP data stream */
350 case STREAM_GZIP_DATA:
351 case STREAM_SPARSE_GZIP_DATA:
352 case STREAM_WIN32_GZIP_DATA:
358 if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
362 wbuf = rec->data + SPARSE_FADDR_SIZE;
363 wsize = rec->data_len - SPARSE_FADDR_SIZE;
364 ser_begin(rec->data, SPARSE_FADDR_SIZE);
366 if (fileAddr != faddr) {
368 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
369 Emsg3(M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
370 edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
377 wsize = rec->data_len;
379 compress_len = compress_buf_size;
380 if ((stat=uncompress((Bytef *)compress_buf, &compress_len,
381 (const Bytef *)wbuf, (uLong)wsize) != Z_OK)) {
382 Emsg1(M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
387 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
388 if ((uLongf)bwrite(&bfd, compress_buf, (size_t)compress_len) != compress_len) {
389 Pmsg0(0, "===Write error===\n");
390 Emsg2(M_ERROR, 0, _("Write error on %s: %s\n"),
391 attr->ofname, strerror(errno));
395 total += compress_len;
396 fileAddr += compress_len;
397 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
402 Emsg0(M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
409 case STREAM_MD5_SIGNATURE:
410 case STREAM_SHA1_SIGNATURE:
413 case STREAM_PROGRAM_NAMES:
414 case STREAM_PROGRAM_DATA:
415 if (!prog_name_msg) {
416 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
422 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
424 if (!is_bopen(&bfd)) {
425 Emsg0(M_ERROR, 0, "Logic error output file should be open but is not.\n");
427 set_attributes(jcr, attr, &bfd);
430 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"),
438 /* Dummies to replace askdir.c */
439 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing) { return 1;}
440 bool dir_find_next_appendable_volume(DCR *dcr) { return 1;}
441 bool dir_update_volume_info(DCR *dcr, bool relabel) { return 1; }
442 bool dir_create_jobmedia_record(DCR *dcr) { return 1; }
443 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr) { return 1; }
444 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
445 bool dir_send_job_status(JCR *jcr) {return 1;}
448 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
451 DEVICE *dev = dcr->dev;
452 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
453 jcr->VolumeName, dev_name(dev));