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

Last change on this file since 1256 was 1256, checked in by Bruno Cornec, 17 years ago

Begining of work on conf file introduction in mondo

  • create a new struct mr_ar_conf to store conf info
  • adds a static mr_ar_store_conf function to store conf file info in that struc
  • mondo.conf is now the .dist version
  • md5 not done yet

Not tested may not work at all nor compile

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