source: MondoRescue/branches/3.1/mondo/src/mondoarchive/mondoarchive.c@ 3148

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

2nd phase for svn merge -r 2935:3146 ../3.0

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