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

Last change on this file since 1389 was 1384, checked in by Bruno Cornec, 19 years ago

Typos again

  • Property svn:keywords set to Id
File size: 16.4 KB
RevLine 
[1]1/***************************************************************************
[1080]2* $Id: mondoarchive.c 1384 2007-04-30 17:19:08Z 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>
18
[1067]19#include "my-stuff.h"
[1190]20#include "mondostructures.h"
21#include "libmondo.h"
[1]22#include "mondo-cli-EXT.h"
23
[1256]24#include "mondoarchive.h"
[1187]25#include "mr_mem.h"
[1087]26#include "mr_str.h"
[1112]27#include "mr_msg.h"
[1102]28#include "mr_file.h"
[1264]29#include "mr_err.h"
[1256]30#include "mr_conf.h"
[1087]31
[1]32// for CVS
[128]33//static char cvsid[] = "$Id: mondoarchive.c 1384 2007-04-30 17:19:08Z bruno $";
[1]34
35/************************* external variables *************************/
36extern void set_signals(int);
37extern int g_current_media_number;
[128]38extern void register_pid(pid_t, char *);
[1]39extern int g_currentY;
40extern bool g_text_mode;
41extern char *g_boot_mountpt;
42extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
43extern char *g_tmpfs_mountpt;
44extern char *g_erase_tmpdir_and_scratchdir;
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;
49extern int g_loglevel;
50
[1384]51extern char *get_uname_m();
52
[128]53static char *g_cdrom_drive_is_here = NULL;
54static char *g_dvd_drive_is_here = NULL;
[1]55
[1256]56struct mr_ar_conf mr_conf;
57
58/***************** global vars ******************/
[1]59bool g_skip_floppies;
60long diffs;
61
[1256]62/****************** subroutines used only here ******************/
[1]63
64
65/**
66 * Print a "don't panic" message to the log and a message about the logfile to the screen.
67 */
[1256]68static void welcome_to_mondoarchive(void)
[1]69{
[1377]70 char *tmp = NULL;
71
[1256]72 mr_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org", PACKAGE_VERSION);
[1378]73 mr_msg(0, "running %s binaries", get_architecture());
[1377]74 tmp = get_uname_m();
[1378]75 mr_msg(0, "running on %s architecture", tmp);
[1377]76 mr_free(tmp);
[1256]77 mr_msg(0, "-----------------------------------------------------------");
78 mr_msg(0, "NB: Mondo logs almost everything, so don't panic if you see");
79 mr_msg(0, "some error messages. Please read them carefully before you");
80 mr_msg(0, "decide to break out in a cold sweat. Despite (or perhaps");
81 mr_msg(0, "because of) the wealth of messages. some users are inclined");
82 mr_msg(0, "to stop reading this log. If Mondo stopped for some reason,");
83 mr_msg(0, "chances are it's detailed here. More than likely there's a");
84 mr_msg(0, "message at the very end of this log that will tell you what");
85 mr_msg(0, "is wrong. Please read it! -Devteam");
86 mr_msg(0, "-----------------------------------------------------------");
[1]87
[1108]88 mr_msg(0, "Zero...");
89 mr_msg(1, "One...");
90 mr_msg(2, "Two...");
91 mr_msg(3, "Three...");
92 mr_msg(4, "Four...");
93 mr_msg(5, "Five...");
94 mr_msg(6, "Six...");
95 mr_msg(7, "Seven...");
96 mr_msg(8, "Eight...");
[128]97 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[1]98}
99
100/**
101 * Do whatever is necessary to insure a successful backup on the Linux distribution
102 * of the day.
103 */
[1256]104static void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]105{
[1108]106 mr_msg(2, "Unmounting old ramdisks if necessary");
[128]107 stop_magicdev_if_necessary(); // for RH+Gnome users
108 run_program_and_log_output
109 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
110 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
111 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
112 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
[1]113}
114
115
116/**
117 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
118 */
[1256]119static void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]120{
[1108]121 mr_msg(2, "Restarting magicdev if necessary");
[128]122 sync();
123 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]124
[1108]125 mr_msg(2, "Restarting supermounts if necessary");
[128]126 sync();
127 remount_supermounts_if_necessary(); // for Mandrake users
[1]128
[1108]129 mr_msg(2, "Unmounting /boot if necessary");
[128]130 sync();
131 unmount_boot_if_necessary(); // for Gentoo users
[1]132}
133
[1256]134/* create the mr_ar_conf structure from mondo's conf file */
[1264]135static void mr_ar_store_conf(struct mr_ar_conf *mr_cnf) {
[1256]136
[1264]137 mr_asprintf(&mr_cnf->iso_creation_cmd, mr_conf_sread("mondo_iso_creation_cmd"));
138 mr_asprintf(&mr_cnf->iso_creation_options, mr_conf_sread("mondo_iso_creation_options"));
139 mr_asprintf(&mr_cnf->iso_burning_cmd, mr_conf_sread("mondo_iso_burning_cmd"));
140 mr_asprintf(&mr_cnf->iso_burning_options, mr_conf_sread("mondo_iso_burning_options"));
141 mr_cnf->iso_burning_speed = mr_conf_iread("mondo_iso_burning_speed");
142 mr_cnf->media_size = mr_conf_iread("mondo_media_size");
143 mr_asprintf(&mr_cnf->media_device, mr_conf_sread("mondo_media_device"));
144 mr_cnf->manual_tray = mr_conf_bread("mondo_manual_tray");
145 mr_cnf->log_level = mr_conf_iread("mondo_log_level");
146 mr_asprintf(&mr_cnf->prefix, mr_conf_sread("mondo_prefix"));
147 mr_cnf->external_tape_blocksize = mr_conf_iread("mondo_external_tape_blocksize");
148 mr_cnf->internal_tape_blocksize = mr_conf_iread("mondo_internal_tape_blocksize");
149 mr_cnf->slice_size = mr_conf_iread("mondo_slice_size");
150 mr_asprintf(&mr_cnf->deplist_file, mr_conf_sread("mondo_deplist_file"));
151 mr_cnf->write_boot_floppy = mr_conf_bread("mondo_write_boot_floppy");
152 mr_cnf->create_mindi_cd = mr_conf_bread("mondo_create_mindi_cd");
153 mr_asprintf(&mr_cnf->kernel, mr_conf_sread("mondo_kernel"));
154 mr_asprintf(&mr_cnf->additional_modules, mr_conf_sread("mondo_additional_modules"));
155 mr_asprintf(&mr_cnf->boot_loader, mr_conf_sread("mondo_boot_loader"));
156 mr_cnf->differential = mr_conf_bread("mondo_differential");
157 mr_asprintf(&mr_cnf->compression_tool, mr_conf_sread("mondo_compression_tool"));
158 mr_cnf->compression_level = mr_conf_iread("mondo_compression_level");
159 mr_asprintf(&mr_cnf->exclude_paths, mr_conf_sread("mondo_exclude_paths"));
160 mr_asprintf(&mr_cnf->include_paths, mr_conf_sread("mondo_include_paths"));
161 mr_asprintf(&mr_cnf->ui_mode, mr_conf_sread("mondo_ui_mode"));
162 mr_cnf->automatic_restore = mr_conf_bread("mondo_automatic_restore");
163 mr_asprintf(&mr_cnf->scratch_dir, mr_conf_sread("mondo_scratch_dir"));
164 mr_asprintf(&mr_cnf->tmp_dir, mr_conf_sread("mondo_tmp_dir"));
165 mr_asprintf(&mr_cnf->images_dir, mr_conf_sread("mondo_images_dir"));
166 mr_msg(5, "Directory for images is %s", mr_cnf->images_dir);
[1256]167}
168
[1264]169/* destroy the mr_ar_conf structure's content */
170static void mr_ar_clean_conf(struct mr_ar_conf *mr_cnf) {
171
172 if (mr_cnf == NULL) {
173 return;
174 }
175 mr_free(mr_cnf->iso_creation_cmd);
176 mr_free(mr_cnf->iso_creation_options);
177 mr_free(mr_cnf->iso_burning_cmd);
178 mr_free(mr_cnf->iso_burning_options);
179 mr_free(mr_cnf->media_device);
180 mr_free(mr_cnf->prefix);
181 mr_free(mr_cnf->deplist_file);
182 mr_free(mr_cnf->kernel);
183 mr_free(mr_cnf->additional_modules);
184 mr_free(mr_cnf->boot_loader);
185 mr_free(mr_cnf->compression_tool);
186 mr_free(mr_cnf->exclude_paths);
187 mr_free(mr_cnf->include_paths);
188 mr_free(mr_cnf->ui_mode);
189 mr_free(mr_cnf->scratch_dir);
190 mr_free(mr_cnf->tmp_dir);
191 mr_free(mr_cnf->images_dir);
192}
193
194/* Create the pointer to the function called in mr_exit */
195void (*mr_cleanup)(void) = NULL;
196
197/* Cleanup all memory allocated in various structures */
198void mr_ar_cleanup(void) {
199 /* Highly incomplete function for the moment */
200 /* We have to free all allocated memory */
201 mr_ar_clean_conf(&mr_conf);
202 /* We have to remove all temporary files */
203 /* We have to unmount what has been mounted */
204 /* We have to properly end newt */
205 /* We have to remind people of log files */
206
207 mr_msg_close();
208}
209
[1]210/*-----------------------------------------------------------*/
211
212
213
214/**
215 * Backup/verify the user's data.
216 * What did you think it did, anyway? :-)
217 */
[128]218int main(int argc, char *argv[])
[1]219{
[1256]220 struct s_bkpinfo *bkpinfo = NULL;
[1100]221 struct stat stbuf;
[1152]222 char *tmp = NULL;
[1187]223 int res = 0;
224 int retval = 0;
225 char *say_at_end = NULL;
[1190]226 char *say_at_end2 = NULL;
[1]227
[1187]228#ifdef ENABLE_NLS
229 setlocale(LC_ALL, "");
230 (void) textdomain("mondo");
231#endif
[1256]232 printf(_("Initializing..."));
233
[1264]234 /* Reference the right cleanup function for mr_exit */
[1269]235 mr_cleanup = &mr_ar_cleanup;
[1264]236
[1256]237 /* initialize log file with time stamp */
238 /* We start with a loglevel of 4 - Adapted later on */
239 /* It's mandatory to set this up first as all mr_ functions rely on it */
240 unlink(MONDO_LOGFILE);
241 mr_msg_init(MONDO_LOGFILE,4);
242 mr_msg(0, _("Time started: %s"), mr_date());
243
244 /* Make sure I'm root; abort if not */
[128]245 if (getuid() != 0) {
[1256]246 mr_log_exit(127, _("Please run as root."));
[128]247 }
[1]248
[1187]249 /* If -V, -v or --version then echo version no. and quit */
[128]250 if (argc == 2
251 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
252 || !strcmp(argv[argc - 1], "--version"))) {
[1187]253 printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
[1256]254 mr_exit(0, NULL);
[128]255 }
[1]256
[1256]257 /* Conf file management */
258 /* Check md5 sum before */
259 /* Get content */
260 if (mr_conf_open(MONDO_CONF_DIR"/mondo.conf.dist") != 0) {
261 mr_log_exit(-1, "Unable to open "MONDO_CONF_DIR"/mondo.conf.dist");
262 }
263 mr_ar_store_conf(&mr_conf);
264 mr_conf_close();
265
266 /* Add MONDO_SHARE + other environment variables for mindi */
267 setenv_mondo_var();
268
[1187]269 /* Initialize variables */
[128]270 malloc_libmondo_global_strings();
271 diffs = 0;
[1132]272 bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
[1100]273 if (stat(MONDO_CACHE, &stbuf) != 0) {
274 mr_mkdir(MONDO_CACHE,0x755);
275 }
276
[1140]277 /* BERLIOS: Hardcoded to be improved */
278 unlink(MONDO_CACHE"/mindi.conf");
[1143]279 unlink(MONDORESTORECFG);
[1140]280
[807]281 /* Configure the bkpinfo structure, global file paths, etc. */
[128]282 g_main_pid = getpid();
[1108]283 mr_msg(9, "This");
[1]284
[128]285 register_pid(g_main_pid, "mondo");
286 set_signals(TRUE); // catch SIGTERM, etc.
287 run_program_and_log_output("dmesg -n1", TRUE);
[1]288
[1108]289 mr_msg(9, "Next");
[128]290 welcome_to_mondoarchive();
291 distro_specific_kludges_at_start_of_mondoarchive();
292 g_kernel_version = get_kernel_version();
[1]293
[128]294 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
295 g_loglevel = 10;
296 g_text_mode = TRUE;
297 setup_newt_stuff();
298 if (!strstr(argv[2], "filelist")) {
[1187]299 mr_msg(1,_("Sorry - filelist goes first\n"));
[128]300 finish(1);
301 } else {
302 finish(get_fattr_list(argv[2], argv[3]));
303 }
304 finish(0);
[1]305 }
[128]306 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
307 g_loglevel = 10;
308 g_text_mode = TRUE;
309 setup_newt_stuff();
310 finish(set_fattr_list(argv[2], argv[3]));
[1]311 }
[128]312
313 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
314 g_loglevel = 10;
315 g_text_mode = TRUE;
316 setup_newt_stuff();
[1241]317 tmp = mr_stresc(argv[2], WILDCHARS, BACKSLASH);
[128]318 printf("in=%s; out=%s\n", argv[2], tmp);
[1152]319 mr_free(tmp);
[128]320 finish(1);
[1]321 }
[128]322
323 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
324 g_loglevel = 10;
325 g_text_mode = TRUE;
326 setup_newt_stuff();
327 if (!strstr(argv[2], "filelist")) {
[1187]328 mr_msg(1,_("Sorry - filelist goes first\n"));
[128]329 finish(1);
330 } else {
331 finish(get_acl_list(argv[2], argv[3]));
332 }
333 finish(0);
334 }
335 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
336 g_loglevel = 10;
337 g_text_mode = TRUE;
338 setup_newt_stuff();
339 finish(set_acl_list(argv[2], argv[3]));
340 }
[1]341
[128]342 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
343 g_loglevel = 10;
344 g_text_mode = TRUE;
345 setup_newt_stuff();
[1152]346 malloc_string(tmp);
[128]347 if (find_cdrw_device(tmp)) {
[1187]348 mr_msg(1,_("Failed to find CDR-RW drive\n"));
[128]349 } else {
[1187]350 mr_msg(1,_("CD-RW is at %s\n"), tmp);
[128]351 }
352 tmp[0] = '\0';
353 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[1187]354 mr_msg(1,_("Failed to find CD-ROM drive\n"));
[128]355 } else {
[1187]356 mr_msg(1,_("CD-ROM is at %s\n"), tmp);
[128]357 }
[1152]358 mr_free(tmp);
[128]359 finish(0);
360 }
[1]361
[128]362 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
363 g_loglevel = 10;
364 g_text_mode = TRUE;
365 setup_newt_stuff();
[1152]366 malloc_string(tmp);
[128]367 if (find_dvd_device(tmp, atoi(argv[2]))) {
[1187]368 mr_msg(1,_("Failed to find DVD drive\n"));
[128]369 } else {
[1187]370 mr_msg(1,_("DVD is at %s\n"), tmp);
[128]371 }
[1152]372 mr_free(tmp);
[128]373 finish(0);
374 }
[1]375
[128]376 if (argc > 2 && !strcmp(argv[1], "disksize")) {
377 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
378 finish(0);
379 }
380 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
381 if (is_dev_an_NTFS_dev(argv[2])) {
[1187]382 mr_msg(1,_("%s is indeed an NTFS dev\n"), argv[2]);
[128]383 } else {
[1187]384 mr_msg(1,_("%s is _not_ an NTFS dev\n"), argv[2]);
[128]385 }
386 finish(0);
387 }
388
389 if (pre_param_configuration(bkpinfo)) {
390 fatal_error
391 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
392 }
[1140]393 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
394 bkpinfo->scratchdir);
[128]395
[1140]396 /* Process command line, if there is one. If not, ask user for info. */
[128]397 if (argc == 1) {
398 g_text_mode = FALSE;
399 setup_newt_stuff();
400 res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
401 if (res) {
402 fatal_error
403 ("Syntax error. Please review the parameters you have supplied and try again.");
404 }
405 } else {
406 res = handle_incoming_parameters(argc, argv, bkpinfo);
407 if (res) {
[1187]408 mr_msg(1,
409 _("Errors were detected in the command line you supplied.\n"));
410 mr_msg(1,_("Please review the log file - %s \n"),MONDO_LOGFILE);
[1108]411 mr_msg(1, "Mondoarchive will now exit.");
[128]412 finish(1);
413 }
414 setup_newt_stuff();
[1]415 }
416
417/* Finish configuring global structures */
[128]418 if (post_param_configuration(bkpinfo)) {
419 fatal_error
420 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
421 }
[1]422
[128]423 log_to_screen
[1187]424 (_("BusyBox's sources are available from http://www.busybox.net"));
425
[128]426 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
427 bkpinfo->scratchdir);
[1]428
[128]429 /* If we're meant to backup then backup */
430 if (bkpinfo->backup_data) {
[541]431 res = backup_data(bkpinfo);
[128]432 retval += res;
433 if (res) {
[1187]434 mr_strcat(say_at_end,
435 _("Data archived. Please check the logs, just as a precaution. "));
[128]436 } else {
[1187]437 mr_strcat(say_at_end, _("Data archived OK. "));
[128]438 }
439 }
[1]440
[1187]441 /* If we're meant to verify then verify */
[128]442 if (bkpinfo->verify_data) {
443 res = verify_data(bkpinfo);
444 if (res < 0) {
[1190]445 mr_asprintf(&say_at_end2, _("%d difference%c found."), -res,
[128]446 (-res != 1) ? 's' : ' ');
447 res = 0;
448 }
449 retval += res;
450 }
[1]451
[1187]452 /* Offer to write floppy disk images to physical disks */
[128]453 if (bkpinfo->backup_data && !g_skip_floppies) {
454 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
455 retval += res;
456 }
[1]457
[1187]458 /* Report result of entire operation (success? errors?) */
[128]459 if (!retval) {
460 mvaddstr_and_log_it(g_currentY++, 0,
[1187]461 _("Backup and/or verify ran to completion. Everything appears to be fine."));
[128]462 } else {
463 mvaddstr_and_log_it(g_currentY++, 0,
[1187]464 _("Backup and/or verify ran to completion. However, errors did occur."));
[128]465 }
[1]466
[1044]467 if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
[128]468 log_to_screen
[1187]469 (_("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it."));
[128]470 }
[1]471
472
[128]473 if (length_of_file("/tmp/changed.files") > 2) {
474 if (g_text_mode) {
475 log_to_screen
[1187]476 (_("Type 'less /tmp/changed.files' to see which files don't match the archives"));
[128]477 } else {
[1108]478 mr_msg(1,
[1187]479 _("Type 'less /tmp/changed.files' to see which files don't match the archives"));
[1108]480 mr_msg(2, "Calling popup_changelist_from_file()");
[128]481 popup_changelist_from_file("/tmp/changed.files");
[1108]482 mr_msg(2, "Returned from popup_changelist_from_file()");
[128]483 }
484 } else {
485 unlink("/tmp/changed.files");
486 }
487 log_to_screen(say_at_end);
[1187]488 mr_free(say_at_end);
[1190]489 if (say_at_end2 != NULL) {
490 log_to_screen(say_at_end2);
491 mr_free(say_at_end2);
492 }
[1187]493
[1152]494 mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
[128]495 run_program_and_log_output(tmp, TRUE);
[1152]496 mr_free(tmp);
[128]497 run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
[1]498
[128]499 run_program_and_log_output("mount", 2);
[1]500
[128]501 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
502 system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
503 if (!retval) {
[1187]504 printf(_("Mondoarchive ran OK.\n"));
[128]505 } else {
[1187]506 printf(_("Errors occurred during backup. Please check logfile.\n"));
[128]507 }
508 distro_specific_kludges_at_end_of_mondoarchive();
509 register_pid(0, "mondo");
510 set_signals(FALSE);
511 chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
512 system(g_erase_tmpdir_and_scratchdir);
513 free_libmondo_global_strings();
[1080]514 mr_free(bkpinfo);
[1]515
[128]516 unlink("/tmp/filelist.full");
517 unlink("/tmp/filelist.full.gz");
[1]518
[128]519 if (!g_cdrom_drive_is_here) {
[1108]520 mr_msg(10, "FYI, g_cdrom_drive_is_here was never used");
[128]521 }
522 if (!g_dvd_drive_is_here) {
[1108]523 mr_msg(10, "FYI, g_dvd_drive_is_here was never used");
[128]524 }
[1]525
[810]526 /* finalize log file with time stamp */
[1187]527 mr_msg(1, "Time finished: %s", mr_date());
[1132]528 mr_msg_close();
[810]529
[128]530 if (!g_text_mode) {
531 popup_and_OK
[1187]532 (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
533 log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
[128]534 finish(retval);
535 } else {
[1187]536 printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
[1264]537 mr_exit(retval, NULL);
[128]538 }
539
540 return EXIT_SUCCESS;
[1]541}
Note: See TracBrowser for help on using the repository browser.