source: branches/3.1/mondo/src/mondoarchive/mondoarchive.c @ 3148

Last change on this file since 3148 was 3148, checked in by Bruno Cornec, 7 years ago

2nd phase for svn merge -r 2935:3146 ../3.0

  • Property svn:keywords set to Id
File size: 13.0 KB
Line 
1/***************************************************************************
2$Id: mondoarchive.c 3148 2013-06-19 06:50:28Z bruno $
3* The main file for mondoarchive.
4*/
5
6/************************* #include statements *************************/
7#include <pthread.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include "my-stuff.h"
11#include "mr_mem.h"
12#include "mr_str.h"
13#include "../common/mondostructures.h"
14#include "../common/libmondo.h"
15#include "../common/libmondo-cli-EXT.h"
16#include "../common/libmondo-tools-EXT.h"
17#include "mondoarchive.h"
18
19// for CVS
20//static char cvsid[] = "$Id: mondoarchive.c 3148 2013-06-19 06:50:28Z bruno $";
21
22/************************* external variables *************************/
23extern void set_signals(int);
24extern int g_current_media_number;
25extern int g_currentY;
26extern bool g_text_mode;
27extern char *g_boot_mountpt;
28extern bool g_remount_floppy_at_end;
29extern char *g_cdrw_drive_is_here;
30static char *g_cdrom_drive_is_here = NULL;
31static char *g_dvd_drive_is_here = NULL;
32extern double g_kernel_version;
33
34/***************** global vars, used only by main.c ******************/
35long diffs = 0L;
36
37extern t_bkptype g_backup_media_type;
38extern int g_loglevel;
39extern char *g_magicdev_command;
40
41/**
42 * Whether we're restoring from ISOs. Obviously not, since this is the
43 * backup program.
44 * @note You @b MUST declare this variable somewhere in your program if
45 * you use libmondo. Otherwise the link will fail.
46 * @ingroup globalGroup
47 */
48bool g_ISO_restore_mode = FALSE;
49
50/* Whether we should fail immediately at first error */
51bool g_fail_immediately = FALSE;
52
53/* Do we use extended attributes and acl ?
54 *  * By default no, use --acl & --attr options to force their usage */
55char *g_getfacl = NULL;
56char *g_getfattr = NULL;
57
58/* Reference to global bkpinfo */
59struct s_bkpinfo *bkpinfo;
60
61/*  Make cleanup the finish function */
62void (*mr_cleanup)(int) = finish;
63
64/* To be coded */
65void free_MR_global_filenames(void) {
66}
67
68/****************** subroutines used only by main.c ******************/
69
70
71/**
72 * Print a "don't panic" message to the log and a message about the logfile to the screen.
73 */
74void welcome_to_mondoarchive(void)
75{
76    char *tmp = NULL;
77
78    log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org", PACKAGE_VERSION);
79    log_msg(0, "running %s binaries", get_architecture());
80    tmp = get_uname_m();
81    log_msg(0, "running on %s architecture", tmp);
82    mr_free(tmp);
83    log_msg(0, "-----------------------------------------------------------");
84    log_msg(0, "NB: Mondo logs almost everything, so don't panic if you see");
85    log_msg(0, "some error messages.  Please read them carefully before you");
86    log_msg(0, "decide to break out in a cold sweat.    Despite (or perhaps");
87    log_msg(0, "because of) the wealth of messages. some users are inclined");
88    log_msg(0, "to stop reading this log. If Mondo stopped for some reason,");
89    log_msg(0, "chances are it's detailed here.  More than likely there's a");
90    log_msg(0, "message at the very end of this log that will tell you what");
91    log_msg(0, "is wrong. Please read it!                          -Devteam");
92    log_msg(0, "-----------------------------------------------------------");
93
94    log_msg(0, "Zero...");
95    log_msg(1, "One...");
96    log_msg(2, "Two...");
97    log_msg(3, "Three...");
98    log_msg(4, "Four...");
99    log_msg(5, "Five...");
100    log_msg(6, "Six...");
101    log_msg(7, "Seven...");
102    log_msg(8, "Eight...");
103}
104
105
106/**
107 * Do whatever is necessary to insure a successful backup on the Linux distribution
108 * of the day.
109 */
110void distro_specific_kludges_at_start_of_mondoarchive(void)
111{
112    log_msg(2, "Unmounting old ramdisks if necessary");
113    stop_magicdev_if_necessary();   // for RH+Gnome users
114    /*
115    run_program_and_log_output
116        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
117        */
118    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
119    //  stop_autofs_if_necessary(); // for Xandros users
120    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
121    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
122}
123
124
125
126/**
127 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
128 */
129void distro_specific_kludges_at_end_of_mondoarchive(void)
130{
131    log_msg(2, "Restarting magicdev if necessary");
132    sync();
133    restart_magicdev_if_necessary();    // for RH+Gnome users
134
135    log_msg(2, "Restarting autofs if necessary");
136    sync();
137    //  restart_autofs_if_necessary(); // for Xandros users
138
139    log_msg(2, "Restarting supermounts if necessary");
140    sync();
141    remount_supermounts_if_necessary(); // for Mandrake users
142
143    log_msg(2, "Unmounting /boot if necessary");
144    sync();
145    unmount_boot_if_necessary();    // for Gentoo users
146
147//  log_msg( 2, "Cleaning up KDE desktop");
148//  clean_up_KDE_desktop_if_necessary();
149}
150
151
152/**
153 * Backup/verify the user's data.
154 * What did you think it did, anyway? :-)
155 */
156int main(int argc, char *argv[])
157{
158    char *tmp = NULL;
159    char *tmp1 = NULL;
160    int res = 0;
161    int i = 0;
162    int retval = 0;
163    char *say_at_end = NULL;
164    FILE *fin = NULL;
165
166    printf("Initializing...\n");
167
168    /* initialize log file with time stamp */
169    unlink(MONDO_LOGFILE);
170    tmp = mr_date();
171    log_msg(0, "Time started: %s", tmp);
172    mr_free(tmp);
173
174    init_bkpinfo();
175
176    /* Memory allocation is done in those functions */
177    malloc_libmondo_global_strings();
178    if (argc == 1) {
179        g_text_mode = FALSE;
180    } else {
181        g_text_mode = TRUE;
182        /* newt mode if -g specified on cli */
183        for (i = 0; i < argc; i++) {
184            if (!strcmp(argv[i],"-g")) {
185                g_text_mode = FALSE;
186            }
187        }
188    }
189    setup_newt_stuff();
190
191    /* Make sure I'm root; abort if not */
192    if (getuid() != 0) {
193        fatal_error("Please run as root.\n");
194    }
195
196    /* If -V, -v or --version then echo version no. and quit */
197    if (argc == 2 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V") || !strcmp(argv[argc - 1], "--version"))) {
198        printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
199        finish(0);
200    }
201
202
203    /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
204    mr_asprintf(tmp1,"%s:/sbin:/usr/sbin:/usr/local/sbin",getenv("PATH"));
205    setenv("PATH", tmp1, 1);
206    mr_free(tmp1);
207
208    /* Add the ARCH environment variable for ia64 purposes */
209    mr_asprintf(tmp1,"%s",get_architecture());
210    setenv("ARCH", tmp1, 1);
211    mr_free(tmp1);
212
213    /* Add MONDO_SHARE environment variable for mindi */
214    setenv_mondo_share();
215
216    /* Configure the bkpinfo structure, global file paths, etc. */
217    g_main_pid = getpid();
218    log_msg(9, "This");
219
220    set_signals(TRUE);          // catch SIGTERM, etc.
221    run_program_and_log_output("dmesg -n1", TRUE);
222
223    log_msg(9, "Next");
224    make_hole_for_dir(MONDO_CACHE);
225
226    welcome_to_mondoarchive();
227    distro_specific_kludges_at_start_of_mondoarchive();
228    g_kernel_version = get_kernel_version();
229
230    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
231        g_loglevel = 10;
232        if (!strstr(argv[2], "filelist")) {
233            printf("Sorry - filelist goes first\n");
234            finish(1);
235        } else {
236            finish(get_fattr_list(argv[2], argv[3]));
237        }
238        finish(0);
239    }
240    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
241        g_loglevel = 10;
242        finish(set_fattr_list(argv[2], argv[3]));
243    }
244
245    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
246        g_loglevel = 10;
247        tmp = mr_stresc(argv[2], "[]*?", '\\');
248        printf("in=%s; out=%s\n", argv[2], tmp);
249        mr_free(tmp);
250        finish(1);
251    }
252
253    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
254        g_loglevel = 10;
255        if (!strstr(argv[2], "filelist")) {
256            printf("Sorry - filelist goes first\n");
257            finish(1);
258        } else {
259            finish(get_acl_list(argv[2], argv[3]));
260        }
261        finish(0);
262    }
263    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
264        g_loglevel = 10;
265        finish(set_acl_list(argv[2], argv[3]));
266    }
267    if (argc >= 2 && !strcmp(argv[1], "mkraidtab")) {
268        g_loglevel = 10;
269        g_text_mode = TRUE;
270        setup_newt_stuff();
271#undef MDSTAT_FILE
272#define MDSTAT_FILE "/tmp/mdstat"
273        if (!(fin = fopen(MDSTAT_FILE, "r"))) {
274            log_msg(1, "Could not open %s.\n", MDSTAT_FILE);
275                finish(1);
276        }
277
278        create_raidtab_from_mdstat(MDSTAT_FILE,"/tmp/raidtab");
279        finish(0);
280    }
281    if (argc >= 2 && !strcmp(argv[1], "mkraidtab")) {
282        g_loglevel = 10;
283        g_text_mode = TRUE;
284        setup_newt_stuff();
285#undef MDSTAT_FILE
286#define MDSTAT_FILE "/tmp/mdstat"
287        if (!(fin = fopen(MDSTAT_FILE, "r"))) {
288            log_msg(1, "Could not open %s.\n", MDSTAT_FILE);
289                finish(1);
290        }
291
292        create_raidtab_from_mdstat(MDSTAT_FILE,"/tmp/raidtab");
293        finish(0);
294    }
295
296    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
297        g_loglevel = 10;
298        if ((tmp = find_cdrw_device()) == NULL) {
299            printf("Failed to find CDR-RW drive\n");
300        } else {
301            printf("CD-RW is at %s\n", tmp);
302        }
303        mr_free(tmp);
304        if ((tmp = find_cdrom_device(FALSE)) == NULL) {
305            printf("Failed to find CD-ROM drive\n");
306        } else {
307            printf("CD-ROM is at %s\n", tmp);
308        }
309        mr_free(tmp);
310        finish(0);
311    }
312
313    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
314        g_loglevel = 10;
315        if ((tmp = find_dvd_device()) == NULL) {
316            printf("Failed to find DVD drive\n");
317        } else {
318            printf("DVD is at %s\n", tmp);
319        }
320        mr_free(tmp);
321        finish(0);
322    }
323
324    if (argc > 2 && !strcmp(argv[1], "disksize")) {
325        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
326        finish(0);
327    }
328    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
329        if (is_dev_an_NTFS_dev(argv[2])) {
330            printf("%s is indeed an NTFS dev\n", argv[2]);
331        } else {
332            printf("%s is _not_ an NTFS dev\n", argv[2]);
333        }
334        finish(0);
335    }
336
337    if (pre_param_configuration()) {
338        fatal_error("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
339    }
340
341    /* Process command line, if there is one. If not, ask user for info. */
342    if (argc == 1) {
343        res = interactively_obtain_media_parameters_from_user(TRUE);    /* yes, archiving */
344        if (res) {
345            fatal_error("Syntax error. Please review the parameters you have supplied and try again.");
346        }
347    } else {
348        res = handle_incoming_parameters(argc, argv);
349        if (res) {
350            printf("Errors were detected in the command line you supplied.\n");
351            printf("Please review the log file - %s\n", MONDO_LOGFILE );
352            log_msg(1, "Mondoarchive will now exit.");
353            finish(1);
354        }
355    }
356
357/* Finish configuring global structures */
358    if (post_param_configuration()) {
359        fatal_error("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
360    }
361
362    /* If we're meant to backup then backup */
363    if (bkpinfo->backup_data) {
364        res = backup_data();
365        retval += res;
366        if (res) {
367            mr_asprintf(say_at_end, "Data archived. Please check the logs, just as a precaution. ");
368        } else {
369            mr_asprintf(say_at_end, "Data archived OK. ");
370        }
371    }
372
373/* If we're meant to verify then verify */
374    if (bkpinfo->verify_data) {
375        res = verify_data();
376        if (res < 0) {
377            mr_asprintf(tmp, "%d difference%c found.", -res, (-res != 1) ? 's' : ' ');
378            mr_asprintf(say_at_end, "%s", tmp);
379            log_to_screen(tmp);
380            mr_free(tmp);
381            res = 0;
382        }
383        retval += res;
384    }
385
386/* Report result of entire operation (success? errors?) */
387    if (!retval) {
388        mvaddstr_and_log_it(g_currentY++, 0,
389                            "Backup and/or verify ran to completion. Everything appears to be fine.");
390    } else {
391        mvaddstr_and_log_it(g_currentY++, 0,
392                            "Backup and/or verify ran to completion. However, errors did occur.");
393    }
394
395    if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
396        log_to_screen
397            (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
398    }
399
400    if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
401        if (g_text_mode) {
402            log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
403        } else {
404            log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
405            log_msg(2, "Calling popup_changelist_from_file()");
406            popup_changelist_from_file(MONDO_CACHE"/changed.files");
407            log_msg(2, "Returned from popup_changelist_from_file()");
408        }
409    } else {
410        unlink(MONDO_CACHE"/changed.files");
411    }
412    if (say_at_end != NULL) {
413        log_to_screen(say_at_end);
414        mr_free(say_at_end);
415    }
416    mr_asprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
417    run_program_and_log_output(tmp, TRUE);
418    mr_free(tmp);
419    if (bkpinfo->backup_media_type == usb) {
420        log_msg(1, "Unmounting USB device.");
421        if (bkpinfo->media_device == NULL) {
422            fatal_error("USB device set to NULL");
423        }
424        mr_asprintf(tmp, "umount %s1", bkpinfo->media_device);
425        run_program_and_log_output(tmp, TRUE);
426        mr_free(tmp);
427    }
428
429    run_program_and_log_output("mount", 2);
430
431    paranoid_system("rm -f "MONDO_CACHE"/last-backup.aborted");
432    if (!retval) {
433        printf("Mondoarchive ran OK.\n");
434    } else {
435        printf("Errors occurred during backup. Please check logfile.\n");
436    }
437    distro_specific_kludges_at_end_of_mondoarchive();
438    set_signals(FALSE);
439
440    free_libmondo_global_strings();
441   
442
443    if (!g_cdrom_drive_is_here) {
444        log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
445    }
446    if (!g_dvd_drive_is_here) {
447        log_msg(10, "FYI, g_dvd_drive_is_here was never used");
448    }
449
450    /* finalize log file with time stamp */
451    tmp = mr_date();
452    log_msg(0, "Time finished: %s", tmp);
453    mr_free(tmp);
454
455    if (chdir("/tmp")) {
456        // FIXME
457    }
458
459    if (!g_text_mode) {
460        popup_and_OK("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
461        log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
462    } else {
463        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
464    }
465    finish(retval);
466
467    return EXIT_SUCCESS;
468}
Note: See TracBrowser for help on using the repository browser.