source: MondoRescue/branches/3.3/mondo/src/mondoarchive/mondoarchive.c@ 3882

Last change on this file since 3882 was 3882, checked in by Bruno Cornec, 4 months ago

More cleanup

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