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

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

r3338@localhost: bruno | 2009-08-11 23:03:30 +0200
bkpinfo->zip_suffix, bkpinfo->image_devs and bkpinfo->restore_path are now allocated dynmically

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