source: MondoRescue/branches/3.0/mondo/src/mondoarchive/mondoarchive.c@ 3185

Last change on this file since 3185 was 3185, checked in by Bruno Cornec, 11 years ago

Simplify the interface of mr_getline and mr_asprintf. With 3.1 compatibility now will allow backports from this branch into 3.0

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