source: MondoRescue/branches/2.2.7/mondo/src/mondorestore/mondorestore.c@ 2030

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