2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: amflush.c,v 1.95 2006/07/25 21:41:24 martinea Exp $
29 * write files from work directory onto tape
41 #include "server_util.h"
42 #include "timestamp.h"
45 static struct option long_options[] = {
46 {"version" , 0, NULL, 1},
50 static char *conf_logdir;
53 char *reporter_program;
54 char *logroll_program;
56 char *amflush_timestamp;
57 char *amflush_datestamp;
60 void flush_holdingdisk(char *diskdir, char *datestamp);
61 static GSList * pick_datestamp(void);
62 void confirm(GSList *datestamp_list);
63 void redirect_stderr(void);
66 static int get_letter_from_user(void);
76 char **datearg = NULL;
81 int conf_usetimestamps;
85 pid_t driver_pid, reporter_pid;
88 GSList *holding_list=NULL, *holding_file;
90 char date_string[100];
91 char date_string_standard[100];
98 GSList *datestamp_list = NULL;
99 config_overrides_t *cfg_ovr;
100 char **config_options;
101 find_result_t *holding_files;
102 disklist_t holding_disklist = { NULL, NULL };
105 * Configure program for internationalization:
106 * 1) Only set the message locale for now.
107 * 2) Set textdomain for all amanda related programs to "amanda"
108 * We don't want to be forced to support dozens of message catalogs.
110 setlocale(LC_MESSAGES, "C");
111 textdomain("amanda");
116 set_pname("amflush");
118 /* Don't die when child closes pipe */
119 signal(SIGPIPE, SIG_IGN);
121 dbopen(DBG_SUBDIR_SERVER);
123 add_amanda_log_handler(amanda_log_stderr);
128 /* process arguments */
130 cfg_ovr = new_config_overrides(argc/2);
131 while((opt = getopt_long(argc, argv, "bfso:D:", long_options, NULL)) != EOF) {
133 case 1 : printf("amflush-%s\n", VERSION);
139 case 'f': foreground = 1;
141 case 's': redirect = 0;
143 case 'o': add_config_override_opt(cfg_ovr, optarg);
145 case 'D': if (datearg == NULL)
146 datearg = alloc(21*SIZEOF(char *));
147 if(nb_datearg == 20) {
148 g_fprintf(stderr,_("maximum of 20 -D arguments.\n"));
151 datearg[nb_datearg++] = stralloc(optarg);
152 datearg[nb_datearg] = NULL;
156 argc -= optind, argv += optind;
158 if(!foreground && !redirect) {
159 g_fprintf(stderr,_("Can't redirect to stdout/stderr if not in forground.\n"));
164 error(_("Usage: amflush [-b] [-f] [-s] [-D date]* [-o configoption]* <confdir> [host [disk]* ]*"));
168 set_config_overrides(cfg_ovr);
169 config_init(CONFIG_INIT_EXPLICIT_NAME,
172 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
173 read_diskfile(conf_diskfile, &diskq);
174 amfree(conf_diskfile);
176 if (config_errors(NULL) >= CFGERR_WARNINGS) {
177 config_print_errors();
178 if (config_errors(NULL) >= CFGERR_ERRORS) {
179 g_critical(_("errors processing config file"));
183 check_running_as(RUNNING_AS_DUMPUSER);
185 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
187 /* load DLEs from the holding disk, in case there's anything to flush there */
188 search_holding_disk(&holding_files, &holding_disklist);
189 /* note that the dumps are added to the global disklist, so we need not
190 * consult holding_files or holding_disklist after this. The holding-only
191 * dumps will be filtered properly by match_disklist, setting the dp->todo
192 * flag appropriately. */
194 errstr = match_disklist(&diskq, argc-1, argv+1);
196 g_printf(_("%s"),errstr);
200 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
201 if(read_tapelist(conf_tapelist)) {
202 error(_("could not load tapelist \"%s\""), conf_tapelist);
205 amfree(conf_tapelist);
207 conf_usetimestamps = getconf_boolean(CNF_USETIMESTAMPS);
209 amflush_datestamp = get_datestamp_from_time(0);
210 if(conf_usetimestamps == 0) {
211 amflush_timestamp = stralloc(amflush_datestamp);
214 amflush_timestamp = get_timestamp_from_time(0);
217 conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
218 conf_logfile = vstralloc(conf_logdir, "/log", NULL);
219 if (access(conf_logfile, F_OK) == 0) {
220 run_amcleanup(get_config_name());
222 if (access(conf_logfile, F_OK) == 0) {
223 char *process_name = get_master_process(conf_logfile);
224 error(_("%s exists: %s is already running, or you must run amcleanup"), conf_logfile, process_name);
228 driver_program = vstralloc(amlibexecdir, "/", "driver", NULL);
229 reporter_program = vstralloc(sbindir, "/", "amreport", NULL);
230 logroll_program = vstralloc(amlibexecdir, "/", "amlogroll", NULL);
232 tapedev = getconf_str(CNF_TAPEDEV);
233 tpchanger = getconf_str(CNF_TPCHANGER);
234 if (tapedev == NULL && tpchanger == NULL) {
235 error(_("No tapedev or tpchanger specified"));
238 /* if dates were specified (-D), then use match_datestamp
239 * against the list of all datestamps to turn that list
240 * into a set of existing datestamps (basically, evaluate the
241 * expressions into actual datestamps) */
243 GSList *all_datestamps;
247 all_datestamps = holding_get_all_datestamps();
248 for(datestamp = all_datestamps; datestamp != NULL; datestamp = datestamp->next) {
250 for(i=0; i<nb_datearg && ok==0; i++) {
251 ok = match_datestamp(datearg[i], (char *)datestamp->data);
254 datestamp_list = g_slist_insert_sorted(datestamp_list,
255 stralloc((char *)datestamp->data),
258 slist_free_full(all_datestamps, g_free);
261 /* otherwise, in batch mode, use all datestamps */
263 datestamp_list = holding_get_all_datestamps();
265 /* or allow the user to pick datestamps */
267 datestamp_list = pick_datestamp();
271 if(!datestamp_list) {
272 g_printf(_("Could not find any Amanda directories to flush.\n"));
276 holding_list = holding_get_files_for_flush(datestamp_list);
277 if (holding_list == NULL) {
278 g_printf(_("Could not find any valid dump image, check directory.\n"));
282 if (access(conf_logfile, F_OK) == 0) {
283 char *process_name = get_master_process(conf_logfile);
284 error(_("%s exists: someone started %s"), conf_logfile, process_name);
287 log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
289 if(!batch) confirm(datestamp_list);
291 for(dp = diskq.head; dp != NULL; dp = dp->next) {
294 qname = quote_string(dp->name);
295 log_add(L_DISK, "%s %s", dp->host->hostname, qname);
300 if(!foreground) { /* write it before redirecting stdout */
301 puts(_("Running in background, you can log off now."));
302 puts(_("You'll get mail when amflush is finished."));
305 if(redirect) redirect_stderr();
307 if(!foreground) detach();
309 add_amanda_log_handler(amanda_log_stderr);
310 add_amanda_log_handler(amanda_log_trace_log);
312 tm = localtime(&today);
314 strftime(date_string, 100, "%a %b %e %H:%M:%S %Z %Y", tm);
315 strftime(date_string_standard, 100, "%Y-%m-%d %H:%M:%S %Z", tm);
317 error(_("BAD DATE")); /* should never happen */
319 g_fprintf(stderr, _("amflush: start at %s\n"), date_string);
320 g_fprintf(stderr, _("amflush: datestamp %s\n"), amflush_timestamp);
321 g_fprintf(stderr, _("amflush: starttime %s\n"), amflush_timestamp);
322 g_fprintf(stderr, _("amflush: starttime-locale-independent %s\n"),
323 date_string_standard);
324 log_add(L_START, _("date %s"), amflush_timestamp);
327 if(pipe(driver_pipe) == -1) {
328 error(_("error [opening pipe to driver: %s]"), strerror(errno));
331 if((driver_pid = fork()) == 0) {
333 * This is the child process.
335 dup2(driver_pipe[0], 0);
336 close(driver_pipe[1]);
337 config_options = get_config_options(3);
338 config_options[0] = "driver";
339 config_options[1] = get_config_name();
340 config_options[2] = "nodump";
342 execve(driver_program, config_options, safe_env());
343 error(_("cannot exec %s: %s"), driver_program, strerror(errno));
345 } else if(driver_pid == -1) {
346 error(_("cannot fork for %s: %s"), driver_program, strerror(errno));
349 driver_stream = fdopen(driver_pipe[1], "w");
350 if (!driver_stream) {
351 error(_("Can't fdopen: %s"), strerror(errno));
355 g_fprintf(driver_stream, "DATE %s\n", amflush_timestamp);
356 for(holding_file=holding_list; holding_file != NULL;
357 holding_file = holding_file->next) {
359 holding_file_get_dumpfile((char *)holding_file->data, &file);
361 if (holding_file_size((char *)holding_file->data, 1) <= 0) {
362 g_debug("%s is empty - ignoring", (char *)holding_file->data);
363 log_add(L_INFO, "%s: removing file with no data.",
364 (char *)holding_file->data);
365 holding_file_unlink((char *)holding_file->data);
366 dumpfile_free_data(&file);
370 /* search_holding_disk should have already ensured that every
371 * holding dumpfile has an entry in the dynamic disklist */
372 dp = lookup_disk(file.name, file.disk);
375 /* but match_disklist may have indicated we should not flush it */
376 if (dp->todo == 0) continue;
378 qdisk = quote_string(file.disk);
379 qhname = quote_string((char *)holding_file->data);
381 "FLUSH %s %s %s %d %s\n",
388 g_debug("flushing '%s'", (char *)holding_file->data);
389 g_fprintf(driver_stream,
390 "FLUSH %s %s %s %d %s\n",
398 dumpfile_free_data(&file);
400 g_fprintf(stderr, "ENDFLUSH\n"); fflush(stderr);
401 g_fprintf(driver_stream, "ENDFLUSH\n"); fflush(driver_stream);
402 fclose(driver_stream);
406 if((pid = wait(&exitcode)) == -1) {
410 error(_("wait for %s: %s"), driver_program, strerror(errno));
413 } else if (pid == driver_pid) {
418 slist_free_full(datestamp_list, g_free);
419 datestamp_list = NULL;
420 slist_free_full(holding_list, g_free);
423 if(redirect) { /* rename errfile */
424 char *errfile, *errfilex, *nerrfilex, number[100];
428 struct stat stat_buf;
430 errfile = vstralloc(conf_logdir, "/amflush", NULL);
433 tapecycle = getconf_int(CNF_TAPECYCLE);
434 maxdays = tapecycle + 2;
436 /* First, find out the last existing errfile, */
437 /* to avoid ``infinite'' loops if tapecycle is infinite */
439 g_snprintf(number,100,"%d",days);
440 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
441 while ( days < maxdays && stat(errfilex,&stat_buf)==0) {
443 g_snprintf(number,100,"%d",days);
444 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
446 g_snprintf(number,100,"%d",days);
447 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
451 nerrfilex = errfilex;
453 g_snprintf(number,100,"%d",days);
454 errfilex = vstralloc(errfile, ".", number, NULL);
455 if (rename(errfilex, nerrfilex) != 0) {
456 error(_("cannot rename \"%s\" to \"%s\": %s"),
457 errfilex, nerrfilex, strerror(errno));
461 errfilex = newvstralloc(errfilex, errfile, ".1", NULL);
462 if (rename(errfile,errfilex) != 0) {
463 error(_("cannot rename \"%s\" to \"%s\": %s"),
464 errfilex, nerrfilex, strerror(errno));
473 * Have amreport generate report and send mail. Note that we do
474 * not bother checking the exit status. If it does not work, it
478 if((reporter_pid = fork()) == 0) {
480 * This is the child process.
482 config_options = get_config_options(3);
483 config_options[0] = "amreport";
484 config_options[1] = get_config_name();
485 config_options[2] = "--from-amdump";
487 execve(reporter_program, config_options, safe_env());
488 error(_("cannot exec %s: %s"), reporter_program, strerror(errno));
490 } else if(reporter_pid == -1) {
491 error(_("cannot fork for %s: %s"), reporter_program, strerror(errno));
495 if((pid = wait(&exitcode)) == -1) {
499 error(_("wait for %s: %s"), reporter_program, strerror(errno));
502 } else if (pid == reporter_pid) {
507 log_add(L_INFO, "pid-done %ld", (long)getpid());
510 * Call amlogroll to rename the log file to its datestamped version.
511 * Since we exec at this point, our exit code will be that of amlogroll.
513 config_options = get_config_options(2);
514 config_options[0] = "amlogroll";
515 config_options[1] = get_config_name();
517 execve(logroll_program, config_options, safe_env());
518 error(_("cannot exec %s: %s"), logroll_program, strerror(errno));
520 return 0; /* keep the compiler happy */
525 get_letter_from_user(void)
529 fflush(stdout); fflush(stderr);
530 while((ch = getchar()) != EOF && ch != '\n' && g_ascii_isspace(ch)) {
531 (void)ch; /* Quite lint */
535 } else if (ch != EOF) {
537 if(islower(r)) r = toupper(r);
538 while((ch = getchar()) != EOF && ch != '\n') {
539 (void)ch; /* Quite lint */
548 /* Allow the user to select a set of datestamps from those in
549 * holding disks. The result can be passed to
550 * holding_get_files_for_flush. If less than two dates are
551 * available, then no user interaction takes place.
553 * @returns: a new GSList listing the selected datestamps
558 GSList *datestamp_list;
559 GSList *r_datestamp_list = NULL;
561 char **datestamps = NULL;
566 char max_char = '\0', chupper = '\0';
568 datestamp_list = holding_get_all_datestamps();
570 if(g_slist_length(datestamp_list) < 2) {
571 return datestamp_list;
573 datestamps = alloc(g_slist_length(datestamp_list) * SIZEOF(char *));
574 for(ds = datestamp_list, i=0; ds != NULL; ds = ds->next,i++) {
575 datestamps[i] = (char *)ds->data; /* borrowing reference */
579 puts(_("\nMultiple Amanda runs in holding disks; please pick one by letter:"));
580 for(ds = datestamp_list, max_char = 'A';
581 ds != NULL && max_char <= 'Z';
582 ds = ds->next, max_char++) {
583 g_printf(" %c. %s\n", max_char, (char *)ds->data);
586 g_printf(_("Select datestamps to flush [A..%c or <enter> for all]: "), max_char);
587 fflush(stdout); fflush(stderr);
589 if ((answer = agets(stdin)) == NULL) {
594 if (*answer == '\0' || strncasecmp(answer, "ALL", 3) == 0) {
599 while ((ch = *a++) != '\0') {
600 if (!g_ascii_isspace(ch))
604 /* rewrite the selected list into r_datestamp_list, then copy it over
605 * to datestamp_list */
607 if (g_ascii_isspace(ch) || ch == ',') {
610 chupper = (char)toupper(ch);
611 if (chupper < 'A' || chupper > max_char) {
612 slist_free_full(r_datestamp_list, g_free);
613 r_datestamp_list = NULL;
616 r_datestamp_list = g_slist_append(r_datestamp_list,
617 stralloc(datestamps[chupper - 'A']));
618 } while ((ch = *a++) != '\0');
619 if (r_datestamp_list && ch == '\0') {
620 slist_free_full(datestamp_list, g_free);
621 datestamp_list = r_datestamp_list;
626 amfree(datestamps); /* references in this array are borrowed */
629 return datestamp_list;
634 * confirm before detaching and running
638 confirm(GSList *datestamp_list)
646 g_printf(_("\nToday is: %s\n"),amflush_datestamp);
647 g_printf(_("Flushing dumps from"));
649 for(datestamp = datestamp_list; datestamp != NULL; datestamp = datestamp->next) {
650 g_printf("%s %s", extra, (char *)datestamp->data);
653 tpchanger = getconf_str(CNF_TPCHANGER);
654 if(*tpchanger != '\0') {
655 g_printf(_(" using tape changer \"%s\".\n"), tpchanger);
657 g_printf(_(" to tape drive \"%s\".\n"), getconf_str(CNF_TAPEDEV));
660 g_printf(_("Expecting "));
661 tp = lookup_last_reusable_tape(0);
663 g_printf(_("tape %s or "), tp->label);
664 g_printf(_("a new tape."));
665 tp = lookup_tapepos(1);
667 g_printf(_(" (The last dumps were to tape %s)"), tp->label);
670 g_printf(_("\nAre you sure you want to do this [yN]? "));
671 if((ch = get_letter_from_user()) == 'Y') {
673 } else if (ch == 'N' || ch == '\0' || ch == EOF) {
681 g_printf(_("Ok, quitting. Run amflush again when you are ready.\n"));
682 log_add(L_INFO, "pid-done %ld", (long)getpid());
687 redirect_stderr(void)
692 fflush(stdout); fflush(stderr);
693 errfile = vstralloc(conf_logdir, "/amflush", NULL);
694 if((fderr = open(errfile, O_WRONLY| O_APPEND | O_CREAT | O_TRUNC, 0600)) == -1) {
695 error(_("could not open %s: %s"), errfile, strerror(errno));
709 fflush(stdout); fflush(stderr);
710 if((fd = open("/dev/null", O_RDWR, 0666)) == -1) {
711 error(_("could not open /dev/null: %s"), strerror(errno));
720 error(_("could not fork: %s"), strerror(errno));