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

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

r5035@localhost: bruno | 2012-11-09 03:17:01 +0100

  • Fix a compilation error and most compilation warnings
  • Property svn:keywords set to Id
File size: 13.0 KB
RevLine 
[1]1/***************************************************************************
[2211]2$Id: mondoarchive.c 3060 2012-11-10 04:05:37Z bruno $
3* The main file for mondoarchive.
4*/
[1]5
6/************************* #include statements *************************/
7#include <pthread.h>
8#include <stdio.h>
9#include <stdlib.h>
[1930]10#include "my-stuff.h"
[2211]11#include "mr_mem.h"
[1]12#include "../common/mondostructures.h"
13#include "../common/libmondo.h"
[1917]14#include "../common/libmondo-cli-EXT.h"
[1383]15#include "../common/libmondo-tools-EXT.h"
[1315]16#include "mondoarchive.h"
[1]17
18// for CVS
[128]19//static char cvsid[] = "$Id: mondoarchive.c 3060 2012-11-10 04:05:37Z bruno $";
[1]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;
[1736]27extern bool g_remount_floppy_at_end;
[1]28extern char *g_cdrw_drive_is_here;
[128]29static char *g_cdrom_drive_is_here = NULL;
30static char *g_dvd_drive_is_here = NULL;
[1]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
[1917]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/* Do we use extended attributes and acl ?
49 * * By default no, use --acl & --attr options to force their usage */
50char *g_getfacl = NULL;
51char *g_getfattr = NULL;
52
[1645]53/* Reference to global bkpinfo */
54struct s_bkpinfo *bkpinfo;
55
[2211]56/* No cleanup for the moment */
57void (*mr_cleanup)(void) = NULL;
58
[1967]59/* To be coded */
60void free_MR_global_filenames(void) {
61}
62
[1]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 */
[1236]69void welcome_to_mondoarchive(void)
[1]70{
[1375]71 char *tmp = NULL;
72
[128]73 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
[251]74 PACKAGE_VERSION);
[1375]75 log_msg(0, "running %s binaries", get_architecture());
76 tmp = get_uname_m();
77 log_msg(0, "running on %s architecture", tmp);
[2241]78 mr_free(tmp);
[128]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 "-----------------------------------------------------------");
[1]99
[128]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);
[1]110}
111
112
113extern 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 */
[1236]119void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]120{
[128]121 log_msg(2, "Unmounting old ramdisks if necessary");
122 stop_magicdev_if_necessary(); // for RH+Gnome users
[2030]123 /*
[128]124 run_program_and_log_output
125 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
[2030]126 */
[128]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
[1]131}
132
133
134
135/**
136 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
137 */
[1236]138void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]139{
[128]140 log_msg(2, "Restarting magicdev if necessary");
141 sync();
142 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]143
[128]144 log_msg(2, "Restarting autofs if necessary");
145 sync();
146 // restart_autofs_if_necessary(); // for Xandros users
[1]147
[128]148 log_msg(2, "Restarting supermounts if necessary");
149 sync();
150 remount_supermounts_if_necessary(); // for Mandrake users
[1]151
[128]152 log_msg(2, "Unmounting /boot if necessary");
153 sync();
154 unmount_boot_if_necessary(); // for Gentoo users
[1]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 */
[128]165int main(int argc, char *argv[])
[1]166{
[2241]167 char *tmp = NULL;
[2113]168 char *tmp1 = NULL;
[128]169 int res, retval;
[2211]170 char *say_at_end = NULL;
[2982]171 FILE *fin = NULL;
[1]172
173/* Make sure I'm root; abort if not */
[128]174 if (getuid() != 0) {
[541]175 fprintf(stderr, "Please run as root.\r\n");
[128]176 exit(127);
177 }
[1]178
179/* If -V, -v or --version then echo version no. and quit */
[128]180 if (argc == 2
181 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
182 || !strcmp(argv[argc - 1], "--version"))) {
[541]183 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
[128]184 exit(0);
185 }
[1]186
187/* Initialize variables */
188
[1652]189 printf("Initializing...\n");
[2687]190 if (!(bkpinfo = (struct s_bkpinfo *)malloc(sizeof(struct s_bkpinfo)))) {
[1652]191 fatal_error("Cannot malloc bkpinfo");
192 }
193 reset_bkpinfo();
[1]194
[128]195 res = 0;
196 retval = 0;
197 diffs = 0;
[1652]198 malloc_libmondo_global_strings();
[1]199
[810]200 /* initialize log file with time stamp */
201 unlink(MONDO_LOGFILE);
[813]202 log_msg(0, "Time started: %s", mr_date());
[1]203
[807]204 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[2211]205 mr_asprintf(&tmp1,"%s:/sbin:/usr/sbin:/usr/local/sbin",getenv("PATH"));
[2113]206 setenv("PATH", tmp1, 1);
207 paranoid_free(tmp1);
[1]208
[807]209 /* Add the ARCH environment variable for ia64 purposes */
[2211]210 mr_asprintf(&tmp1,"%s",get_architecture());
[2113]211 setenv("ARCH", tmp1, 1);
212 paranoid_free(tmp1);
[1]213
[424]214 /* Add MONDO_SHARE environment variable for mindi */
215 setenv_mondo_share();
[258]216
[807]217 /* Configure the bkpinfo structure, global file paths, etc. */
[128]218 g_main_pid = getpid();
219 log_msg(9, "This");
[1]220
[128]221 set_signals(TRUE); // catch SIGTERM, etc.
222 run_program_and_log_output("dmesg -n1", TRUE);
[1]223
[128]224 log_msg(9, "Next");
[1747]225 make_hole_for_dir(MONDO_CACHE);
226
[128]227 welcome_to_mondoarchive();
228 distro_specific_kludges_at_start_of_mondoarchive();
229 g_kernel_version = get_kernel_version();
[1]230
[128]231 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
232 g_loglevel = 10;
233 g_text_mode = TRUE;
234 setup_newt_stuff();
235 if (!strstr(argv[2], "filelist")) {
[541]236 printf("Sorry - filelist goes first\n");
[128]237 finish(1);
238 } else {
239 finish(get_fattr_list(argv[2], argv[3]));
240 }
241 finish(0);
[1]242 }
[128]243 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
244 g_loglevel = 10;
245 g_text_mode = TRUE;
246 setup_newt_stuff();
247 finish(set_fattr_list(argv[2], argv[3]));
[1]248 }
[128]249
250 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
251 g_loglevel = 10;
252 g_text_mode = TRUE;
253 setup_newt_stuff();
[2241]254 malloc_string(tmp);
[128]255 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
256 printf("in=%s; out=%s\n", argv[2], tmp);
[2241]257 paranoid_free(tmp);
[128]258 finish(1);
[1]259 }
[128]260
261 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
262 g_loglevel = 10;
263 g_text_mode = TRUE;
264 setup_newt_stuff();
265 if (!strstr(argv[2], "filelist")) {
[541]266 printf("Sorry - filelist goes first\n");
[128]267 finish(1);
268 } else {
269 finish(get_acl_list(argv[2], argv[3]));
270 }
271 finish(0);
272 }
273 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
274 g_loglevel = 10;
275 g_text_mode = TRUE;
276 setup_newt_stuff();
277 finish(set_acl_list(argv[2], argv[3]));
278 }
[2982]279 if (argc >= 2 && !strcmp(argv[1], "mkraidtab")) {
280 g_loglevel = 10;
281 g_text_mode = TRUE;
282 setup_newt_stuff();
283#undef MDSTAT_FILE
284#define MDSTAT_FILE "/tmp/mdstat"
285 if (!(fin = fopen(MDSTAT_FILE, "r"))) {
286 log_msg(1, "Could not open %s.\n", MDSTAT_FILE);
287 finish(1);
288 }
[1]289
[2982]290 create_raidtab_from_mdstat(MDSTAT_FILE,"/tmp/raidtab");
291 finish(0);
292 }
293
[128]294 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
295 g_loglevel = 10;
296 g_text_mode = TRUE;
297 setup_newt_stuff();
[2241]298 malloc_string(tmp);
[128]299 if (find_cdrw_device(tmp)) {
[541]300 printf("Failed to find CDR-RW drive\n");
[128]301 } else {
[541]302 printf("CD-RW is at %s\n", tmp);
[128]303 }
304 tmp[0] = '\0';
305 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[541]306 printf("Failed to find CD-ROM drive\n");
[128]307 } else {
[541]308 printf("CD-ROM is at %s\n", tmp);
[128]309 }
[2241]310 paranoid_free(tmp);
[128]311 finish(0);
312 }
[1]313
[128]314 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
315 g_loglevel = 10;
316 g_text_mode = TRUE;
317 setup_newt_stuff();
[2241]318 malloc_string(tmp);
[128]319 if (find_dvd_device(tmp, atoi(argv[2]))) {
[541]320 printf("Failed to find DVD drive\n");
[128]321 } else {
[541]322 printf("DVD is at %s\n", tmp);
[128]323 }
[2241]324 paranoid_free(tmp);
[128]325 finish(0);
326 }
[1]327
[128]328 if (argc > 2 && !strcmp(argv[1], "disksize")) {
329 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
330 finish(0);
331 }
332 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
333 if (is_dev_an_NTFS_dev(argv[2])) {
[541]334 printf("%s is indeed an NTFS dev\n", argv[2]);
[128]335 } else {
[541]336 printf("%s is _not_ an NTFS dev\n", argv[2]);
[128]337 }
338 finish(0);
339 }
340
[1645]341 if (pre_param_configuration()) {
[128]342 fatal_error
343 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
344 }
345
[1]346/* Process command line, if there is one. If not, ask user for info. */
[128]347 if (argc == 1) {
348 g_text_mode = FALSE;
349 setup_newt_stuff();
[1645]350 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
[128]351 if (res) {
352 fatal_error
353 ("Syntax error. Please review the parameters you have supplied and try again.");
354 }
355 } else {
[1645]356 res = handle_incoming_parameters(argc, argv);
[128]357 if (res) {
358 printf
[541]359 ("Errors were detected in the command line you supplied.\n");
[1319]360 printf("Please review the log file - %s\n", MONDO_LOGFILE );
[128]361 log_msg(1, "Mondoarchive will now exit.");
362 finish(1);
363 }
364 setup_newt_stuff();
[1]365 }
366
367/* Finish configuring global structures */
[1645]368 if (post_param_configuration()) {
[128]369 fatal_error
370 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
371 }
[1]372
[128]373 log_to_screen
[541]374 ("BusyBox's sources are available from http://www.busybox.net");
[1]375
[128]376 /* If we're meant to backup then backup */
377 if (bkpinfo->backup_data) {
[1645]378 res = backup_data();
[128]379 retval += res;
380 if (res) {
[2290]381 mr_asprintf(&say_at_end, "Data archived. Please check the logs, just as a precaution. ");
[128]382 } else {
[2211]383 mr_asprintf(&say_at_end, "Data archived OK. ");
[128]384 }
385 }
[1]386
387/* If we're meant to verify then verify */
[128]388 if (bkpinfo->verify_data) {
[1645]389 res = verify_data();
[128]390 if (res < 0) {
[2241]391 mr_asprintf(&tmp, "%d difference%c found.", -res,
[128]392 (-res != 1) ? 's' : ' ');
[2290]393 mr_asprintf(&say_at_end, "%s", tmp);
[128]394 log_to_screen(tmp);
[2241]395 mr_free(tmp);
[128]396 res = 0;
397 }
398 retval += res;
399 }
[1]400
401/* Report result of entire operation (success? errors?) */
[128]402 if (!retval) {
403 mvaddstr_and_log_it(g_currentY++, 0,
[541]404 "Backup and/or verify ran to completion. Everything appears to be fine.");
[128]405 } else {
406 mvaddstr_and_log_it(g_currentY++, 0,
[541]407 "Backup and/or verify ran to completion. However, errors did occur.");
[128]408 }
[1]409
[1437]410 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
[128]411 log_to_screen
[1437]412 (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
[128]413 }
[1]414
[1747]415 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
[128]416 if (g_text_mode) {
[1747]417 log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]418 } else {
[1747]419 log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]420 log_msg(2, "Calling popup_changelist_from_file()");
[1747]421 popup_changelist_from_file(MONDO_CACHE"/changed.files");
[128]422 log_msg(2, "Returned from popup_changelist_from_file()");
423 }
424 } else {
[1747]425 unlink(MONDO_CACHE"/changed.files");
[128]426 }
[2211]427 if (say_at_end != NULL) {
428 log_to_screen(say_at_end);
429 paranoid_free(say_at_end);
430 }
[2241]431 mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
[128]432 run_program_and_log_output(tmp, TRUE);
[2241]433 mr_free(tmp);
[1697]434 if (bkpinfo->backup_media_type == usb) {
435 log_msg(1, "Unmounting USB device.");
[2241]436 mr_asprintf(&tmp, "umount %s1", bkpinfo->media_device);
[1697]437 run_program_and_log_output(tmp, TRUE);
[2241]438 mr_free(tmp);
[1697]439 }
[1]440
[128]441 run_program_and_log_output("mount", 2);
[1]442
[3060]443 paranoid_system("rm -f "MONDO_CACHE"/last-backup.aborted");
[128]444 if (!retval) {
[541]445 printf("Mondoarchive ran OK.\n");
[128]446 } else {
[541]447 printf("Errors occurred during backup. Please check logfile.\n");
[128]448 }
449 distro_specific_kludges_at_end_of_mondoarchive();
450 set_signals(FALSE);
[1644]451
[128]452 free_libmondo_global_strings();
[1703]453
[1]454
[128]455 if (!g_cdrom_drive_is_here) {
456 log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
457 }
458 if (!g_dvd_drive_is_here) {
459 log_msg(10, "FYI, g_dvd_drive_is_here was never used");
460 }
[1]461
[810]462 /* finalize log file with time stamp */
[813]463 log_msg(0, "Time finished: %s", mr_date());
[810]464
[3060]465 if (chdir("/tmp")) {
466 // FIXME
467 }
[1644]468
[128]469 if (!g_text_mode) {
470 popup_and_OK
[541]471 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
472 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
[128]473 } else {
[541]474 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[128]475 }
[1703]476 finish(retval);
[128]477
478 return EXIT_SUCCESS;
[1]479}
Note: See TracBrowser for help on using the repository browser.