source: MondoRescue/branches/3.0/mondo/src/mondorestore/mondorestore.c@ 3141

Last change on this file since 3141 was 3141, checked in by Bruno Cornec, 11 years ago

r5345@localhost: bruno | 2013-06-13 12:46:54 +0200

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