source: MondoRescue/trunk/mondo/mondo/mondorestore/mondo-restore.c@ 30

Last change on this file since 30 was 30, checked in by bcornec, 19 years ago

Id property added on files to allow for better conf. management

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