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"
43 static char *conf_logdir;
46 char *reporter_program;
47 char *logroll_program;
49 char *amflush_timestamp;
50 char *amflush_datestamp;
54 int main(int main_argc, char **main_argv);
55 void flush_holdingdisk(char *diskdir, char *datestamp);
57 void redirect_stderr(void);
60 static int get_letter_from_user(void);
72 char **datearg = NULL;
78 int conf_usetimestamps;
82 pid_t driver_pid, reporter_pid;
86 sl_t *holding_list=NULL;
89 char date_string[100];
91 int new_argc, my_argc;
92 char **new_argv, **my_argv;
101 dbopen(DBG_SUBDIR_SERVER);
103 /* Don't die when child closes pipe */
104 signal(SIGPIPE, SIG_IGN);
106 erroutput_type = ERR_INTERACTIVE;
111 /* process arguments */
113 parse_server_conf(main_argc, main_argv, &new_argc, &new_argv);
117 while((opt = getopt(my_argc, my_argv, "bfsD:")) != EOF) {
121 case 'f': foreground = 1;
123 case 's': redirect = 0;
125 case 'D': if (datearg == NULL)
126 datearg = alloc(21*SIZEOF(char *));
127 if(nb_datearg == 20) {
128 fprintf(stderr,"maximum of 20 -D arguments.\n");
131 datearg[nb_datearg++] = stralloc(optarg);
132 datearg[nb_datearg] = NULL;
136 if(!foreground && !redirect) {
137 fprintf(stderr,"Can't redirect to stdout/stderr if not in forground.\n");
141 my_argc -= optind, my_argv += optind;
144 error("Usage: amflush%s [-b] [-f] [-s] [-D date]* <confdir> [host [disk]* ]* [-o configoption]*", versionsuffix());
148 config_name = my_argv[0];
149 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
151 conffile = stralloc2(config_dir, CONFFILE_NAME);
152 if(read_conffile(conffile)) {
153 error("errors processing config file \"%s\"", conffile);
158 dbrename(config_name, DBG_SUBDIR_SERVER);
160 report_bad_conf_arg();
162 conf_diskfile = getconf_str(CNF_DISKFILE);
163 if (*conf_diskfile == '/') {
164 conf_diskfile = stralloc(conf_diskfile);
166 conf_diskfile = stralloc2(config_dir, conf_diskfile);
168 if (read_diskfile(conf_diskfile, &diskq) < 0) {
169 error("could not read disklist file \"%s\"", conf_diskfile);
172 errstr = match_disklist(&diskq, my_argc-1, my_argv+1);
177 amfree(conf_diskfile);
179 conf_tapelist = getconf_str(CNF_TAPELIST);
180 if (*conf_tapelist == '/') {
181 conf_tapelist = stralloc(conf_tapelist);
183 conf_tapelist = stralloc2(config_dir, conf_tapelist);
185 if(read_tapelist(conf_tapelist)) {
186 error("could not load tapelist \"%s\"", conf_tapelist);
189 amfree(conf_tapelist);
191 conf_usetimestamps = getconf_boolean(CNF_USETIMESTAMPS);
193 amflush_datestamp = construct_datestamp(NULL);
194 if(conf_usetimestamps == 0) {
195 amflush_timestamp = stralloc(amflush_datestamp);
198 amflush_timestamp = construct_timestamp(NULL);
201 dumpuser = getconf_str(CNF_DUMPUSER);
202 if((pw = getpwnam(dumpuser)) == NULL) {
203 error("dumpuser %s not found in password file", dumpuser);
206 if(pw->pw_uid != getuid()) {
207 error("must run amflush as user %s", dumpuser);
211 conf_logdir = getconf_str(CNF_LOGDIR);
212 if (*conf_logdir == '/') {
213 conf_logdir = stralloc(conf_logdir);
215 conf_logdir = stralloc2(config_dir, conf_logdir);
217 conf_logfile = vstralloc(conf_logdir, "/log", NULL);
218 if (access(conf_logfile, F_OK) == 0) {
219 error("%s exists: amdump or amflush is already running, or you must run amcleanup", conf_logfile);
222 amfree(conf_logfile);
224 driver_program = vstralloc(libexecdir, "/", "driver", versionsuffix(),
226 reporter_program = vstralloc(sbindir, "/", "amreport", versionsuffix(),
228 logroll_program = vstralloc(libexecdir, "/", "amlogroll", versionsuffix(),
232 sle_t *dir, *next_dir;
235 datestamp_list = pick_all_datestamp(1);
236 for(dir = datestamp_list->first; dir != NULL;) {
237 next_dir = dir->next;
239 for(i=0; i<nb_datearg && ok==0; i++) {
240 ok = match_datestamp(datearg[i], dir->name);
242 if(ok == 0) { /* remove dir */
243 remove_sl(datestamp_list, dir);
250 datestamp_list = pick_all_datestamp(1);
253 datestamp_list = pick_datestamp(1);
257 if(is_empty_sl(datestamp_list)) {
258 printf("Could not find any Amanda directories to flush.\n");
262 holding_list = get_flush(datestamp_list, NULL, 1, 0);
263 if(holding_list->first == NULL) {
264 printf("Could not find any valid dump image, check directory.\n");
268 if(!batch) confirm();
270 for(dp = diskq.head; dp != NULL; dp = dp->next) {
272 log_add(L_DISK, "%s %s", dp->host->hostname, dp->name);
275 if(!foreground) { /* write it before redirecting stdout */
276 puts("Running in background, you can log off now.");
277 puts("You'll get mail when amflush is finished.");
280 if(redirect) redirect_stderr();
282 if(!foreground) detach();
284 erroutput_type = (ERR_AMANDALOG|ERR_INTERACTIVE);
285 set_logerror(logerror);
287 tm = localtime(&today);
289 strftime(date_string, 100, "%a %b %e %H:%M:%S %Z %Y", tm);
291 error("BAD DATE"); /* should never happen */
292 fprintf(stderr, "amflush: start at %s\n", date_string);
293 fprintf(stderr, "amflush: datestamp %s\n", amflush_timestamp);
294 log_add(L_START, "date %s", amflush_timestamp);
297 if(pipe(driver_pipe) == -1) {
298 error("error [opening pipe to driver: %s]", strerror(errno));
301 if((driver_pid = fork()) == 0) {
303 * This is the child process.
305 dup2(driver_pipe[0], 0);
306 close(driver_pipe[1]);
307 execle(driver_program,
308 "driver", config_name, "nodump", (char *)0,
310 error("cannot exec %s: %s", driver_program, strerror(errno));
312 } else if(driver_pid == -1) {
313 error("cannot fork for %s: %s", driver_program, strerror(errno));
316 driver_stream = fdopen(driver_pipe[1], "w");
317 if (!driver_stream) {
318 error("Can't fdopen: %s", strerror(errno));
322 fprintf(driver_stream, "DATE %s\n", amflush_timestamp);
323 for(holding_file=holding_list->first; holding_file != NULL;
324 holding_file = holding_file->next) {
325 get_dumpfile(holding_file->name, &file);
327 dp = lookup_disk(file.name, file.disk);
332 if (dp->todo == 0) continue;
335 "FLUSH %s %s %s %d %s\n",
341 fprintf(driver_stream,
342 "FLUSH %s %s %s %d %s\n",
349 fprintf(stderr, "ENDFLUSH\n"); fflush(stderr);
350 fprintf(driver_stream, "ENDFLUSH\n"); fflush(driver_stream);
351 fclose(driver_stream);
355 if((pid = wait(&exitcode)) == -1) {
359 error("wait for %s: %s", driver_program, strerror(errno));
362 } else if (pid == driver_pid) {
367 free_sl(datestamp_list);
368 datestamp_list = NULL;
369 free_sl(holding_list);
372 if(redirect) { /* rename errfile */
373 char *errfile, *errfilex, *nerrfilex, number[100];
377 struct stat stat_buf;
379 errfile = vstralloc(conf_logdir, "/amflush", NULL);
382 tapecycle = getconf_int(CNF_TAPECYCLE);
383 maxdays = tapecycle + 2;
385 /* First, find out the last existing errfile, */
386 /* to avoid ``infinite'' loops if tapecycle is infinite */
388 snprintf(number,100,"%d",days);
389 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
390 while ( days < maxdays && stat(errfilex,&stat_buf)==0) {
392 snprintf(number,100,"%d",days);
393 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
395 snprintf(number,100,"%d",days);
396 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
400 nerrfilex = errfilex;
402 snprintf(number,100,"%d",days);
403 errfilex = vstralloc(errfile, ".", number, NULL);
404 if (rename(errfilex, nerrfilex) != 0) {
405 error("cannot rename \"%s\" to \"%s\": %s",
406 errfilex, nerrfilex, strerror(errno));
410 errfilex = newvstralloc(errfilex, errfile, ".1", NULL);
411 if (rename(errfile,errfilex) != 0) {
412 error("cannot rename \"%s\" to \"%s\": %s",
413 errfilex, nerrfilex, strerror(errno));
422 * Have amreport generate report and send mail. Note that we do
423 * not bother checking the exit status. If it does not work, it
427 if((reporter_pid = fork()) == 0) {
429 * This is the child process.
431 execle(reporter_program,
432 "amreport", config_name, (char *)0,
434 error("cannot exec %s: %s", reporter_program, strerror(errno));
436 } else if(reporter_pid == -1) {
437 error("cannot fork for %s: %s", reporter_program, strerror(errno));
441 if((pid = wait(&exitcode)) == -1) {
445 error("wait for %s: %s", reporter_program, strerror(errno));
448 } else if (pid == reporter_pid) {
454 * Call amlogroll to rename the log file to its datestamped version.
455 * Since we exec at this point, our exit code will be that of amlogroll.
457 execle(logroll_program,
458 "amlogroll", config_name, (char *)0,
460 error("cannot exec %s: %s", logroll_program, strerror(errno));
462 return 0; /* keep the compiler happy */
467 get_letter_from_user(void)
471 fflush(stdout); fflush(stderr);
472 while((ch = getchar()) != EOF && ch != '\n' && isspace(ch)) {
473 (void)ch; /* Quite lint */
477 } else if (ch != EOF) {
479 if(islower(r)) r = toupper(r);
480 while((ch = getchar()) != EOF && ch != '\n') {
481 (void)ch; /* Quite lint */
492 * confirm before detaching and running
504 printf("\nToday is: %s\n",amflush_datestamp);
505 printf("Flushing dumps in");
507 for(dir = datestamp_list->first; dir != NULL; dir = dir->next) {
508 printf("%s %s", extra, dir->name);
511 tpchanger = getconf_str(CNF_TPCHANGER);
512 if(*tpchanger != '\0') {
513 printf(" using tape changer \"%s\".\n", tpchanger);
515 printf(" to tape drive \"%s\".\n", getconf_str(CNF_TAPEDEV));
518 printf("Expecting ");
519 tp = lookup_last_reusable_tape(0);
520 if(tp != NULL) printf("tape %s or ", tp->label);
521 printf("a new tape.");
522 tp = lookup_tapepos(1);
523 if(tp != NULL) printf(" (The last dumps were to tape %s)", tp->label);
526 printf("\nAre you sure you want to do this [yN]? ");
527 if((ch = get_letter_from_user()) == 'Y') {
529 } else if (ch == 'N' || ch == '\0' || ch == EOF) {
537 printf("Ok, quitting. Run amflush again when you are ready.\n");
542 redirect_stderr(void)
547 fflush(stdout); fflush(stderr);
548 errfile = vstralloc(conf_logdir, "/amflush", NULL);
549 if((fderr = open(errfile, O_WRONLY| O_CREAT | O_TRUNC, 0600)) == -1) {
550 error("could not open %s: %s", errfile, strerror(errno));
564 fflush(stdout); fflush(stderr);
565 if((fd = open("/dev/null", O_RDWR, 0666)) == -1) {
566 error("could not open /dev/null: %s", strerror(errno));
575 error("could not fork: %s", strerror(errno));