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
RevLine 
[1]1/***************************************************************************
[2211]2$Id: mondoarchive.c 3147 2013-06-19 06:34:46Z 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 3147 2013-06-19 06:34:46Z 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;
[2340]39extern char *g_magicdev_command;
[1]40
[1917]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
[3147]50/* Whether we should fail immediately at first error */
51bool g_fail_immediately = FALSE;
52
[1917]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
[1645]58/* Reference to global bkpinfo */
59struct s_bkpinfo *bkpinfo;
60
[2320]61/*  Make cleanup the finish function */
62void (*mr_cleanup)(int) = finish;
[2211]63
[1967]64/* To be coded */
65void free_MR_global_filenames(void) {
66}
67
[1]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 */
[1236]74void welcome_to_mondoarchive(void)
[1]75{
[1375]76    char *tmp = NULL;
77
[2340]78    log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org", PACKAGE_VERSION);
[1375]79    log_msg(0, "running %s binaries", get_architecture());
80    tmp = get_uname_m();
81    log_msg(0, "running on %s architecture", tmp);
[2241]82    mr_free(tmp);
[2340]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, "-----------------------------------------------------------");
[1]93
[128]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...");
[1]103}
104
105
106/**
107 * Do whatever is necessary to insure a successful backup on the Linux distribution
108 * of the day.
109 */
[1236]110void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]111{
[128]112    log_msg(2, "Unmounting old ramdisks if necessary");
113    stop_magicdev_if_necessary();   // for RH+Gnome users
[2030]114    /*
[128]115    run_program_and_log_output
116        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
[2030]117        */
[128]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
[1]122}
123
124
125
126/**
127 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
128 */
[1236]129void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]130{
[128]131    log_msg(2, "Restarting magicdev if necessary");
132    sync();
133    restart_magicdev_if_necessary();    // for RH+Gnome users
[1]134
[128]135    log_msg(2, "Restarting autofs if necessary");
136    sync();
137    //  restart_autofs_if_necessary(); // for Xandros users
[1]138
[128]139    log_msg(2, "Restarting supermounts if necessary");
140    sync();
141    remount_supermounts_if_necessary(); // for Mandrake users
[1]142
[128]143    log_msg(2, "Unmounting /boot if necessary");
144    sync();
145    unmount_boot_if_necessary();    // for Gentoo users
[1]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 */
[128]156int main(int argc, char *argv[])
[1]157{
[2241]158    char *tmp = NULL;
[2113]159    char *tmp1 = NULL;
[2322]160    int res = 0;
161    int i = 0;
162    int retval = 0;
[2211]163    char *say_at_end = NULL;
[3147]164    FILE *fin = NULL;
[1]165
[2321]166    printf("Initializing...\n");
167
[810]168    /* initialize log file with time stamp */
169    unlink(MONDO_LOGFILE);
[2421]170    tmp = mr_date();
171    log_msg(0, "Time started: %s", tmp);
172    mr_free(tmp);
[1]173
[2421]174    init_bkpinfo();
175
[2321]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;
[2322]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        }
[2321]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
[807]203    /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[2323]204    mr_asprintf(tmp1,"%s:/sbin:/usr/sbin:/usr/local/sbin",getenv("PATH"));
[2113]205    setenv("PATH", tmp1, 1);
[2321]206    mr_free(tmp1);
[1]207
[807]208    /* Add the ARCH environment variable for ia64 purposes */
[2323]209    mr_asprintf(tmp1,"%s",get_architecture());
[2113]210    setenv("ARCH", tmp1, 1);
[2321]211    mr_free(tmp1);
[1]212
[424]213    /* Add MONDO_SHARE environment variable for mindi */
214    setenv_mondo_share();
[258]215
[807]216    /* Configure the bkpinfo structure, global file paths, etc. */
[128]217    g_main_pid = getpid();
218    log_msg(9, "This");
[1]219
[128]220    set_signals(TRUE);          // catch SIGTERM, etc.
221    run_program_and_log_output("dmesg -n1", TRUE);
[1]222
[128]223    log_msg(9, "Next");
[1747]224    make_hole_for_dir(MONDO_CACHE);
225
[128]226    welcome_to_mondoarchive();
227    distro_specific_kludges_at_start_of_mondoarchive();
228    g_kernel_version = get_kernel_version();
[1]229
[128]230    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
231        g_loglevel = 10;
232        if (!strstr(argv[2], "filelist")) {
[541]233            printf("Sorry - filelist goes first\n");
[128]234            finish(1);
235        } else {
236            finish(get_fattr_list(argv[2], argv[3]));
237        }
238        finish(0);
[1]239    }
[128]240    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
241        g_loglevel = 10;
242        finish(set_fattr_list(argv[2], argv[3]));
[1]243    }
[128]244
245    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
246        g_loglevel = 10;
[2413]247        tmp = mr_stresc(argv[2], "[]*?", '\\');
[128]248        printf("in=%s; out=%s\n", argv[2], tmp);
[2413]249        mr_free(tmp);
[128]250        finish(1);
[1]251    }
[128]252
253    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
254        g_loglevel = 10;
255        if (!strstr(argv[2], "filelist")) {
[541]256            printf("Sorry - filelist goes first\n");
[128]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    }
[3147]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        }
[1]277
[3147]278        create_raidtab_from_mdstat(MDSTAT_FILE,"/tmp/raidtab");
279        finish(0);
280    }
281
[128]282    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
283        g_loglevel = 10;
[2325]284        if ((tmp = find_cdrw_device()) == NULL) {
[541]285            printf("Failed to find CDR-RW drive\n");
[128]286        } else {
[541]287            printf("CD-RW is at %s\n", tmp);
[128]288        }
[2325]289        mr_free(tmp);
290        if ((tmp = find_cdrom_device(FALSE)) == NULL) {
[541]291            printf("Failed to find CD-ROM drive\n");
[128]292        } else {
[541]293            printf("CD-ROM is at %s\n", tmp);
[128]294        }
[2325]295        mr_free(tmp);
[128]296        finish(0);
297    }
[1]298
[128]299    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
300        g_loglevel = 10;
[2325]301        if ((tmp = find_dvd_device()) == NULL) {
[541]302            printf("Failed to find DVD drive\n");
[128]303        } else {
[541]304            printf("DVD is at %s\n", tmp);
[128]305        }
[2325]306        mr_free(tmp);
[128]307        finish(0);
308    }
[1]309
[128]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])) {
[541]316            printf("%s is indeed an NTFS dev\n", argv[2]);
[128]317        } else {
[541]318            printf("%s is _not_ an NTFS dev\n", argv[2]);
[128]319        }
320        finish(0);
321    }
322
[1645]323    if (pre_param_configuration()) {
[2340]324        fatal_error("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
[128]325    }
326
[2340]327    /* Process command line, if there is one. If not, ask user for info. */
[128]328    if (argc == 1) {
[1645]329        res = interactively_obtain_media_parameters_from_user(TRUE);    /* yes, archiving */
[128]330        if (res) {
[2340]331            fatal_error("Syntax error. Please review the parameters you have supplied and try again.");
[128]332        }
333    } else {
[1645]334        res = handle_incoming_parameters(argc, argv);
[128]335        if (res) {
[2340]336            printf("Errors were detected in the command line you supplied.\n");
[1319]337            printf("Please review the log file - %s\n", MONDO_LOGFILE );
[128]338            log_msg(1, "Mondoarchive will now exit.");
339            finish(1);
340        }
[1]341    }
342
343/* Finish configuring global structures */
[1645]344    if (post_param_configuration()) {
[2321]345        fatal_error("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
[128]346    }
[1]347
[128]348    /* If we're meant to backup then backup */
349    if (bkpinfo->backup_data) {
[1645]350        res = backup_data();
[128]351        retval += res;
352        if (res) {
[2323]353            mr_asprintf(say_at_end, "Data archived. Please check the logs, just as a precaution. ");
[128]354        } else {
[2323]355            mr_asprintf(say_at_end, "Data archived OK. ");
[128]356        }
357    }
[1]358
359/* If we're meant to verify then verify */
[128]360    if (bkpinfo->verify_data) {
[1645]361        res = verify_data();
[128]362        if (res < 0) {
[2324]363            mr_asprintf(tmp, "%d difference%c found.", -res, (-res != 1) ? 's' : ' ');
[2323]364            mr_asprintf(say_at_end, "%s", tmp);
[128]365            log_to_screen(tmp);
[2241]366            mr_free(tmp);
[128]367            res = 0;
368        }
369        retval += res;
370    }
[1]371
372/* Report result of entire operation (success? errors?) */
[128]373    if (!retval) {
374        mvaddstr_and_log_it(g_currentY++, 0,
[541]375                            "Backup and/or verify ran to completion. Everything appears to be fine.");
[128]376    } else {
377        mvaddstr_and_log_it(g_currentY++, 0,
[541]378                            "Backup and/or verify ran to completion. However, errors did occur.");
[128]379    }
[1]380
[1437]381    if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
[128]382        log_to_screen
[1437]383            (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
[128]384    }
[1]385
[1747]386    if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
[128]387        if (g_text_mode) {
[1747]388            log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]389        } else {
[1747]390            log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]391            log_msg(2, "Calling popup_changelist_from_file()");
[1747]392            popup_changelist_from_file(MONDO_CACHE"/changed.files");
[128]393            log_msg(2, "Returned from popup_changelist_from_file()");
394        }
395    } else {
[1747]396        unlink(MONDO_CACHE"/changed.files");
[128]397    }
[2211]398    if (say_at_end != NULL) {
399        log_to_screen(say_at_end);
[2321]400        mr_free(say_at_end);
[2211]401    }
[2323]402    mr_asprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
[128]403    run_program_and_log_output(tmp, TRUE);
[2241]404    mr_free(tmp);
[1697]405    if (bkpinfo->backup_media_type == usb) {
406        log_msg(1, "Unmounting USB device.");
[2325]407        if (bkpinfo->media_device == NULL) {
408            fatal_error("USB device set to NULL");
409        }
[2323]410        mr_asprintf(tmp, "umount %s1", bkpinfo->media_device);
[1697]411        run_program_and_log_output(tmp, TRUE);
[2241]412        mr_free(tmp);
[1697]413    }
[1]414
[128]415    run_program_and_log_output("mount", 2);
[1]416
[3147]417    paranoid_system("rm -f "MONDO_CACHE"/last-backup.aborted");
[128]418    if (!retval) {
[541]419        printf("Mondoarchive ran OK.\n");
[128]420    } else {
[541]421        printf("Errors occurred during backup. Please check logfile.\n");
[128]422    }
423    distro_specific_kludges_at_end_of_mondoarchive();
424    set_signals(FALSE);
[1644]425
[128]426    free_libmondo_global_strings();
[1703]427   
[1]428
[128]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    }
[1]435
[810]436    /* finalize log file with time stamp */
[2421]437    tmp = mr_date();
438    log_msg(0, "Time finished: %s", tmp);
439    mr_free(tmp);
[810]440
[3147]441    if (chdir("/tmp")) {
442        // FIXME
443    }
[1644]444
[128]445    if (!g_text_mode) {
[2322]446        popup_and_OK("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
[541]447        log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
[128]448    } else {
[541]449        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[128]450    }
[1703]451    finish(retval);
[128]452
453    return EXIT_SUCCESS;
[1]454}
Note: See TracBrowser for help on using the repository browser.