source: MondoRescue/branches/stable/mondo/src/mondorestore/mondo-restore.c@ 912

Last change on this file since 912 was 912, checked in by Bruno Cornec, 17 years ago

Attempt to fix #66 (setfacl not existing => no error)

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