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