source: MondoRescue/branches/3.2/mondo/src/mondoarchive/mondoarchive.c@ 3208

Last change on this file since 3208 was 3208, checked in by Bruno Cornec, 10 years ago
  • Fix typos preventnig compilation !
  • revert mr_getline modifications which where in fact incorrect
  • initialize log earlier so all messages go indeed in it
  • Property svn:keywords set to Id
File size: 13.0 KB
Line 
1/***************************************************************************
2$Id: mondoarchive.c 3208 2013-12-10 07:50:00Z 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 3208 2013-12-10 07:50:00Z bruno $";
21
22/************************* external variables *************************/
23extern void set_signals(int);
24extern int g_current_media_number;
25extern int g_currentY;
26extern bool g_text_mode;
27extern char *g_boot_mountpt;
28extern bool g_remount_floppy_at_end;
29extern char *g_cdrw_drive_is_here;
30static char *g_cdrom_drive_is_here = NULL;
31static char *g_dvd_drive_is_here = NULL;
32extern double g_kernel_version;
33
34/***************** global vars, used only by main.c ******************/
35long diffs = 0L;
36
37extern t_bkptype g_backup_media_type;
38extern int g_loglevel;
39
40extern 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 */
49bool g_ISO_restore_mode = FALSE;
50
51/* Whether we should fail immediately at first error */
52bool 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 */
56char *g_getfacl = NULL;
57char *g_getfattr = NULL;
58
59/* Reference to global bkpinfo */
60struct s_bkpinfo *bkpinfo;
61
62/* No cleanup for the moment */
63void (*mr_cleanup)(void) = NULL;
64
65/* To be coded */
66void 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 */
75void 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 */
112void 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 */
131void 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 */
158int 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 /* Make sure I'm root; abort if not */
168 if (getuid() != 0) {
169 fprintf(stderr, "Please run as root.\r\n");
170 finish(127);
171 }
172
173 /* If -V, -v or --version then echo version no. and quit */
174 if (argc == 2 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V") || !strcmp(argv[argc - 1], "--version"))) {
175 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
176 finish(0);
177 }
178
179 /* initialize log file with time stamp */
180 unlink(MONDO_LOGFILE);
181 log_msg(0, "Time started: %s", mr_date());
182
183 /* Initialize variables */
184 if (!(bkpinfo = (struct s_bkpinfo *)malloc(sizeof(struct s_bkpinfo)))) {
185 fprintf(stderr, "Cannot malloc bkpinfo\n");
186 finish(-1);
187 }
188 log_msg(9, "reset_bkpinfo");
189 reset_bkpinfo();
190
191 /* Memory allocation is done in those functions */
192 malloc_libmondo_global_strings();
193
194 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
195 mr_asprintf(tmp1,"%s:/sbin:/usr/sbin:/usr/local/sbin",getenv("PATH"));
196 setenv("PATH", tmp1, 1);
197 mr_free(tmp1);
198
199 /* Add the ARCH environment variable for ia64 purposes */
200 mr_asprintf(tmp1,"%s",get_architecture());
201 setenv("ARCH", tmp1, 1);
202 mr_free(tmp1);
203
204 /* Add MONDO_SHARE environment variable for mindi */
205 setenv_mondo_share();
206
207 /* Configure the bkpinfo structure, global file paths, etc. */
208 g_main_pid = getpid();
209 log_msg(9, "This");
210
211 set_signals(TRUE); // catch SIGTERM, etc.
212 run_program_and_log_output("dmesg -n1", TRUE);
213
214 log_msg(9, "Next");
215 make_hole_for_dir(MONDO_CACHE);
216
217 welcome_to_mondoarchive();
218 distro_specific_kludges_at_start_of_mondoarchive();
219 g_kernel_version = get_kernel_version();
220
221 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
222 g_loglevel = 10;
223 g_text_mode = TRUE;
224 setup_newt_stuff();
225 if (!strstr(argv[2], "filelist")) {
226 printf("Sorry - filelist goes first\n");
227 finish(1);
228 } else {
229 finish(get_fattr_list(argv[2], argv[3]));
230 }
231 finish(0);
232 }
233 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
234 g_loglevel = 10;
235 g_text_mode = TRUE;
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 g_text_mode = TRUE;
243 setup_newt_stuff();
244 tmp1 = mr_stresc(argv[2], "[]*?", '\\', '\'');
245 printf("in=%s; out=%s\n", argv[2], tmp1);
246 mr_free(tmp1);
247 finish(1);
248 }
249
250 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
251 g_loglevel = 10;
252 g_text_mode = TRUE;
253 setup_newt_stuff();
254 if (!strstr(argv[2], "filelist")) {
255 printf("Sorry - filelist goes first\n");
256 finish(1);
257 } else {
258 finish(get_acl_list(argv[2], argv[3]));
259 }
260 finish(0);
261 }
262 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
263 g_loglevel = 10;
264 g_text_mode = TRUE;
265 setup_newt_stuff();
266 finish(set_acl_list(argv[2], argv[3]));
267 }
268 if (argc >= 2 && !strcmp(argv[1], "mkraidtab")) {
269 g_loglevel = 10;
270 g_text_mode = TRUE;
271 setup_newt_stuff();
272#undef MDSTAT_FILE
273#define MDSTAT_FILE "/tmp/mdstat"
274 if (!(fin = fopen(MDSTAT_FILE, "r"))) {
275 log_msg(1, "Could not open %s.\n", MDSTAT_FILE);
276 finish(1);
277 }
278
279 create_raidtab_from_mdstat(MDSTAT_FILE,"/tmp/raidtab");
280 finish(0);
281 }
282
283 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
284 g_loglevel = 10;
285 g_text_mode = TRUE;
286 setup_newt_stuff();
287 malloc_string(tmp);
288 if (find_cdrw_device(tmp)) {
289 printf("Failed to find CDR-RW drive\n");
290 } else {
291 printf("CD-RW is at %s\n", tmp);
292 }
293 tmp[0] = '\0';
294 if (find_cdrom_device(tmp, atoi(argv[2]))) {
295 printf("Failed to find CD-ROM drive\n");
296 } else {
297 printf("CD-ROM is at %s\n", tmp);
298 }
299 mr_free(tmp);
300 finish(0);
301 }
302
303 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
304 g_loglevel = 10;
305 g_text_mode = TRUE;
306 setup_newt_stuff();
307 malloc_string(tmp);
308 if (find_dvd_device(tmp, atoi(argv[2]))) {
309 printf("Failed to find DVD drive\n");
310 } else {
311 printf("DVD is at %s\n", tmp);
312 }
313 mr_free(tmp);
314 finish(0);
315 }
316
317 if (argc > 2 && !strcmp(argv[1], "disksize")) {
318 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
319 finish(0);
320 }
321 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
322 if (is_dev_an_NTFS_dev(argv[2])) {
323 printf("%s is indeed an NTFS dev\n", argv[2]);
324 } else {
325 printf("%s is _not_ an NTFS dev\n", argv[2]);
326 }
327 finish(0);
328 }
329
330 if (pre_param_configuration()) {
331 fatal_error("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
332 }
333
334 /* Process command line, if there is one. If not, ask user for info. */
335 if (argc == 1) {
336 g_text_mode = FALSE;
337 setup_newt_stuff();
338 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
339 if (res) {
340 fatal_error("Syntax error. Please review the parameters you have supplied and try again.");
341 }
342 } else {
343 res = handle_incoming_parameters(argc, argv);
344 if (res) {
345 printf("Errors were detected in the command line you supplied.\n");
346 printf("Please review the log file - %s\n", MONDO_LOGFILE );
347 log_msg(1, "Mondoarchive will now exit.");
348 finish(1);
349 }
350 setup_newt_stuff();
351 }
352
353 /* Finish configuring global structures */
354 if (post_param_configuration()) {
355 fatal_error("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
356 }
357
358 /* If we're meant to backup then backup */
359 if (bkpinfo->backup_data) {
360 res = backup_data();
361 retval += res;
362 if (res) {
363 mr_asprintf(say_at_end, "Data archived. Please check the logs, just as a precaution. ");
364 } else {
365 mr_asprintf(say_at_end, "Data archived OK. ");
366 }
367 }
368
369/* If we're meant to verify then verify */
370 if (bkpinfo->verify_data) {
371 res = verify_data();
372 if (res < 0) {
373 mr_asprintf(tmp, "%d difference%c found.", -res, (-res != 1) ? 's' : ' ');
374 mr_asprintf(say_at_end, "%s", tmp);
375 log_to_screen(tmp);
376 mr_free(tmp);
377 res = 0;
378 }
379 retval += res;
380 }
381
382/* Report result of entire operation (success? errors?) */
383 if (!retval) {
384 mvaddstr_and_log_it(g_currentY++, 0,
385 "Backup and/or verify ran to completion. Everything appears to be fine.");
386 } else {
387 mvaddstr_and_log_it(g_currentY++, 0,
388 "Backup and/or verify ran to completion. However, errors did occur.");
389 }
390
391 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
392 log_to_screen
393 (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
394 }
395
396 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
397 if (g_text_mode) {
398 log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
399 } else {
400 log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
401 log_msg(2, "Calling popup_changelist_from_file()");
402 popup_changelist_from_file(MONDO_CACHE"/changed.files");
403 log_msg(2, "Returned from popup_changelist_from_file()");
404 }
405 } else {
406 unlink(MONDO_CACHE"/changed.files");
407 }
408 if (say_at_end != NULL) {
409 log_to_screen(say_at_end);
410 mr_free(say_at_end);
411 }
412 mr_asprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
413 run_program_and_log_output(tmp, TRUE);
414 mr_free(tmp);
415 if (bkpinfo->backup_media_type == usb) {
416 log_msg(1, "Unmounting USB device.");
417 if (bkpinfo->media_device == NULL) {
418 fatal_error("USB device set to NULL");
419 }
420 mr_asprintf(tmp, "umount %s1", bkpinfo->media_device);
421 run_program_and_log_output(tmp, TRUE);
422 mr_free(tmp);
423 }
424
425 run_program_and_log_output("mount", 2);
426
427 paranoid_system("rm -f "MONDO_CACHE"/last-backup.aborted");
428 if (!retval) {
429 printf("Mondoarchive ran OK.\n");
430 } else {
431 printf("Errors occurred during backup. Please check logfile.\n");
432 }
433 distro_specific_kludges_at_end_of_mondoarchive();
434 set_signals(FALSE);
435
436 free_libmondo_global_strings();
437
438
439 if (!g_cdrom_drive_is_here) {
440 log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
441 }
442 if (!g_dvd_drive_is_here) {
443 log_msg(10, "FYI, g_dvd_drive_is_here was never used");
444 }
445
446 /* finalize log file with time stamp */
447 log_msg(0, "Time finished: %s", mr_date());
448
449 if (chdir("/tmp")) {
450 // FIXME
451 }
452
453 if (!g_text_mode) {
454 popup_and_OK("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}
Note: See TracBrowser for help on using the repository browser.