source: MondoRescue/branches/2.2.9/mondo/src/mondorestore/mondorestore.c@ 2242

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

r3145@localhost: bruno | 2009-06-29 17:18:58 +0200

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