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

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

Initialization message in mondoarchive now uses mr_msg also

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