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