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

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