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

Last change on this file since 2323 was 2323, checked in by Bruno Cornec, 15 years ago

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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