source: MondoRescue/branches/stable/mondo/src/mondoarchive/mondoarchive.c@ 1241

Last change on this file since 1241 was 1241, checked in by Bruno Cornec, 17 years ago
  • Fix a bug for tape mangement where block size was missing during extract (Michel Loiseleur)
  • Remove turn_wildcard_chars_into_literal_chars and use mr_stresc instead (based on an idea from Michel)
  • Property svn:keywords set to Id
File size: 12.5 KB
RevLine 
[1]1/***************************************************************************
[1080]2* $Id: mondoarchive.c 1241 2007-03-12 01:22:55Z bruno $
3*/
[1]4
5
6/**
7 * @file
8 * The main file for mondoarchive.
9 */
10
11/************************* #include statements *************************/
12#include <pthread.h>
13#include <stdio.h>
14#include <stdlib.h>
[1100]15#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
18
[1067]19#include "my-stuff.h"
[1190]20#include "mondostructures.h"
21#include "libmondo.h"
[1]22#include "mondo-cli-EXT.h"
23
[1187]24#include "mr_mem.h"
[1087]25#include "mr_str.h"
[1112]26#include "mr_msg.h"
[1102]27#include "mr_file.h"
[1087]28
[1]29// for CVS
[128]30//static char cvsid[] = "$Id: mondoarchive.c 1241 2007-03-12 01:22:55Z bruno $";
[1]31
32/************************* external variables *************************/
33extern void set_signals(int);
34extern int g_current_media_number;
[128]35extern void register_pid(pid_t, char *);
[1]36extern int g_currentY;
37extern bool g_text_mode;
38extern char *g_boot_mountpt;
39extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
40extern char *g_tmpfs_mountpt;
41extern char *g_erase_tmpdir_and_scratchdir;
42extern char *g_cdrw_drive_is_here;
[128]43static char *g_cdrom_drive_is_here = NULL;
44static char *g_dvd_drive_is_here = NULL;
[1]45extern double g_kernel_version;
46
47/***************** global vars, used only by main.c ******************/
48bool g_skip_floppies;
49long diffs;
[792]50char *ps_options = "auxww";
[928]51char *ps_proc_id = "$2";
[1]52
53extern t_bkptype g_backup_media_type;
54extern int g_loglevel;
55
56/****************** subroutines used only by main.c ******************/
57
58
59/**
60 * Print a "don't panic" message to the log and a message about the logfile to the screen.
61 */
[1151]62void welcome_to_mondoarchive(void)
[1]63{
[1108]64 mr_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
[251]65 PACKAGE_VERSION);
[1108]66 mr_msg(0, "running on %s architecture", get_architecture());
67 mr_msg(0,
[128]68 "-----------------------------------------------------------");
[1108]69 mr_msg(0,
[128]70 "NB: Mondo logs almost everything, so don't panic if you see");
[1108]71 mr_msg(0,
[128]72 "some error messages. Please read them carefully before you");
[1108]73 mr_msg(0,
[128]74 "decide to break out in a cold sweat. Despite (or perhaps");
[1108]75 mr_msg(0,
[128]76 "because of) the wealth of messages. some users are inclined");
[1108]77 mr_msg(0,
[128]78 "to stop reading this log. If Mondo stopped for some reason,");
[1108]79 mr_msg(0,
[128]80 "chances are it's detailed here. More than likely there's a");
[1108]81 mr_msg(0,
[128]82 "message at the very end of this log that will tell you what");
[1108]83 mr_msg(0,
[128]84 "is wrong. Please read it! -Devteam");
[1108]85 mr_msg(0,
[128]86 "-----------------------------------------------------------");
[1]87
[1108]88 mr_msg(0, "Zero...");
89 mr_msg(1, "One...");
90 mr_msg(2, "Two...");
91 mr_msg(3, "Three...");
92 mr_msg(4, "Four...");
93 mr_msg(5, "Five...");
94 mr_msg(6, "Six...");
95 mr_msg(7, "Seven...");
96 mr_msg(8, "Eight...");
[128]97 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[1]98}
99
100
101extern char *g_magicdev_command;
102
103/**
104 * Do whatever is necessary to insure a successful backup on the Linux distribution
105 * of the day.
106 */
[1151]107void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]108{
[1108]109 mr_msg(2, "Unmounting old ramdisks if necessary");
[128]110 stop_magicdev_if_necessary(); // for RH+Gnome users
111 run_program_and_log_output
112 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
113 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
114 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
115 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
[1]116}
117
118
119/**
120 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
121 */
[1151]122void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]123{
[1108]124 mr_msg(2, "Restarting magicdev if necessary");
[128]125 sync();
126 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]127
[1108]128 mr_msg(2, "Restarting supermounts if necessary");
[128]129 sync();
130 remount_supermounts_if_necessary(); // for Mandrake users
[1]131
[1108]132 mr_msg(2, "Unmounting /boot if necessary");
[128]133 sync();
134 unmount_boot_if_necessary(); // for Gentoo users
[1]135}
136
137/*-----------------------------------------------------------*/
138
139
140
141/**
142 * Backup/verify the user's data.
143 * What did you think it did, anyway? :-)
144 */
[128]145int main(int argc, char *argv[])
[1]146{
[128]147 struct s_bkpinfo *bkpinfo;
[1100]148 struct stat stbuf;
[1152]149 char *tmp = NULL;
[1187]150 int res = 0;
151 int retval = 0;
152 char *say_at_end = NULL;
[1190]153 char *say_at_end2 = NULL;
[1]154
[1187]155#ifdef ENABLE_NLS
156 setlocale(LC_ALL, "");
157 (void) textdomain("mondo");
158#endif
[1]159/* Make sure I'm root; abort if not */
[128]160 if (getuid() != 0) {
[1187]161 fprintf(stderr, _("Please run as root.\n"));
[128]162 exit(127);
163 }
[1]164
[1187]165 /* If -V, -v or --version then echo version no. and quit */
[128]166 if (argc == 2
167 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
168 || !strcmp(argv[argc - 1], "--version"))) {
[1187]169 printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
[128]170 exit(0);
171 }
[1]172
[1187]173 /* Initialize variables */
[128]174 malloc_libmondo_global_strings();
[1]175
[128]176 res = 0;
177 retval = 0;
178 diffs = 0;
[1187]179 printf(_("Initializing...\n"));
[1]180
[810]181 /* initialize log file with time stamp */
182 unlink(MONDO_LOGFILE);
[1132]183 mr_msg_init(MONDO_LOGFILE,4);
[1108]184 mr_msg(0, "Time started: %s", mr_date());
[1]185
[1132]186 bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
187
[807]188 /* Add the ARCH environment variable for ia64 purposes */
[1152]189 setenv("ARCH", get_architecture(), 1);
[1]190
[1228]191 /* Add MONDO_SHARE + other environment variables for mindi */
[1152]192 setenv_mondo_var();
[258]193
[1100]194 if (stat(MONDO_CACHE, &stbuf) != 0) {
195 mr_mkdir(MONDO_CACHE,0x755);
196 }
197
[1140]198 /* BERLIOS: Hardcoded to be improved */
199 unlink(MONDO_CACHE"/mindi.conf");
[1143]200 unlink(MONDORESTORECFG);
[1140]201
[807]202 /* Configure the bkpinfo structure, global file paths, etc. */
[128]203 g_main_pid = getpid();
[1108]204 mr_msg(9, "This");
[1]205
[128]206 register_pid(g_main_pid, "mondo");
207 set_signals(TRUE); // catch SIGTERM, etc.
208 run_program_and_log_output("dmesg -n1", TRUE);
[1]209
[1108]210 mr_msg(9, "Next");
[128]211 welcome_to_mondoarchive();
212 distro_specific_kludges_at_start_of_mondoarchive();
213 g_kernel_version = get_kernel_version();
[1]214
[128]215 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
216 g_loglevel = 10;
217 g_text_mode = TRUE;
218 setup_newt_stuff();
219 if (!strstr(argv[2], "filelist")) {
[1187]220 mr_msg(1,_("Sorry - filelist goes first\n"));
[128]221 finish(1);
222 } else {
223 finish(get_fattr_list(argv[2], argv[3]));
224 }
225 finish(0);
[1]226 }
[128]227 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
228 g_loglevel = 10;
229 g_text_mode = TRUE;
230 setup_newt_stuff();
231 finish(set_fattr_list(argv[2], argv[3]));
[1]232 }
[128]233
234 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
235 g_loglevel = 10;
236 g_text_mode = TRUE;
237 setup_newt_stuff();
[1241]238 tmp = mr_stresc(argv[2], WILDCHARS, BACKSLASH);
[128]239 printf("in=%s; out=%s\n", argv[2], tmp);
[1152]240 mr_free(tmp);
[128]241 finish(1);
[1]242 }
[128]243
244 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
245 g_loglevel = 10;
246 g_text_mode = TRUE;
247 setup_newt_stuff();
248 if (!strstr(argv[2], "filelist")) {
[1187]249 mr_msg(1,_("Sorry - filelist goes first\n"));
[128]250 finish(1);
251 } else {
252 finish(get_acl_list(argv[2], argv[3]));
253 }
254 finish(0);
255 }
256 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
257 g_loglevel = 10;
258 g_text_mode = TRUE;
259 setup_newt_stuff();
260 finish(set_acl_list(argv[2], argv[3]));
261 }
[1]262
[128]263 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
264 g_loglevel = 10;
265 g_text_mode = TRUE;
266 setup_newt_stuff();
[1152]267 malloc_string(tmp);
[128]268 if (find_cdrw_device(tmp)) {
[1187]269 mr_msg(1,_("Failed to find CDR-RW drive\n"));
[128]270 } else {
[1187]271 mr_msg(1,_("CD-RW is at %s\n"), tmp);
[128]272 }
273 tmp[0] = '\0';
274 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[1187]275 mr_msg(1,_("Failed to find CD-ROM drive\n"));
[128]276 } else {
[1187]277 mr_msg(1,_("CD-ROM is at %s\n"), tmp);
[128]278 }
[1152]279 mr_free(tmp);
[128]280 finish(0);
281 }
[1]282
[128]283 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
284 g_loglevel = 10;
285 g_text_mode = TRUE;
286 setup_newt_stuff();
[1152]287 malloc_string(tmp);
[128]288 if (find_dvd_device(tmp, atoi(argv[2]))) {
[1187]289 mr_msg(1,_("Failed to find DVD drive\n"));
[128]290 } else {
[1187]291 mr_msg(1,_("DVD is at %s\n"), tmp);
[128]292 }
[1152]293 mr_free(tmp);
[128]294 finish(0);
295 }
[1]296
[128]297 if (argc > 2 && !strcmp(argv[1], "disksize")) {
298 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
299 finish(0);
300 }
301 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
302 if (is_dev_an_NTFS_dev(argv[2])) {
[1187]303 mr_msg(1,_("%s is indeed an NTFS dev\n"), argv[2]);
[128]304 } else {
[1187]305 mr_msg(1,_("%s is _not_ an NTFS dev\n"), argv[2]);
[128]306 }
307 finish(0);
308 }
309
310 if (pre_param_configuration(bkpinfo)) {
311 fatal_error
312 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
313 }
[1140]314 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
315 bkpinfo->scratchdir);
[128]316
[1140]317 /* Process command line, if there is one. If not, ask user for info. */
[128]318 if (argc == 1) {
319 g_text_mode = FALSE;
320 setup_newt_stuff();
321 res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
322 if (res) {
323 fatal_error
324 ("Syntax error. Please review the parameters you have supplied and try again.");
325 }
326 } else {
327 res = handle_incoming_parameters(argc, argv, bkpinfo);
328 if (res) {
[1187]329 mr_msg(1,
330 _("Errors were detected in the command line you supplied.\n"));
331 mr_msg(1,_("Please review the log file - %s \n"),MONDO_LOGFILE);
[1108]332 mr_msg(1, "Mondoarchive will now exit.");
[128]333 finish(1);
334 }
335 setup_newt_stuff();
[1]336 }
337
338/* Finish configuring global structures */
[128]339 if (post_param_configuration(bkpinfo)) {
340 fatal_error
341 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
342 }
[1]343
[128]344 log_to_screen
[1187]345 (_("BusyBox's sources are available from http://www.busybox.net"));
346
[128]347 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
348 bkpinfo->scratchdir);
[1]349
[128]350 /* If we're meant to backup then backup */
351 if (bkpinfo->backup_data) {
[541]352 res = backup_data(bkpinfo);
[128]353 retval += res;
354 if (res) {
[1187]355 mr_strcat(say_at_end,
356 _("Data archived. Please check the logs, just as a precaution. "));
[128]357 } else {
[1187]358 mr_strcat(say_at_end, _("Data archived OK. "));
[128]359 }
360 }
[1]361
[1187]362 /* If we're meant to verify then verify */
[128]363 if (bkpinfo->verify_data) {
364 res = verify_data(bkpinfo);
365 if (res < 0) {
[1190]366 mr_asprintf(&say_at_end2, _("%d difference%c found."), -res,
[128]367 (-res != 1) ? 's' : ' ');
368 res = 0;
369 }
370 retval += res;
371 }
[1]372
[1187]373 /* Offer to write floppy disk images to physical disks */
[128]374 if (bkpinfo->backup_data && !g_skip_floppies) {
375 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
376 retval += res;
377 }
[1]378
[1187]379 /* Report result of entire operation (success? errors?) */
[128]380 if (!retval) {
381 mvaddstr_and_log_it(g_currentY++, 0,
[1187]382 _("Backup and/or verify ran to completion. Everything appears to be fine."));
[128]383 } else {
384 mvaddstr_and_log_it(g_currentY++, 0,
[1187]385 _("Backup and/or verify ran to completion. However, errors did occur."));
[128]386 }
[1]387
[1044]388 if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
[128]389 log_to_screen
[1187]390 (_("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it."));
[128]391 }
[1]392
393
[128]394 if (length_of_file("/tmp/changed.files") > 2) {
395 if (g_text_mode) {
396 log_to_screen
[1187]397 (_("Type 'less /tmp/changed.files' to see which files don't match the archives"));
[128]398 } else {
[1108]399 mr_msg(1,
[1187]400 _("Type 'less /tmp/changed.files' to see which files don't match the archives"));
[1108]401 mr_msg(2, "Calling popup_changelist_from_file()");
[128]402 popup_changelist_from_file("/tmp/changed.files");
[1108]403 mr_msg(2, "Returned from popup_changelist_from_file()");
[128]404 }
405 } else {
406 unlink("/tmp/changed.files");
407 }
408 log_to_screen(say_at_end);
[1187]409 mr_free(say_at_end);
[1190]410 if (say_at_end2 != NULL) {
411 log_to_screen(say_at_end2);
412 mr_free(say_at_end2);
413 }
[1187]414
[1152]415 mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
[128]416 run_program_and_log_output(tmp, TRUE);
[1152]417 mr_free(tmp);
[128]418 run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
[1]419
[128]420 run_program_and_log_output("mount", 2);
[1]421
[128]422 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
423 system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
424 if (!retval) {
[1187]425 printf(_("Mondoarchive ran OK.\n"));
[128]426 } else {
[1187]427 printf(_("Errors occurred during backup. Please check logfile.\n"));
[128]428 }
429 distro_specific_kludges_at_end_of_mondoarchive();
430 register_pid(0, "mondo");
431 set_signals(FALSE);
432 chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
433 system(g_erase_tmpdir_and_scratchdir);
434 free_libmondo_global_strings();
[1080]435 mr_free(bkpinfo);
[1]436
[128]437 unlink("/tmp/filelist.full");
438 unlink("/tmp/filelist.full.gz");
[1]439
[128]440 if (!g_cdrom_drive_is_here) {
[1108]441 mr_msg(10, "FYI, g_cdrom_drive_is_here was never used");
[128]442 }
443 if (!g_dvd_drive_is_here) {
[1108]444 mr_msg(10, "FYI, g_dvd_drive_is_here was never used");
[128]445 }
[1]446
[810]447 /* finalize log file with time stamp */
[1187]448 mr_msg(1, "Time finished: %s", mr_date());
[1132]449 mr_msg_close();
[810]450
[128]451 if (!g_text_mode) {
452 popup_and_OK
[1187]453 (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
454 log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
[128]455 finish(retval);
456 } else {
[1187]457 printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
[128]458 exit(retval);
459 }
460
461 return EXIT_SUCCESS;
[1]462}
Note: See TracBrowser for help on using the repository browser.