2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Dumb program to extract files from a Bacula backup.
40 #include "findlib/find.h"
42 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
44 static void do_extract(char *fname);
45 static bool record_cb(DCR *dcr, DEV_RECORD *rec);
47 static DEVICE *dev = NULL;
52 static BSR *bsr = NULL;
53 static bool extract = false;
54 static int non_support_data = 0;
55 static long total = 0;
58 static uint32_t num_files = 0;
59 static uint32_t compress_buf_size = 70000;
60 static POOLMEM *compress_buf;
61 static int prog_name_msg = 0;
62 static int win32_data_msg = 0;
63 static char *VolumeName = NULL;
65 static char *wbuf; /* write buffer address */
66 static uint32_t wsize; /* write size */
67 static uint64_t fileAddr = 0; /* file write address */
69 static CONFIG *config;
70 #define CONFIG_FILE "bacula-sd.conf"
71 char *configfile = NULL;
72 STORES *me = NULL; /* our Global resource */
73 bool forge_on = false;
74 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
75 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
81 "\nVersion: %s (%s)\n\n"
82 "Usage: bextract <options> <bacula-archive-device-name> <directory-to-store-files>\n"
83 " -b <file> specify a bootstrap file\n"
84 " -c <file> specify a Storage configuration file\n"
85 " -d <nn> set debug level to <nn>\n"
86 " -dt print timestamp in debug output\n"
87 " -e <file> exclude list\n"
88 " -i <file> include list\n"
89 " -p proceed inspite of I/O errors\n"
91 " -V <volumes> specify Volume names (separated by |)\n"
92 " -? print this message\n\n"), 2000, VERSION, BDATE);
97 int main (int argc, char *argv[])
102 bool got_inc = false;
104 setlocale(LC_ALL, "");
105 bindtextdomain("bacula", LOCALEDIR);
106 textdomain("bacula");
109 working_directory = "/tmp";
110 my_name_is(argc, argv, "bextract");
111 init_msg(NULL, NULL); /* setup message handler */
115 ff = init_find_files();
118 while ((ch = getopt(argc, argv, "b:c:d:e:i:pvV:?")) != -1) {
120 case 'b': /* bootstrap file */
121 bsr = parse_bsr(NULL, optarg);
122 // dump_bsr(bsr, true);
125 case 'c': /* specify config file */
126 if (configfile != NULL) {
129 configfile = bstrdup(optarg);
132 case 'd': /* debug level */
133 if (*optarg == 't') {
134 dbg_timestamp = true;
136 debug_level = atoi(optarg);
137 if (debug_level <= 0) {
143 case 'e': /* exclude list */
144 if ((fd = fopen(optarg, "rb")) == NULL) {
146 Pmsg2(0, _("Could not open exclude file: %s, ERR=%s\n"),
147 optarg, be.bstrerror());
150 while (fgets(line, sizeof(line), fd) != NULL) {
151 strip_trailing_junk(line);
152 Dmsg1(900, "add_exclude %s\n", line);
153 add_fname_to_exclude_list(ff, line);
158 case 'i': /* include list */
159 if ((fd = fopen(optarg, "rb")) == NULL) {
161 Pmsg2(0, _("Could not open include file: %s, ERR=%s\n"),
162 optarg, be.bstrerror());
165 while (fgets(line, sizeof(line), fd) != NULL) {
166 strip_trailing_junk(line);
167 Dmsg1(900, "add_include %s\n", line);
168 add_fname_to_include_list(ff, 0, line);
182 case 'V': /* Volume name */
196 Pmsg0(0, _("Wrong number of arguments: \n"));
200 if (configfile == NULL) {
201 configfile = bstrdup(CONFIG_FILE);
204 config = new_config_parser();
205 parse_sd_config(config, configfile, M_ERROR_TERM);
207 if (!got_inc) { /* If no include file, */
208 add_fname_to_include_list(ff, 0, "/"); /* include everything */
218 Pmsg1(000, _("%d Program Name and/or Program Data Stream records ignored.\n"),
221 if (win32_data_msg) {
222 Pmsg1(000, _("%d Win32 data or Win32 gzip data stream records. Ignored.\n"),
225 term_include_exclude_files(ff);
230 static void do_extract(char *devname)
234 enable_backup_privileges(NULL, 1);
236 jcr = setup_jcr("bextract", devname, bsr, VolumeName, 1); /* acquire for read */
240 dev = jcr->read_dcr->dev;
246 /* Make sure where directory exists and that it is a directory */
247 if (stat(where, &statp) < 0) {
249 Emsg2(M_ERROR_TERM, 0, _("Cannot stat %s. It must exist. ERR=%s\n"),
250 where, be.bstrerror());
252 if (!S_ISDIR(statp.st_mode)) {
253 Emsg1(M_ERROR_TERM, 0, _("%s must be a directory.\n"), where);
257 jcr->where = bstrdup(where);
258 attr = new_attr(jcr);
260 compress_buf = get_memory(compress_buf_size);
262 read_records(dcr, record_cb, mount_next_read_volume);
263 /* If output file is still open, it was the last one in the
264 * archive since we just hit an end of file, so close the file.
266 if (is_bopen(&bfd)) {
267 set_attributes(jcr, attr, &bfd);
274 printf(_("%u files restored.\n"), num_files);
278 static bool store_data(BFILE *bfd, char *data, const int32_t length)
280 if (is_win32_stream(attr->data_stream) && !have_win32_api()) {
281 set_portable_backup(bfd);
282 if (!processWin32BackupAPIBlock(bfd, data, length)) {
284 Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"),
285 attr->ofname, be.bstrerror());
288 } else if (bwrite(bfd, data, length) != (ssize_t)length) {
290 Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"),
291 attr->ofname, be.bstrerror());
299 * Called here for each record from read_records()
301 static bool record_cb(DCR *dcr, DEV_RECORD *rec)
306 if (rec->FileIndex < 0) {
307 return true; /* we don't want labels */
310 /* File Attributes stream */
312 switch (rec->Stream) {
313 case STREAM_UNIX_ATTRIBUTES:
314 case STREAM_UNIX_ATTRIBUTES_EX:
316 /* If extracting, it was from previous stream, so
317 * close the output file.
320 if (!is_bopen(&bfd)) {
321 Emsg0(M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
323 set_attributes(jcr, attr, &bfd);
327 if (!unpack_attributes_record(jcr, rec->Stream, rec->data, attr)) {
328 Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
331 if (attr->file_index != rec->FileIndex) {
332 Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
333 rec->FileIndex, attr->file_index);
336 if (file_is_included(ff, attr->fname) && !file_is_excluded(ff, attr->fname)) {
338 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
339 if (!is_restore_stream_supported(attr->data_stream)) {
340 if (!non_support_data++) {
341 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
342 stream_to_ascii(attr->data_stream));
348 build_attr_output_fnames(jcr, attr);
350 if (attr->type == FT_DELETED) { /* TODO: choose the right fname/ofname */
351 Jmsg(jcr, M_INFO, 0, _("%s was deleted.\n"), attr->fname);
357 stat = create_file(jcr, attr, &bfd, REPLACE_ALWAYS);
364 print_ls_output(jcr, attr);
369 set_attributes(jcr, attr, &bfd);
370 print_ls_output(jcr, attr);
378 /* Data stream and extracting */
379 case STREAM_FILE_DATA:
380 case STREAM_SPARSE_DATA:
381 case STREAM_WIN32_DATA:
384 if (rec->Stream == STREAM_SPARSE_DATA) {
387 wbuf = rec->data + SPARSE_FADDR_SIZE;
388 wsize = rec->data_len - SPARSE_FADDR_SIZE;
389 ser_begin(rec->data, SPARSE_FADDR_SIZE);
391 if (fileAddr != faddr) {
393 if (blseek(&bfd, (boffset_t)fileAddr, SEEK_SET) < 0) {
395 Emsg2(M_ERROR_TERM, 0, _("Seek error on %s: %s\n"),
396 attr->ofname, be.bstrerror());
401 wsize = rec->data_len;
404 Dmsg2(8, "Write %u bytes, total=%u\n", wsize, total);
405 store_data(&bfd, wbuf, wsize);
410 /* GZIP data stream */
411 case STREAM_GZIP_DATA:
412 case STREAM_SPARSE_GZIP_DATA:
413 case STREAM_WIN32_GZIP_DATA:
419 if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
423 wbuf = rec->data + SPARSE_FADDR_SIZE;
424 wsize = rec->data_len - SPARSE_FADDR_SIZE;
425 ser_begin(rec->data, SPARSE_FADDR_SIZE);
427 if (fileAddr != faddr) {
429 if (blseek(&bfd, (boffset_t)fileAddr, SEEK_SET) < 0) {
431 Emsg3(M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
432 edit_uint64(fileAddr, ec1), attr->ofname, be.bstrerror());
439 wsize = rec->data_len;
441 compress_len = compress_buf_size;
442 if ((stat=uncompress((Bytef *)compress_buf, &compress_len,
443 (const Bytef *)wbuf, (uLong)wsize) != Z_OK)) {
444 Emsg1(M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
449 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
450 store_data(&bfd, compress_buf, compress_len);
451 total += compress_len;
452 fileAddr += compress_len;
453 Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
458 Emsg0(M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
465 case STREAM_MD5_DIGEST:
466 case STREAM_SHA1_DIGEST:
467 case STREAM_SHA256_DIGEST:
468 case STREAM_SHA512_DIGEST:
471 case STREAM_SIGNED_DIGEST:
472 case STREAM_ENCRYPTED_SESSION_DATA:
473 // TODO landonf: Investigate crypto support in the storage daemon
476 case STREAM_PROGRAM_NAMES:
477 case STREAM_PROGRAM_DATA:
478 if (!prog_name_msg) {
479 Pmsg0(000, _("Got Program Name or Data Stream. Ignored.\n"));
485 /* If extracting, weird stream (not 1 or 2), close output file anyway */
487 if (!is_bopen(&bfd)) {
488 Emsg0(M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
490 set_attributes(jcr, attr, &bfd);
493 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"),
501 /* Dummies to replace askdir.c */
502 bool dir_find_next_appendable_volume(DCR *dcr) { return 1;}
503 bool dir_update_volume_info(DCR *dcr, bool relabel, bool update_LastWritten) { return 1; }
504 bool dir_create_jobmedia_record(DCR *dcr, bool zero) { return 1; }
505 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr) { return 1; }
506 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
507 bool dir_send_job_status(JCR *jcr) {return 1;}
510 bool dir_ask_sysop_to_mount_volume(DCR *dcr, int /*mode*/)
512 DEVICE *dev = dcr->dev;
513 fprintf(stderr, _("Mount Volume \"%s\" on device %s and press return when ready: "),
514 dcr->VolumeName, dev->print_name());
520 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing)
522 Dmsg0(100, "Fake dir_get_volume_info\n");
523 bstrncpy(dcr->VolCatInfo.VolCatName, dcr->VolumeName, sizeof(dcr->VolCatInfo.VolCatName));
524 dcr->VolCatInfo.VolCatParts = find_num_dvd_parts(dcr);
525 Dmsg2(500, "Vol=%s num_parts=%d\n", dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.VolCatParts);