3 * Dumb program to extract files from a Bacula backup.
11 Copyright (C) 2000-2005 Kern Sibbald
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(DCR *dcr, DEV_RECORD *rec);
43 static DEVICE *dev = NULL;
48 static BSR *bsr = NULL;
49 static bool extract = false;
50 static int non_support_data = 0;
51 static long total = 0;
54 static uint32_t num_files = 0;
55 static uint32_t compress_buf_size = 70000;
56 static POOLMEM *compress_buf;
57 static int prog_name_msg = 0;
58 static int win32_data_msg = 0;
59 static char *VolumeName = NULL;
61 static char *wbuf; /* write buffer address */
62 static uint32_t wsize; /* write size */
63 static uint64_t fileAddr = 0; /* file write address */
65 #define CONFIG_FILE "bacula-sd.conf"
67 STORES *me = NULL; /* our Global resource */
68 bool forge_on = false;
69 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
70 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
75 "Copyright (C) 2000-2005 Kern Sibbald.\n"
76 "\nVersion: " VERSION " (" BDATE ")\n\n"
77 "Usage: bextract <options> <bacula-archive-device-name> <directory-to-store-files>\n"
78 " -b <file> specify a bootstrap file\n"
79 " -c <file> specify a configuration file\n"
80 " -d <nn> set debug level to nn\n"
81 " -e <file> exclude list\n"
82 " -i <file> include list\n"
83 " -p proceed inspite of I/O errors\n"
85 " -V <volumes> specify Volume names (separated by |)\n"
86 " -? print this message\n\n");
91 int main (int argc, char *argv[])
98 working_directory = "/tmp";
99 my_name_is(argc, argv, "bextract");
100 init_msg(NULL, NULL); /* setup message handler */
102 ff = init_find_files();
105 while ((ch = getopt(argc, argv, "b:c:d:e:i:pvV:?")) != -1) {
107 case 'b': /* bootstrap file */
108 bsr = parse_bsr(NULL, optarg);
109 // dump_bsr(bsr, true);
112 case 'c': /* specify config file */
113 if (configfile != NULL) {
116 configfile = bstrdup(optarg);
119 case 'd': /* debug level */
120 debug_level = atoi(optarg);
121 if (debug_level <= 0)
125 case 'e': /* exclude list */
126 if ((fd = fopen(optarg, "r")) == NULL) {
128 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
129 optarg, be.strerror());
132 while (fgets(line, sizeof(line), fd) != NULL) {
133 strip_trailing_junk(line);
134 Dmsg1(900, "add_exclude %s\n", line);
135 add_fname_to_exclude_list(ff, line);
140 case 'i': /* include list */
141 if ((fd = fopen(optarg, "r")) == NULL) {
143 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
144 optarg, be.strerror());
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);
164 case 'V': /* Volume name */
178 Pmsg0(0, "Wrong number of arguments: \n");
182 if (configfile == NULL) {
183 configfile = bstrdup(CONFIG_FILE);
186 parse_config(configfile);
188 if (!got_inc) { /* If no include file, */
189 add_fname_to_include_list(ff, 0, "/"); /* include everything */
199 Pmsg1(000, "%d Program Name and/or Program Data Stream records ignored.\n",
202 if (win32_data_msg) {
203 Pmsg1(000, "%d Win32 data or Win32 gzip data stream records. Ignored.\n",
206 term_include_exclude_files(ff);
211 static void do_extract(char *devname)
214 jcr = setup_jcr("bextract", devname, bsr, VolumeName, 1); /* acquire for read */
224 /* Make sure where directory exists and that it is a directory */
225 if (stat(where, &statp) < 0) {
227 Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
228 where, be.strerror());
230 if (!S_ISDIR(statp.st_mode)) {
231 Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
235 jcr->where = bstrdup(where);
238 compress_buf = get_memory(compress_buf_size);
240 read_records(dcr, record_cb, mount_next_read_volume);
241 /* If output file is still open, it was the last one in the
242 * archive since we just hit an end of file, so close the file.
244 if (is_bopen(&bfd)) {
245 set_attributes(jcr, attr, &bfd);
252 printf("%u files restored.\n", num_files);
257 * Called here for each record from read_records()
259 static bool record_cb(DCR *dcr, DEV_RECORD *rec)
264 if (rec->FileIndex < 0) {
265 return true; /* we don't want labels */
268 /* File Attributes stream */
270 switch (rec->Stream) {
271 case STREAM_UNIX_ATTRIBUTES:
272 case STREAM_UNIX_ATTRIBUTES_EX:
274 /* If extracting, it was from previous stream, so
275 * close the output file.
278 if (!is_bopen(&bfd)) {
279 Emsg0(M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
281 set_attributes(jcr, attr, &bfd);
285 if (!unpack_attributes_record(jcr, rec->Stream, rec->data, attr)) {
286 Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
289 if (attr->file_index != rec->FileIndex) {
290 Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
291 rec->FileIndex, attr->file_index);
294 if (file_is_included(ff, attr->fname) && !file_is_excluded(ff, attr->fname)) {
296 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
297 if (!is_stream_supported(attr->data_stream)) {
298 if (!non_support_data++) {
299 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
300 stream_to_ascii(attr->data_stream));
307 build_attr_output_fnames(jcr, attr);
310 stat = create_file(jcr, attr, &bfd, REPLACE_ALWAYS);
317 print_ls_output(jcr, attr);
322 set_attributes(jcr, attr, &bfd);
323 print_ls_output(jcr, attr);
331 /* Data stream and extracting */
332 case STREAM_FILE_DATA:
333 case STREAM_SPARSE_DATA:
334 case STREAM_WIN32_DATA:
337 if (rec->Stream == STREAM_SPARSE_DATA) {
340 wbuf = rec->data + SPARSE_FADDR_SIZE;
341 wsize = rec->data_len - SPARSE_FADDR_SIZE;
342 ser_begin(rec->data, SPARSE_FADDR_SIZE);
344 if (fileAddr != faddr) {
346 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
348 Emsg2(M_ERROR_TERM, 0, _("Seek error on %s: %s\n"),
349 attr->ofname, be.strerror());
354 wsize = rec->data_len;
357 Dmsg2(8, "Write %u bytes, total=%u\n", wsize, total);
358 if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
360 Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"),
361 attr->ofname, be.strerror());
367 /* GZIP data stream */
368 case STREAM_GZIP_DATA:
369 case STREAM_SPARSE_GZIP_DATA:
370 case STREAM_WIN32_GZIP_DATA:
376 if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
380 wbuf = rec->data + SPARSE_FADDR_SIZE;
381 wsize = rec->data_len - SPARSE_FADDR_SIZE;
382 ser_begin(rec->data, SPARSE_FADDR_SIZE);
384 if (fileAddr != faddr) {
386 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
388 Emsg3(M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
389 edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
396 wsize = rec->data_len;
398 compress_len = compress_buf_size;
399 if ((stat=uncompress((Bytef *)compress_buf, &compress_len,
400 (const Bytef *)wbuf, (uLong)wsize) != Z_OK)) {
401 Emsg1(M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
406 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
407 if ((uLongf)bwrite(&bfd, compress_buf, (size_t)compress_len) != compress_len) {
409 Pmsg0(0, "===Write error===\n");
410 Emsg2(M_ERROR, 0, _("Write error on %s: %s\n"),
411 attr->ofname, be.strerror());
415 total += compress_len;
416 fileAddr += compress_len;
417 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
422 Emsg0(M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
429 case STREAM_MD5_SIGNATURE:
430 case STREAM_SHA1_SIGNATURE:
433 case STREAM_PROGRAM_NAMES:
434 case STREAM_PROGRAM_DATA:
435 if (!prog_name_msg) {
436 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
442 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
444 if (!is_bopen(&bfd)) {
445 Emsg0(M_ERROR, 0, "Logic error output file should be open but is not.\n");
447 set_attributes(jcr, attr, &bfd);
450 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"),
458 /* Dummies to replace askdir.c */
459 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing) { return 1;}
460 bool dir_find_next_appendable_volume(DCR *dcr) { return 1;}
461 bool dir_update_volume_info(DCR *dcr, bool relabel) { return 1; }
462 bool dir_create_jobmedia_record(DCR *dcr) { return 1; }
463 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr) { return 1; }
464 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
465 bool dir_send_job_status(JCR *jcr) {return 1;}
468 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
470 DEVICE *dev = dcr->dev;
471 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
472 dcr->VolumeName, dev->print_name());