1 | /*************************************************************************** |
---|
2 | * $Id: mondoarchive.c 1213 2007-02-26 13:04:43Z 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 1213 2007-02-26 13:04:43Z bruno $"; |
---|
31 | |
---|
32 | /************************* external variables *************************/ |
---|
33 | extern void set_signals(int); |
---|
34 | extern int g_current_media_number; |
---|
35 | extern void register_pid(pid_t, char *); |
---|
36 | extern int g_currentY; |
---|
37 | extern bool g_text_mode; |
---|
38 | extern char *g_boot_mountpt; |
---|
39 | extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end; |
---|
40 | extern char *g_tmpfs_mountpt; |
---|
41 | extern char *g_erase_tmpdir_and_scratchdir; |
---|
42 | extern char *g_cdrw_drive_is_here; |
---|
43 | static char *g_cdrom_drive_is_here = NULL; |
---|
44 | static char *g_dvd_drive_is_here = NULL; |
---|
45 | extern double g_kernel_version; |
---|
46 | |
---|
47 | /***************** global vars, used only by main.c ******************/ |
---|
48 | bool g_skip_floppies; |
---|
49 | long diffs; |
---|
50 | char *ps_options = "auxww"; |
---|
51 | char *ps_proc_id = "$2"; |
---|
52 | |
---|
53 | extern t_bkptype g_backup_media_type; |
---|
54 | extern 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 | */ |
---|
62 | void 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 | |
---|
101 | extern 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 | */ |
---|
107 | void 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 | */ |
---|
122 | void 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 | */ |
---|
145 | int 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 | printf(_("Initializing...\n")); |
---|
180 | |
---|
181 | /* initialize log file with time stamp */ |
---|
182 | unlink(MONDO_LOGFILE); |
---|
183 | mr_msg_init(MONDO_LOGFILE,4); |
---|
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 + others environment variable 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 | malloc_string(tmp); |
---|
239 | turn_wildcard_chars_into_literal_chars(tmp, argv[2]); |
---|
240 | printf("in=%s; out=%s\n", argv[2], tmp); |
---|
241 | mr_free(tmp); |
---|
242 | finish(1); |
---|
243 | } |
---|
244 | |
---|
245 | if (argc == 4 && !strcmp(argv[1], "getfacl")) { |
---|
246 | g_loglevel = 10; |
---|
247 | g_text_mode = TRUE; |
---|
248 | setup_newt_stuff(); |
---|
249 | if (!strstr(argv[2], "filelist")) { |
---|
250 | mr_msg(1,_("Sorry - filelist goes first\n")); |
---|
251 | finish(1); |
---|
252 | } else { |
---|
253 | finish(get_acl_list(argv[2], argv[3])); |
---|
254 | } |
---|
255 | finish(0); |
---|
256 | } |
---|
257 | if (argc == 4 && !strcmp(argv[1], "setfacl")) { |
---|
258 | g_loglevel = 10; |
---|
259 | g_text_mode = TRUE; |
---|
260 | setup_newt_stuff(); |
---|
261 | finish(set_acl_list(argv[2], argv[3])); |
---|
262 | } |
---|
263 | |
---|
264 | if (argc > 2 && !strcmp(argv[1], "find-cd")) { |
---|
265 | g_loglevel = 10; |
---|
266 | g_text_mode = TRUE; |
---|
267 | setup_newt_stuff(); |
---|
268 | malloc_string(tmp); |
---|
269 | if (find_cdrw_device(tmp)) { |
---|
270 | mr_msg(1,_("Failed to find CDR-RW drive\n")); |
---|
271 | } else { |
---|
272 | mr_msg(1,_("CD-RW is at %s\n"), tmp); |
---|
273 | } |
---|
274 | tmp[0] = '\0'; |
---|
275 | if (find_cdrom_device(tmp, atoi(argv[2]))) { |
---|
276 | mr_msg(1,_("Failed to find CD-ROM drive\n")); |
---|
277 | } else { |
---|
278 | mr_msg(1,_("CD-ROM is at %s\n"), tmp); |
---|
279 | } |
---|
280 | mr_free(tmp); |
---|
281 | finish(0); |
---|
282 | } |
---|
283 | |
---|
284 | if (argc > 2 && !strcmp(argv[1], "find-dvd")) { |
---|
285 | g_loglevel = 10; |
---|
286 | g_text_mode = TRUE; |
---|
287 | setup_newt_stuff(); |
---|
288 | malloc_string(tmp); |
---|
289 | if (find_dvd_device(tmp, atoi(argv[2]))) { |
---|
290 | mr_msg(1,_("Failed to find DVD drive\n")); |
---|
291 | } else { |
---|
292 | mr_msg(1,_("DVD is at %s\n"), tmp); |
---|
293 | } |
---|
294 | mr_free(tmp); |
---|
295 | finish(0); |
---|
296 | } |
---|
297 | |
---|
298 | if (argc > 2 && !strcmp(argv[1], "disksize")) { |
---|
299 | printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2])); |
---|
300 | finish(0); |
---|
301 | } |
---|
302 | if (argc > 2 && !strcmp(argv[1], "test-dev")) { |
---|
303 | if (is_dev_an_NTFS_dev(argv[2])) { |
---|
304 | mr_msg(1,_("%s is indeed an NTFS dev\n"), argv[2]); |
---|
305 | } else { |
---|
306 | mr_msg(1,_("%s is _not_ an NTFS dev\n"), argv[2]); |
---|
307 | } |
---|
308 | finish(0); |
---|
309 | } |
---|
310 | |
---|
311 | if (pre_param_configuration(bkpinfo)) { |
---|
312 | fatal_error |
---|
313 | ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting..."); |
---|
314 | } |
---|
315 | sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, |
---|
316 | bkpinfo->scratchdir); |
---|
317 | |
---|
318 | /* Process command line, if there is one. If not, ask user for info. */ |
---|
319 | if (argc == 1) { |
---|
320 | g_text_mode = FALSE; |
---|
321 | setup_newt_stuff(); |
---|
322 | res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */ |
---|
323 | if (res) { |
---|
324 | fatal_error |
---|
325 | ("Syntax error. Please review the parameters you have supplied and try again."); |
---|
326 | } |
---|
327 | } else { |
---|
328 | res = handle_incoming_parameters(argc, argv, bkpinfo); |
---|
329 | if (res) { |
---|
330 | mr_msg(1, |
---|
331 | _("Errors were detected in the command line you supplied.\n")); |
---|
332 | mr_msg(1,_("Please review the log file - %s \n"),MONDO_LOGFILE); |
---|
333 | mr_msg(1, "Mondoarchive will now exit."); |
---|
334 | finish(1); |
---|
335 | } |
---|
336 | setup_newt_stuff(); |
---|
337 | } |
---|
338 | |
---|
339 | /* Finish configuring global structures */ |
---|
340 | if (post_param_configuration(bkpinfo)) { |
---|
341 | fatal_error |
---|
342 | ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting..."); |
---|
343 | } |
---|
344 | |
---|
345 | log_to_screen |
---|
346 | (_("BusyBox's sources are available from http://www.busybox.net")); |
---|
347 | |
---|
348 | sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, |
---|
349 | bkpinfo->scratchdir); |
---|
350 | |
---|
351 | /* If we're meant to backup then backup */ |
---|
352 | if (bkpinfo->backup_data) { |
---|
353 | res = backup_data(bkpinfo); |
---|
354 | retval += res; |
---|
355 | if (res) { |
---|
356 | mr_strcat(say_at_end, |
---|
357 | _("Data archived. Please check the logs, just as a precaution. ")); |
---|
358 | } else { |
---|
359 | mr_strcat(say_at_end, _("Data archived OK. ")); |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | /* If we're meant to verify then verify */ |
---|
364 | if (bkpinfo->verify_data) { |
---|
365 | res = verify_data(bkpinfo); |
---|
366 | if (res < 0) { |
---|
367 | mr_asprintf(&say_at_end2, _("%d difference%c found."), -res, |
---|
368 | (-res != 1) ? 's' : ' '); |
---|
369 | res = 0; |
---|
370 | } |
---|
371 | retval += res; |
---|
372 | } |
---|
373 | |
---|
374 | /* Offer to write floppy disk images to physical disks */ |
---|
375 | if (bkpinfo->backup_data && !g_skip_floppies) { |
---|
376 | res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo); |
---|
377 | retval += res; |
---|
378 | } |
---|
379 | |
---|
380 | /* Report result of entire operation (success? errors?) */ |
---|
381 | if (!retval) { |
---|
382 | mvaddstr_and_log_it(g_currentY++, 0, |
---|
383 | _("Backup and/or verify ran to completion. Everything appears to be fine.")); |
---|
384 | } else { |
---|
385 | mvaddstr_and_log_it(g_currentY++, 0, |
---|
386 | _("Backup and/or verify ran to completion. However, errors did occur.")); |
---|
387 | } |
---|
388 | |
---|
389 | if (does_file_exist("/var/cache/mindi/mondorescue.iso")) { |
---|
390 | log_to_screen |
---|
391 | (_("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it.")); |
---|
392 | } |
---|
393 | |
---|
394 | |
---|
395 | if (length_of_file("/tmp/changed.files") > 2) { |
---|
396 | if (g_text_mode) { |
---|
397 | log_to_screen |
---|
398 | (_("Type 'less /tmp/changed.files' to see which files don't match the archives")); |
---|
399 | } else { |
---|
400 | mr_msg(1, |
---|
401 | _("Type 'less /tmp/changed.files' to see which files don't match the archives")); |
---|
402 | mr_msg(2, "Calling popup_changelist_from_file()"); |
---|
403 | popup_changelist_from_file("/tmp/changed.files"); |
---|
404 | mr_msg(2, "Returned from popup_changelist_from_file()"); |
---|
405 | } |
---|
406 | } else { |
---|
407 | unlink("/tmp/changed.files"); |
---|
408 | } |
---|
409 | log_to_screen(say_at_end); |
---|
410 | mr_free(say_at_end); |
---|
411 | if (say_at_end2 != NULL) { |
---|
412 | log_to_screen(say_at_end2); |
---|
413 | mr_free(say_at_end2); |
---|
414 | } |
---|
415 | |
---|
416 | mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir); |
---|
417 | run_program_and_log_output(tmp, TRUE); |
---|
418 | mr_free(tmp); |
---|
419 | run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE); |
---|
420 | |
---|
421 | run_program_and_log_output("mount", 2); |
---|
422 | |
---|
423 | system("rm -f /var/cache/mondo-archive/last-backup.aborted"); |
---|
424 | system("rm -Rf /tmp.mondo.* /mondo.scratch.*"); |
---|
425 | if (!retval) { |
---|
426 | printf(_("Mondoarchive ran OK.\n")); |
---|
427 | } else { |
---|
428 | printf(_("Errors occurred during backup. Please check logfile.\n")); |
---|
429 | } |
---|
430 | distro_specific_kludges_at_end_of_mondoarchive(); |
---|
431 | register_pid(0, "mondo"); |
---|
432 | set_signals(FALSE); |
---|
433 | chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir |
---|
434 | system(g_erase_tmpdir_and_scratchdir); |
---|
435 | free_libmondo_global_strings(); |
---|
436 | mr_free(bkpinfo); |
---|
437 | |
---|
438 | unlink("/tmp/filelist.full"); |
---|
439 | unlink("/tmp/filelist.full.gz"); |
---|
440 | |
---|
441 | if (!g_cdrom_drive_is_here) { |
---|
442 | mr_msg(10, "FYI, g_cdrom_drive_is_here was never used"); |
---|
443 | } |
---|
444 | if (!g_dvd_drive_is_here) { |
---|
445 | mr_msg(10, "FYI, g_dvd_drive_is_here was never used"); |
---|
446 | } |
---|
447 | |
---|
448 | /* finalize log file with time stamp */ |
---|
449 | mr_msg(1, "Time finished: %s", mr_date()); |
---|
450 | mr_msg_close(); |
---|
451 | |
---|
452 | if (!g_text_mode) { |
---|
453 | popup_and_OK |
---|
454 | (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.")); |
---|
455 | log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE); |
---|
456 | finish(retval); |
---|
457 | } else { |
---|
458 | printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE); |
---|
459 | exit(retval); |
---|
460 | } |
---|
461 | |
---|
462 | return EXIT_SUCCESS; |
---|
463 | } |
---|