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

Last change on this file since 3147 was 3147, checked in by Bruno Cornec, 7 years ago
  • First pass on svn merge -r 2935:3146 ../3.0
  • Property svn:keywords set to Id
File size: 12.7 KB
Line 
1/***************************************************************************
2$Id: mondoarchive.c 3147 2013-06-19 06:34:46Z 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 3147 2013-06-19 06:34:46Z 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
282    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
283        g_loglevel = 10;
284        if ((tmp = find_cdrw_device()) == NULL) {
285            printf("Failed to find CDR-RW drive\n");
286        } else {
287            printf("CD-RW is at %s\n", tmp);
288        }
289        mr_free(tmp);
290        if ((tmp = find_cdrom_device(FALSE)) == NULL) {
291            printf("Failed to find CD-ROM drive\n");
292        } else {
293            printf("CD-ROM is at %s\n", tmp);
294        }
295        mr_free(tmp);
296        finish(0);
297    }
298
299    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
300        g_loglevel = 10;
301        if ((tmp = find_dvd_device()) == NULL) {
302            printf("Failed to find DVD drive\n");
303        } else {
304            printf("DVD is at %s\n", tmp);
305        }
306        mr_free(tmp);
307        finish(0);
308    }
309
310    if (argc > 2 && !strcmp(argv[1], "disksize")) {
311        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
312        finish(0);
313    }
314    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
315        if (is_dev_an_NTFS_dev(argv[2])) {
316            printf("%s is indeed an NTFS dev\n", argv[2]);
317        } else {
318            printf("%s is _not_ an NTFS dev\n", argv[2]);
319        }
320        finish(0);
321    }
322
323    if (pre_param_configuration()) {
324        fatal_error("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
325    }
326
327    /* Process command line, if there is one. If not, ask user for info. */
328    if (argc == 1) {
329        res = interactively_obtain_media_parameters_from_user(TRUE);    /* yes, archiving */
330        if (res) {
331            fatal_error("Syntax error. Please review the parameters you have supplied and try again.");
332        }
333    } else {
334        res = handle_incoming_parameters(argc, argv);
335        if (res) {
336            printf("Errors were detected in the command line you supplied.\n");
337            printf("Please review the log file - %s\n", MONDO_LOGFILE );
338            log_msg(1, "Mondoarchive will now exit.");
339            finish(1);
340        }
341    }
342
343/* Finish configuring global structures */
344    if (post_param_configuration()) {
345        fatal_error("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
346    }
347
348    /* If we're meant to backup then backup */
349    if (bkpinfo->backup_data) {
350        res = backup_data();
351        retval += res;
352        if (res) {
353            mr_asprintf(say_at_end, "Data archived. Please check the logs, just as a precaution. ");
354        } else {
355            mr_asprintf(say_at_end, "Data archived OK. ");
356        }
357    }
358
359/* If we're meant to verify then verify */
360    if (bkpinfo->verify_data) {
361        res = verify_data();
362        if (res < 0) {
363            mr_asprintf(tmp, "%d difference%c found.", -res, (-res != 1) ? 's' : ' ');
364            mr_asprintf(say_at_end, "%s", tmp);
365            log_to_screen(tmp);
366            mr_free(tmp);
367            res = 0;
368        }
369        retval += res;
370    }
371
372/* Report result of entire operation (success? errors?) */
373    if (!retval) {
374        mvaddstr_and_log_it(g_currentY++, 0,
375                            "Backup and/or verify ran to completion. Everything appears to be fine.");
376    } else {
377        mvaddstr_and_log_it(g_currentY++, 0,
378                            "Backup and/or verify ran to completion. However, errors did occur.");
379    }
380
381    if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
382        log_to_screen
383            (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
384    }
385
386    if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
387        if (g_text_mode) {
388            log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
389        } else {
390            log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
391            log_msg(2, "Calling popup_changelist_from_file()");
392            popup_changelist_from_file(MONDO_CACHE"/changed.files");
393            log_msg(2, "Returned from popup_changelist_from_file()");
394        }
395    } else {
396        unlink(MONDO_CACHE"/changed.files");
397    }
398    if (say_at_end != NULL) {
399        log_to_screen(say_at_end);
400        mr_free(say_at_end);
401    }
402    mr_asprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
403    run_program_and_log_output(tmp, TRUE);
404    mr_free(tmp);
405    if (bkpinfo->backup_media_type == usb) {
406        log_msg(1, "Unmounting USB device.");
407        if (bkpinfo->media_device == NULL) {
408            fatal_error("USB device set to NULL");
409        }
410        mr_asprintf(tmp, "umount %s1", bkpinfo->media_device);
411        run_program_and_log_output(tmp, TRUE);
412        mr_free(tmp);
413    }
414
415    run_program_and_log_output("mount", 2);
416
417    paranoid_system("rm -f "MONDO_CACHE"/last-backup.aborted");
418    if (!retval) {
419        printf("Mondoarchive ran OK.\n");
420    } else {
421        printf("Errors occurred during backup. Please check logfile.\n");
422    }
423    distro_specific_kludges_at_end_of_mondoarchive();
424    set_signals(FALSE);
425
426    free_libmondo_global_strings();
427   
428
429    if (!g_cdrom_drive_is_here) {
430        log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
431    }
432    if (!g_dvd_drive_is_here) {
433        log_msg(10, "FYI, g_dvd_drive_is_here was never used");
434    }
435
436    /* finalize log file with time stamp */
437    tmp = mr_date();
438    log_msg(0, "Time finished: %s", tmp);
439    mr_free(tmp);
440
441    if (chdir("/tmp")) {
442        // FIXME
443    }
444
445    if (!g_text_mode) {
446        popup_and_OK("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
447        log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
448    } else {
449        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
450    }
451    finish(retval);
452
453    return EXIT_SUCCESS;
454}
Note: See TracBrowser for help on using the repository browser.