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

Last change on this file was 1977, checked in by Bruno Cornec, 16 years ago

svn merge -r 1938:1976 $SVN_M/branches/2.2.6

  • Property svn:keywords set to Id
File size: 33.1 KB
RevLine 
[1]1/***************************************************************************
[1080]2* $Id: mondoarchive.c 1977 2008-06-02 08:49:01Z bruno $
3*/
[1]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>
[1100]15#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
[1549]18#include <locale.h>
[1100]19
[1458]20#include "mr_gettext.h"
[1256]21#include "mondoarchive.h"
[1187]22#include "mr_mem.h"
[1087]23#include "mr_str.h"
[1112]24#include "mr_msg.h"
[1102]25#include "mr_file.h"
[1264]26#include "mr_err.h"
[1256]27#include "mr_conf.h"
[1087]28
[1458]29#include "my-stuff.h"
30#include "mondostructures.h"
31#include "libmondo.h"
32#include "mondo-cli-EXT.h"
33
[1]34// for CVS
[128]35//static char cvsid[] = "$Id: mondoarchive.c 1977 2008-06-02 08:49:01Z bruno $";
[1]36
37/************************* external variables *************************/
38extern void set_signals(int);
39extern int g_current_media_number;
[128]40extern void register_pid(pid_t, char *);
[1]41extern int g_currentY;
42extern bool g_text_mode;
43extern char *g_boot_mountpt;
44extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
45extern char *g_cdrw_drive_is_here;
[1256]46extern double g_kernel_version;
47extern char *g_magicdev_command;
48extern t_bkptype g_backup_media_type;
49
[1549]50extern char *get_uname_m(void);
[1384]51
[128]52static char *g_cdrom_drive_is_here = NULL;
53static char *g_dvd_drive_is_here = NULL;
[1]54
[1543]55struct mr_ar_conf *mr_conf = NULL;
[1256]56
57/***************** global vars ******************/
[1]58long diffs;
59
[1256]60/****************** subroutines used only here ******************/
[1]61
[1924]62/**
63 * Whether we're restoring from ISOs. Obviously not, since this is the
64 * backup program.
65 * @note You @b MUST declare this variable somewhere in your program if
66 * you use libmondo. Otherwise the link will fail.
67 * @ingroup globalGroup
68 */
69bool g_ISO_restore_mode = FALSE;
70
71/* Do we use extended attributes and acl ?
72 * * By default no, use --acl & --attr options to force their usage */
73char *g_getfacl = NULL;
74char *g_getfattr = NULL;
75
[1663]76/* Reference to global bkpinfo */
77struct s_bkpinfo *bkpinfo;
[1]78
[1663]79/****************** subroutines used only by main.c ******************/
80
[1]81/**
82 * Print a "don't panic" message to the log and a message about the logfile to the screen.
83 */
[1256]84static void welcome_to_mondoarchive(void)
[1]85{
[1377]86 char *tmp = NULL;
87
[1256]88 mr_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org", PACKAGE_VERSION);
[1378]89 mr_msg(0, "running %s binaries", get_architecture());
[1377]90 tmp = get_uname_m();
[1378]91 mr_msg(0, "running on %s architecture", tmp);
[1377]92 mr_free(tmp);
[1256]93 mr_msg(0, "-----------------------------------------------------------");
94 mr_msg(0, "NB: Mondo logs almost everything, so don't panic if you see");
95 mr_msg(0, "some error messages. Please read them carefully before you");
96 mr_msg(0, "decide to break out in a cold sweat. Despite (or perhaps");
97 mr_msg(0, "because of) the wealth of messages. some users are inclined");
98 mr_msg(0, "to stop reading this log. If Mondo stopped for some reason,");
99 mr_msg(0, "chances are it's detailed here. More than likely there's a");
100 mr_msg(0, "message at the very end of this log that will tell you what");
101 mr_msg(0, "is wrong. Please read it! -Devteam");
102 mr_msg(0, "-----------------------------------------------------------");
[1]103
[1108]104 mr_msg(0, "Zero...");
105 mr_msg(1, "One...");
106 mr_msg(2, "Two...");
107 mr_msg(3, "Three...");
108 mr_msg(4, "Four...");
109 mr_msg(5, "Five...");
110 mr_msg(6, "Six...");
111 mr_msg(7, "Seven...");
112 mr_msg(8, "Eight...");
[1594]113 printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
[1]114}
115
116/**
117 * Do whatever is necessary to insure a successful backup on the Linux distribution
118 * of the day.
119 */
[1256]120static void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]121{
[1108]122 mr_msg(2, "Unmounting old ramdisks if necessary");
[128]123 stop_magicdev_if_necessary(); // for RH+Gnome users
124 run_program_and_log_output
125 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
126 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
127 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
128 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
[1]129}
130
131
132/**
133 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
134 */
[1256]135static void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]136{
[1108]137 mr_msg(2, "Restarting magicdev if necessary");
[128]138 sync();
139 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]140
[1108]141 mr_msg(2, "Restarting supermounts if necessary");
[128]142 sync();
143 remount_supermounts_if_necessary(); // for Mandrake users
[1]144
[1108]145 mr_msg(2, "Unmounting /boot if necessary");
[128]146 sync();
147 unmount_boot_if_necessary(); // for Gentoo users
[1]148}
149
[1594]150/* reset/empty the mr_ar_conf structure from mondo's conf file */
151static void mr_ar_reset_conf(struct mr_ar_conf *mr_cnf) {
152
153 /* This should correspond to the default conf file */
154 /* Especially for boolean values */
155 mr_cnf->iso_creation_cmd = NULL;
156 mr_cnf->iso_creation_opt = NULL;
157 mr_cnf->iso_burning_cmd = NULL;
158 mr_cnf->iso_burning_opt = NULL;
159 mr_cnf->iso_burning_dev = NULL;
160 mr_cnf->iso_burning_speed = 0;
161 mr_cnf->media_size = 0;
162 mr_cnf->media_device = NULL;
163 mr_cnf->iso_burning_dev = NULL;
164 mr_cnf->manual_tray = FALSE;
165 mr_cnf->log_level = 0;
166 mr_cnf->prefix = NULL;
167 mr_cnf->external_tape_blocksize = 0;
168 mr_cnf->internal_tape_blocksize = 0;
169 mr_cnf->kernel = NULL;
170 mr_cnf->additional_modules = NULL;
171 mr_cnf->boot_loader = NULL;
172 mr_cnf->differential = FALSE;
173 mr_cnf->compression_tool = NULL;
174 mr_cnf->compression_level = 0;
175 mr_cnf->exclude_paths = NULL;
176 mr_cnf->include_paths = NULL;
177 mr_cnf->ui_mode = NULL;
178 mr_cnf->automatic_restore = FALSE;
179 mr_cnf->scratch_dir = NULL;
180 mr_cnf->tmp_dir = NULL;
181 mr_cnf->images_dir = NULL;
182}
183
[1535]184/* fill the mr_ar_conf structure from mindi's conf file */
185static void mr_ar_store_conf_mindi(struct mr_ar_conf *mr_cnf) {
186
187 char *p = NULL;
188
189 p = mr_conf_sread("mr_iso_creation_cmd");
190 if (p != NULL) {
191 mr_cnf->iso_creation_cmd = p;
192 }
193 p = mr_conf_sread("mr_iso_creation_opt");
194 if (p != NULL) {
195 mr_cnf->iso_creation_opt = p;
196 }
197}
198
[1256]199/* create the mr_ar_conf structure from mondo's conf file */
[1264]200static void mr_ar_store_conf(struct mr_ar_conf *mr_cnf) {
[1256]201
[1415]202 char *p = NULL;
203
[1594]204 p = mr_conf_sread("mondo_iso_burning_cmd");
205 if (p != NULL) {
206 mr_cnf->iso_burning_cmd = p;
207 }
[1422]208
[1594]209 p = mr_conf_sread("mondo_iso_burning_opt");
210 if (p != NULL) {
211 mr_cnf->iso_burning_opt = p;
212 }
[1422]213
[1594]214 p = mr_conf_sread("mondo_iso_burning_dev");
215 if (p != NULL) {
216 mr_cnf->iso_burning_dev = p;
217 }
[1422]218
[1594]219 p = mr_conf_iread("mondo_iso_burning_speed");
220 if (p != NULL ) {
221 mr_cnf->iso_burning_speed = atoi(p);
222 mr_free(p);
223 }
[1422]224
[1594]225 p = mr_conf_iread("mondo_media_size");
226 if (p != NULL ) {
227 mr_cnf->media_size = atoi(p);
228 mr_free(p);
229 }
[1422]230
[1594]231 p = mr_conf_sread("mondo_media_device");
232 if (p != NULL) {
233 mr_cnf->media_device = p;
234 }
235 /* If no specific device name for the burning command (a la ATAPI:0,0,0),
236 * then use the normal device name (a la /dev/hda) */
237 if ((mr_cnf->iso_burning_dev == NULL) && (mr_cnf->media_device != NULL)) {
238 mr_asprintf(&p, mr_cnf->media_device);
239 mr_cnf->iso_burning_dev = p;
240 }
[1422]241
[1594]242 p = mr_conf_bread("mondo_manual_tray");
243 if (p != NULL) {
244 mr_cnf->manual_tray = mr_atob(p);
245 mr_free(p);
246 }
[1422]247
[1594]248 p = mr_conf_iread("mondo_log_level");
249 if (p != NULL) {
250 mr_cnf->log_level = atoi(p);
251 mr_free(p);
252 }
[1422]253
[1594]254 p = mr_conf_sread("mondo_prefix");
255 if (p != NULL) {
256 mr_cnf->prefix = p;
257 }
[1422]258
[1594]259 p = mr_conf_iread("mondo_external_tape_blocksize");
260 if (p != NULL) {
261 mr_cnf->external_tape_blocksize = atoi(p);
262 mr_free(p);
263 }
[1422]264
[1594]265 p = mr_conf_iread("mondo_internal_tape_blocksize");
266 if (p != NULL) {
267 mr_cnf->internal_tape_blocksize = atoi(p);
268 mr_free(p);
269 }
[1422]270
[1594]271 p = mr_conf_sread("mondo_kernel");
272 if (p != NULL) {
273 mr_cnf->kernel = p;
274 }
[1422]275
[1594]276 p = mr_conf_sread("mondo_additional_modules");
277 if (p != NULL) {
278 mr_cnf->additional_modules = p;
279 }
[1422]280
[1594]281 p = mr_conf_sread("mondo_boot_loader");
282 if (p != NULL) {
283 mr_cnf->boot_loader = p;
284 }
[1422]285
[1594]286 p = mr_conf_bread("mondo_differential");
287 if (p != NULL) {
288 mr_cnf->differential = mr_atob(p);
289 mr_free(p);
290 }
[1422]291
[1594]292 p = mr_conf_sread("mondo_compression_tool");
293 if (p != NULL) {
294 mr_cnf->compression_tool = p;
295 }
[1422]296
[1769]297 p = mr_conf_sread("mondo_compression_suffix");
298 if (p != NULL) {
299 mr_cnf->compression_suffix = p;
300 }
301
[1594]302 p = mr_conf_iread("mondo_compression_level");
303 if (p != NULL) {
304 mr_cnf->compression_level = atoi(p);
305 mr_free(p);
306 }
[1422]307
[1594]308 p = mr_conf_sread("mondo_exclude_paths");
309 if (p != NULL) {
310 mr_cnf->exclude_paths = p;
311 }
[1422]312
[1594]313 p = mr_conf_sread("mondo_include_paths");
314 if (p != NULL) {
315 mr_cnf->include_paths = p;
316 }
317
318 p = mr_conf_sread("mondo_ui_mode");
319 if (p != NULL) {
320 mr_cnf->ui_mode = p;
321 }
322
323 p = mr_conf_bread("mondo_automatic_restore");
324 if (p != NULL) {
325 mr_cnf->automatic_restore = mr_atob(p);
326 mr_free(p);
327 }
328
329 p = mr_conf_sread("mondo_scratch_dir");
330 if (p != NULL) {
331 mr_cnf->scratch_dir = p;
332 }
333
334 p = mr_conf_sread("mondo_tmp_dir");
335 if (p != NULL) {
336 mr_cnf->tmp_dir = p;
337 }
338
339 p = mr_conf_sread("mondo_images_dir");
340 if (p != NULL) {
341 mr_cnf->images_dir = p;
342 }
[1264]343 mr_msg(5, "Directory for images is %s", mr_cnf->images_dir);
[1256]344}
345
[1264]346/* destroy the mr_ar_conf structure's content */
347static void mr_ar_clean_conf(struct mr_ar_conf *mr_cnf) {
348
349 if (mr_cnf == NULL) {
350 return;
351 }
352 mr_free(mr_cnf->iso_creation_cmd);
[1535]353 mr_free(mr_cnf->iso_creation_opt);
[1264]354 mr_free(mr_cnf->iso_burning_cmd);
[1594]355 mr_free(mr_cnf->iso_burning_dev);
[1592]356 mr_free(mr_cnf->iso_burning_opt);
[1264]357 mr_free(mr_cnf->media_device);
358 mr_free(mr_cnf->prefix);
359 mr_free(mr_cnf->kernel);
360 mr_free(mr_cnf->additional_modules);
361 mr_free(mr_cnf->boot_loader);
362 mr_free(mr_cnf->compression_tool);
363 mr_free(mr_cnf->exclude_paths);
364 mr_free(mr_cnf->include_paths);
365 mr_free(mr_cnf->ui_mode);
366 mr_free(mr_cnf->scratch_dir);
367 mr_free(mr_cnf->tmp_dir);
368 mr_free(mr_cnf->images_dir);
[1543]369 mr_free(mr_cnf);
[1264]370}
371
372/* Create the pointer to the function called in mr_exit */
373void (*mr_cleanup)(void) = NULL;
374
[1390]375/* Just for init */
376void mr_ar_cleanup_empty(void) {
377}
378
[1264]379/* Cleanup all memory allocated in various structures */
380void mr_ar_cleanup(void) {
381 /* Highly incomplete function for the moment */
382 /* We have to free all allocated memory */
[1543]383 mr_ar_clean_conf(mr_conf);
[1264]384 /* We have to remove all temporary files */
385 /* We have to unmount what has been mounted */
386 /* We have to properly end newt */
387 /* We have to remind people of log files */
388
389 mr_msg_close();
390}
391
[1]392/*-----------------------------------------------------------*/
393
394
395
396/**
397 * Backup/verify the user's data.
398 * What did you think it did, anyway? :-)
399 */
[128]400int main(int argc, char *argv[])
[1]401{
[1100]402 struct stat stbuf;
[1152]403 char *tmp = NULL;
[1187]404 int res = 0;
405 int retval = 0;
406 char *say_at_end = NULL;
[1190]407 char *say_at_end2 = NULL;
[1]408
[1187]409#ifdef ENABLE_NLS
410 setlocale(LC_ALL, "");
411 (void) textdomain("mondo");
412#endif
[1594]413 printf(_("Initializing...\n"));
[1256]414
[1663]415 bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
416 reset_bkpinfo();
417
[1390]418 /* Reference a dummy cleanup function for mr_exit temporarily */
419 mr_cleanup = &mr_ar_cleanup_empty;
[1264]420
[1256]421 /* initialize log file with time stamp */
422 /* We start with a loglevel of 4 - Adapted later on */
423 /* It's mandatory to set this up first as all mr_ functions rely on it */
424 unlink(MONDO_LOGFILE);
425 mr_msg_init(MONDO_LOGFILE,4);
426 mr_msg(0, _("Time started: %s"), mr_date());
427
428 /* Make sure I'm root; abort if not */
[128]429 if (getuid() != 0) {
[1256]430 mr_log_exit(127, _("Please run as root."));
[128]431 }
[1]432
[1187]433 /* If -V, -v or --version then echo version no. and quit */
[128]434 if (argc == 2
435 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
436 || !strcmp(argv[argc - 1], "--version"))) {
[1187]437 printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
[1256]438 mr_exit(0, NULL);
[128]439 }
[1]440
[1256]441 /* Conf file management */
[1594]442 mr_conf = mr_malloc(sizeof(struct mr_ar_conf));
443 mr_ar_reset_conf(mr_conf);
[1256]444 /* Check md5 sum before */
445 /* Get content */
446 if (mr_conf_open(MONDO_CONF_DIR"/mondo.conf.dist") != 0) {
447 mr_log_exit(-1, "Unable to open "MONDO_CONF_DIR"/mondo.conf.dist");
448 }
[1543]449 mr_ar_store_conf(mr_conf);
[1390]450 /* Reference the right cleanup function for mr_exit now it's allocated */
451 mr_cleanup = &mr_ar_cleanup;
[1256]452 mr_conf_close();
453
[1594]454 if (mr_conf_open(MONDO_CONF_DIR"/mondo.conf") == 0) {
455 mr_ar_store_conf(mr_conf);
456 mr_conf_close();
457 }
458
[1535]459 /* Check md5 sum before */
460 /* Get content of mindi conf file now to finish structure initialization */
461/* Tempo Hack */
[1571]462#define MINDI_CONF_DIR MONDO_CONF_DIR"/../mindi"
[1535]463 if (mr_conf_open(MINDI_CONF_DIR"/mindi.conf.dist") != 0) {
464 mr_log_exit(-1, "Unable to open "MINDI_CONF_DIR"/mindi.conf.dist");
465 }
[1543]466 mr_ar_store_conf_mindi(mr_conf);
[1535]467 mr_conf_close();
468
469 if (mr_conf_open(MINDI_CONF_DIR"/mindi.conf") == 0) {
[1543]470 mr_ar_store_conf_mindi(mr_conf);
[1535]471 mr_conf_close();
472 }
[1554]473 mr_msg(5, "Command for ISO images is %s", mr_conf->iso_creation_cmd);
[1535]474
[1256]475 /* Add MONDO_SHARE + other environment variables for mindi */
476 setenv_mondo_var();
477
[1187]478 /* Initialize variables */
[128]479 malloc_libmondo_global_strings();
480 diffs = 0;
[1100]481 if (stat(MONDO_CACHE, &stbuf) != 0) {
482 mr_mkdir(MONDO_CACHE,0x755);
483 }
484
[1140]485 /* BERLIOS: Hardcoded to be improved */
486 unlink(MONDO_CACHE"/mindi.conf");
[1143]487 unlink(MONDORESTORECFG);
[1140]488
[807]489 /* Configure the bkpinfo structure, global file paths, etc. */
[128]490 g_main_pid = getpid();
[1108]491 mr_msg(9, "This");
[1]492
[128]493 register_pid(g_main_pid, "mondo");
494 set_signals(TRUE); // catch SIGTERM, etc.
495 run_program_and_log_output("dmesg -n1", TRUE);
[1]496
[1108]497 mr_msg(9, "Next");
[1770]498 make_hole_for_dir(MONDO_CACHE);
499
[128]500 welcome_to_mondoarchive();
501 distro_specific_kludges_at_start_of_mondoarchive();
502 g_kernel_version = get_kernel_version();
[1]503
[128]504 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
[1625]505 mr_conf->log_level = 10;
[128]506 g_text_mode = TRUE;
507 setup_newt_stuff();
508 if (!strstr(argv[2], "filelist")) {
[1187]509 mr_msg(1,_("Sorry - filelist goes first\n"));
[128]510 finish(1);
511 } else {
512 finish(get_fattr_list(argv[2], argv[3]));
513 }
514 finish(0);
[1]515 }
[128]516 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
[1625]517 mr_conf->log_level = 10;
[128]518 g_text_mode = TRUE;
519 setup_newt_stuff();
520 finish(set_fattr_list(argv[2], argv[3]));
[1]521 }
[128]522
523 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
[1625]524 mr_conf->log_level = 10;
[128]525 g_text_mode = TRUE;
526 setup_newt_stuff();
[1241]527 tmp = mr_stresc(argv[2], WILDCHARS, BACKSLASH);
[128]528 printf("in=%s; out=%s\n", argv[2], tmp);
[1152]529 mr_free(tmp);
[128]530 finish(1);
[1]531 }
[128]532
533 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
[1625]534 mr_conf->log_level = 10;
[128]535 g_text_mode = TRUE;
536 setup_newt_stuff();
537 if (!strstr(argv[2], "filelist")) {
[1187]538 mr_msg(1,_("Sorry - filelist goes first\n"));
[128]539 finish(1);
540 } else {
541 finish(get_acl_list(argv[2], argv[3]));
542 }
543 finish(0);
544 }
545 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
[1625]546 mr_conf->log_level = 10;
[128]547 g_text_mode = TRUE;
548 setup_newt_stuff();
549 finish(set_acl_list(argv[2], argv[3]));
550 }
[1]551
[128]552 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
[1625]553 mr_conf->log_level = 10;
[128]554 g_text_mode = TRUE;
555 setup_newt_stuff();
[1152]556 malloc_string(tmp);
[128]557 if (find_cdrw_device(tmp)) {
[1187]558 mr_msg(1,_("Failed to find CDR-RW drive\n"));
[128]559 } else {
[1187]560 mr_msg(1,_("CD-RW is at %s\n"), tmp);
[128]561 }
562 tmp[0] = '\0';
563 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[1187]564 mr_msg(1,_("Failed to find CD-ROM drive\n"));
[128]565 } else {
[1187]566 mr_msg(1,_("CD-ROM is at %s\n"), tmp);
[128]567 }
[1152]568 mr_free(tmp);
[128]569 finish(0);
570 }
[1]571
[128]572 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
[1625]573 mr_conf->log_level = 10;
[128]574 g_text_mode = TRUE;
575 setup_newt_stuff();
[1152]576 malloc_string(tmp);
[128]577 if (find_dvd_device(tmp, atoi(argv[2]))) {
[1187]578 mr_msg(1,_("Failed to find DVD drive\n"));
[128]579 } else {
[1187]580 mr_msg(1,_("DVD is at %s\n"), tmp);
[128]581 }
[1152]582 mr_free(tmp);
[128]583 finish(0);
584 }
[1]585
[128]586 if (argc > 2 && !strcmp(argv[1], "disksize")) {
587 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
588 finish(0);
589 }
590 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
591 if (is_dev_an_NTFS_dev(argv[2])) {
[1187]592 mr_msg(1,_("%s is indeed an NTFS dev\n"), argv[2]);
[128]593 } else {
[1187]594 mr_msg(1,_("%s is _not_ an NTFS dev\n"), argv[2]);
[128]595 }
596 finish(0);
597 }
598
[1663]599 if (pre_param_configuration()) {
[128]600 fatal_error
601 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
602 }
[1140]603 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
604 bkpinfo->scratchdir);
[128]605
[1140]606 /* Process command line, if there is one. If not, ask user for info. */
[128]607 if (argc == 1) {
608 g_text_mode = FALSE;
609 setup_newt_stuff();
[1663]610 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
[128]611 if (res) {
612 fatal_error
613 ("Syntax error. Please review the parameters you have supplied and try again.");
614 }
615 } else {
[1663]616 res = handle_incoming_parameters(argc, argv);
[128]617 if (res) {
[1187]618 mr_msg(1,
619 _("Errors were detected in the command line you supplied.\n"));
620 mr_msg(1,_("Please review the log file - %s \n"),MONDO_LOGFILE);
[1108]621 mr_msg(1, "Mondoarchive will now exit.");
[128]622 finish(1);
623 }
624 setup_newt_stuff();
[1]625 }
626
627/* Finish configuring global structures */
[1663]628 if (post_param_configuration()) {
[128]629 fatal_error
630 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
631 }
[1]632
[128]633 log_to_screen
[1187]634 (_("BusyBox's sources are available from http://www.busybox.net"));
635
[1]636
[128]637 /* If we're meant to backup then backup */
638 if (bkpinfo->backup_data) {
[1663]639 res = backup_data();
[128]640 retval += res;
641 if (res) {
[1187]642 mr_strcat(say_at_end,
643 _("Data archived. Please check the logs, just as a precaution. "));
[128]644 } else {
[1187]645 mr_strcat(say_at_end, _("Data archived OK. "));
[128]646 }
647 }
[1]648
[1187]649 /* If we're meant to verify then verify */
[128]650 if (bkpinfo->verify_data) {
[1663]651 res = verify_data();
[128]652 if (res < 0) {
[1190]653 mr_asprintf(&say_at_end2, _("%d difference%c found."), -res,
[128]654 (-res != 1) ? 's' : ' ');
655 res = 0;
656 }
657 retval += res;
658 }
[1]659
[1187]660 /* Report result of entire operation (success? errors?) */
[128]661 if (!retval) {
662 mvaddstr_and_log_it(g_currentY++, 0,
[1187]663 _("Backup and/or verify ran to completion. Everything appears to be fine."));
[128]664 } else {
665 mvaddstr_and_log_it(g_currentY++, 0,
[1187]666 _("Backup and/or verify ran to completion. However, errors did occur."));
[128]667 }
[1]668
[1438]669 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
[128]670 log_to_screen
[1438]671 (_(MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it."));
[128]672 }
[1]673
[1770]674 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
[128]675 if (g_text_mode) {
676 log_to_screen
[1770]677 (_("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives"));
[128]678 } else {
[1108]679 mr_msg(1,
[1770]680 _("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives"));
[1108]681 mr_msg(2, "Calling popup_changelist_from_file()");
[1770]682 popup_changelist_from_file(MONDO_CACHE"/changed.files");
[1108]683 mr_msg(2, "Returned from popup_changelist_from_file()");
[128]684 }
685 } else {
[1770]686 unlink(MONDO_CACHE"/changed.files");
[128]687 }
688 log_to_screen(say_at_end);
[1187]689 mr_free(say_at_end);
[1190]690 if (say_at_end2 != NULL) {
691 log_to_screen(say_at_end2);
692 mr_free(say_at_end2);
693 }
[1187]694
[1152]695 mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
[128]696 run_program_and_log_output(tmp, TRUE);
[1152]697 mr_free(tmp);
[1770]698 if (bkpinfo->backup_media_type == usb) {
699 log_msg(1, "Unmounting USB device.");
700 mr_asprintf(&tmp, "umount %s1", bkpinfo->media_device);
701 run_program_and_log_output(tmp, TRUE);
702 mr_free(tmp);
703 }
[1]704
[128]705 run_program_and_log_output("mount", 2);
[1]706
[1770]707 system("rm -f "MONDO_CACHE"/last-backup.aborted");
[128]708 if (!retval) {
[1187]709 printf(_("Mondoarchive ran OK.\n"));
[128]710 } else {
[1187]711 printf(_("Errors occurred during backup. Please check logfile.\n"));
[128]712 }
713 distro_specific_kludges_at_end_of_mondoarchive();
714 register_pid(0, "mondo");
715 set_signals(FALSE);
[1663]716
[128]717 free_libmondo_global_strings();
[1080]718 mr_free(bkpinfo);
[1]719
[128]720 if (!g_cdrom_drive_is_here) {
[1108]721 mr_msg(10, "FYI, g_cdrom_drive_is_here was never used");
[128]722 }
723 if (!g_dvd_drive_is_here) {
[1108]724 mr_msg(10, "FYI, g_dvd_drive_is_here was never used");
[128]725 }
[1]726
[810]727 /* finalize log file with time stamp */
[1187]728 mr_msg(1, "Time finished: %s", mr_date());
[1132]729 mr_msg_close();
[810]730
[1770]731 chdir("/tmp");
[1663]732
[128]733 if (!g_text_mode) {
734 popup_and_OK
[1187]735 (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
736 log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
[128]737 } else {
[1187]738 printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
[1264]739 mr_exit(retval, NULL);
[128]740 }
[1770]741 finish(retval);
[128]742
743 return EXIT_SUCCESS;
[1]744}
[1939]745=======
746/***************************************************************************
747 main.c - description
748 -------------------
749 begin : Fri Apr 19 16:40:35 EDT 2002
750 copyright : (C) 2002 by Stan Benoit
751 email : troff@nakedsoul.org
752 cvsid : $Id: mondoarchive.c 1977 2008-06-02 08:49:01Z bruno $
753 ***************************************************************************/
754
755/***************************************************************************
756 * *
757 * This program is free software; you can redistribute it and/or modify *
758 * it under the terms of the GNU General Public License as published by *
759 * the Free Software Foundation; either version 2 of the License, or *
760 * (at your option) any later version. *
761 * *
762 ***************************************************************************/
763
764/**
765 * @file
766 * The main file for mondoarchive.
767 */
768
769/************************* #include statements *************************/
770#include <pthread.h>
771#include <stdio.h>
772#include <stdlib.h>
773#include "my-stuff.h"
774#include "../common/mondostructures.h"
775#include "../common/libmondo.h"
776#include "../common/libmondo-cli-EXT.h"
777#include "../common/libmondo-tools-EXT.h"
778#include "mondoarchive.h"
779
780// for CVS
781//static char cvsid[] = "$Id: mondoarchive.c 1977 2008-06-02 08:49:01Z bruno $";
782
783/************************* external variables *************************/
784extern void set_signals(int);
785extern int g_current_media_number;
786extern void register_pid(pid_t, char *);
787extern int g_currentY;
788extern bool g_text_mode;
789extern char *g_boot_mountpt;
790extern bool g_remount_floppy_at_end;
791extern char *g_cdrw_drive_is_here;
792static char *g_cdrom_drive_is_here = NULL;
793static char *g_dvd_drive_is_here = NULL;
794extern double g_kernel_version;
795
796/***************** global vars, used only by main.c ******************/
797long diffs;
798
799extern t_bkptype g_backup_media_type;
800extern int g_loglevel;
801
802/**
803 * Whether we're restoring from ISOs. Obviously not, since this is the
804 * backup program.
805 * @note You @b MUST declare this variable somewhere in your program if
806 * you use libmondo. Otherwise the link will fail.
807 * @ingroup globalGroup
808 */
809bool g_ISO_restore_mode = FALSE;
810
811/* Do we use extended attributes and acl ?
812 * * By default no, use --acl & --attr options to force their usage */
813char *g_getfacl = NULL;
814char *g_getfattr = NULL;
815
816/* Reference to global bkpinfo */
817struct s_bkpinfo *bkpinfo;
818
[1977]819/* To be coded */
820void free_MR_global_filenames(void) {
821}
822
[1939]823/****************** subroutines used only by main.c ******************/
824
825
826/**
827 * Print a "don't panic" message to the log and a message about the logfile to the screen.
828 */
829void welcome_to_mondoarchive(void)
830{
831 char *tmp = NULL;
832
833 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
834 PACKAGE_VERSION);
835 log_msg(0, "running %s binaries", get_architecture());
836 tmp = get_uname_m();
837 log_msg(0, "running on %s architecture", tmp);
838 free(tmp);
839 log_msg(0,
840 "-----------------------------------------------------------");
841 log_msg(0,
842 "NB: Mondo logs almost everything, so don't panic if you see");
843 log_msg(0,
844 "some error messages. Please read them carefully before you");
845 log_msg(0,
846 "decide to break out in a cold sweat. Despite (or perhaps");
847 log_msg(0,
848 "because of) the wealth of messages. some users are inclined");
849 log_msg(0,
850 "to stop reading this log. If Mondo stopped for some reason,");
851 log_msg(0,
852 "chances are it's detailed here. More than likely there's a");
853 log_msg(0,
854 "message at the very end of this log that will tell you what");
855 log_msg(0,
856 "is wrong. Please read it! -Devteam");
857 log_msg(0,
858 "-----------------------------------------------------------");
859
860 log_msg(0, "Zero...");
861 log_msg(1, "One...");
862 log_msg(2, "Two...");
863 log_msg(3, "Three...");
864 log_msg(4, "Four...");
865 log_msg(5, "Five...");
866 log_msg(6, "Six...");
867 log_msg(7, "Seven...");
868 log_msg(8, "Eight...");
869 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
870}
871
872
873extern char *g_magicdev_command;
874
875/**
876 * Do whatever is necessary to insure a successful backup on the Linux distribution
877 * of the day.
878 */
879void distro_specific_kludges_at_start_of_mondoarchive(void)
880{
881 log_msg(2, "Unmounting old ramdisks if necessary");
882 stop_magicdev_if_necessary(); // for RH+Gnome users
883 run_program_and_log_output
884 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
885 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
886 // stop_autofs_if_necessary(); // for Xandros users
887 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
888 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
889}
890
891
892
893/**
894 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
895 */
896void distro_specific_kludges_at_end_of_mondoarchive(void)
897{
898// char tmp[500];
899 log_msg(2, "Restarting magicdev if necessary");
900 sync();
901 restart_magicdev_if_necessary(); // for RH+Gnome users
902
903 log_msg(2, "Restarting autofs if necessary");
904 sync();
905 // restart_autofs_if_necessary(); // for Xandros users
906
907 log_msg(2, "Restarting supermounts if necessary");
908 sync();
909 remount_supermounts_if_necessary(); // for Mandrake users
910
911 log_msg(2, "Unmounting /boot if necessary");
912 sync();
913 unmount_boot_if_necessary(); // for Gentoo users
914
915// log_msg( 2, "Cleaning up KDE desktop");
916// clean_up_KDE_desktop_if_necessary();
917}
918
919
920/**
921 * Backup/verify the user's data.
922 * What did you think it did, anyway? :-)
923 */
924int main(int argc, char *argv[])
925{
926 char *tmp;
927 int res, retval;
928 char *say_at_end;
929
930/* Make sure I'm root; abort if not */
931 if (getuid() != 0) {
932 fprintf(stderr, "Please run as root.\r\n");
933 exit(127);
934 }
935
936/* If -V, -v or --version then echo version no. and quit */
937 if (argc == 2
938 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
939 || !strcmp(argv[argc - 1], "--version"))) {
940 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
941 exit(0);
942 }
943
944/* Initialize variables */
945
946 printf("Initializing...\n");
947 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
948 fatal_error("Cannot malloc bkpinfo");
949 }
950 reset_bkpinfo();
951
952 res = 0;
953 retval = 0;
954 diffs = 0;
955 malloc_string(tmp);
956 malloc_string(say_at_end);
957 say_at_end[0] = '\0';
958 malloc_libmondo_global_strings();
959
960 /* initialize log file with time stamp */
961 unlink(MONDO_LOGFILE);
962 log_msg(0, "Time started: %s", mr_date());
963
964 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
965 strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
966 tmp[MAX_STR_LEN - 1] = '\0';
967 if (strlen(tmp) >= MAX_STR_LEN - 33) {
968 fatal_error
969 ("Your PATH environmental variable is too long. Please shorten it.");
970 }
971 strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
972 setenv("PATH", tmp, 1);
973
974 /* Add the ARCH environment variable for ia64 purposes */
975 strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
976 tmp[MAX_STR_LEN - 1] = '\0';
977 setenv("ARCH", tmp, 1);
978
979 /* Add MONDO_SHARE environment variable for mindi */
980 setenv_mondo_share();
981
982 /* Configure the bkpinfo structure, global file paths, etc. */
983 g_main_pid = getpid();
984 log_msg(9, "This");
985
986 register_pid(g_main_pid, "mondo");
987 set_signals(TRUE); // catch SIGTERM, etc.
988 run_program_and_log_output("dmesg -n1", TRUE);
989
990 log_msg(9, "Next");
991 make_hole_for_dir(MONDO_CACHE);
992
993 welcome_to_mondoarchive();
994 distro_specific_kludges_at_start_of_mondoarchive();
995 g_kernel_version = get_kernel_version();
996
997 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
998 g_loglevel = 10;
999 g_text_mode = TRUE;
1000 setup_newt_stuff();
1001 if (!strstr(argv[2], "filelist")) {
1002 printf("Sorry - filelist goes first\n");
1003 finish(1);
1004 } else {
1005 finish(get_fattr_list(argv[2], argv[3]));
1006 }
1007 finish(0);
1008 }
1009 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
1010 g_loglevel = 10;
1011// chdir("/tmp");
1012 g_text_mode = TRUE;
1013 setup_newt_stuff();
1014 finish(set_fattr_list(argv[2], argv[3]));
1015 }
1016
1017 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
1018 g_loglevel = 10;
1019 g_text_mode = TRUE;
1020 setup_newt_stuff();
1021 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
1022 printf("in=%s; out=%s\n", argv[2], tmp);
1023 finish(1);
1024 }
1025
1026 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
1027 g_loglevel = 10;
1028 g_text_mode = TRUE;
1029 setup_newt_stuff();
1030 if (!strstr(argv[2], "filelist")) {
1031 printf("Sorry - filelist goes first\n");
1032 finish(1);
1033 } else {
1034 finish(get_acl_list(argv[2], argv[3]));
1035 }
1036 finish(0);
1037 }
1038 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
1039 g_loglevel = 10;
1040// chdir("/tmp");
1041 g_text_mode = TRUE;
1042 setup_newt_stuff();
1043 finish(set_acl_list(argv[2], argv[3]));
1044 }
1045
1046 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
1047 g_loglevel = 10;
1048 g_text_mode = TRUE;
1049 setup_newt_stuff();
1050 if (find_cdrw_device(tmp)) {
1051 printf("Failed to find CDR-RW drive\n");
1052 } else {
1053 printf("CD-RW is at %s\n", tmp);
1054 }
1055 tmp[0] = '\0';
1056 if (find_cdrom_device(tmp, atoi(argv[2]))) {
1057 printf("Failed to find CD-ROM drive\n");
1058 } else {
1059 printf("CD-ROM is at %s\n", tmp);
1060 }
1061 finish(0);
1062 }
1063
1064 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
1065 g_loglevel = 10;
1066 g_text_mode = TRUE;
1067 setup_newt_stuff();
1068 if (find_dvd_device(tmp, atoi(argv[2]))) {
1069 printf("Failed to find DVD drive\n");
1070 } else {
1071 printf("DVD is at %s\n", tmp);
1072 }
1073 finish(0);
1074 }
1075
1076 if (argc > 2 && !strcmp(argv[1], "disksize")) {
1077 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
1078 finish(0);
1079 }
1080 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
1081 if (is_dev_an_NTFS_dev(argv[2])) {
1082 printf("%s is indeed an NTFS dev\n", argv[2]);
1083 } else {
1084 printf("%s is _not_ an NTFS dev\n", argv[2]);
1085 }
1086 finish(0);
1087 }
1088
1089 if (pre_param_configuration()) {
1090 fatal_error
1091 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
1092 }
1093
1094/* Process command line, if there is one. If not, ask user for info. */
1095 if (argc == 1) {
1096 g_text_mode = FALSE;
1097 setup_newt_stuff();
1098 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
1099 if (res) {
1100 fatal_error
1101 ("Syntax error. Please review the parameters you have supplied and try again.");
1102 }
1103 } else {
1104 res = handle_incoming_parameters(argc, argv);
1105 if (res) {
1106 printf
1107 ("Errors were detected in the command line you supplied.\n");
1108 printf("Please review the log file - %s\n", MONDO_LOGFILE );
1109 log_msg(1, "Mondoarchive will now exit.");
1110 finish(1);
1111 }
1112 setup_newt_stuff();
1113 }
1114
1115/* Finish configuring global structures */
1116 if (post_param_configuration()) {
1117 fatal_error
1118 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
1119 }
1120
1121 log_to_screen
1122 ("BusyBox's sources are available from http://www.busybox.net");
1123
1124 /* If we're meant to backup then backup */
1125 if (bkpinfo->backup_data) {
1126 res = backup_data();
1127 retval += res;
1128 if (res) {
1129 strcat(say_at_end,
1130 "Data archived. Please check the logs, just as a precaution. ");
1131 } else {
1132 strcat(say_at_end, "Data archived OK. ");
1133 }
1134 }
1135
1136/* If we're meant to verify then verify */
1137 if (bkpinfo->verify_data) {
1138 res = verify_data();
1139 if (res < 0) {
1140 sprintf(tmp, "%d difference%c found.", -res,
1141 (-res != 1) ? 's' : ' ');
1142 strcat(say_at_end, tmp);
1143 log_to_screen(tmp);
1144 res = 0;
1145 }
1146 retval += res;
1147 }
1148
1149/* Report result of entire operation (success? errors?) */
1150 if (!retval) {
1151 mvaddstr_and_log_it(g_currentY++, 0,
1152 "Backup and/or verify ran to completion. Everything appears to be fine.");
1153 } else {
1154 mvaddstr_and_log_it(g_currentY++, 0,
1155 "Backup and/or verify ran to completion. However, errors did occur.");
1156 }
1157
1158 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
1159 log_to_screen
1160 (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
1161 }
1162
1163 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
1164 if (g_text_mode) {
1165 log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
1166 } else {
1167 log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
1168 log_msg(2, "Calling popup_changelist_from_file()");
1169 popup_changelist_from_file(MONDO_CACHE"/changed.files");
1170 log_msg(2, "Returned from popup_changelist_from_file()");
1171 }
1172 } else {
1173 unlink(MONDO_CACHE"/changed.files");
1174 }
1175 log_to_screen(say_at_end);
1176 sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
1177 run_program_and_log_output(tmp, TRUE);
1178 if (bkpinfo->backup_media_type == usb) {
1179 log_msg(1, "Unmounting USB device.");
1180 sprintf(tmp, "umount %s1", bkpinfo->media_device);
1181 run_program_and_log_output(tmp, TRUE);
1182 }
1183
1184 run_program_and_log_output("mount", 2);
1185
1186 system("rm -f "MONDO_CACHE"/last-backup.aborted");
1187 if (!retval) {
1188 printf("Mondoarchive ran OK.\n");
1189 } else {
1190 printf("Errors occurred during backup. Please check logfile.\n");
1191 }
1192 distro_specific_kludges_at_end_of_mondoarchive();
1193 register_pid(0, "mondo");
1194 set_signals(FALSE);
1195
1196 free_libmondo_global_strings();
1197 paranoid_free(say_at_end);
1198
1199
1200 if (!g_cdrom_drive_is_here) {
1201 log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
1202 }
1203 if (!g_dvd_drive_is_here) {
1204 log_msg(10, "FYI, g_dvd_drive_is_here was never used");
1205 }
1206
1207 /* finalize log file with time stamp */
1208 log_msg(0, "Time finished: %s", mr_date());
1209
1210 chdir("/tmp");
1211
1212 if (!g_text_mode) {
1213 popup_and_OK
1214 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
1215 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
1216 } else {
1217 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
1218 }
1219 finish(retval);
1220
1221 return EXIT_SUCCESS;
1222}
Note: See TracBrowser for help on using the repository browser.