source: MondoRescue/trunk/mondo/src/mondoarchive/mondoarchive.c@ 1188

Last change on this file since 1188 was 1188, checked in by Bruno Cornec, 17 years ago

Stable merges again

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