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

Last change on this file since 1384 was 1384, checked in by Bruno Cornec, 13 years ago

Typos again

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