source: MondoRescue/branches/3.1/mondo/src/mondoarchive/mondoarchive.c

Last change on this file was 3193, checked in by Bruno Cornec, 8 years ago
  • Finish with backports from 3.1 for now. Still some work to do, but we will now make that version compile and work again and serve as a base

so the gettext patch can be added

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