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

Last change on this file since 2395 was 2395, checked in by Bruno Cornec, 15 years ago
  • Fix interface of evaluate_mountlist (remove 2nd param useless) and fix nuke mode which wasn't working.
  • Tries to add support for bzip2 and lzma initramfs (preliminary, not tested) for 2.6.30

(Backport from 2.2.9)

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