source: MondoRescue/branches/2.2.5/mondo/src/mondorestore/mondo-restore.c@ 1547

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