source: branches/stable/mondo/src/mondoarchive/mondoarchive.c @ 1378

Last change on this file since 1378 was 1378, checked in by bruno, 12 years ago

Fixes for merges

  • Property svn:keywords set to Id
File size: 16.4 KB
Line 
1/***************************************************************************
2* $Id: mondoarchive.c 1378 2007-04-30 13:14:47Z bruno $
3*/
4
5
6/**
7 * @file
8 * The main file for mondoarchive.
9 */
10
11/************************* #include statements *************************/
12#include <pthread.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
18
19#include "my-stuff.h"
20#include "mondostructures.h"
21#include "libmondo.h"
22#include "libmondo-tools.h"
23#include "mondo-cli-EXT.h"
24
25#include "mondoarchive.h"
26#include "mr_mem.h"
27#include "mr_str.h"
28#include "mr_msg.h"
29#include "mr_file.h"
30#include "mr_err.h"
31#include "mr_conf.h"
32
33// for CVS
34//static char cvsid[] = "$Id: mondoarchive.c 1378 2007-04-30 13:14:47Z bruno $";
35
36/************************* external variables *************************/
37extern void set_signals(int);
38extern int g_current_media_number;
39extern void register_pid(pid_t, char *);
40extern int g_currentY;
41extern bool g_text_mode;
42extern char *g_boot_mountpt;
43extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
44extern char *g_tmpfs_mountpt;
45extern char *g_erase_tmpdir_and_scratchdir;
46extern char *g_cdrw_drive_is_here;
47extern double g_kernel_version;
48extern char *g_magicdev_command;
49extern t_bkptype g_backup_media_type;
50extern int g_loglevel;
51
52static char *g_cdrom_drive_is_here = NULL;
53static char *g_dvd_drive_is_here = NULL;
54
55struct mr_ar_conf mr_conf;
56
57/***************** global vars ******************/
58bool g_skip_floppies;
59long diffs;
60
61/****************** subroutines used only here ******************/
62
63
64/**
65 * Print a "don't panic" message to the log and a message about the logfile to the screen.
66 */
67static void welcome_to_mondoarchive(void)
68{
69    char *tmp = NULL;
70
71    mr_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org", PACKAGE_VERSION);
72    mr_msg(0, "running %s binaries", get_architecture());
73    tmp = get_uname_m();
74    mr_msg(0, "running on %s architecture", tmp);
75    mr_free(tmp);
76    mr_msg(0, "-----------------------------------------------------------");
77    mr_msg(0, "NB: Mondo logs almost everything, so don't panic if you see");
78    mr_msg(0, "some error messages.  Please read them carefully before you");
79    mr_msg(0, "decide to break out in a cold sweat.    Despite (or perhaps");
80    mr_msg(0, "because of) the wealth of messages. some users are inclined");
81    mr_msg(0, "to stop reading this log. If Mondo stopped for some reason,");
82    mr_msg(0, "chances are it's detailed here.  More than likely there's a");
83    mr_msg(0, "message at the very end of this log that will tell you what");
84    mr_msg(0, "is wrong. Please read it!                          -Devteam");
85    mr_msg(0, "-----------------------------------------------------------");
86
87    mr_msg(0, "Zero...");
88    mr_msg(1, "One...");
89    mr_msg(2, "Two...");
90    mr_msg(3, "Three...");
91    mr_msg(4, "Four...");
92    mr_msg(5, "Five...");
93    mr_msg(6, "Six...");
94    mr_msg(7, "Seven...");
95    mr_msg(8, "Eight...");
96    printf("See %s for details of backup run.\n", MONDO_LOGFILE);
97}
98
99/**
100 * Do whatever is necessary to insure a successful backup on the Linux distribution
101 * of the day.
102 */
103static void distro_specific_kludges_at_start_of_mondoarchive(void)
104{
105    mr_msg(2, "Unmounting old ramdisks if necessary");
106    stop_magicdev_if_necessary();   // for RH+Gnome users
107    run_program_and_log_output
108        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
109    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
110    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
111    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
112}
113
114
115/**
116 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
117 */
118static void distro_specific_kludges_at_end_of_mondoarchive(void)
119{
120    mr_msg(2, "Restarting magicdev if necessary");
121    sync();
122    restart_magicdev_if_necessary();    // for RH+Gnome users
123
124    mr_msg(2, "Restarting supermounts if necessary");
125    sync();
126    remount_supermounts_if_necessary(); // for Mandrake users
127
128    mr_msg(2, "Unmounting /boot if necessary");
129    sync();
130    unmount_boot_if_necessary();    // for Gentoo users
131}
132
133/* create the mr_ar_conf structure from mondo's conf file */
134static void mr_ar_store_conf(struct mr_ar_conf *mr_cnf) {
135   
136    mr_asprintf(&mr_cnf->iso_creation_cmd, mr_conf_sread("mondo_iso_creation_cmd"));
137    mr_asprintf(&mr_cnf->iso_creation_options, mr_conf_sread("mondo_iso_creation_options"));
138    mr_asprintf(&mr_cnf->iso_burning_cmd, mr_conf_sread("mondo_iso_burning_cmd"));
139    mr_asprintf(&mr_cnf->iso_burning_options, mr_conf_sread("mondo_iso_burning_options"));
140    mr_cnf->iso_burning_speed = mr_conf_iread("mondo_iso_burning_speed");
141    mr_cnf->media_size = mr_conf_iread("mondo_media_size");
142    mr_asprintf(&mr_cnf->media_device, mr_conf_sread("mondo_media_device"));
143    mr_cnf->manual_tray = mr_conf_bread("mondo_manual_tray");
144    mr_cnf->log_level = mr_conf_iread("mondo_log_level");
145    mr_asprintf(&mr_cnf->prefix, mr_conf_sread("mondo_prefix"));
146    mr_cnf->external_tape_blocksize = mr_conf_iread("mondo_external_tape_blocksize");
147    mr_cnf->internal_tape_blocksize = mr_conf_iread("mondo_internal_tape_blocksize");
148    mr_cnf->slice_size = mr_conf_iread("mondo_slice_size");
149    mr_asprintf(&mr_cnf->deplist_file, mr_conf_sread("mondo_deplist_file"));
150    mr_cnf->write_boot_floppy = mr_conf_bread("mondo_write_boot_floppy");
151    mr_cnf->create_mindi_cd = mr_conf_bread("mondo_create_mindi_cd");
152    mr_asprintf(&mr_cnf->kernel, mr_conf_sread("mondo_kernel"));
153    mr_asprintf(&mr_cnf->additional_modules, mr_conf_sread("mondo_additional_modules"));
154    mr_asprintf(&mr_cnf->boot_loader, mr_conf_sread("mondo_boot_loader"));
155    mr_cnf->differential = mr_conf_bread("mondo_differential");
156    mr_asprintf(&mr_cnf->compression_tool, mr_conf_sread("mondo_compression_tool"));
157    mr_cnf->compression_level = mr_conf_iread("mondo_compression_level");
158    mr_asprintf(&mr_cnf->exclude_paths, mr_conf_sread("mondo_exclude_paths"));
159    mr_asprintf(&mr_cnf->include_paths, mr_conf_sread("mondo_include_paths"));
160    mr_asprintf(&mr_cnf->ui_mode, mr_conf_sread("mondo_ui_mode"));
161    mr_cnf->automatic_restore = mr_conf_bread("mondo_automatic_restore");
162    mr_asprintf(&mr_cnf->scratch_dir, mr_conf_sread("mondo_scratch_dir"));
163    mr_asprintf(&mr_cnf->tmp_dir, mr_conf_sread("mondo_tmp_dir"));
164    mr_asprintf(&mr_cnf->images_dir, mr_conf_sread("mondo_images_dir"));
165    mr_msg(5, "Directory for images is %s", mr_cnf->images_dir);
166}
167
168/* destroy the mr_ar_conf structure's content */
169static void mr_ar_clean_conf(struct mr_ar_conf *mr_cnf) {
170   
171    if (mr_cnf == NULL) {
172        return;
173    }
174    mr_free(mr_cnf->iso_creation_cmd);
175    mr_free(mr_cnf->iso_creation_options);
176    mr_free(mr_cnf->iso_burning_cmd);
177    mr_free(mr_cnf->iso_burning_options);
178    mr_free(mr_cnf->media_device);
179    mr_free(mr_cnf->prefix);
180    mr_free(mr_cnf->deplist_file);
181    mr_free(mr_cnf->kernel);
182    mr_free(mr_cnf->additional_modules);
183    mr_free(mr_cnf->boot_loader);
184    mr_free(mr_cnf->compression_tool);
185    mr_free(mr_cnf->exclude_paths);
186    mr_free(mr_cnf->include_paths);
187    mr_free(mr_cnf->ui_mode);
188    mr_free(mr_cnf->scratch_dir);
189    mr_free(mr_cnf->tmp_dir);
190    mr_free(mr_cnf->images_dir);
191}
192
193/* Create the pointer to the function called in mr_exit */
194void (*mr_cleanup)(void) = NULL;
195
196/* Cleanup all memory allocated in various structures */
197void mr_ar_cleanup(void) {
198    /* Highly incomplete function for the moment */
199    /* We have to free all allocated memory */
200    mr_ar_clean_conf(&mr_conf);
201    /* We have to remove all temporary files */
202    /* We have to unmount what has been mounted */
203    /* We have to properly end newt */
204    /* We have to remind people of log files */
205
206    mr_msg_close();
207}
208
209/*-----------------------------------------------------------*/
210
211
212
213/**
214 * Backup/verify the user's data.
215 * What did you think it did, anyway? :-)
216 */
217int main(int argc, char *argv[])
218{
219    struct s_bkpinfo *bkpinfo = NULL;
220    struct stat stbuf;
221    char *tmp = NULL;
222    int res = 0;
223    int retval = 0;
224    char *say_at_end = NULL;
225    char *say_at_end2 = NULL;
226
227#ifdef ENABLE_NLS
228    setlocale(LC_ALL, "");
229    (void) textdomain("mondo");
230#endif
231    printf(_("Initializing..."));
232
233    /* Reference the right cleanup function for mr_exit */
234    mr_cleanup = &mr_ar_cleanup;
235
236    /* initialize log file with time stamp */
237    /* We start with a loglevel of 4 - Adapted later on */
238    /* It's mandatory to set this up first as all mr_ functions rely on it */
239    unlink(MONDO_LOGFILE);
240    mr_msg_init(MONDO_LOGFILE,4);
241    mr_msg(0, _("Time started: %s"), mr_date());
242
243    /* Make sure I'm root; abort if not */
244    if (getuid() != 0) {
245        mr_log_exit(127, _("Please run as root."));
246    }
247
248    /* If -V, -v or --version then echo version no. and quit */
249    if (argc == 2
250        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
251            || !strcmp(argv[argc - 1], "--version"))) {
252        printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
253        mr_exit(0, NULL);
254    }
255
256    /* Conf file management */
257    /* Check md5 sum before */
258    /* Get content */
259    if (mr_conf_open(MONDO_CONF_DIR"/mondo.conf.dist") != 0) {
260            mr_log_exit(-1, "Unable to open "MONDO_CONF_DIR"/mondo.conf.dist");
261    }
262    mr_ar_store_conf(&mr_conf);
263    mr_conf_close();
264
265    /* Add MONDO_SHARE + other environment variables for mindi */
266    setenv_mondo_var();
267
268    /* Initialize variables */
269    malloc_libmondo_global_strings();
270    diffs = 0;
271    bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
272    if (stat(MONDO_CACHE, &stbuf) != 0) {
273        mr_mkdir(MONDO_CACHE,0x755);
274    }
275
276    /* BERLIOS: Hardcoded to be improved */
277    unlink(MONDO_CACHE"/mindi.conf");
278    unlink(MONDORESTORECFG);
279
280    /* Configure the bkpinfo structure, global file paths, etc. */
281    g_main_pid = getpid();
282    mr_msg(9, "This");
283
284    register_pid(g_main_pid, "mondo");
285    set_signals(TRUE);          // catch SIGTERM, etc.
286    run_program_and_log_output("dmesg -n1", TRUE);
287
288    mr_msg(9, "Next");
289    welcome_to_mondoarchive();
290    distro_specific_kludges_at_start_of_mondoarchive();
291    g_kernel_version = get_kernel_version();
292
293    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
294        g_loglevel = 10;
295        g_text_mode = TRUE;
296        setup_newt_stuff();
297        if (!strstr(argv[2], "filelist")) {
298            mr_msg(1,_("Sorry - filelist goes first\n"));
299            finish(1);
300        } else {
301            finish(get_fattr_list(argv[2], argv[3]));
302        }
303        finish(0);
304    }
305    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
306        g_loglevel = 10;
307        g_text_mode = TRUE;
308        setup_newt_stuff();
309        finish(set_fattr_list(argv[2], argv[3]));
310    }
311
312    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
313        g_loglevel = 10;
314        g_text_mode = TRUE;
315        setup_newt_stuff();
316        tmp = mr_stresc(argv[2], WILDCHARS, BACKSLASH);
317        printf("in=%s; out=%s\n", argv[2], tmp);
318        mr_free(tmp);
319        finish(1);
320    }
321
322    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
323        g_loglevel = 10;
324        g_text_mode = TRUE;
325        setup_newt_stuff();
326        if (!strstr(argv[2], "filelist")) {
327            mr_msg(1,_("Sorry - filelist goes first\n"));
328            finish(1);
329        } else {
330            finish(get_acl_list(argv[2], argv[3]));
331        }
332        finish(0);
333    }
334    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
335        g_loglevel = 10;
336        g_text_mode = TRUE;
337        setup_newt_stuff();
338        finish(set_acl_list(argv[2], argv[3]));
339    }
340
341    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
342        g_loglevel = 10;
343        g_text_mode = TRUE;
344        setup_newt_stuff();
345        malloc_string(tmp);
346        if (find_cdrw_device(tmp)) {
347            mr_msg(1,_("Failed to find CDR-RW drive\n"));
348        } else {
349            mr_msg(1,_("CD-RW is at %s\n"), tmp);
350        }
351        tmp[0] = '\0';
352        if (find_cdrom_device(tmp, atoi(argv[2]))) {
353            mr_msg(1,_("Failed to find CD-ROM drive\n"));
354        } else {
355            mr_msg(1,_("CD-ROM is at %s\n"), tmp);
356        }
357        mr_free(tmp);
358        finish(0);
359    }
360
361    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
362        g_loglevel = 10;
363        g_text_mode = TRUE;
364        setup_newt_stuff();
365        malloc_string(tmp);
366        if (find_dvd_device(tmp, atoi(argv[2]))) {
367            mr_msg(1,_("Failed to find DVD drive\n"));
368        } else {
369            mr_msg(1,_("DVD is at %s\n"), tmp);
370        }
371        mr_free(tmp);
372        finish(0);
373    }
374
375    if (argc > 2 && !strcmp(argv[1], "disksize")) {
376        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
377        finish(0);
378    }
379    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
380        if (is_dev_an_NTFS_dev(argv[2])) {
381            mr_msg(1,_("%s is indeed an NTFS dev\n"), argv[2]);
382        } else {
383            mr_msg(1,_("%s is _not_ an NTFS dev\n"), argv[2]);
384        }
385        finish(0);
386    }
387
388    if (pre_param_configuration(bkpinfo)) {
389        fatal_error
390            ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
391    }
392    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
393            bkpinfo->scratchdir);
394
395    /* Process command line, if there is one. If not, ask user for info. */
396    if (argc == 1) {
397        g_text_mode = FALSE;
398        setup_newt_stuff();
399        res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE);   /* yes, archiving */
400        if (res) {
401            fatal_error
402                ("Syntax error. Please review the parameters you have supplied and try again.");
403        }
404    } else {
405        res = handle_incoming_parameters(argc, argv, bkpinfo);
406        if (res) {
407            mr_msg(1,
408                _("Errors were detected in the command line you supplied.\n"));
409            mr_msg(1,_("Please review the log file - %s \n"),MONDO_LOGFILE);
410            mr_msg(1, "Mondoarchive will now exit.");
411            finish(1);
412        }
413        setup_newt_stuff();
414    }
415
416/* Finish configuring global structures */
417    if (post_param_configuration(bkpinfo)) {
418        fatal_error
419            ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
420    }
421
422    log_to_screen
423        (_("BusyBox's sources are available from http://www.busybox.net"));
424
425    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
426            bkpinfo->scratchdir);
427
428    /* If we're meant to backup then backup */
429    if (bkpinfo->backup_data) {
430        res = backup_data(bkpinfo);
431        retval += res;
432        if (res) {
433            mr_strcat(say_at_end,
434                   _("Data archived. Please check the logs, just as a precaution. "));
435        } else {
436            mr_strcat(say_at_end, _("Data archived OK. "));
437        }
438    }
439
440    /* If we're meant to verify then verify */
441    if (bkpinfo->verify_data) {
442        res = verify_data(bkpinfo);
443        if (res < 0) {
444            mr_asprintf(&say_at_end2, _("%d difference%c found."), -res,
445                    (-res != 1) ? 's' : ' ');
446            res = 0;
447        }
448        retval += res;
449    }
450
451    /* Offer to write floppy disk images to physical disks */
452    if (bkpinfo->backup_data && !g_skip_floppies) {
453        res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
454        retval += res;
455    }
456
457    /* Report result of entire operation (success? errors?) */
458    if (!retval) {
459        mvaddstr_and_log_it(g_currentY++, 0,
460                            _("Backup and/or verify ran to completion. Everything appears to be fine."));
461    } else {
462        mvaddstr_and_log_it(g_currentY++, 0,
463                            _("Backup and/or verify ran to completion. However, errors did occur."));
464    }
465
466    if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
467        log_to_screen
468            (_("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it."));
469    }
470
471
472    if (length_of_file("/tmp/changed.files") > 2) {
473        if (g_text_mode) {
474            log_to_screen
475                (_("Type 'less /tmp/changed.files' to see which files don't match the archives"));
476        } else {
477            mr_msg(1,
478                    _("Type 'less /tmp/changed.files' to see which files don't match the archives"));
479            mr_msg(2, "Calling popup_changelist_from_file()");
480            popup_changelist_from_file("/tmp/changed.files");
481            mr_msg(2, "Returned from popup_changelist_from_file()");
482        }
483    } else {
484        unlink("/tmp/changed.files");
485    }
486    log_to_screen(say_at_end);
487    mr_free(say_at_end);
488    if (say_at_end2 != NULL) {
489        log_to_screen(say_at_end2);
490        mr_free(say_at_end2);
491    }
492
493    mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
494    run_program_and_log_output(tmp, TRUE);
495    mr_free(tmp);
496    run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
497
498    run_program_and_log_output("mount", 2);
499
500    system("rm -f /var/cache/mondo-archive/last-backup.aborted");
501    system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
502    if (!retval) {
503        printf(_("Mondoarchive ran OK.\n"));
504    } else {
505        printf(_("Errors occurred during backup. Please check logfile.\n"));
506    }
507    distro_specific_kludges_at_end_of_mondoarchive();
508    register_pid(0, "mondo");
509    set_signals(FALSE);
510    chdir("/tmp");              // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
511    system(g_erase_tmpdir_and_scratchdir);
512    free_libmondo_global_strings();
513    mr_free(bkpinfo);
514
515    unlink("/tmp/filelist.full");
516    unlink("/tmp/filelist.full.gz");
517
518    if (!g_cdrom_drive_is_here) {
519        mr_msg(10, "FYI, g_cdrom_drive_is_here was never used");
520    }
521    if (!g_dvd_drive_is_here) {
522        mr_msg(10, "FYI, g_dvd_drive_is_here was never used");
523    }
524
525    /* finalize log file with time stamp */
526    mr_msg(1, "Time finished: %s", mr_date());
527    mr_msg_close();
528
529    if (!g_text_mode) {
530        popup_and_OK
531            (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
532        log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
533        finish(retval);
534    } else {
535        printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
536        mr_exit(retval, NULL);
537    }
538
539    return EXIT_SUCCESS;
540}
Note: See TracBrowser for help on using the repository browser.