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

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