source: MondoRescue/branches/2.2.6/mondo/src/mondoarchive/mondoarchive.c@ 1967

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