source: MondoRescue/branches/3.2/mondo/src/mondorestore/mondorestore.c@ 3236

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