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

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