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

Last change on this file since 1959 was 1939, checked in by Bruno Cornec, 16 years ago

svn merge -r 1923:1938 $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 1939 2008-05-16 23:29:54Z 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 1939 2008-05-16 23:29:54Z 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 1939 2008-05-16 23:29:54Z 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 1939 2008-05-16 23:29:54Z 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
819/****************** subroutines used only by main.c ******************/
820
821
822/**
823 * Print a "don't panic" message to the log and a message about the logfile to the screen.
824 */
825void welcome_to_mondoarchive(void)
826{
827 char *tmp = NULL;
828
829 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
830 PACKAGE_VERSION);
831 log_msg(0, "running %s binaries", get_architecture());
832 tmp = get_uname_m();
833 log_msg(0, "running on %s architecture", tmp);
834 free(tmp);
835 log_msg(0,
836 "-----------------------------------------------------------");
837 log_msg(0,
838 "NB: Mondo logs almost everything, so don't panic if you see");
839 log_msg(0,
840 "some error messages. Please read them carefully before you");
841 log_msg(0,
842 "decide to break out in a cold sweat. Despite (or perhaps");
843 log_msg(0,
844 "because of) the wealth of messages. some users are inclined");
845 log_msg(0,
846 "to stop reading this log. If Mondo stopped for some reason,");
847 log_msg(0,
848 "chances are it's detailed here. More than likely there's a");
849 log_msg(0,
850 "message at the very end of this log that will tell you what");
851 log_msg(0,
852 "is wrong. Please read it! -Devteam");
853 log_msg(0,
854 "-----------------------------------------------------------");
855
856 log_msg(0, "Zero...");
857 log_msg(1, "One...");
858 log_msg(2, "Two...");
859 log_msg(3, "Three...");
860 log_msg(4, "Four...");
861 log_msg(5, "Five...");
862 log_msg(6, "Six...");
863 log_msg(7, "Seven...");
864 log_msg(8, "Eight...");
865 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
866}
867
868
869extern char *g_magicdev_command;
870
871/**
872 * Do whatever is necessary to insure a successful backup on the Linux distribution
873 * of the day.
874 */
875void distro_specific_kludges_at_start_of_mondoarchive(void)
876{
877 log_msg(2, "Unmounting old ramdisks if necessary");
878 stop_magicdev_if_necessary(); // for RH+Gnome users
879 run_program_and_log_output
880 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
881 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
882 // stop_autofs_if_necessary(); // for Xandros users
883 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
884 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
885}
886
887
888
889/**
890 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
891 */
892void distro_specific_kludges_at_end_of_mondoarchive(void)
893{
894// char tmp[500];
895 log_msg(2, "Restarting magicdev if necessary");
896 sync();
897 restart_magicdev_if_necessary(); // for RH+Gnome users
898
899 log_msg(2, "Restarting autofs if necessary");
900 sync();
901 // restart_autofs_if_necessary(); // for Xandros users
902
903 log_msg(2, "Restarting supermounts if necessary");
904 sync();
905 remount_supermounts_if_necessary(); // for Mandrake users
906
907 log_msg(2, "Unmounting /boot if necessary");
908 sync();
909 unmount_boot_if_necessary(); // for Gentoo users
910
911// log_msg( 2, "Cleaning up KDE desktop");
912// clean_up_KDE_desktop_if_necessary();
913}
914
915
916/**
917 * Backup/verify the user's data.
918 * What did you think it did, anyway? :-)
919 */
920int main(int argc, char *argv[])
921{
922 char *tmp;
923 int res, retval;
924 char *say_at_end;
925
926/* Make sure I'm root; abort if not */
927 if (getuid() != 0) {
928 fprintf(stderr, "Please run as root.\r\n");
929 exit(127);
930 }
931
932/* If -V, -v or --version then echo version no. and quit */
933 if (argc == 2
934 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
935 || !strcmp(argv[argc - 1], "--version"))) {
936 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
937 exit(0);
938 }
939
940/* Initialize variables */
941
942 printf("Initializing...\n");
943 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
944 fatal_error("Cannot malloc bkpinfo");
945 }
946 reset_bkpinfo();
947
948 res = 0;
949 retval = 0;
950 diffs = 0;
951 malloc_string(tmp);
952 malloc_string(say_at_end);
953 say_at_end[0] = '\0';
954 malloc_libmondo_global_strings();
955
956 /* initialize log file with time stamp */
957 unlink(MONDO_LOGFILE);
958 log_msg(0, "Time started: %s", mr_date());
959
960 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
961 strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
962 tmp[MAX_STR_LEN - 1] = '\0';
963 if (strlen(tmp) >= MAX_STR_LEN - 33) {
964 fatal_error
965 ("Your PATH environmental variable is too long. Please shorten it.");
966 }
967 strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
968 setenv("PATH", tmp, 1);
969
970 /* Add the ARCH environment variable for ia64 purposes */
971 strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
972 tmp[MAX_STR_LEN - 1] = '\0';
973 setenv("ARCH", tmp, 1);
974
975 /* Add MONDO_SHARE environment variable for mindi */
976 setenv_mondo_share();
977
978 /* Configure the bkpinfo structure, global file paths, etc. */
979 g_main_pid = getpid();
980 log_msg(9, "This");
981
982 register_pid(g_main_pid, "mondo");
983 set_signals(TRUE); // catch SIGTERM, etc.
984 run_program_and_log_output("dmesg -n1", TRUE);
985
986 log_msg(9, "Next");
987 make_hole_for_dir(MONDO_CACHE);
988
989 welcome_to_mondoarchive();
990 distro_specific_kludges_at_start_of_mondoarchive();
991 g_kernel_version = get_kernel_version();
992
993 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
994 g_loglevel = 10;
995 g_text_mode = TRUE;
996 setup_newt_stuff();
997 if (!strstr(argv[2], "filelist")) {
998 printf("Sorry - filelist goes first\n");
999 finish(1);
1000 } else {
1001 finish(get_fattr_list(argv[2], argv[3]));
1002 }
1003 finish(0);
1004 }
1005 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
1006 g_loglevel = 10;
1007// chdir("/tmp");
1008 g_text_mode = TRUE;
1009 setup_newt_stuff();
1010 finish(set_fattr_list(argv[2], argv[3]));
1011 }
1012
1013 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
1014 g_loglevel = 10;
1015 g_text_mode = TRUE;
1016 setup_newt_stuff();
1017 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
1018 printf("in=%s; out=%s\n", argv[2], tmp);
1019 finish(1);
1020 }
1021
1022 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
1023 g_loglevel = 10;
1024 g_text_mode = TRUE;
1025 setup_newt_stuff();
1026 if (!strstr(argv[2], "filelist")) {
1027 printf("Sorry - filelist goes first\n");
1028 finish(1);
1029 } else {
1030 finish(get_acl_list(argv[2], argv[3]));
1031 }
1032 finish(0);
1033 }
1034 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
1035 g_loglevel = 10;
1036// chdir("/tmp");
1037 g_text_mode = TRUE;
1038 setup_newt_stuff();
1039 finish(set_acl_list(argv[2], argv[3]));
1040 }
1041
1042 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
1043 g_loglevel = 10;
1044 g_text_mode = TRUE;
1045 setup_newt_stuff();
1046 if (find_cdrw_device(tmp)) {
1047 printf("Failed to find CDR-RW drive\n");
1048 } else {
1049 printf("CD-RW is at %s\n", tmp);
1050 }
1051 tmp[0] = '\0';
1052 if (find_cdrom_device(tmp, atoi(argv[2]))) {
1053 printf("Failed to find CD-ROM drive\n");
1054 } else {
1055 printf("CD-ROM is at %s\n", tmp);
1056 }
1057 finish(0);
1058 }
1059
1060 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
1061 g_loglevel = 10;
1062 g_text_mode = TRUE;
1063 setup_newt_stuff();
1064 if (find_dvd_device(tmp, atoi(argv[2]))) {
1065 printf("Failed to find DVD drive\n");
1066 } else {
1067 printf("DVD is at %s\n", tmp);
1068 }
1069 finish(0);
1070 }
1071
1072 if (argc > 2 && !strcmp(argv[1], "disksize")) {
1073 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
1074 finish(0);
1075 }
1076 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
1077 if (is_dev_an_NTFS_dev(argv[2])) {
1078 printf("%s is indeed an NTFS dev\n", argv[2]);
1079 } else {
1080 printf("%s is _not_ an NTFS dev\n", argv[2]);
1081 }
1082 finish(0);
1083 }
1084
1085 if (pre_param_configuration()) {
1086 fatal_error
1087 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
1088 }
1089
1090/* Process command line, if there is one. If not, ask user for info. */
1091 if (argc == 1) {
1092 g_text_mode = FALSE;
1093 setup_newt_stuff();
1094 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
1095 if (res) {
1096 fatal_error
1097 ("Syntax error. Please review the parameters you have supplied and try again.");
1098 }
1099 } else {
1100 res = handle_incoming_parameters(argc, argv);
1101 if (res) {
1102 printf
1103 ("Errors were detected in the command line you supplied.\n");
1104 printf("Please review the log file - %s\n", MONDO_LOGFILE );
1105 log_msg(1, "Mondoarchive will now exit.");
1106 finish(1);
1107 }
1108 setup_newt_stuff();
1109 }
1110
1111/* Finish configuring global structures */
1112 if (post_param_configuration()) {
1113 fatal_error
1114 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
1115 }
1116
1117 log_to_screen
1118 ("BusyBox's sources are available from http://www.busybox.net");
1119
1120 /* If we're meant to backup then backup */
1121 if (bkpinfo->backup_data) {
1122 res = backup_data();
1123 retval += res;
1124 if (res) {
1125 strcat(say_at_end,
1126 "Data archived. Please check the logs, just as a precaution. ");
1127 } else {
1128 strcat(say_at_end, "Data archived OK. ");
1129 }
1130 }
1131
1132/* If we're meant to verify then verify */
1133 if (bkpinfo->verify_data) {
1134 res = verify_data();
1135 if (res < 0) {
1136 sprintf(tmp, "%d difference%c found.", -res,
1137 (-res != 1) ? 's' : ' ');
1138 strcat(say_at_end, tmp);
1139 log_to_screen(tmp);
1140 res = 0;
1141 }
1142 retval += res;
1143 }
1144
1145/* Report result of entire operation (success? errors?) */
1146 if (!retval) {
1147 mvaddstr_and_log_it(g_currentY++, 0,
1148 "Backup and/or verify ran to completion. Everything appears to be fine.");
1149 } else {
1150 mvaddstr_and_log_it(g_currentY++, 0,
1151 "Backup and/or verify ran to completion. However, errors did occur.");
1152 }
1153
1154 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
1155 log_to_screen
1156 (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
1157 }
1158
1159 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
1160 if (g_text_mode) {
1161 log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
1162 } else {
1163 log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
1164 log_msg(2, "Calling popup_changelist_from_file()");
1165 popup_changelist_from_file(MONDO_CACHE"/changed.files");
1166 log_msg(2, "Returned from popup_changelist_from_file()");
1167 }
1168 } else {
1169 unlink(MONDO_CACHE"/changed.files");
1170 }
1171 log_to_screen(say_at_end);
1172 sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
1173 run_program_and_log_output(tmp, TRUE);
1174 if (bkpinfo->backup_media_type == usb) {
1175 log_msg(1, "Unmounting USB device.");
1176 sprintf(tmp, "umount %s1", bkpinfo->media_device);
1177 run_program_and_log_output(tmp, TRUE);
1178 }
1179
1180 run_program_and_log_output("mount", 2);
1181
1182 system("rm -f "MONDO_CACHE"/last-backup.aborted");
1183 if (!retval) {
1184 printf("Mondoarchive ran OK.\n");
1185 } else {
1186 printf("Errors occurred during backup. Please check logfile.\n");
1187 }
1188 distro_specific_kludges_at_end_of_mondoarchive();
1189 register_pid(0, "mondo");
1190 set_signals(FALSE);
1191
1192 free_libmondo_global_strings();
1193 paranoid_free(say_at_end);
1194
1195
1196 if (!g_cdrom_drive_is_here) {
1197 log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
1198 }
1199 if (!g_dvd_drive_is_here) {
1200 log_msg(10, "FYI, g_dvd_drive_is_here was never used");
1201 }
1202
1203 /* finalize log file with time stamp */
1204 log_msg(0, "Time finished: %s", mr_date());
1205
1206 chdir("/tmp");
1207
1208 if (!g_text_mode) {
1209 popup_and_OK
1210 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
1211 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
1212 } else {
1213 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
1214 }
1215 finish(retval);
1216
1217 return EXIT_SUCCESS;
1218}
Note: See TracBrowser for help on using the repository browser.