source: MondoRescue/branches/2.2.10/mondo/src/mondorestore/mondorestore.c@ 2623

Last change on this file since 2623 was 2623, checked in by Bruno Cornec, 14 years ago

r3783@localhost: bruno | 2010-04-11 11:57:27 +0200

  • Use MINDI_CACHE for temp file communication between archive/restore
  • Remove some now useless globals in mondo
  • Remove function get_cfg_file_from_archive_or_bust and get_cfg_file_from_archive
  • Adds function get_cfg_file_from_initrd (Not finished)
  • Recovery doesn't work in that version atm (checkpoint version)
  • Property svn:keywords set to Id
File size: 92.5 KB
Line 
1/***************************************************************************
2$Id: mondorestore.c 2623 2010-04-12 13:23:24Z bruno $
3restores mondoarchive data
4The main file for mondorestore.
5***************************************************************************/
6
7/**************************************************************************
8 * #include statements *
9 **************************************************************************/
10#include <pthread.h>
11#include "my-stuff.h"
12#include "mr_mem.h"
13#include "../common/mondostructures.h"
14#include "../common/libmondo.h"
15#include "mr-externs.h"
16#include "mondo-restore.h"
17#include "mondorestore.h"
18#include "mondo-rstr-compare-EXT.h"
19#include "mondo-rstr-tools-EXT.h"
20
21extern void twenty_seconds_til_yikes(void);
22
23/* Let mr_cleanup pojnt to finish for the moment */
24void (*mr_cleanup)(int) = finish;
25
26/* Reference to global bkpinfo */
27struct s_bkpinfo *bkpinfo;
28
29
30/* For use in other programs (ex. XMondo) */
31#ifdef MONDORESTORE_MODULE
32#define main __mondorestore_main
33#define g_ISO_restore_mode __mondorestore_g_ISO_restore_mode
34#endif
35
36//static char cvsid[] = "$Id: mondorestore.c 2623 2010-04-12 13:23:24Z bruno $";
37
38/**************************************************************************
39 * Globals *
40 **************************************************************************/
41/*
42extern char *g_tmpfs_mountpt; // declared in libmondo-tools.c
43*/
44extern bool g_text_mode;
45extern FILE *g_fprep;
46extern double g_kernel_version;
47extern int g_partition_table_locked_up;
48extern int g_noof_rows;
49
50extern int partition_everything(struct mountlist_itself *mountlist);
51extern int handle_incoming_parameters(int argc, char *argv[]);
52
53/**
54 * @name Restore-Time Globals
55 * @ingroup globalGroup
56 * @{
57 */
58
59/**
60 * If TRUE, then we're restoring from ISOs or an NFS server.
61 * If FALSE, then we're restoring from some kind of real media (tape, CD, etc.)
62 */
63bool g_ISO_restore_mode = FALSE; /* are we in Iso Mode? */
64
65/**
66 * If TRUE, then we have had a successful "nuke" restore.
67 */
68bool g_I_have_just_nuked = FALSE;
69
70/**
71 * The device to mount to get at the ISO images. Ignored unless @p g_ISO_restore_mode.
72 */
73char *g_isodir_device;
74
75/**
76 * The format of @p g_isodir_device. Ignored unless @p g_ISO_restore_mode.
77 */
78char *g_isodir_format;
79
80/**
81 * The location of 'filelist.full', containing all files (<em>including biggiefiles</em>) on
82 * the current archive set.
83 */
84char *g_filelist_full;
85
86/**
87 * The location of a file containing a list of the devices that were archived
88 * as images, not as individual files.
89 */
90char *g_filelist_imagedevs;
91
92/**
93 * The location of a file containing a list of imagedevs to actually restore.
94 * @see g_filelist_imagedevs
95 */
96char *g_imagedevs_restthese;
97
98/**
99 * Mondo's home directory during backup. Unused in mondorestore; included
100 * to avoid link errors.
101 */
102char *g_mondo_home;
103
104extern char *g_getfacl;
105extern char *g_getfattr;
106
107/* @} - end of "Restore-Time Globals" in globalGroup */
108
109
110
111extern int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived,
112 char direction);
113
114
115
116/**************************************************************************
117 * COMPAQ PROLIANT Stuff: needs some special help *
118**************************************************************************/
119
120/**
121 * The message to display if we detect that the user is using a Compaq Proliant.
122 */
123#define COMPAQ_PROLIANTS_SUCK "Partition and format your disk using Compaq's disaster recovery CD. After you've done that, please reboot with your Mondo media in Interactive Mode."
124
125
126
127
128/**
129 * Allow the user to modify the mountlist before we partition & format their drives.
130 * @param bkpinfo The backup information structure. @c disaster_recovery is the only field used.
131 * @param mountlist The mountlist to let the user modify.
132 * @param raidlist The raidlist that goes with @p mountlist.
133 * @return 0 for success, nonzero for failure.
134 * @ingroup restoreGuiGroup
135 */
136int let_user_edit_the_mountlist(struct mountlist_itself *mountlist,
137 struct raidlist_itself *raidlist)
138{
139 int retval = 0, res = 0;
140
141 log_msg(2, "let_user_edit_the_mountlist() --- starting");
142
143 assert(bkpinfo != NULL);
144 assert(mountlist != NULL);
145 assert(raidlist != NULL);
146 if (!does_file_exist(MINDI_CACHE"/mountlist.txt")) {
147 log_to_screen(MINDI_CACHE"/mountlist.txt doesn't exist");
148 return (1);
149 }
150
151 retval = load_mountlist(mountlist, MINDI_CACHE"/mountlist.txt");
152 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
153 if (retval) {
154 log_to_screen
155 ("Warning - load_raidtab_into_raidlist returned an error");
156 }
157 res = edit_mountlist(MINDI_CACHE"/mountlist.txt", mountlist, raidlist);
158 if (res) {
159 return (1);
160 }
161
162 save_mountlist_to_disk(mountlist, MINDI_CACHE"/mountlist.txt");
163 save_raidlist_to_raidtab(raidlist, RAIDTAB_FNAME);
164
165 log_to_screen("I have finished editing the mountlist for you.");
166
167 return (retval);
168}
169
170
171
172
173
174/**
175 * Determine whether @p mountlist contains a Compaq diagnostic partition.
176 * @param mountlist The mountlist to examine.
177 * @return TRUE if there's a Compaq diagnostic partition; FALSE if not.
178 * @ingroup restoreUtilityGroup
179 */
180bool
181partition_table_contains_Compaq_diagnostic_partition(struct
182 mountlist_itself *
183 mountlist)
184{
185 int i;
186
187 assert(mountlist != NULL);
188
189 for (i = 0; i < mountlist->entries; i++) {
190 if (strstr(mountlist->el[i].format, "ompaq")) {
191 log_msg(2, "mountlist[%d] (%s) is %s (Compaq alert!)",
192 i, mountlist->el[i].device, mountlist->el[i].format);
193
194 return (TRUE);
195 }
196 }
197 return (FALSE);
198}
199
200/**************************************************************************
201 *END_PARTITION_TABLE_CONTAINS_COMPAQ_DIAGNOSTIC_PARTITION *
202 **************************************************************************/
203
204
205/**
206 * Allow the user to abort the backup if we find that there is a Compaq diagnostic partition.
207 * @note This function does not actually check for the presence of a Compaq partition.
208 * @ingroup restoreUtilityGroup
209 */
210void offer_to_abort_because_Compaq_Proliants_suck(void)
211{
212 popup_and_OK(COMPAQ_PROLIANTS_SUCK);
213 if (ask_me_yes_or_no
214 ("Would you like to reboot and use your Compaq CD to prep your hard drive?"))
215 {
216 fatal_error
217 ("Aborting. Please reboot and prep your hard drive with your Compaq CD.");
218 }
219}
220
221/**************************************************************************
222 *END_OFFER_TO_ABORT_BECAUSE_COMPAQ_PROLIANTS_SUCK *
223 **************************************************************************/
224
225
226
227/**
228 * Call interactive_mode(), nuke_mode(), or compare_mode() depending on the user's choice.
229 * @param bkpinfo The backup information structure. Most fields are used.
230 * @param mountlist The mountlist containing information about the user's partitions.
231 * @param raidlist The raidlist to go with @p mountlist.
232 * @return The return code from the mode function called.
233 * @ingroup restoreGroup
234 */
235int
236catchall_mode(struct mountlist_itself *mountlist,
237 struct raidlist_itself *raidlist)
238{
239 char c;
240 char *tmp = NULL;
241 int retval = 0;
242
243 log_it("inside catchall");
244 assert(bkpinfo != NULL);
245 assert(mountlist != NULL);
246 assert(raidlist != NULL);
247 log_it("pre wrm");
248 c = which_restore_mode();
249 log_it("post wrm");
250 if (c == 'I' || c == 'C') {
251 interactively_obtain_media_parameters_from_user(FALSE);
252 } else if (c == 'N') {
253 // Auto mode nothing special to do
254 } else {
255 popup_and_OK("No restoring or comparing will take place today.");
256 if (is_this_device_mounted("/mnt/cdrom")) {
257 run_program_and_log_output("umount /mnt/cdrom", FALSE);
258 }
259 if (g_ISO_restore_mode) {
260 mr_asprintf(tmp, "umount %s", bkpinfo->isodir);
261 run_program_and_log_output(tmp, FALSE);
262 mr_free(tmp);
263 }
264 paranoid_MR_finish(0);
265 }
266
267 log_it("post int");
268
269 if (bkpinfo->backup_media_type == iso) {
270 if (iso_fiddly_bits((c == 'N') ? TRUE : FALSE)) {
271 log_msg(2,
272 "catchall_mode --- iso_fiddly_bits returned w/ error");
273 return (1);
274 } else {
275 log_msg(2, "catchall_mode --- iso_fiddly_bits ok");
276 }
277 }
278
279 if (c == 'I') {
280 log_msg(2, "IM selected");
281 retval += interactive_mode(mountlist, raidlist);
282 } else if (c == 'N') {
283 log_msg(2, "NM selected");
284 retval += nuke_mode(mountlist, raidlist);
285 } else if (c == 'C') {
286 log_msg(2, "CM selected");
287 retval += compare_mode(mountlist, raidlist);
288 }
289 return (retval);
290}
291
292/**************************************************************************
293 *END_CATCHALL_MODE *
294 **************************************************************************/
295
296/**************************************************************************
297 *END_ EXTRACT_CONFIG_FILE_FROM_RAMDISK *
298 **************************************************************************/
299
300
301/**
302 * Locate an executable in the directory structure rooted at @p restg.
303 * @param out_path Where to put the executable.
304 * @param fname The basename of the executable.
305 * @param restg The directory structure to look in.
306 * @note If it could not be found in @p restg then @p fname is put in @p out_path.
307 * @ingroup restoreUtilityGroup
308 */
309void
310find_pathname_of_executable_preferably_in_RESTORING(char *out_path,
311 char *fname,
312 char *restg)
313{
314 assert(out_path != NULL);
315 assert_string_is_neither_NULL_nor_zerolength(fname);
316
317 sprintf(out_path, "%s/sbin/%s", restg, fname);
318 if (does_file_exist(out_path)) {
319 sprintf(out_path, "%s/usr/sbin/%s", restg, fname);
320 if (does_file_exist(out_path)) {
321 sprintf(out_path, "%s/bin/%s", restg, fname);
322 if (does_file_exist(out_path)) {
323 sprintf(out_path, "%s/usr/bin/%s", restg, fname);
324 if (does_file_exist(out_path)) {
325 strcpy(out_path, fname);
326 }
327 }
328 }
329 }
330}
331
332/**************************************************************************
333 *END_FIND_PATHNAME_OF_EXECUTABLE_PREFERABLY_IN_RESTORING *
334 **************************************************************************/
335
336static void clean_blkid() {
337
338 char *tmp1 = NULL;
339
340 if (bkpinfo->restore_path) {
341 /* Clean up blkid cache file if they exist */
342 mr_asprintf(tmp1,"%s/etc/blkid.tab",bkpinfo->restore_path);
343 (void)unlink(tmp1);
344 paranoid_free(tmp1);
345 mr_asprintf(tmp1,"%s/etc/blkid.tab.old",bkpinfo->restore_path);
346 (void)unlink(tmp1);
347 paranoid_free(tmp1);
348 }
349}
350
351
352static void clean_multipathconf() {
353
354 char *tmp1 = NULL;
355 char *editor = NULL;
356
357 if (bkpinfo->restore_path) {
358 /* Clean up multiconf cache file if they exist */
359 mr_asprintf(tmp1,"%s/var/lib/multipath/bindings",bkpinfo->restore_path);
360 (void)unlink(tmp1);
361 paranoid_free(tmp1);
362 }
363
364 /* Edit multipath.conf if needed to adapt wwid */
365 mr_asprintf(tmp1,"%s/etc/multipath.conf", MNT_RESTORING);
366 if (does_file_exist(tmp1)) {
367 log_msg(2, "We may need to clean /etc/multipath.conf");
368 } else {
369 paranoid_free(tmp1);
370 return;
371 }
372 paranoid_free(tmp1);
373
374 if (bkpinfo->restore_mode != nuke) {
375 mr_asprintf(editor, "%s", find_my_editor());
376 mr_asprintf(tmp1,"chroot %s %s /etc/multipath.conf", MNT_RESTORING, editor);
377 popup_and_OK("You will now edit multipath.conf");
378 if (!g_text_mode) {
379 newtSuspend();
380 }
381 paranoid_system(tmp1);
382 if (!g_text_mode) {
383 newtResume();
384 }
385 paranoid_free(tmp1);
386 paranoid_free(editor);
387 } else {
388 log_to_screen("Non-interactive mode: no way to give you the keyboard so that you edit your multipath.conf. Hope it's OK");
389 log_msg(1,"Non-interactive mode: no way to give you the keyboard so that you edit your multipath.conf. Hope it's OK");
390 }
391}
392
393
394
395
396/**
397 * @addtogroup restoreGroup
398 * @{
399 */
400/**
401 * Restore the user's data, in a disaster recovery situation, prompting the
402 * user about whether or not to do every step.
403 * The user can edit the mountlist, choose files to restore, etc.
404 * @param bkpinfo The backup information structure. Most fields are used.
405 * @param mountlist The mountlist containing information about the user's partitions.
406 * @param raidlist The raidlist to go with @p mountlist.
407 * @return 0 for success, or the number of errors encountered.
408 */
409int interactive_mode(struct mountlist_itself *mountlist, struct raidlist_itself *raidlist)
410{
411 int retval = 0;
412 int res;
413 int ptn_errs = 0;
414 int fmt_errs = 0;
415
416 bool done;
417 bool restore_all;
418
419 /** needs malloc **********/
420 char *tmp;
421 char *p = NULL;
422 char *tmp1 = NULL;
423 char *fstab_fname = NULL;
424 char *old_restpath = NULL;
425
426 struct s_node *filelist;
427
428 /* try to partition and format */
429
430 log_msg(2, "interactive_mode --- starting (great, assertions OK)");
431
432 malloc_string(tmp);
433 assert(bkpinfo != NULL);
434 assert(mountlist != NULL);
435 assert(raidlist != NULL);
436
437 log_msg(2, "interactive_mode --- assertions OK");
438
439 if (g_text_mode) {
440 if (!ask_me_yes_or_no("Interactive Mode + textonly = experimental! Proceed anyway?")) {
441 fatal_error("Wise move.");
442 }
443 }
444
445 log_it("About to load config file");
446 read_cfg_file_into_bkpinfo(MINDI_CACHE"/mondorestore.cfg");
447 log_it("Done loading config file; resizing ML");
448
449 tmp1 = call_program_and_get_last_line_of_output("cat " CMDLINE);
450 if (strstr(tmp1, "noresize")) {
451 log_msg(1, "Not resizing mountlist.");
452 } else {
453 resize_mountlist_proportionately_to_suit_new_drives(mountlist);
454 }
455 mr_free(tmp1);
456
457 for (done = FALSE; !done;) {
458 log_it("About to edit mountlist");
459 if (g_text_mode) {
460 save_mountlist_to_disk(mountlist, MINDI_CACHE"/mountlist.txt");
461 mr_asprintf(tmp1, "%s %s", find_my_editor(), MINDI_CACHE"/mountlist.txt");
462 res = system(tmp1);
463 mr_free(tmp1);
464 load_mountlist(mountlist, MINDI_CACHE"/mountlist.txt");
465 } else {
466 res = edit_mountlist(MINDI_CACHE"/mountlist.txt", mountlist, raidlist);
467 }
468 log_it("Finished editing mountlist");
469 if (res) {
470 paranoid_MR_finish(1);
471 }
472 log_msg(2, "Proceeding...");
473 save_mountlist_to_disk(mountlist, MINDI_CACHE"/mountlist.txt");
474 save_raidlist_to_raidtab(raidlist, RAIDTAB_FNAME);
475 mvaddstr_and_log_it(1, 30, "Restoring Interactively");
476 if (bkpinfo->differential) {
477 log_to_screen("Because this is a differential backup, disk");
478 log_to_screen
479 (" partitioning and formatting will not take place.");
480 done = TRUE;
481 } else {
482 if (ask_me_yes_or_no
483 ("Do you want to erase and partition your hard drives?")) {
484 if (partition_table_contains_Compaq_diagnostic_partition
485 (mountlist)) {
486 offer_to_abort_because_Compaq_Proliants_suck();
487 done = TRUE;
488 } else {
489 twenty_seconds_til_yikes();
490 g_fprep = fopen("/tmp/prep.sh", "w");
491 ptn_errs = partition_everything(mountlist);
492 if (ptn_errs) {
493 log_to_screen
494 ("Warning. Errors occurred during disk partitioning.");
495 }
496
497 fmt_errs = format_everything(mountlist, FALSE, raidlist);
498 if (!fmt_errs) {
499 log_to_screen
500 ("Errors during disk partitioning were handled OK.");
501 log_to_screen
502 ("Partitions were formatted OK despite those errors.");
503 ptn_errs = 0;
504 }
505 if (!ptn_errs && !fmt_errs) {
506 done = TRUE;
507 }
508 }
509 paranoid_fclose(g_fprep);
510 } else {
511 mvaddstr_and_log_it(g_currentY++, 0,
512 "User opted not to partition the devices");
513 if (ask_me_yes_or_no
514 ("Do you want to format your hard drives?")) {
515 fmt_errs = format_everything(mountlist, TRUE, raidlist);
516 if (!fmt_errs) {
517 done = TRUE;
518 }
519 } else {
520 ptn_errs = fmt_errs = 0;
521 done = TRUE;
522 }
523 }
524 if (fmt_errs) {
525 mvaddstr_and_log_it(g_currentY++,
526 0,
527 "Errors occurred. Please repartition and format drives manually.");
528 done = FALSE;
529 }
530 if (ptn_errs & !fmt_errs) {
531 mvaddstr_and_log_it(g_currentY++,
532 0,
533 "Errors occurred during partitioning. Formatting, however, went OK.");
534 done = TRUE;
535 }
536 if (!done) {
537 if (!ask_me_yes_or_no("Re-edit the mountlist?")) {
538 retval++;
539 goto end_of_func;
540 }
541 }
542 }
543 }
544
545 /* mount */
546 if (mount_all_devices(mountlist, TRUE)) {
547 unmount_all_devices(mountlist);
548 retval++;
549 goto end_of_func;
550 }
551 /* restore */
552 if ((restore_all = ask_me_yes_or_no("Do you want me to restore all of your data?"))) {
553 log_msg(1, "Restoring all data");
554 retval += restore_everything(NULL);
555 } else if ((restore_all = ask_me_yes_or_no("Do you want me to restore _some_ of your data?"))) {
556 mr_asprintf(old_restpath, "%s", bkpinfo->restore_path);
557 for (done = FALSE; !done;) {
558 unlink("/tmp/filelist.full");
559 filelist = process_filelist_and_biggielist();
560 /* Now you have /tmp/tmpfs/filelist.restore-these and /tmp/tmpfs/biggielist.restore-these;
561 the former is a list of regular files; the latter, biggiefiles and imagedevs.
562 */
563 if (filelist) {
564 gotos_suck:
565 strcpy(tmp, old_restpath);
566 // (NB: %s is where your filesystem is mounted now, by default)", MNT_RESTORING);
567 p = popup_and_get_string("Restore path", "Restore files to where?", tmp);
568 if (p != NULL) {
569 strcpy(tmp, p);
570 if (!strcmp(tmp, "/")) {
571 if (!ask_me_yes_or_no("Are you sure?")) {
572 goto gotos_suck;
573 }
574 tmp[0] = '\0'; // so we restore to [blank]/file/name :)
575 }
576 mr_free(bkpinfo->restore_path);
577 mr_asprintf(bkpinfo->restore_path, "%s", tmp);
578 log_msg(1, "Restoring subset");
579 retval += restore_everything(filelist);
580 } else {
581 mr_free(bkpinfo->restore_path);
582 mr_asprintf(bkpinfo->restore_path, "%s", old_restpath);
583 }
584 free_filelist(filelist);
585 mr_free(p);
586
587 if (!ask_me_yes_or_no("Restore another subset of your backup?")) {
588 done = TRUE;
589 }
590 } else {
591 done = TRUE;
592 }
593 }
594 mr_free(old_restpath);
595 } else {
596 mvaddstr_and_log_it(g_currentY++, 0, "User opted not to restore any data. ");
597 }
598 if (retval) {
599 mvaddstr_and_log_it(g_currentY++, 0, "Errors occurred during the restore phase. ");
600 }
601
602 clean_multipathconf();
603 if (ask_me_yes_or_no("Initialize the boot loader?")) {
604 run_boot_loader(TRUE);
605 } else {
606 mvaddstr_and_log_it(g_currentY++,
607 0,
608 "User opted not to initialize the boot loader.");
609 }
610
611 clean_blkid();
612 protect_against_braindead_sysadmins();
613 retval += unmount_all_devices(mountlist);
614
615 /* if (restore_some || restore_all || */
616 if (ask_me_yes_or_no
617 ("Label/Identify your ext2/ext3/ext4 partitions if necessary?")) {
618 mvaddstr_and_log_it(g_currentY, 0,
619 "Using tune2fs to identify your ext2,3,4 partitions");
620 if (does_file_exist("/tmp/fstab.new")) {
621 mr_asprintf(fstab_fname, "/tmp/fstab.new");
622 } else {
623 mr_asprintf(fstab_fname, "/tmp/fstab");
624 }
625 mr_asprintf(tmp1, "label-partitions-as-necessary %s < %s >> %s 2>> %s", MINDI_CACHE"/mountlist.txt", fstab_fname, MONDO_LOGFILE, MONDO_LOGFILE);
626 mr_free(fstab_fname);
627
628 res = system(tmp1);
629 mr_free(tmp1);
630 if (res) {
631 log_to_screen
632 ("label-partitions-as-necessary returned an error");
633 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
634 } else {
635 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
636 }
637 retval += res;
638 }
639
640 log_it("About to leave interactive_mode()");
641 if (retval) {
642 mvaddstr_and_log_it(g_currentY++,
643 0,
644 "Warning - errors occurred during the restore phase.");
645 }
646 end_of_func:
647 paranoid_free(tmp);
648 log_it("Leaving interactive_mode()");
649 return (retval);
650}
651
652/**************************************************************************
653 *END_INTERACTIVE_MODE *
654 **************************************************************************/
655
656
657
658/**
659 * Run an arbitrary restore mode (prompt the user), but from ISO images
660 * instead of real media.
661 * @param mountlist The mountlist containing information about the user's partitions.
662 * @param raidlist The raidlist that goes with @p mountlist.
663 * @param nuke_me_please If TRUE, we plan to run Nuke Mode.
664 * @return 0 for success, or the number of errors encountered.
665 */
666int
667iso_mode(struct mountlist_itself *mountlist,
668 struct raidlist_itself *raidlist, bool nuke_me_please)
669{
670 char c;
671 int retval = 0;
672
673 assert(mountlist != NULL);
674 assert(raidlist != NULL);
675 if (iso_fiddly_bits(nuke_me_please)) {
676 log_msg(1, "iso_mode --- returning w/ error");
677 return (1);
678 } else {
679 c = which_restore_mode();
680 if (c == 'I' || c == 'N' || c == 'C') {
681 interactively_obtain_media_parameters_from_user(FALSE);
682 }
683 if (c == 'I') {
684 retval += interactive_mode(mountlist, raidlist);
685 } else if (c == 'N') {
686 retval += nuke_mode(mountlist, raidlist);
687 } else if (c == 'C') {
688 retval += compare_mode(mountlist, raidlist);
689 } else {
690 log_to_screen("OK, I shan't restore/compare any files.");
691 }
692 }
693 if (is_this_device_mounted(MNT_CDROM)) {
694 paranoid_system("umount " MNT_CDROM);
695 }
696// if (! already_mounted)
697// {
698 if (system("umount /tmp/isodir 2> /dev/null")) {
699 log_to_screen
700 ("WARNING - unable to unmount device where the ISO files are stored.");
701 }
702// }
703 return (retval);
704}
705
706/**************************************************************************
707 *END_ISO_MODE *
708 **************************************************************************/
709
710
711/* MONDO - saving your a$$ since Feb 18th, 2000 */
712
713
714
715
716/**
717 * Restore the user's data automatically (no prompts), after a twenty-second
718 * warning period.
719 * @param bkpinfo The backup information structure. Most fields are used.
720 * @param mountlist The mountlist containing information about the user's partitions.
721 * @param raidlist The raidlist that goes with @p mountlist.
722 * @return 0 for success, or the number of errors encountered.
723 * @warning <b><i>THIS WILL ERASE ALL EXISTING DATA!</i></b>
724 */
725int
726nuke_mode(struct mountlist_itself *mountlist,
727 struct raidlist_itself *raidlist)
728{
729 int retval = 0;
730 int res = 0;
731 bool boot_loader_installed = FALSE;
732 char *tmp = NULL;
733 char *tmp1 = NULL;
734 char *flaws_str = NULL;
735
736 assert(bkpinfo != NULL);
737 assert(mountlist != NULL);
738 assert(raidlist != NULL);
739
740 log_msg(2, "nuke_mode --- starting");
741 load_mountlist(mountlist, MINDI_CACHE"/mountlist.txt"); // in case read_cfg_file_into_bkpinfo updated the mountlist
742
743 tmp = call_program_and_get_last_line_of_output("cat " CMDLINE);
744 if (strstr(tmp, "noresize")) {
745 log_msg(2, "Not resizing mountlist.");
746 } else {
747 resize_mountlist_proportionately_to_suit_new_drives(mountlist);
748 }
749 mr_free(tmp);
750
751 flaws_str = evaluate_mountlist(mountlist);
752 if (flaws_str != NULL) {
753 mr_asprintf(tmp, "Mountlist analyzed. Result: \"%s\" Switch to Interactive Mode?", flaws_str);
754 mr_free(flaws_str);
755 res = ask_me_yes_or_no(tmp);
756 mr_free(tmp);
757 if (res) {
758 retval = interactive_mode(mountlist, raidlist);
759 goto after_the_nuke;
760 } else {
761 fatal_error("Nuke Mode aborted. ");
762 }
763 }
764
765 save_mountlist_to_disk(mountlist, MINDI_CACHE"/mountlist.txt");
766 mvaddstr_and_log_it(1, 30, "Restoring Automatically");
767 if (bkpinfo->differential) {
768 log_to_screen("Because this is a differential backup, disk");
769 log_to_screen("partitioning and formatting will not take place.");
770 res = 0;
771 } else {
772 if (partition_table_contains_Compaq_diagnostic_partition
773 (mountlist)) {
774 offer_to_abort_because_Compaq_Proliants_suck();
775 } else {
776 twenty_seconds_til_yikes();
777 g_fprep = fopen("/tmp/prep.sh", "w");
778 tmp = call_program_and_get_last_line_of_output("cat " CMDLINE);
779 if (strstr(tmp, "nopart")) {
780 log_msg(2, "Not partitioning drives due to 'nopart' option.");
781 res = 0;
782 } else {
783 res = partition_everything(mountlist);
784 if (res) {
785 log_to_screen("Warning. Errors occurred during partitioning.");
786 res = 0;
787 }
788 }
789 mr_free(tmp);
790
791 retval += res;
792 if (!res) {
793 log_to_screen("Preparing to format your disk(s)");
794 sleep(1);
795 sync();
796 log_to_screen("Please wait. This may take a few minutes.");
797 res += format_everything(mountlist, FALSE, raidlist);
798 }
799 paranoid_fclose(g_fprep);
800 }
801 }
802 retval += res;
803 if (res) {
804 mvaddstr_and_log_it(g_currentY++,
805 0,
806 "Failed to partition and/or format your hard drives.");
807
808 if (ask_me_yes_or_no("Try in interactive mode instead?")) {
809 retval = interactive_mode(mountlist, raidlist);
810 goto after_the_nuke;
811 } else
812 if (!ask_me_yes_or_no
813 ("Would you like to try to proceed anyway?")) {
814 return (retval);
815 }
816 }
817 retval = mount_all_devices(mountlist, TRUE);
818 if (retval) {
819 unmount_all_devices(mountlist);
820 log_to_screen
821 ("Unable to mount all partitions. Sorry, I cannot proceed.");
822 return (retval);
823 }
824 log_it("Restoring everything");
825 retval += restore_everything(NULL);
826 clean_multipathconf();
827 if (!run_boot_loader(FALSE)) {
828 log_msg(1,
829 "Great! Boot loader was installed. No need for msg at end.");
830 boot_loader_installed = TRUE;
831 }
832 clean_blkid();
833 protect_against_braindead_sysadmins();
834 retval += unmount_all_devices(mountlist);
835 mvaddstr_and_log_it(g_currentY,
836 0,
837 "Using tune2fs to identify your ext2,3 partitions");
838
839 mr_asprintf(tmp, "label-partitions-as-necessary %s < /tmp/fstab", MINDI_CACHE"/mountlist.txt");
840 res = run_program_and_log_output(tmp, TRUE);
841 mr_free(tmp);
842 if (res) {
843 log_to_screen("label-partitions-as-necessary returned an error");
844 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
845 } else {
846 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
847 }
848 retval += res;
849
850 after_the_nuke:
851 tmp1 = call_program_and_get_last_line_of_output("cat " CMDLINE);
852 if (retval) {
853 log_to_screen("Errors occurred during the nuke phase.");
854 } else if (strstr(tmp1, "RESTORE")) {
855 log_to_screen("PC was restored successfully. Thank you for using Mondo Rescue.");
856 log_to_screen("Please visit our website at http://www.mondorescue.org for more information.");
857 } else {
858 mr_asprintf(tmp,"%s","Mondo has restored your system.\n\nPlease wait for the command prompt. Then remove the backup media and reboot.\n\nPlease visit our website at http://www.mondorescue.org for more information.");
859 popup_and_OK(tmp);
860 mr_free(tmp);
861 log_to_screen("Mondo has restored your system. Please wait for the command prompt.");
862 log_to_screen("Then remove the backup media and reboot.");
863 log_to_screen("Please visit our website at http://www.mondorescue.org for more information.");
864 }
865 mr_free(tmp1);
866
867 g_I_have_just_nuked = TRUE;
868 return (retval);
869}
870
871/**************************************************************************
872 *END_NUKE_MODE *
873 **************************************************************************/
874
875
876
877/**
878 * Restore the user's data (or a subset of it) to the live filesystem.
879 * This should not be called if we're booted from CD!
880 * @param bkpinfo The backup information structure. Most fields are used.
881 * @return 0 for success, or the number of errors encountered.
882 */
883int restore_to_live_filesystem()
884{
885 int retval = 0;
886
887 char *old_restpath = NULL;
888 char *p = NULL;
889
890 struct mountlist_itself *mountlist = NULL;
891 struct raidlist_itself *raidlist = NULL;
892 struct s_node *filelist = NULL;
893
894 log_msg(1, "restore_to_live_filesystem() - starting");
895 assert(bkpinfo != NULL);
896 mountlist = malloc(sizeof(struct mountlist_itself));
897 raidlist = malloc(sizeof(struct raidlist_itself));
898 if (!mountlist || !raidlist) {
899 fatal_error("Cannot malloc() mountlist and/or raidlist");
900 }
901
902 mr_free(bkpinfo->restore_path);
903 mr_asprintf(bkpinfo->restore_path, "/");
904 if (bkpinfo->backup_media_type != netfs) {
905 popup_and_OK
906 ("Please insert tape/CD/USB Key, then hit 'OK' to continue.");
907 sleep(1);
908 interactively_obtain_media_parameters_from_user(FALSE);
909 }
910 if (!bkpinfo->media_device) {
911 log_msg(2, "WARNING: failed to find media dev");
912 } else {
913 log_msg(2, "bkpinfo->media_device = %s", bkpinfo->media_device);
914 }
915
916
917 log_msg(2, "bkpinfo->isodir = %s", bkpinfo->isodir);
918
919 open_evalcall_form("Thinking...");
920 read_cfg_file_into_bkpinfo(MINDI_CACHE"/mondorestore.cfg");
921 load_mountlist(mountlist, MINDI_CACHE"/mountlist.txt"); // in case read_cfg_file_into_bkpinfo
922
923 close_evalcall_form();
924 retval = load_mountlist(mountlist, MINDI_CACHE"/mountlist.txt");
925 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
926
927 if ((bkpinfo->backup_media_type != netfs) && (filelist = process_filelist_and_biggielist())) {
928 save_filelist(filelist, "/tmp/selected-files.txt");
929 mr_asprintf(old_restpath, "%s", bkpinfo->restore_path);
930 p = popup_and_get_string("Restore path", "Restore files to where? )", bkpinfo->restore_path);
931 if (p != NULL) {
932 mr_free(bkpinfo->restore_path);
933 bkpinfo->restore_path = p;
934
935 log_it("Restoring everything");
936 retval += restore_everything(filelist);
937 }
938 free_filelist(filelist);
939 mr_free(bkpinfo->restore_path);
940 bkpinfo->restore_path = old_restpath;
941 } else {
942 if (filelist != NULL) {
943 retval += restore_everything(NULL);
944 }
945 }
946 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
947 log_msg(2,
948 "Tape : I don't need to unmount or eject the CD-ROM.");
949 } else {
950 run_program_and_log_output("umount " MNT_CDROM, FALSE);
951 if (!bkpinfo->please_dont_eject) {
952 eject_device(bkpinfo->media_device);
953 }
954 }
955 run_program_and_log_output("umount " MNT_CDROM, FALSE);
956 if (!bkpinfo->please_dont_eject) {
957 eject_device(bkpinfo->media_device);
958 }
959 free(mountlist);
960 free(raidlist);
961 return (retval);
962}
963
964/**************************************************************************
965 *END_RESTORE_TO_LIVE_FILESYSTEM *
966 **************************************************************************/
967
968/* @} - end of restoreGroup */
969
970
971#include <utime.h>
972/**
973 * @addtogroup LLrestoreGroup
974 * @{
975 */
976/**
977 * Restore biggiefile @p bigfileno from the currently mounted CD.
978 * @param bkpinfo The backup information structure. Fields used:
979 * - @c bkpinfo->backup_media_type
980 * - @c bkpinfo->restore_path
981 * @param bigfileno The biggiefile number (starting from 0) to restore.
982 * @param filelist The node structure containing the list of files to restore.
983 * If the biggiefile is not in this list, it will be skipped (return value will
984 * still indicate success).
985 * @return 0 for success (or skip), nonzero for failure.
986 */
987int
988restore_a_biggiefile_from_CD(long bigfileno,
989 struct s_node *filelist,
990 char *pathname_of_last_file_restored)
991{
992 FILE *fin;
993 FILE *fout;
994 FILE *fbzip2;
995
996 /** malloc ***/
997 char *checksum = NULL;
998 char *outfile_fname = NULL;
999 char *tmp = NULL;
1000 char *bzip2_command = NULL;
1001 char *suffix = NULL;
1002 char *sz_devfile = NULL;
1003 char *bigblk;
1004 char *mds = NULL;
1005 int retval = 0;
1006 int finished = FALSE;
1007 long sliceno;
1008 long siz;
1009 char *ntfsprog_fifo = NULL;
1010 char *file_to_openout = NULL;
1011 struct s_filename_and_lstat_info biggiestruct;
1012 struct utimbuf the_utime_buf, *ubuf;
1013 bool use_ntfsprog_hack = FALSE;
1014 pid_t pid;
1015 int res = 0;
1016 int old_loglevel;
1017 struct s_node *node;
1018
1019 old_loglevel = g_loglevel;
1020 ubuf = &the_utime_buf;
1021 assert(bkpinfo != NULL);
1022
1023 pathname_of_last_file_restored[0] = '\0';
1024 if (!(bigblk = malloc(TAPE_BLOCK_SIZE))) {
1025 fatal_error("Cannot malloc bigblk");
1026 }
1027
1028 if (!(fin = fopen(slice_fname(bigfileno, 0, ARCHIVES_PATH, ""), "r"))) {
1029 log_to_screen("Cannot even open bigfile's info file");
1030 return (1);
1031 }
1032
1033 memset((void *) &biggiestruct, 0, sizeof(biggiestruct));
1034 if (fread((void *) &biggiestruct, 1, sizeof(biggiestruct), fin) <
1035 sizeof(biggiestruct)) {
1036 log_msg(2, "Warning - unable to get biggiestruct of bigfile #%d",
1037 bigfileno + 1);
1038 }
1039 paranoid_fclose(fin);
1040
1041 mr_asprintf(checksum, "%s", biggiestruct.checksum);
1042 if (!checksum[0]) {
1043 log_msg(3, "Warning - bigfile %ld does not have a checksum", bigfileno + 1);
1044 }
1045 mr_free(checksum);
1046
1047 if (!strncmp(biggiestruct.filename, "/dev/", 5)) // Whether NTFS or not :)
1048 {
1049 mr_asprintf(outfile_fname, "%s", biggiestruct.filename);
1050 } else {
1051 mr_asprintf(outfile_fname, "%s/%s", bkpinfo->restore_path, biggiestruct.filename);
1052 }
1053
1054 /* skip file if we have a selective restore subset & it doesn't match */
1055 if (filelist != NULL) {
1056 node = find_string_at_node(filelist, biggiestruct.filename);
1057 if (!node) {
1058 log_msg(0, "Skipping %s (name isn't in filelist)", biggiestruct.filename);
1059 pathname_of_last_file_restored[0] = '\0';
1060 return (0);
1061 } else if (!(node->selected)) {
1062 log_msg(1, "Skipping %s (name isn't in biggielist subset)", biggiestruct.filename);
1063 pathname_of_last_file_restored[0] = '\0';
1064 return (0);
1065 }
1066 }
1067
1068 /* otherwise, continue */
1069 log_msg(1, "DEFINITELY restoring %s", biggiestruct.filename);
1070 if (biggiestruct.use_ntfsprog) {
1071 if (strncmp(biggiestruct.filename, "/dev/", 5)) {
1072 log_msg(1, "I was in error when I set biggiestruct.use_ntfsprog to TRUE.");
1073 log_msg(1, "%s isn't even in /dev", biggiestruct.filename);
1074 biggiestruct.use_ntfsprog = FALSE;
1075 }
1076 }
1077
1078 if (biggiestruct.use_ntfsprog) // if it's an NTFS device
1079 {
1080 g_loglevel = 4;
1081 use_ntfsprog_hack = TRUE;
1082 log_msg(2, "Calling ntfsclone in background because %s is an NTFS /dev entry", outfile_fname);
1083 mr_asprintf(sz_devfile, "/tmp/%d.%d.000", (int) (random() % 32768), (int) (random() % 32768));
1084 mkfifo(sz_devfile, 0x770);
1085 mr_asprintf(ntfsprog_fifo, "%s", sz_devfile);
1086 mr_free(sz_devfile);
1087 file_to_openout = ntfsprog_fifo;
1088 switch (pid = fork()) {
1089 case -1:
1090 fatal_error("Fork failure");
1091 case 0:
1092 log_msg(3, "CHILD - fip - calling feed_outfrom_ntfsprog(%s, %s)", biggiestruct.filename, ntfsprog_fifo);
1093 res = feed_outfrom_ntfsprog(biggiestruct.filename, ntfsprog_fifo);
1094 mr_free(ntfsprog_fifo);
1095 exit(res);
1096 break;
1097 default:
1098 log_msg(3, "feed_into_ntfsprog() called in background --- pid=%ld", (long int) (pid));
1099 }
1100 mr_free(ntfsprog_fifo);
1101 } else {
1102 use_ntfsprog_hack = FALSE;
1103 file_to_openout = outfile_fname;
1104 if (!does_file_exist(outfile_fname)) // yes, it looks weird with the '!' but it's correct that way
1105 {
1106 make_hole_for_file(outfile_fname);
1107 }
1108 }
1109
1110 log_msg(2, "Reassembling big file %ld (%s)", bigfileno + 1, outfile_fname);
1111
1112 /*
1113 last slice is zero-length and uncompressed; when we find it, we stop.
1114 We DON'T wait until there are no more slices; if we did that,
1115 We might stop at end of CD, not at last slice (which is 0-len and uncompd)
1116 */
1117
1118 strncpy(pathname_of_last_file_restored, biggiestruct.filename, MAX_STR_LEN - 1);
1119 pathname_of_last_file_restored[MAX_STR_LEN - 1] = '\0';
1120
1121 log_msg(3, "file_to_openout = %s", file_to_openout);
1122 if (!(fout = fopen(file_to_openout, "w"))) {
1123 log_to_screen("Cannot openout outfile_fname - hard disk full?");
1124 return (1);
1125 }
1126 log_msg(3, "Opened out to %s", outfile_fname); // CD/DVD --> mondorestore --> ntfsclone --> hard disk itself
1127
1128 for (sliceno = 1, finished = FALSE; !finished;) {
1129 if (!does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "")) &&
1130 !does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "lzo")) &&
1131 !does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "gz")) &&
1132 !does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "lzma")) &&
1133 !does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "bz2"))) {
1134 log_msg(3, "Cannot find a data slice or terminator slice on CD %d", g_current_media_number);
1135 g_current_media_number++;
1136 mds = media_descriptor_string(bkpinfo->backup_media_type);
1137 log_msg(2, "Asking for %s #%d so that I may read slice #%ld\n", mds, g_current_media_number, sliceno);
1138 mr_free(mds);
1139
1140 log_to_screen("Restoring from %s #%d", mds, g_current_media_number);
1141
1142 insist_on_this_cd_number(g_current_media_number);
1143 log_to_screen("Continuing to restore.");
1144 } else {
1145 mr_asprintf(tmp, "%s", slice_fname(bigfileno, sliceno, ARCHIVES_PATH, ""));
1146 if (does_file_exist(tmp) && length_of_file(tmp) == 0) {
1147 log_msg(2, "End of bigfile # %ld (slice %ld is the terminator)", bigfileno + 1, sliceno);
1148 finished = TRUE;
1149 mr_free(tmp);
1150 continue;
1151 } else {
1152 if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "lzo"))) {
1153 mr_asprintf(bzip2_command, "lzop");
1154 mr_asprintf(suffix, "lzo");
1155 } else
1156 if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "gz"))) {
1157 mr_asprintf(bzip2_command, "gzip");
1158 mr_asprintf(suffix, "gz");
1159 } else
1160 if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "lzma"))) {
1161 mr_asprintf(bzip2_command, "lzma");
1162 mr_asprintf(suffix, "lzma");
1163 } else
1164 if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "bz2"))) {
1165 mr_asprintf(bzip2_command, "bzip2");
1166 mr_asprintf(suffix, "bz2");
1167 } else
1168 if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, ""))) {
1169 mr_asprintf(bzip2_command, "");
1170 mr_asprintf(suffix, "");
1171 } else {
1172 log_to_screen("OK, that's pretty fsck0red...");
1173 mr_free(tmp);
1174 return (1);
1175 }
1176 }
1177 mr_free(tmp);
1178 if (bzip2_command != NULL) {
1179 mr_strcat(bzip2_command, " -dc %s 2>> %s", slice_fname(bigfileno, sliceno, ARCHIVES_PATH, suffix), MONDO_LOGFILE);
1180 } else {
1181 mr_asprintf(bzip2_command, "cat %s 2>> %s", slice_fname(bigfileno, sliceno, ARCHIVES_PATH, suffix), MONDO_LOGFILE);
1182 }
1183 mr_free(suffix);
1184
1185 mds = media_descriptor_string(bkpinfo->backup_media_type);
1186 mr_asprintf(tmp, "Working on %s #%d, file #%ld, slice #%ld ", mds, g_current_media_number, bigfileno + 1, sliceno);
1187 mr_free(mds);
1188 log_msg(2, tmp);
1189
1190 if (!g_text_mode) {
1191 newtDrawRootText(0, g_noof_rows - 2, tmp);
1192 newtRefresh();
1193 strip_spaces(tmp);
1194 update_progress_form(tmp);
1195 }
1196 mr_free(tmp);
1197
1198 if (!(fbzip2 = popen(bzip2_command, "r"))) {
1199 fatal_error("Can't run popen command");
1200 }
1201 mr_free(bzip2_command);
1202
1203 while (!feof(fbzip2)) {
1204 siz = fread(bigblk, 1, TAPE_BLOCK_SIZE, fbzip2);
1205 if (siz > 0) {
1206 siz = fwrite(bigblk, 1, siz, fout);
1207 }
1208 }
1209 paranoid_pclose(fbzip2);
1210
1211
1212 sliceno++;
1213 g_current_progress++;
1214 }
1215 }
1216 paranoid_fclose(fout);
1217 g_loglevel = old_loglevel;
1218
1219 if (use_ntfsprog_hack) {
1220 log_msg(3, "Waiting for ntfsclone to finish");
1221 mr_asprintf(tmp, " ps | grep \" ntfsclone \" | grep -v grep > /dev/null 2> /dev/null");
1222 while (system(tmp) == 0) {
1223 sleep(1);
1224 }
1225 mr_free(tmp);
1226 log_it("OK, ntfsclone has really finished");
1227 }
1228
1229 if (strcmp(outfile_fname, "/dev/null")) {
1230 chown(outfile_fname, biggiestruct.properties.st_uid, biggiestruct.properties.st_gid);
1231 chmod(outfile_fname, biggiestruct.properties.st_mode);
1232 ubuf->actime = biggiestruct.properties.st_atime;
1233 ubuf->modtime = biggiestruct.properties.st_mtime;
1234 utime(outfile_fname, ubuf);
1235 }
1236 mr_free(outfile_fname);
1237 paranoid_free(bigblk);
1238
1239 return (retval);
1240}
1241
1242/**************************************************************************
1243 *END_ RESTORE_A_BIGGIEFILE_FROM_CD *
1244 **************************************************************************/
1245
1246
1247
1248/**
1249 * Restore a biggiefile from the currently opened stream.
1250 * @param bkpinfo The backup information structure. Fields used:
1251 * - @c bkpinfo->restore_path
1252 * - @c bkpinfo->zip_exe
1253 * @param orig_bf_fname The original filename of the biggiefile.
1254 * @param biggiefile_number The number of the biggiefile (starting from 0).
1255 * @param orig_checksum Unused.
1256 * @param biggiefile_size Unused.
1257 * @param filelist The node structure containing the list of files to be restored.
1258 * If @p orig_bf_fname is not in the list, it will be ignored.
1259 * @return 0 for success (or skip), nonzero for failure.
1260 * @bug orig_checksum and biggiefile_size are unused (except to check that they are non-NULL).
1261 */
1262int restore_a_biggiefile_from_stream(char *orig_bf_fname, long biggiefile_number, char *orig_checksum, //UNUSED
1263 long long biggiefile_size, //UNUSED
1264 struct s_node *filelist,
1265 int use_ntfsprog,
1266 char *pathname_of_last_file_restored)
1267{
1268 FILE *pout;
1269 FILE *fin;
1270
1271 /** mallocs ********/
1272 char *tmp = NULL;
1273 char *tmp1 = NULL;
1274 char *command = NULL;
1275 char *outfile_fname = NULL;
1276 char *sz_devfile = NULL;
1277 char *ntfsprog_fifo = NULL;
1278 char *file_to_openout = NULL;
1279
1280 struct s_node *node;
1281
1282 int old_loglevel;
1283 long current_slice_number = 0;
1284 int retval = 0;
1285 int res = 0;
1286 int ctrl_chr = '\0';
1287 long long slice_siz;
1288 bool dummy_restore = FALSE;
1289 bool use_ntfsprog_hack = FALSE;
1290 pid_t pid;
1291 struct s_filename_and_lstat_info biggiestruct;
1292 struct utimbuf the_utime_buf, *ubuf;
1293 ubuf = &the_utime_buf;
1294
1295 malloc_string(tmp);
1296 old_loglevel = g_loglevel;
1297 assert(bkpinfo != NULL);
1298 assert(orig_bf_fname != NULL);
1299 assert(orig_checksum != NULL);
1300
1301 pathname_of_last_file_restored[0] = '\0';
1302 if (use_ntfsprog == BLK_START_A_PIHBIGGIE) {
1303 use_ntfsprog = 1;
1304 log_msg(1, "%s --- pih=YES", orig_bf_fname);
1305 } else if (use_ntfsprog == BLK_START_A_NORMBIGGIE) {
1306 use_ntfsprog = 0;
1307 log_msg(1, "%s --- pih=NO", orig_bf_fname);
1308 } else {
1309 use_ntfsprog = 0;
1310 log_msg(1, "%s --- pih=NO (weird marker though)", orig_bf_fname);
1311 }
1312
1313 strncpy(pathname_of_last_file_restored, orig_bf_fname,
1314 MAX_STR_LEN - 1);
1315 pathname_of_last_file_restored[MAX_STR_LEN - 1] = '\0';
1316
1317 /* open out to biggiefile to be restored (or /dev/null if biggiefile is not to be restored) */
1318
1319 if (filelist != NULL) {
1320 node = find_string_at_node(filelist, orig_bf_fname);
1321 if (!node) {
1322 dummy_restore = TRUE;
1323 log_msg(1,
1324 "Skipping big file %ld (%s) - not in biggielist subset",
1325 biggiefile_number + 1, orig_bf_fname);
1326 pathname_of_last_file_restored[0] = '\0';
1327 } else if (!(node->selected)) {
1328 dummy_restore = TRUE;
1329 log_msg(1, "Skipping %s (name isn't in biggielist subset)",
1330 orig_bf_fname);
1331 pathname_of_last_file_restored[0] = '\0';
1332 }
1333 }
1334
1335 if (use_ntfsprog) {
1336 if (strncmp(orig_bf_fname, "/dev/", 5)) {
1337 log_msg(1,
1338 "I was in error when I set use_ntfsprog to TRUE.");
1339 log_msg(1, "%s isn't even in /dev", orig_bf_fname);
1340 use_ntfsprog = FALSE;
1341 }
1342 }
1343
1344 if (use_ntfsprog) {
1345 g_loglevel = 4;
1346 mr_asprintf(outfile_fname, "%s", orig_bf_fname);
1347 use_ntfsprog_hack = TRUE;
1348 log_msg(2, "Calling ntfsclone in background because %s is a /dev entry", outfile_fname);
1349 mr_asprintf(sz_devfile, "%s/%d.%d.000", bkpinfo->tmpdir, (int) (random() % 32768), (int) (random() % 32768));
1350 mkfifo(sz_devfile, 0x770);
1351 mr_asprintf(ntfsprog_fifo, "%s", sz_devfile);
1352 mr_free(sz_devfile);
1353
1354 file_to_openout = ntfsprog_fifo;
1355 switch (pid = fork()) {
1356 case -1:
1357 fatal_error("Fork failure");
1358 case 0:
1359 log_msg(3, "CHILD - fip - calling feed_outfrom_ntfsprog(%s, %s)", outfile_fname, ntfsprog_fifo);
1360 res = feed_outfrom_ntfsprog(outfile_fname, ntfsprog_fifo);
1361 mr_free(ntfsprog_fifo);
1362 exit(res);
1363 break;
1364 default:
1365 log_msg(3, "feed_into_ntfsprog() called in background --- pid=%ld", (long int) (pid));
1366 }
1367 mr_free(ntfsprog_fifo);
1368 } else {
1369 if (!strncmp(orig_bf_fname, "/dev/", 5)) {
1370 // non-NTFS partition
1371 mr_asprintf(outfile_fname, "%s", orig_bf_fname);
1372 } else {
1373 // biggiefile
1374 mr_asprintf(outfile_fname, "%s/%s", bkpinfo->restore_path, orig_bf_fname);
1375 }
1376 use_ntfsprog_hack = FALSE;
1377 file_to_openout = outfile_fname;
1378 if (!does_file_exist(outfile_fname)) // yes, it looks weird with the '!' but it's correct that way
1379 {
1380 make_hole_for_file(outfile_fname);
1381 }
1382 log_msg(2, "Reassembling big file %ld (%s)", biggiefile_number + 1, orig_bf_fname);
1383 }
1384
1385 if (dummy_restore) {
1386 mr_free(outfile_fname);
1387 mr_asprintf(outfile_fname, "/dev/null");
1388 }
1389
1390 if (bkpinfo->zip_exe == NULL) {
1391 mr_asprintf(command, "cat > \"%s\"", file_to_openout);
1392 } else {
1393 mr_asprintf(command, "%s -dc > \"%s\" 2>> %s", bkpinfo->zip_exe, file_to_openout, MONDO_LOGFILE);
1394 if (strcmp(bkpinfo->zip_exe, "gzip") == 0) {
1395 /* Ignore SIGPIPE for gzip as it causes errors on big files
1396 * Cf: http://trac.mondorescue.org/ticket/244 */
1397 signal(SIGPIPE,SIG_IGN);
1398 }
1399 }
1400 log_msg(3, "Pipe command = '%s'", command);
1401
1402 /* restore biggiefile, one slice at a time */
1403 if (!(pout = popen(command, "w"))) {
1404 fatal_error("Cannot pipe out");
1405 }
1406 mr_free(command);
1407
1408 for (res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr);
1409 ctrl_chr != BLK_STOP_A_BIGGIE;
1410 res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr)) {
1411 if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1412 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1413 }
1414 log_msg(2, "Working on file #%ld, slice #%ld ", biggiefile_number + 1, current_slice_number);
1415 if (!g_text_mode) {
1416 newtDrawRootText(0, g_noof_rows - 2, tmp);
1417 newtRefresh();
1418 }
1419 strip_spaces(tmp);
1420 update_progress_form(tmp);
1421 if (current_slice_number == 0) {
1422 res =
1423 read_file_from_stream_to_file("/tmp/biggie-blah.txt",
1424 slice_siz);
1425 if (!(fin = fopen("/tmp/biggie-blah.txt", "r"))) {
1426 log_OS_error("blah blah");
1427 } else {
1428 if (fread
1429 ((void *) &biggiestruct, 1, sizeof(biggiestruct),
1430 fin) < sizeof(biggiestruct)) {
1431 log_msg(2,
1432 "Warning - unable to get biggiestruct of bigfile #%d",
1433 biggiefile_number + 1);
1434 }
1435 paranoid_fclose(fin);
1436 }
1437 } else {
1438 res =
1439 read_file_from_stream_to_stream(pout, slice_siz);
1440 }
1441 retval += res;
1442 res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr);
1443 if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
1444 wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
1445 }
1446 current_slice_number++;
1447 g_current_progress++;
1448 }
1449 paranoid_pclose(pout);
1450
1451 if (bkpinfo->zip_exe != NULL) {
1452 if (strcmp(bkpinfo->zip_exe, "gzip") == 0) {
1453 /* Re-enable SIGPIPE for gzip */
1454 signal(SIGPIPE, terminate_daemon);
1455 }
1456 }
1457
1458 log_msg(1, "pathname_of_last_file_restored is now %s",
1459 pathname_of_last_file_restored);
1460
1461 if (use_ntfsprog_hack) {
1462 log_msg(3, "Waiting for ntfsclone to finish");
1463 mr_asprintf(tmp1, " ps | grep \" ntfsclone \" | grep -v grep > /dev/null 2> /dev/null");
1464 while (system(tmp1) == 0) {
1465 sleep(1);
1466 }
1467 mr_free(tmp1);
1468 log_msg(3, "OK, ntfsclone has really finished");
1469 }
1470
1471 log_msg(3, "biggiestruct.filename = %s", biggiestruct.filename);
1472 log_msg(3, "biggiestruct.checksum = %s", biggiestruct.checksum);
1473 if (strcmp(outfile_fname, "/dev/null")) {
1474 chmod(outfile_fname, biggiestruct.properties.st_mode);
1475 chown(outfile_fname, biggiestruct.properties.st_uid, biggiestruct.properties.st_gid);
1476 ubuf->actime = biggiestruct.properties.st_atime;
1477 ubuf->modtime = biggiestruct.properties.st_mtime;
1478 utime(outfile_fname, ubuf);
1479 }
1480 mr_free(outfile_fname);
1481
1482 paranoid_free(tmp);
1483 g_loglevel = old_loglevel;
1484 return (retval);
1485}
1486
1487/**************************************************************************
1488 *END_RESTORE_A_BIGGIEFILE_FROM_STREAM *
1489 **************************************************************************/
1490
1491
1492
1493/**
1494 * Restore @p tarball_fname from CD.
1495 * @param tarball_fname The filename of the tarball to restore (in /mnt/cdrom).
1496 * This will be used unmodified.
1497 * @param current_tarball_number The number (starting from 0) of the fileset
1498 * we're restoring now.
1499 * @param filelist The node structure containing the list of files to be
1500 * restored. If no file in the afioball is in this list, afio will still be
1501 * called, but nothing will be written.
1502 * @return 0 for success, nonzero for failure.
1503 */
1504int
1505restore_a_tarball_from_CD(char *tarball_fname,
1506 long current_tarball_number,
1507 struct s_node *filelist)
1508{
1509 int retval = 0;
1510 int res;
1511 char *p;
1512
1513 /** malloc **/
1514 char *command = NULL;
1515 char *tmp = NULL;
1516 char *filelist_name = NULL;
1517 char *filelist_subset_fname = NULL;
1518 char *executable = NULL;
1519 char *temp_log = NULL;
1520 long matches = 0;
1521 bool use_star;
1522 char *xattr_fname = NULL;
1523 char *acl_fname = NULL;
1524
1525 assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
1526
1527 log_msg(5, "Entering");
1528 use_star = (strstr(tarball_fname, ".star")) ? TRUE : FALSE;
1529 mr_asprintf(command, "mkdir -p %s/tmp", MNT_RESTORING);
1530 run_program_and_log_output(command, 9);
1531 paranoid_free(command);
1532
1533 mr_asprintf(filelist_name, MNT_CDROM "/archives/filelist.%ld", current_tarball_number);
1534 if (length_of_file(filelist_name) <= 2) {
1535 log_msg(2, "There are _zero_ files in filelist '%s'", filelist_name);
1536 log_msg(2, "This is a bit silly (ask dev-team to fix mondo_makefilelist, please)");
1537 log_msg(2, "but it's non-critical. It's cosmetic. Don't worry about it.");
1538 retval = 0;
1539 mr_free(filelist_name);
1540 log_msg(5, "Leaving");
1541 return(0);
1542 }
1543 if (count_lines_in_file(filelist_name) <= 0 || length_of_file(tarball_fname) <= 0) {
1544 log_msg(3, "length_of_file(%s) = %llu", tarball_fname, length_of_file(tarball_fname));
1545 log_msg(3, "count_lines_in_file(%s) = %llu", tarball_fname, count_lines_in_file(tarball_fname));
1546 log_to_screen("Unable to restore fileset #%ld (CD I/O error)", current_tarball_number);
1547 mr_free(filelist_name);
1548 log_msg(5, "Leaving");
1549 return(1);
1550 }
1551
1552 if (filelist) {
1553 mr_asprintf(filelist_subset_fname, "/tmp/filelist-subset-%ld.tmp", current_tarball_number);
1554 if ((matches =
1555 save_filelist_entries_in_common(filelist_name, filelist,
1556 filelist_subset_fname,
1557 use_star)) <= 0) {
1558 log_msg(1, "Skipping fileset %ld", current_tarball_number);
1559 } else {
1560 log_msg(3, "Saved fileset %ld's subset to %s", current_tarball_number, filelist_subset_fname);
1561 }
1562 log_to_screen("Tarball #%ld --- %ld matches", current_tarball_number, matches);
1563 }
1564 mr_free(filelist_name);
1565
1566 if (filelist == NULL || matches > 0) {
1567 if (g_getfattr) {
1568 mr_asprintf(xattr_fname, XATTR_LIST_FNAME_RAW_SZ, MNT_CDROM "/archives", current_tarball_number);
1569 }
1570 if (g_getfacl) {
1571 mr_asprintf(acl_fname, ACL_LIST_FNAME_RAW_SZ, MNT_CDROM "/archives", current_tarball_number);
1572 }
1573 if (strstr(tarball_fname, ".bz2")) {
1574 mr_asprintf(executable, "bzip2");
1575 } else if (strstr(tarball_fname, ".lzma")) {
1576 mr_asprintf(executable, "lzma");
1577 } else if (strstr(tarball_fname, ".gz")) {
1578 mr_asprintf(executable, "gzip");
1579 } else if (strstr(tarball_fname, ".lzo")) {
1580 mr_asprintf(executable, "lzop");
1581 }
1582 if (executable) {
1583 mr_asprintf(tmp, "which %s > /dev/null 2> /dev/null", executable);
1584 res = run_program_and_log_output(tmp, FALSE);
1585 mr_free(tmp);
1586
1587 if (res) {
1588 log_to_screen("(compare_a_tarball) Compression program %s not found - oh no!", executable);
1589 paranoid_MR_finish(1);
1590 }
1591 tmp = executable;
1592 mr_asprintf(executable, "-P %s -Z", tmp);
1593 mr_free(tmp);
1594 }
1595#ifdef __FreeBSD__
1596#define BUFSIZE 512
1597#else
1598#define BUFSIZE (1024L*1024L)/TAPE_BLOCK_SIZE
1599#endif
1600
1601 if (use_star) {
1602 mr_asprintf(command, "star -x -force-remove -U " STAR_ACL_SZ " errctl= file=%s", tarball_fname);
1603 if (strstr(tarball_fname, ".bz2")) {
1604 mr_strcat(command, " -bz");
1605 }
1606 } else {
1607 if (! executable) {
1608 log_msg(2, "Mo executable, this shouldn't happen !");
1609 } else {
1610 if (filelist_subset_fname != NULL) {
1611 mr_asprintf(command, "afio -i -M 8m -b %ld -c %ld %s -w '%s' %s", TAPE_BLOCK_SIZE, BUFSIZE, executable, filelist_subset_fname, tarball_fname);
1612 } else {
1613 mr_asprintf(command, "afio -i -b %ld -c %ld -M 8m %s %s", TAPE_BLOCK_SIZE, BUFSIZE, executable, tarball_fname);
1614 }
1615 }
1616 }
1617 mr_free(executable);
1618
1619#undef BUFSIZE
1620 mr_asprintf(temp_log, "/tmp/%d.%d", (int) (random() % 32768), (int) (random() % 32768));
1621
1622 mr_strcat(command, " 2>> %s >> %s", temp_log, temp_log);
1623 log_msg(1, "command = '%s'", command);
1624 unlink(temp_log);
1625 res = system(command);
1626 if (res) {
1627 p = strstr(command, "-acl ");
1628 if (p) {
1629 p[0] = p[1] = p[2] = p[3] = ' ';
1630 log_msg(1, "new command = '%s'", command);
1631 res = system(command);
1632 }
1633 }
1634 paranoid_free(command);
1635
1636 if (res && length_of_file(temp_log) < 5) {
1637 res = 0;
1638 }
1639
1640 if (g_getfattr) {
1641 log_msg(1, "Setting fattr list %s", xattr_fname);
1642 if (length_of_file(xattr_fname) > 0) {
1643 res = set_fattr_list(filelist_subset_fname, xattr_fname);
1644 if (res) {
1645 log_to_screen("Errors occurred while setting extended attributes");
1646 } else {
1647 log_msg(1, "I set xattr OK");
1648 }
1649 retval += res;
1650 }
1651 }
1652 if (g_getfacl) {
1653 log_msg(1, "Setting acl list %s", acl_fname);
1654 if (length_of_file(acl_fname) > 0) {
1655 res = set_acl_list(filelist_subset_fname, acl_fname);
1656 if (res) {
1657 log_to_screen
1658 ("Errors occurred while setting access control lists");
1659 } else {
1660 log_msg(1, "I set ACL OK");
1661 }
1662 retval += res;
1663 }
1664 }
1665 if (retval) {
1666 mr_asprintf(command, "cat %s >> %s", temp_log, MONDO_LOGFILE);
1667 system(command);
1668 paranoid_free(command);
1669
1670 log_msg(2, "Errors occurred while processing fileset #%d",
1671 current_tarball_number);
1672 } else {
1673 log_msg(2, "Fileset #%d processed OK", current_tarball_number);
1674 }
1675 unlink(temp_log);
1676 mr_free(temp_log);
1677 }
1678 if (does_file_exist("/PAUSE")) {
1679 popup_and_OK
1680 ("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
1681 }
1682 unlink(filelist_subset_fname);
1683 mr_free(filelist_subset_fname);
1684 if (g_getfattr) {
1685 unlink(xattr_fname);
1686 mr_free(xattr_fname);
1687 }
1688 if (g_getfacl) {
1689 unlink(acl_fname);
1690 mr_free(acl_fname);
1691 }
1692
1693 log_msg(5, "Leaving");
1694 return (retval);
1695}
1696
1697/**************************************************************************
1698 *END_RESTORE_A_TARBALL_FROM_CD *
1699 **************************************************************************/
1700
1701
1702/**
1703 * Restore a tarball from the currently opened stream.
1704 * @param bkpinfo The backup information structure. Fields used:
1705 * - @c bkpinfo->backup_media_type
1706 * - @c bkpinfo->media_device
1707 * - @c bkpinfo->zip_exe
1708 * @param tarball_fname The filename of the afioball to restore.
1709 * @param current_tarball_number The number (starting from 0) of the fileset
1710 * we're restoring now.
1711 * @param filelist The node structure containing the list of files to be
1712 * restored. If no file in the afioball is in this list, afio will still be
1713 * called, but nothing will be written.
1714 * @param size The size (in @b bytes) of the afioball.
1715 * @return 0 for success, nonzero for failure.
1716 */
1717int
1718restore_a_tarball_from_stream(char *tarball_fname,
1719 long current_tarball_number,
1720 struct s_node *filelist,
1721 long long size, char *xattr_fname,
1722 char *acl_fname)
1723{
1724 int retval = 0;
1725 int res = 0;
1726
1727 /** malloc add ***/
1728 char *mds = NULL;
1729 char *command = NULL;
1730 char *afio_fname = NULL;
1731 char *filelist_fname = NULL;
1732 char *filelist_subset_fname = NULL;
1733 char *executable = NULL;
1734 long matches = 0;
1735 bool restore_this_fileset = FALSE;
1736 bool use_star;
1737
1738 assert(bkpinfo != NULL);
1739 assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
1740
1741 /* to do it with a file... */
1742 use_star = (strstr(tarball_fname, ".star")) ? TRUE : FALSE;
1743 mds = media_descriptor_string(bkpinfo->backup_media_type);
1744 log_msg(2, "Restoring from fileset #%ld (%ld KB) on %s #%d",
1745 current_tarball_number, (long) size >> 10, mds, g_current_media_number);
1746 mr_free(mds);
1747
1748 run_program_and_log_output("mkdir -p " MNT_RESTORING "/tmp", FALSE);
1749
1750 /****************************************************************************
1751 * Use RAMDISK's /tmp; saves time; oh wait, it's too small *
1752 * Well, pipe from tape to afio, then; oh wait, can't do that either: bug *
1753 * in afio or someting; oh darn.. OK, use tmpfs :-) *
1754 ****************************************************************************/
1755 mr_asprintf(afio_fname, "/tmp/tmpfs/archive.tmp.%ld", current_tarball_number);
1756 mr_asprintf(filelist_fname, "%s/filelist.%ld", bkpinfo->tmpdir, current_tarball_number);
1757 mr_asprintf(filelist_subset_fname, "%s/filelist-subset-%ld.tmp", bkpinfo->tmpdir, current_tarball_number);
1758
1759 res = read_file_from_stream_to_file(afio_fname, size);
1760 if (strstr(tarball_fname, ".star")) {
1761 bkpinfo->use_star = TRUE;
1762 }
1763 if (res) {
1764 log_msg(1, "Warning - error reading afioball from tape");
1765 }
1766 if (bkpinfo->compression_level == 0) {
1767 mr_asprintf(executable, "%s", "");
1768 } else {
1769 if (bkpinfo->use_star) {
1770 mr_asprintf(executable, "%s", " -bz");
1771 } else {
1772 mr_asprintf(executable, "-P %s -Z", bkpinfo->zip_exe);
1773 }
1774 }
1775
1776 if (!filelist) // if unconditional restore then restore entire fileset
1777 {
1778 restore_this_fileset = TRUE;
1779 } else // If restoring selectively then get TOC from tarball
1780 {
1781 if (strstr(tarball_fname, ".star.")) {
1782 use_star = TRUE;
1783 mr_asprintf(command, "star -t file=%s %s", afio_fname, executable);
1784 } else {
1785 use_star = FALSE;
1786 mr_asprintf(command, "afio -t -M 8m -b %ld %s %s", TAPE_BLOCK_SIZE, executable, afio_fname);
1787 }
1788 mr_strcat(command, " > %s 2>> %s", filelist_fname, MONDO_LOGFILE);
1789 log_msg(1, "command = %s", command);
1790 if (system(command)) {
1791 log_msg(4, "Warning - error occurred while retrieving TOC");
1792 }
1793 mr_free(command);
1794
1795 if ((matches =
1796 save_filelist_entries_in_common(filelist_fname, filelist,
1797 filelist_subset_fname,
1798 use_star))
1799 <= 0 || length_of_file(filelist_subset_fname) < 2) {
1800 if (length_of_file(filelist_subset_fname) < 2) {
1801 log_msg(1, "No matches found in fileset %ld",
1802 current_tarball_number);
1803 }
1804 log_msg(2, "Skipping fileset %ld", current_tarball_number);
1805 restore_this_fileset = FALSE;
1806 } else {
1807 log_msg(5, "%ld matches. Saved fileset %ld's subset to %s",
1808 matches, current_tarball_number,
1809 filelist_subset_fname);
1810 restore_this_fileset = TRUE;
1811 }
1812 }
1813
1814// Concoct the call to star/afio to restore files
1815 if (strstr(tarball_fname, ".star.")) {
1816 // star
1817 mr_asprintf(command, "star -x file=%s %s", afio_fname, executable);
1818 if (filelist) {
1819 mr_strcat(command, " list=%s", filelist_subset_fname);
1820 }
1821 } else {
1822 // afio
1823 mr_asprintf(command, "afio -i -M 8m -b %ld %s", TAPE_BLOCK_SIZE, executable);
1824 if (filelist) {
1825 mr_strcat(command, " -w %s", filelist_subset_fname);
1826 }
1827 mr_strcat(command, " %s", afio_fname);
1828 }
1829 mr_strcat(command, " 2>> %s", MONDO_LOGFILE);
1830 mr_free(executable);
1831
1832// Call if IF there are files to restore (selectively/unconditionally)
1833 if (restore_this_fileset) {
1834 log_msg(1, "Calling command='%s'", command);
1835 paranoid_system(command);
1836
1837 if (g_getfattr) {
1838 log_it("Restoring xattr stuff");
1839 res = set_fattr_list(filelist_subset_fname, xattr_fname);
1840 if (res) {
1841 log_msg(1, "Errors occurred while setting xattr");
1842 } else {
1843 log_msg(1, "I set xattr OK");
1844 }
1845 retval += res;
1846 }
1847
1848 if (g_getfacl) {
1849 log_it("Restoring acl stuff");
1850 res = set_acl_list(filelist_subset_fname, acl_fname);
1851 if (res) {
1852 log_msg(1, "Errors occurred while setting ACL");
1853 } else {
1854 log_msg(1, "I set ACL OK");
1855 }
1856 retval += res;
1857 }
1858
1859 } else {
1860 log_msg(1, "NOT CALLING '%s'", command);
1861 }
1862 mr_free(command);
1863
1864 if (does_file_exist("/PAUSE") && current_tarball_number >= 50) {
1865 log_to_screen("Paused after set %ld", current_tarball_number);
1866 popup_and_OK("Pausing. Press ENTER to continue.");
1867 }
1868
1869 unlink(filelist_subset_fname);
1870 mr_free(filelist_subset_fname);
1871 unlink(filelist_fname);
1872 mr_free(filelist_fname);
1873 unlink(afio_fname);
1874 mr_free(afio_fname);
1875
1876 return (retval);
1877}
1878
1879/**************************************************************************
1880 *END_RESTORE_A_TARBALL_FROM_STREAM *
1881 **************************************************************************/
1882
1883
1884
1885
1886/**
1887 * Restore all biggiefiles from all media in this CD backup.
1888 * The CD with the last afioball should be currently mounted.
1889 * @param bkpinfo The backup information structure. @c backup_media_type is the
1890 * only field used in this function.
1891 * @param filelist The node structure containing the list of files to be
1892 * restored. If a prospective biggiefile is not in this list, it will be ignored.
1893 * @return 0 for success, nonzero for failure.
1894 */
1895int restore_all_biggiefiles_from_CD(struct s_node *filelist) {
1896
1897 int retval = 0;
1898 int res = 0;
1899 long noof_biggiefiles, bigfileno = 0, total_slices;
1900 /** malloc **/
1901 char *tmp1 = NULL;
1902 char *mds = NULL;
1903 bool just_changed_cds = FALSE;
1904 char *xattr_fname = NULL;
1905 char *acl_fname = NULL;
1906 char *biggies_whose_EXATs_we_should_set = NULL; // EXtended ATtributes
1907 char *pathname_of_last_biggie_restored;
1908 FILE *fbw = NULL;
1909
1910 malloc_string(pathname_of_last_biggie_restored);
1911 assert(bkpinfo != NULL);
1912
1913 mr_asprintf(biggies_whose_EXATs_we_should_set, "%s/biggies-whose-EXATs-we-should-set", bkpinfo->tmpdir);
1914 if (!(fbw = fopen(biggies_whose_EXATs_we_should_set, "w"))) {
1915 log_msg(1, "Warning - cannot openout %s", biggies_whose_EXATs_we_should_set);
1916 }
1917
1918 tmp1 = read_cfg_var(MINDI_CACHE"/mondorestore.cfg", "total-slices");
1919 total_slices = atol(tmp1);
1920 mr_free(tmp1);
1921
1922 mr_asprintf(tmp1, "Reassembling large files ");
1923 mvaddstr_and_log_it(g_currentY, 0, tmp1);
1924 mr_free(tmp1);
1925
1926 if (length_of_file(BIGGIELIST) < 6) {
1927 log_msg(1, "OK, no biggielist; not restoring biggiefiles");
1928 return (0);
1929 }
1930 noof_biggiefiles = count_lines_in_file(BIGGIELIST);
1931 if (noof_biggiefiles <= 0) {
1932 log_msg(2, "OK, no biggiefiles in biggielist; not restoring biggiefiles");
1933 return (0);
1934 }
1935 log_msg(2, "OK, there are %ld biggiefiles in the archives", noof_biggiefiles);
1936
1937 open_progress_form("Reassembling large files",
1938 "I am now reassembling all the large files.",
1939 "Please wait. This may take some time.",
1940 "", total_slices);
1941 for (bigfileno = 0 ; bigfileno < noof_biggiefiles ;) {
1942 log_msg(2, "Thinking about restoring bigfile %ld", bigfileno + 1);
1943 if (!does_file_exist(slice_fname(bigfileno, 0, ARCHIVES_PATH, ""))) {
1944 log_msg(3, "...but its first slice isn't on this CD. Perhaps this was a selective restore?");
1945 mds = media_descriptor_string(bkpinfo->backup_media_type);
1946 log_msg(3, "Cannot find bigfile #%ld 's first slice on %s #%d", bigfileno + 1, mds, g_current_media_number);
1947 log_msg(3, "Slicename would have been %s",
1948 slice_fname(bigfileno, 0, ARCHIVES_PATH, ""));
1949 // I'm not positive 'just_changed_cds' is even necessary...
1950 if (just_changed_cds) {
1951 just_changed_cds = FALSE;
1952 log_msg(3, "I'll continue to scan this CD for bigfiles to be restored.");
1953 } else if (does_file_exist(MNT_CDROM "/archives/NOT-THE-LAST")) {
1954 insist_on_this_cd_number(++g_current_media_number);
1955 log_to_screen("Restoring from %s #%d", mds, g_current_media_number);
1956 just_changed_cds = TRUE;
1957 } else {
1958 /* That big file doesn't exist, but the followings may */
1959 /* So we need to continue looping */
1960 log_msg(2, "There was no bigfile #%ld. That's OK.", bigfileno + 1);
1961 log_msg(2, "I'm going to stop restoring bigfiles now.");
1962 retval++;
1963 bigfileno++;
1964 }
1965 mr_free(mds);
1966 } else {
1967 just_changed_cds = FALSE;
1968 mr_asprintf(tmp1, "Restoring big file %ld", bigfileno + 1);
1969 update_progress_form(tmp1);
1970 mr_free(tmp1);
1971 res = restore_a_biggiefile_from_CD(bigfileno, filelist, pathname_of_last_biggie_restored);
1972 log_it("%s",pathname_of_last_biggie_restored);
1973 if (fbw && pathname_of_last_biggie_restored[0]) {
1974 fprintf(fbw, "%s\n", pathname_of_last_biggie_restored);
1975 }
1976 retval += res;
1977 bigfileno++;
1978
1979 }
1980 }
1981
1982 if (fbw) {
1983 fclose(fbw);
1984 if (g_getfattr) {
1985 mr_asprintf(xattr_fname, XATTR_BIGGLST_FNAME_RAW_SZ, ARCHIVES_PATH);
1986 if (length_of_file(xattr_fname) > 0) {
1987 set_fattr_list(biggies_whose_EXATs_we_should_set, xattr_fname);
1988 }
1989 mr_free(xattr_fname);
1990 }
1991 if (g_getfacl) {
1992 mr_asprintf(acl_fname, ACL_BIGGLST_FNAME_RAW_SZ, ARCHIVES_PATH);
1993 if (length_of_file(acl_fname) > 0) {
1994 set_acl_list(biggies_whose_EXATs_we_should_set, acl_fname);
1995 }
1996 mr_free(acl_fname);
1997 }
1998 }
1999 mr_free(biggies_whose_EXATs_we_should_set);
2000
2001 if (does_file_exist("/PAUSE")) {
2002 popup_and_OK("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
2003 }
2004 close_progress_form();
2005 if (retval) {
2006 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2007 } else {
2008 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2009 }
2010 paranoid_free(pathname_of_last_biggie_restored);
2011 return (retval);
2012}
2013
2014/**************************************************************************
2015 *END_RESTORE_ALL_BIGGIFILES_FROM_CD *
2016 **************************************************************************/
2017
2018
2019
2020/**
2021 * Restore all afioballs from all CDs in the backup.
2022 * The first CD should be inserted (if not, it will be asked for).
2023 * @param bkpinfo The backup information structure. @c backup_media_type is the
2024 * only field used in @e this function.
2025 * @param filelist The node structure containing the list of files to be
2026 * restored. If no file in some particular afioball is in this list, afio will
2027 * still be called for that fileset, but nothing will be written.
2028 * @return 0 for success, or the number of filesets that failed.
2029 */
2030int
2031restore_all_tarballs_from_CD(struct s_node *filelist)
2032{
2033 int retval = 0;
2034 int res;
2035 int attempts;
2036 long current_tarball_number = 0;
2037 long max_val;
2038 /**malloc ***/
2039 char *mds = NULL;
2040 char *tmp1 = NULL;
2041 char *tarball_fname = NULL;
2042 char *progress_str = NULL;
2043
2044 assert(bkpinfo != NULL);
2045
2046 mvaddstr_and_log_it(g_currentY, 0, "Restoring from archives");
2047 log_msg(2, "Insisting on 1st CD");
2048 if (g_current_media_number != 1) {
2049 log_msg(3, "OK, that's jacked up.");
2050 g_current_media_number = 1;
2051 }
2052 insist_on_this_cd_number(g_current_media_number);
2053 tmp1 = read_cfg_var(MINDI_CACHE"/mondorestore.cfg", "last-filelist-number");
2054 max_val = atol(tmp1) + 1;
2055 mr_free(tmp1);
2056
2057 mds = media_descriptor_string(bkpinfo->backup_media_type);
2058 mr_asprintf(progress_str, "Restoring from %s #%d", mds, g_current_media_number);
2059
2060 log_to_screen(progress_str);
2061 open_progress_form("Restoring from archives",
2062 "Restoring data from the archives.",
2063 "Please wait. This may take some time.",
2064 progress_str, max_val);
2065 for (;;) {
2066 insist_on_this_cd_number(g_current_media_number);
2067 update_progress_form(progress_str);
2068 mr_free(progress_str);
2069
2070 mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.bz2", current_tarball_number);
2071 if (!does_file_exist(tarball_fname)) {
2072 mr_free(tarball_fname);
2073 mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.gz", current_tarball_number);
2074 }
2075 if (!does_file_exist(tarball_fname)) {
2076 mr_free(tarball_fname);
2077 mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.lzma", current_tarball_number);
2078 }
2079 if (!does_file_exist(tarball_fname)) {
2080 mr_free(tarball_fname);
2081 mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.lzo", current_tarball_number);
2082 }
2083 if (!does_file_exist(tarball_fname)) {
2084 mr_free(tarball_fname);
2085 mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.", current_tarball_number);
2086 }
2087 if (!does_file_exist(tarball_fname)) {
2088 mr_free(tarball_fname);
2089 mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.star.bz2", current_tarball_number);
2090 }
2091 if (!does_file_exist(tarball_fname)) {
2092 mr_free(tarball_fname);
2093 mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.star.", current_tarball_number);
2094 }
2095 if (!does_file_exist(tarball_fname)) {
2096 if (current_tarball_number == 0) {
2097 log_to_screen
2098 ("No tarballs. Strange. Maybe you only backed up freakin' big files?");
2099 mr_free(tarball_fname);
2100 return (0);
2101 }
2102 if (!does_file_exist(MNT_CDROM "/archives/NOT-THE-LAST")
2103 || system("find " MNT_CDROM
2104 "/archives/slice* > /dev/null 2> /dev/null") ==
2105 0) {
2106 break;
2107 }
2108 g_current_media_number++;
2109 mr_asprintf(progress_str, "Restoring from %s #%d", media_descriptor_string(bkpinfo->backup_media_type), g_current_media_number);
2110 log_to_screen(progress_str);
2111 } else {
2112 mr_asprintf(progress_str, "Restoring from fileset #%ld on %s #%d", current_tarball_number, mds, g_current_media_number);
2113 for (res = 999, attempts = 0; attempts < 3 && res != 0; attempts++) {
2114 res = restore_a_tarball_from_CD(tarball_fname, current_tarball_number, filelist);
2115 }
2116 mr_asprintf(tmp1, "%s #%d, fileset #%ld - restore ", mds, g_current_media_number, current_tarball_number);
2117 if (res) {
2118 mr_strcat(tmp1, "reported errors");
2119 } else if (attempts > 1) {
2120 mr_strcat(tmp1, "succeeded");
2121 } else {
2122 mr_strcat(tmp1, "succeeded");
2123 }
2124 if (attempts > 1) {
2125 mr_strcat(tmp1, " (%d attempts) - review logs", attempts);
2126 }
2127 if (attempts > 1) {
2128 log_to_screen(tmp1);
2129 }
2130 mr_free(tmp1);
2131
2132 retval += res;
2133 current_tarball_number++;
2134 g_current_progress++;
2135 }
2136 mr_free(tarball_fname);
2137 }
2138 mr_free(mds);
2139 mr_free(progress_str);
2140
2141 close_progress_form();
2142 if (retval) {
2143 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2144 } else {
2145 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2146 }
2147
2148 return (retval);
2149}
2150
2151/**************************************************************************
2152 *END_RESTORE_ALL_TARBALLS_FROM_CD *
2153 **************************************************************************/
2154
2155
2156
2157/**
2158 * Restore all biggiefiles from the currently opened stream.
2159 * @param bkpinfo The backup information structure. Passed to other functions.
2160 * @param filelist The node structure containing the list of files to be
2161 * restored. If a prospective biggiefile is not in the list, it will be ignored.
2162 * @return 0 for success, or the number of biggiefiles that failed.
2163 */
2164int
2165restore_all_biggiefiles_from_stream(struct s_node *filelist)
2166{
2167 long noof_biggiefiles;
2168 long current_bigfile_number = 0;
2169 long total_slices;
2170
2171 int retval = 0;
2172 int res = 0;
2173 int ctrl_chr;
2174
2175 /** malloc add ****/
2176 char *tmp1 = NULL;
2177 char *biggie_fname;
2178 char *biggie_cksum;
2179 char *xattr_fname = NULL;
2180 char *acl_fname = NULL;
2181 char *p;
2182 char *pathname_of_last_biggie_restored;
2183 char *biggies_whose_EXATs_we_should_set = NULL; // EXtended ATtributes
2184 long long biggie_size;
2185 FILE *fbw = NULL;
2186
2187 malloc_string(biggie_fname);
2188 malloc_string(biggie_cksum);
2189 malloc_string(pathname_of_last_biggie_restored);
2190 assert(bkpinfo != NULL);
2191
2192 tmp1 = read_cfg_var(MINDI_CACHE"/mondorestore.cfg", "total-slices");
2193 total_slices = atol(tmp1);
2194 mr_free(tmp1);
2195
2196 if (g_getfattr) {
2197 mr_asprintf(xattr_fname, XATTR_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
2198 }
2199 if (g_getfacl) {
2200 mr_asprintf(acl_fname, ACL_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
2201 }
2202 mr_asprintf(tmp1, "Reassembling large files ");
2203 mvaddstr_and_log_it(g_currentY, 0, tmp1);
2204 mr_free(tmp1);
2205
2206 mr_asprintf(biggies_whose_EXATs_we_should_set, "%s/biggies-whose-EXATs-we-should-set", bkpinfo->tmpdir);
2207 if (!(fbw = fopen(biggies_whose_EXATs_we_should_set, "w"))) {
2208 log_msg(1, "Warning - cannot openout %s", biggies_whose_EXATs_we_should_set);
2209 }
2210
2211 // get xattr and acl files if they're there
2212 res = read_header_block_from_stream(&biggie_size, biggie_fname, &ctrl_chr);
2213 if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
2214 res =
2215 read_EXAT_files_from_tape(&biggie_size, biggie_fname, &ctrl_chr, xattr_fname, acl_fname);
2216 }
2217
2218 noof_biggiefiles = atol(biggie_fname);
2219 log_msg(2, "OK, there are %ld biggiefiles in the archives", noof_biggiefiles);
2220 open_progress_form("Reassembling large files",
2221 "I am now reassembling all the large files.",
2222 "Please wait. This may take some time.",
2223 "", total_slices);
2224
2225 for (res =
2226 read_header_block_from_stream(&biggie_size, biggie_fname,
2227 &ctrl_chr);
2228 ctrl_chr != BLK_STOP_BIGGIEFILES;
2229 res =
2230 read_header_block_from_stream(&biggie_size, biggie_fname,
2231 &ctrl_chr)) {
2232 if (ctrl_chr != BLK_START_A_NORMBIGGIE
2233 && ctrl_chr != BLK_START_A_PIHBIGGIE) {
2234 wrong_marker(BLK_START_A_NORMBIGGIE, ctrl_chr);
2235 }
2236 p = strrchr(biggie_fname, '/');
2237 if (!p) {
2238 p = biggie_fname;
2239 } else {
2240 p++;
2241 }
2242 mr_asprintf(tmp1, "Restoring big file %ld (%lld K)", current_bigfile_number + 1, biggie_size / 1024);
2243 update_progress_form(tmp1);
2244 mr_free(tmp1);
2245 res = restore_a_biggiefile_from_stream(biggie_fname,
2246 current_bigfile_number,
2247 biggie_cksum,
2248 biggie_size,
2249 filelist, ctrl_chr,
2250 pathname_of_last_biggie_restored);
2251 log_msg(1, "I believe I have restored %s",
2252 pathname_of_last_biggie_restored);
2253 if (fbw && pathname_of_last_biggie_restored[0]) {
2254 fprintf(fbw, "%s\n", pathname_of_last_biggie_restored);
2255 }
2256 retval += res;
2257 current_bigfile_number++;
2258
2259 }
2260 if (current_bigfile_number != noof_biggiefiles
2261 && noof_biggiefiles != 0) {
2262 log_msg(1, "Warning - bigfileno=%ld but noof_biggiefiles=%ld\n", current_bigfile_number, noof_biggiefiles);
2263 } else {
2264 log_msg(1, "%ld biggiefiles in biggielist.txt; %ld biggiefiles processed today.", noof_biggiefiles, current_bigfile_number);
2265 }
2266
2267 if (fbw) {
2268 fclose(fbw);
2269 if (length_of_file(biggies_whose_EXATs_we_should_set) > 2) {
2270 log_it("Setting biggie-EXATs");
2271 if (g_getfattr) {
2272 if (length_of_file(xattr_fname) > 0) {
2273 log_msg(1, "set_fattr_List(%s,%s)", biggies_whose_EXATs_we_should_set, xattr_fname);
2274 set_fattr_list(biggies_whose_EXATs_we_should_set, xattr_fname);
2275 }
2276 }
2277 if (g_getfacl) {
2278 if (length_of_file(acl_fname) > 0) {
2279 log_msg(1, "set_acl_list(%s,%s)", biggies_whose_EXATs_we_should_set, acl_fname);
2280 set_acl_list(biggies_whose_EXATs_we_should_set, acl_fname);
2281 }
2282 }
2283 } else {
2284 log_it("No biggiefiles selected. So, no biggie-EXATs to set.");
2285 }
2286 }
2287 mr_free(xattr_fname);
2288 mr_free(acl_fname);
2289 mr_free(biggies_whose_EXATs_we_should_set);
2290
2291 if (does_file_exist("/PAUSE")) {
2292 popup_and_OK
2293 ("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
2294 }
2295
2296 close_progress_form();
2297 if (retval) {
2298 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2299 } else {
2300 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2301 }
2302 paranoid_free(pathname_of_last_biggie_restored);
2303 paranoid_free(biggie_fname);
2304 paranoid_free(biggie_cksum);
2305 return (retval);
2306}
2307
2308/**************************************************************************
2309 *END_RESTORE_ALL_BIGGIEFILES_FROM_STREAM *
2310 **************************************************************************/
2311
2312
2313
2314
2315
2316
2317/**
2318 * Restore all afioballs from the currently opened tape stream.
2319 * @param bkpinfo The backup information structure. Fields used:
2320 * - @c bkpinfo->backup_media_type
2321 * - @c bkpinfo->restore_path
2322 * @param filelist The node structure containing the list of files to be
2323 * restored. If no file in an afioball is in this list, afio will still be
2324 * called for that fileset, but nothing will be written.
2325 * @return 0 for success, or the number of filesets that failed.
2326 */
2327int restore_all_tarballs_from_stream(struct s_node *filelist)
2328{
2329 int retval = 0;
2330 int res;
2331 long current_afioball_number = 0;
2332 int ctrl_chr;
2333 long max_val /*, total_noof_files */ ;
2334
2335 /** malloc **/
2336 char *tmp = NULL;
2337 char *mds = NULL;
2338 char *progress_str = NULL;
2339 char *tmp_fname;
2340 char *xattr_fname = NULL;
2341 char *acl_fname = NULL;
2342
2343 long long tmp_size;
2344
2345 malloc_string(tmp_fname);
2346 assert(bkpinfo != NULL);
2347 mvaddstr_and_log_it(g_currentY, 0, "Restoring from archives");
2348 tmp = read_cfg_var(MINDI_CACHE"/mondorestore.cfg", "last-filelist-number");
2349 max_val = atol(tmp) + 1;
2350 mr_free(tmp);
2351
2352 chdir(bkpinfo->restore_path); /* I don't know why this is needed _here_ but it seems to be. -HR, 02/04/2002 */
2353
2354 run_program_and_log_output("pwd", 5);
2355
2356 mr_asprintf(progress_str, "Restoring from media #%d", g_current_media_number);
2357 log_to_screen(progress_str);
2358 open_progress_form("Restoring from archives",
2359 "Restoring data from the archives.",
2360 "Please wait. This may take some time.",
2361 progress_str, max_val);
2362
2363 log_msg(3, "hey");
2364
2365 res = read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
2366 if (res) {
2367 log_msg(2, "Warning - error reading afioball from tape");
2368 }
2369 retval += res;
2370 if (ctrl_chr != BLK_START_AFIOBALLS) {
2371 wrong_marker(BLK_START_AFIOBALLS, ctrl_chr);
2372 }
2373 log_msg(2, "ho");
2374 res = read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
2375 while (ctrl_chr != BLK_STOP_AFIOBALLS) {
2376 update_progress_form(progress_str);
2377 if (g_getfattr) {
2378 mr_asprintf(xattr_fname, "%s/xattr-subset-%ld.tmp", bkpinfo->tmpdir, current_afioball_number);
2379 unlink(xattr_fname);
2380 }
2381 if (g_getfacl) {
2382 mr_asprintf(acl_fname, "%s/acl-subset-%ld.tmp", bkpinfo->tmpdir, current_afioball_number);
2383 unlink(acl_fname);
2384 }
2385 if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
2386 log_it("Reading EXAT files from tape");
2387 res = read_EXAT_files_from_tape(&tmp_size, tmp_fname, &ctrl_chr, xattr_fname, acl_fname);
2388 }
2389 if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
2390 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
2391 }
2392 log_msg(4, "Restoring from fileset #%ld (name=%s, size=%ld K)", current_afioball_number, tmp_fname, (long) tmp_size >> 10);
2393 res = restore_a_tarball_from_stream(tmp_fname, current_afioball_number, filelist, tmp_size, xattr_fname, acl_fname);
2394 retval += res;
2395 if (res) {
2396 log_to_screen("Fileset %ld - errors occurred", current_afioball_number);
2397 }
2398 res =
2399 read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
2400 if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
2401 wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
2402 }
2403
2404 current_afioball_number++;
2405 g_current_progress++;
2406 mds = media_descriptor_string(bkpinfo->backup_media_type),
2407
2408 mr_free(progress_str);
2409 mr_asprintf(progress_str, "Restoring from fileset #%ld on %s #%d", current_afioball_number, mds, g_current_media_number);
2410 mr_free(mds);
2411 res = read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
2412 if (g_getfattr) {
2413 unlink(xattr_fname);
2414 }
2415 if (g_getfacl) {
2416 unlink(acl_fname);
2417 }
2418 } // next
2419 mr_free(progress_str);
2420 if (g_getfattr) {
2421 mr_free(xattr_fname);
2422 }
2423 if (g_getfacl) {
2424 mr_free(acl_fname);
2425 }
2426
2427 log_msg(1, "All done with afioballs");
2428 close_progress_form();
2429 if (retval) {
2430 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2431 } else {
2432 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2433 }
2434 paranoid_free(tmp_fname);
2435 return (retval);
2436}
2437
2438/**************************************************************************
2439 *END_ RESTORE_ALL_TARBALLS_FROM_STREAM *
2440 **************************************************************************/
2441
2442/* @} - end of LLrestoreGroup */
2443
2444
2445/**
2446 * Restore all files in @p filelist.
2447 * @param bkpinfo The backup information structure. Most fields are used.
2448 * @param filelist The node structure containing the list of files to be
2449 * restored.
2450 * @return 0 for success, or the number of afioballs and biggiefiles that failed.
2451 * @ingroup restoreGroup
2452 */
2453int restore_everything(struct s_node *filelist)
2454{
2455 int resA;
2456 int resB;
2457
2458 /** mallco ***/
2459 char *cwd;
2460 char *newpath;
2461 char *tmp = NULL;
2462 assert(bkpinfo != NULL);
2463
2464 malloc_string(cwd);
2465 malloc_string(newpath);
2466 log_msg(2, "restore_everything() --- starting");
2467 g_current_media_number = 1;
2468 getcwd(cwd, MAX_STR_LEN - 1);
2469 mr_asprintf(tmp, "mkdir -p %s", bkpinfo->restore_path);
2470 run_program_and_log_output(tmp, FALSE);
2471 mr_free(tmp);
2472
2473 log_msg(1, "Changing dir to %s", bkpinfo->restore_path);
2474 chdir(bkpinfo->restore_path);
2475 getcwd(newpath, MAX_STR_LEN - 1);
2476 log_msg(1, "path is now %s", newpath);
2477 log_msg(1, "restoring everything");
2478 tmp = find_home_of_exe("petris");
2479 if (!tmp && !g_text_mode) {
2480 newtDrawRootText(0, g_noof_rows - 2,
2481 "Press ALT-<left cursor> twice to play Petris :-) ");
2482 newtRefresh();
2483 }
2484 mr_free(tmp);
2485
2486 mvaddstr_and_log_it(g_currentY, 0, "Preparing to read your archives");
2487 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2488 mount_media();
2489 mvaddstr_and_log_it(g_currentY++, 0,
2490 "Restoring OS and data from streaming media");
2491 if (bkpinfo->backup_media_type == cdstream) {
2492 openin_cdstream();
2493 } else {
2494 openin_tape();
2495 }
2496 resA = restore_all_tarballs_from_stream(filelist);
2497 resB = restore_all_biggiefiles_from_stream(filelist);
2498 if (bkpinfo->backup_media_type == cdstream) {
2499 closein_cdstream();
2500 } else {
2501 closein_tape();
2502 }
2503 } else {
2504 mvaddstr_and_log_it(g_currentY++, 0,
2505 "Restoring OS and data from CD/USB ");
2506 mount_media();
2507 resA = restore_all_tarballs_from_CD(filelist);
2508 resB = restore_all_biggiefiles_from_CD(filelist);
2509 }
2510 chdir(cwd);
2511 if (resA + resB) {
2512 log_to_screen("Errors occurred while data was being restored.");
2513 }
2514 if (length_of_file("/etc/raidtab") > 0) {
2515 log_msg(2, "Copying local raidtab to restored filesystem");
2516 run_program_and_log_output("cp -f /etc/raidtab " MNT_RESTORING
2517 "/etc/raidtab", FALSE);
2518 }
2519 kill_petris();
2520 log_msg(2, "restore_everything() --- leaving");
2521 paranoid_free(cwd);
2522 paranoid_free(newpath);
2523 return (resA + resB);
2524}
2525
2526/**************************************************************************
2527 *END_RESTORE_EVERYTHING *
2528 **************************************************************************/
2529
2530
2531extern void wait_until_software_raids_are_prepped(char *, int);
2532
2533
2534char which_restore_mode(void);
2535
2536
2537/**
2538 * Log a "don't panic" message to the logfile.
2539 */
2540void welcome_to_mondorestore()
2541{
2542 log_msg(0, "-------------- Mondo Restore v%s -------------", PACKAGE_VERSION);
2543 log_msg(0, "DON'T PANIC! Mondorestore logs almost everything, so please ");
2544 log_msg(0, "don't break out in a cold sweat just because you see a few ");
2545 log_msg(0, "error messages in the log. Read them; analyze them; see if ");
2546 log_msg(0, "they are significant; above all, verify your backups! Please");
2547 log_msg(0, "attach a compressed copy of this log to any e-mail you send ");
2548 log_msg(0, "to the Mondo mailing list when you are seeking technical ");
2549 log_msg(0, "support. Without it, we can't help you. - DevTeam");
2550 log_msg(0, "------------------------------------------------------------");
2551 log_msg(0, "BTW, despite (or perhaps because of) the wealth of messages,");
2552 log_msg(0, "some users are inclined to stop reading this log. If Mondo ");
2553 log_msg(0, "stopped for some reason, chances are it's detailed here. ");
2554 log_msg(0, "More than likely there's a message at the very end of this ");
2555 log_msg(0, "log that will tell you what is wrong. Please read it! ");
2556 log_msg(0, "------------------------------------------------------------");
2557}
2558
2559
2560
2561/**
2562 * Restore the user's data.
2563 * What did you think it did, anyway? :-)
2564 */
2565int main(int argc, char *argv[])
2566{
2567 FILE *fin;
2568 FILE *fout;
2569 int retval = 0;
2570 int res;
2571 char *tmp = NULL;
2572
2573 struct mountlist_itself *mountlist;
2574 struct raidlist_itself *raidlist;
2575 struct s_node *filelist;
2576 char *tmp1 = NULL;
2577 char *tmp2 = NULL;
2578 bool run_postnuke = FALSE;
2579
2580 if (getuid() != 0) {
2581 fprintf(stderr, "Please run as root.\r\n");
2582 exit(127);
2583 }
2584 bkpinfo = (struct s_bkpinfo *)mr_malloc(sizeof(struct s_bkpinfo));
2585 init_bkpinfo();
2586
2587 g_loglevel = DEFAULT_MR_LOGLEVEL;
2588
2589 /* Configure global variables */
2590 malloc_libmondo_global_strings();
2591 tmp1 = call_program_and_get_last_line_of_output("cat " CMDLINE);
2592 if (strstr(tmp1, "textonly")) {
2593 g_text_mode = TRUE;
2594 log_msg(1, "TEXTONLY MODE");
2595 } else {
2596 g_text_mode = FALSE;
2597 } // newt :-)
2598 mr_free(tmp1);
2599
2600 /* Init GUI */
2601 setup_newt_stuff(); /* call newtInit and setup screen log */
2602
2603 tmp1 = call_program_and_get_last_line_of_output("which mondorestore");
2604 strcpy(g_mondo_home, tmp1);
2605 mr_free(tmp1);
2606
2607 g_current_media_number = 1; // precaution
2608
2609 run_program_and_log_output("mkdir -p " MNT_CDROM, FALSE);
2610
2611 setup_MR_global_filenames(); // malloc() and set globals, using bkpinfo->tmpdir etc.
2612 bkpinfo->backup_media_type = none; // in case boot disk was made for one backup type but user wants to restore from another backup type
2613 bkpinfo->restore_data = TRUE; // Well, yeah :-)
2614 if (am_I_in_disaster_recovery_mode()) {
2615 run_program_and_log_output("mount / -o remount,rw", 2);
2616 } // for b0rken distros
2617 g_main_pid = getpid();
2618 srandom((int) (time(NULL)));
2619 set_signals(TRUE);
2620 g_kernel_version = get_kernel_version();
2621
2622 log_msg(1, "FYI - mountlist = %s", MINDI_CACHE"/mountlist.txt");
2623 mkdir(MNT_CDROM, 0x770);
2624 make_hole_for_dir(MONDO_CACHE);
2625
2626 /* Backup original mountlist.txt */
2627 mr_asprintf(tmp, "cp -f "MINDI_CACHE"/mountlist.txt "MINDI_CACHE"/mountlist.txt.orig");
2628 run_program_and_log_output(tmp, FALSE);
2629 mr_free(tmp);
2630
2631 /* Init directories */
2632 make_hole_for_dir("/var/log");
2633 make_hole_for_dir("/tmp/tmpfs"); /* just in case... */
2634 run_program_and_log_output("umount " MNT_CDROM, FALSE);
2635
2636 run_program_and_log_output("rm -Rf /tmp/tmpfs/mondo.tmp.*", FALSE);
2637
2638 welcome_to_mondorestore();
2639 if (bkpinfo->disaster_recovery) {
2640 log_msg(1, "I am in disaster recovery mode");
2641 } else {
2642 log_msg(1, "I am in normal, live mode");
2643 }
2644
2645 log_it("what time is it");
2646
2647 mountlist = (struct mountlist_itself *)mr_malloc(sizeof(struct mountlist_itself));
2648 raidlist = (struct raidlist_itself *)mr_malloc(sizeof(struct raidlist_itself));
2649
2650 /* Process command-line parameters */
2651 if (argc == 2 && strcmp(argv[1], "--edit-mountlist") == 0) {
2652#ifdef __FreeBSD__
2653 system("mv -f /tmp/raidconf.txt /etc/raidtab");
2654 if (!does_file_exist("/etc/raidtab"))
2655 system("vinum printconfig > /etc/raidtab");
2656#endif
2657 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2658 res = let_user_edit_the_mountlist(mountlist, raidlist);
2659#ifdef __FreeBSD__
2660 system("mv -f /etc/raidtab /tmp/raidconf.txt");
2661#endif
2662 paranoid_MR_finish(res);
2663 }
2664
2665 if (argc == 3 && strcmp(argv[1], "--echo-to-screen") == 0) {
2666 fout = fopen("/tmp/out.txt", "w");
2667 fput_string_one_char_at_a_time(stderr, argv[2]);
2668 finish(0);
2669 }
2670
2671 if (argc == 3 && strcmp(argv[1], "--gendf") == 0) {
2672 make_grub_install_scriptlet(argv[2]);
2673 finish(0);
2674 }
2675
2676 if (argc == 5 && strcmp(argv[1], "--common") == 0) {
2677 g_loglevel = 6;
2678 filelist = load_filelist(argv[2]);
2679 if (!filelist) {
2680 fatal_error("Failed to load filelist");
2681 }
2682 toggle_node_selection(filelist, FALSE);
2683 toggle_all_root_dirs_on(filelist);
2684 // BERLIOS: /usr/lib ???
2685 toggle_path_selection(filelist, "/usr/share", TRUE);
2686 save_filelist(filelist, "/tmp/out.txt");
2687 mr_asprintf(tmp1, "%s", argv[3]);
2688 mr_asprintf(tmp2, "%s", argv[4]);
2689
2690 res = save_filelist_entries_in_common(tmp1, filelist, tmp2, FALSE);
2691 mr_free(tmp1);
2692 mr_free(tmp2);
2693
2694 free_filelist(filelist);
2695 printf("res = %d", res);
2696 finish(0);
2697 }
2698
2699 if (argc == 3 && strcmp(argv[1], "--popuplist") == 0) {
2700 popup_changelist_from_file(argv[2]);
2701 paranoid_MR_finish(0);
2702 }
2703
2704 if (argc == 5 && strcmp(argv[1], "--copy") == 0) {
2705 log_msg(1, "SCORE");
2706 g_loglevel = 10;
2707 if (strstr(argv[2], "save")) {
2708 log_msg(1, "Saving from %s to %s", argv[3], argv[4]);
2709 fin = fopen(argv[3], "r");
2710 fout = fopen(argv[4], "w");
2711 copy_from_src_to_dest(fin, fout, 'w');
2712 fclose(fin);
2713 fin = fopen(argv[3], "r");
2714 copy_from_src_to_dest(fin, fout, 'w');
2715 fclose(fout);
2716 fclose(fin);
2717 } else if (strstr(argv[2], "restore")) {
2718 fout = fopen(argv[3], "w");
2719 fin = fopen(argv[4], "r");
2720 copy_from_src_to_dest(fout, fin, 'r');
2721 fclose(fin);
2722 fin = fopen(argv[4], "r");
2723 copy_from_src_to_dest(fout, fin, 'r');
2724 fclose(fout);
2725 fclose(fin);
2726 } else {
2727 fatal_error("Unknown additional param");
2728 }
2729 finish(0);
2730 }
2731
2732 if (argc == 3 && strcmp(argv[1], "--mdstat") == 0) {
2733 wait_until_software_raids_are_prepped(argv[2], 100);
2734 finish(0);
2735 }
2736
2737 if (argc == 3 && strcmp(argv[1], "--mdconv") == 0) {
2738 finish(create_raidtab_from_mdstat(argv[2]));
2739 }
2740
2741 if (argc == 2 && strcmp(argv[1], "--live-grub") == 0) {
2742 retval = run_grub(FALSE, "/dev/hda");
2743 if (retval) {
2744 log_to_screen("Failed to write Master Boot Record");
2745 }
2746 paranoid_MR_finish(0);
2747 }
2748 if (argc == 3 && strcmp(argv[1], "--paa") == 0) {
2749 g_current_media_number = atoi(argv[2]);
2750 pause_and_ask_for_cdr(5, NULL);
2751 paranoid_MR_finish(0);
2752 }
2753 if ((argc == 2 && strcmp(argv[1], "--partition-only") == 0) && (bkpinfo->disaster_recovery)) {
2754 log_msg(0, "Partitioning only.");
2755 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2756 load_mountlist(mountlist, MINDI_CACHE"/mountlist.txt");
2757 res = partition_everything(mountlist);
2758 finish(res);
2759 }
2760
2761 if ((argc == 2 && strcmp(argv[1], "--format-only") == 0) && (bkpinfo->disaster_recovery)) {
2762 log_msg(0, "Formatting only.");
2763 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2764 res = format_everything(mountlist, FALSE, raidlist);
2765 finish(res);
2766 }
2767
2768 if ((argc == 2 && strcmp(argv[1], "--stop-lvm-and-raid") == 0) && (bkpinfo->disaster_recovery)) {
2769 log_msg(0, "Stopping LVM and RAID");
2770 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2771 load_mountlist(mountlist, MINDI_CACHE"/mountlist.txt");
2772 res = do_my_funky_lvm_stuff(TRUE, FALSE);
2773 res += stop_all_raid_devices(mountlist);
2774 finish(res);
2775 }
2776
2777 // Handle params here first
2778 handle_incoming_parameters(argc,argv);
2779
2780 if (!bkpinfo->disaster_recovery) { // live!
2781 log_msg(1, "I am in normal, live mode.");
2782 mount_boot_if_necessary(); /* for Gentoo users */
2783 log_msg(2, "Still here.");
2784 /* Adding an initialisation in order to avoid to hndle NULL pointer later */
2785 mr_free(bkpinfo->restore_path);
2786 mr_asprintf(bkpinfo->restore_path, "%s", "/tmp");
2787
2788 log_msg(2, "Calling restore_to_live_filesystem()");
2789 retval = restore_to_live_filesystem();
2790
2791 log_msg(2, "Still here. Yay.");
2792 if ((strlen(bkpinfo->tmpdir) > 0) && (strstr(bkpinfo->tmpdir,"mondo.tmp.") != NULL)) {
2793 mr_asprintf(tmp, "rm -Rf %s/*", bkpinfo->tmpdir);
2794 run_program_and_log_output(tmp, FALSE);
2795 mr_free(tmp);
2796 }
2797 unmount_boot_if_necessary(); /* for Gentoo users */
2798 paranoid_MR_finish(retval);
2799 } else {
2800 /* Disaster recovery mode (must be) */
2801 log_msg(1, "I must be in disaster recovery mode.");
2802
2803 log_it("About to call load_mountlist and load_raidtab");
2804 mr_free(bkpinfo->restore_path);
2805 mr_asprintf(bkpinfo->restore_path, "%s", MNT_RESTORING);
2806 read_cfg_file_into_bkpinfo(MINDI_CACHE"/mondorestore.cfg");
2807 retval = load_mountlist(mountlist, MINDI_CACHE"/mountlist.txt");
2808 retval += load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2809 log_it("Returned from calling load_mountlist and load_raidtab successfully");
2810
2811 // BCO:To be reviewed
2812 if ((bkpinfo->restore_mode == compare) || (bkpinfo->restore_mode == nuke)) {
2813 if ((bkpinfo->backup_media_type == netfs) && bkpinfo->netfs_mount && !is_this_device_mounted(bkpinfo->netfs_mount)) {
2814 log_msg(1, "Mounting remote %s dir", bkpinfo->netfs_proto);
2815 mr_free(bkpinfo->isodir);
2816 mr_asprintf(bkpinfo->isodir, "/tmp/isodir");
2817 run_program_and_log_output("mkdir -p /tmp/isodir", 5);
2818 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
2819 mr_asprintf(tmp, "sshfs -o ro %s /tmp/isodir", bkpinfo->netfs_mount);
2820 } else {
2821 mr_asprintf(tmp, "mount %s -o nolock,ro /tmp/isodir", bkpinfo->netfs_mount);
2822 }
2823 run_program_and_log_output(tmp, 1);
2824 mr_free(tmp);
2825 }
2826 }
2827
2828 if (retval) {
2829 log_to_screen("Warning - load_raidtab_into_raidlist returned an error");
2830 }
2831
2832 log_msg(1, "Send in the clowns.");
2833
2834
2835 if (bkpinfo->restore_mode == nuke) {
2836 log_it("nuking");
2837 retval += nuke_mode(mountlist, raidlist);
2838 } else if (bkpinfo->restore_mode == interactive) {
2839 log_it("catchall");
2840 retval += catchall_mode(mountlist, raidlist);
2841 } else if (bkpinfo->restore_mode == compare) {
2842 log_it("compare");
2843 retval += compare_mode(mountlist, raidlist);
2844 } else if (bkpinfo->restore_mode == isoonly) {
2845 log_it("iso");
2846 retval = iso_mode(mountlist, raidlist, FALSE);
2847 } else if (bkpinfo->restore_mode == mbr) {
2848 log_it("mbr");
2849 retval = mount_all_devices(mountlist, TRUE);
2850 if (!retval) {
2851 retval += run_boot_loader(FALSE);
2852 retval += unmount_all_devices(mountlist);
2853 }
2854 if (retval) {
2855 log_to_screen("Failed to write Master Boot Record");
2856 }
2857 } else if (bkpinfo->restore_mode == isonuke) {
2858 log_it("isonuke");
2859 retval = iso_mode(mountlist, raidlist, TRUE);
2860 } else {
2861 log_it("catchall (no mode specified in command-line call");
2862 retval += catchall_mode(mountlist, raidlist);
2863 }
2864 }
2865
2866 /* clean up at the end */
2867 if (retval) {
2868 if (does_file_exist(MONDO_CACHE"/changed.files")) {
2869 log_to_screen
2870 ("See "MONDO_CACHE"/changed.files for list of files that have changed.");
2871 }
2872 mvaddstr_and_log_it(g_currentY++,
2873 0,
2874 "Run complete. Errors were reported. Please review the logfile.");
2875 } else {
2876 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2877 mvaddstr_and_log_it(g_currentY++,
2878 0,
2879 "Run complete. Please remove media and reboot.");
2880 } else {
2881 sync();
2882 if (is_this_device_mounted(MNT_CDROM)) {
2883 run_program_and_log_output("umount " MNT_CDROM, FALSE);
2884 }
2885
2886 if (!bkpinfo->please_dont_eject) {
2887 (void)eject_device("/dev/cdrom");
2888 }
2889 mvaddstr_and_log_it(g_currentY++,
2890 0,
2891 "Run complete. Please remove media and reboot.");
2892 }
2893 }
2894
2895// g_I_have_just_nuked is set true by nuke_mode() just before it returns
2896 if (!system("which post-nuke > /dev/null 2> /dev/null")) {
2897 log_msg(1, "post-nuke found; find out whether we should run it...");
2898 if (g_I_have_just_nuked || does_file_exist("/POST-NUKE-ANYWAY")) {
2899 run_postnuke = TRUE;
2900 log_msg(1, "Yes, will run post-nuke because in nuke mode or file /POST-NUKE-ANYWAY exists.");
2901 } else if (ask_me_yes_or_no("post-nuke script found. Do you want to run it?")) {
2902 run_postnuke = TRUE;
2903 log_msg(1, "Yes, will run post-nuke because user interactively asked for it.");
2904 } else {
2905 run_postnuke = FALSE;
2906 log_msg(1, "No, will not run post-nuke.");
2907 }
2908 } else {
2909 log_msg(1, "No post-nuke found.");
2910 }
2911 if (run_postnuke) {
2912 log_to_screen("Running post-nuke...");
2913 if (mount_all_devices(mountlist, TRUE)) {
2914 log_to_screen
2915 ("Unable to re-mount partitions for post-nuke stuff");
2916 } else {
2917 log_msg(1, "Re-mounted partitions for post-nuke stuff");
2918 mr_asprintf(tmp, "post-nuke %s %d", bkpinfo->restore_path, retval);
2919 log_msg(2, "Calling '%s'", tmp);
2920 if ((res = run_program_and_log_output(tmp, 0))) {
2921 log_OS_error(tmp);
2922 }
2923 mr_free(tmp);
2924 log_msg(1, "post-nuke returned w/ res=%d", res);
2925 }
2926 unmount_all_devices(mountlist);
2927 log_to_screen("I've finished post-nuking.");
2928 }
2929
2930/*
2931 log_to_screen("If you are REALLY in a hurry, hit Ctrl-Alt-Del now.");
2932 log_to_screen("Otherwise, please wait until the RAID disks are done.");
2933 wait_until_software_raids_are_prepped("/proc/mdstat", 100);
2934 log_to_screen("Thank you.");
2935*/
2936 unlink("/tmp/mondo-run-prog.tmp");
2937 set_signals(FALSE);
2938 log_to_screen("Restore log (%s) copied to /var/log on your hard disk", MONDO_LOGFILE);
2939 log_to_screen("Mondo-restore is exiting (retval=%d) ", retval);
2940
2941 mr_asprintf(tmp, "umount %s", bkpinfo->isodir);
2942 run_program_and_log_output(tmp, 5);
2943 mr_free(tmp);
2944
2945 paranoid_free(mountlist);
2946 paranoid_free(raidlist);
2947 if (am_I_in_disaster_recovery_mode()) {
2948 run_program_and_log_output("mount / -o remount,rw", 2);
2949 } // for b0rken distros
2950 if (strstr(bkpinfo->tmpdir,"mondo.tmp.") != NULL) {
2951 mr_asprintf(tmp, "rm -Rf %s", bkpinfo->tmpdir);
2952 system(tmp);
2953 mr_free(tmp);
2954 }
2955 paranoid_MR_finish(retval); // frees global stuff plus bkpinfo
2956 free_libmondo_global_strings(); // it's fine to have this here :) really :)
2957
2958 unlink("/tmp/filelist.full");
2959
2960 exit(retval);
2961}
2962
2963/**************************************************************************
2964 *END_MAIN *
2965 **************************************************************************/
2966
2967
2968
2969
2970
2971/**************************************************************************
2972 *END_MONDO-RESTORE.C *
2973 **************************************************************************/
Note: See TracBrowser for help on using the repository browser.