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: amfetchdump.c,v 1.7 2006/03/14 13:12:01 martinea Exp $
29 * retrieves specific dumps from a set of amanda tapes
34 #include "fileheader.h"
43 #define CREAT_MODE 0640
45 extern char *rst_conf_logdir;
46 extern char *rst_conf_logfile;
47 extern char *config_dir;
50 typedef struct needed_tapes_s {
54 struct needed_tapes_s *next;
55 struct needed_tapes_s *prev;
60 void errexit P((void));
61 void handle_sigpipe P((int sig));
62 tapelist_t *list_needed_tapes P((match_list_t *match_list));
64 int main P((int argc, char **argv));
67 static int parent_pid = -1;
68 static void cleanup P((void));
72 * Do exit(2) after an error, rather than exit(1).
81 * Print usage message and terminate.
84 fprintf(stderr, "Usage: amfetchdump [options] config hostname [diskname [datestamp [level [hostname [diskname [datestamp [level ... ]]]]]]]\n\n");
85 fprintf(stderr, "Goes and grabs a dump from tape, moving tapes around and assembling parts as\n");
86 fprintf(stderr, "necessary. Files are restored to the current directory, unless otherwise\nspecified.\n\n");
87 fprintf(stderr, " -p Pipe exactly *one* complete dumpfile to stdout, instead of to disk.\n");
88 fprintf(stderr, " -o <output dir> Restore files to this directory.\n");
89 fprintf(stderr, " -d <device> Force restoration from a particular tape device.\n");
90 fprintf(stderr, " -c Compress output, fastest method available.\n");
91 fprintf(stderr, " -C Compress output, best filesize method available.\n");
92 fprintf(stderr, " -l Leave dumps (un)compressed, whichever way they were originally on tape.\n");
93 fprintf(stderr, " -a Assume all tapes are available via changer, do not prompt for initial load.\n");
94 fprintf(stderr, " -i <dst_file> Search through tapes and write out an inventory while we\n restore. Useful only if normal logs are unavailable.\n");
95 fprintf(stderr, " -w Wait to put split dumps together until all chunks have been restored.\n");
96 fprintf(stderr, " -n Do not reassemble split dumpfiles.\n");
97 fprintf(stderr, " -k Skip the rewind/label read when reading a new tape.\n");
98 fprintf(stderr, " -s Do not use fast forward to skip files we won't restore. Use only if fsf\n causes your tapes to skip too far.\n");
99 fprintf(stderr, " -b <blocksize> Force a particular block size (default is 32kb).\n");
104 * Build the list of tapes we'll be wanting, and include data about the
105 * files we want from said tapes while we're at it (the whole find_result
108 tapelist_t *list_needed_tapes(match_list)
109 match_list_t *match_list;
111 needed_tape_t *needed_tapes = NULL, *curtape = NULL;
113 match_list_t *me = NULL;
114 find_result_t *alldumps = NULL;
115 tapelist_t *tapes = NULL;
117 char *conf_diskfile, *conf_tapelist;
119 /* For disks and tape lists */
120 conf_diskfile = getconf_str(CNF_DISKFILE);
121 conf_tapelist = getconf_str(CNF_TAPELIST);
122 if (*conf_diskfile == '/') {
123 conf_diskfile = stralloc(conf_diskfile);
125 conf_diskfile = stralloc2(config_dir, conf_diskfile);
127 if(read_diskfile(conf_diskfile, &diskqp) != 0) {
128 error("could not load disklist \"%s\"", conf_diskfile);
130 if (*conf_tapelist == '/') {
131 conf_tapelist = stralloc(conf_tapelist);
133 conf_tapelist = stralloc2(config_dir, conf_tapelist);
135 if(read_tapelist(conf_tapelist)) {
136 error("could not load tapelist \"%s\"", conf_tapelist);
138 amfree(conf_diskfile);
139 amfree(conf_tapelist);
141 /* Grab a find_output_t of all logged dumps */
142 alldumps = find_dump(1, &diskqp);
143 free_disklist(&diskqp);
144 if(alldumps == NULL){
145 fprintf(stderr, "No dump records found\n");
149 /* Compare all known dumps to our match list, note what we'll need */
150 for(me = match_list; me; me = me->next) {
151 find_result_t *curmatch = NULL;
152 find_result_t *matches = NULL;
154 matches = dumps_match(alldumps, me->hostname, me->diskname,
155 me->datestamp, me->level, 1);
156 sort_find_result("Dhklp", &matches);
157 for(curmatch = matches; curmatch; curmatch = curmatch->next){
159 if(strcmp("OK", curmatch->status)){
160 fprintf(stderr,"Dump %d %s %s %d had status '%s', skipping\n",
161 curmatch->datestamp, curmatch->hostname,
162 curmatch->diskname, curmatch->level,
166 for(curtape = needed_tapes; curtape; curtape = curtape->next) {
167 if(!strcmp(curtape->label, curmatch->label)){
168 find_result_t *rsttemp = NULL;
169 find_result_t *rstfile = alloc(sizeof(find_result_t));
172 memcpy(rstfile, curmatch, sizeof(find_result_t));
176 for(rsttemp = curtape->files;
178 rsttemp=rsttemp->next){
179 if(rstfile->filenum == rsttemp->filenum){
180 fprintf(stderr, "Seeing multiple entries for tape %s file %d, using most recent\n", curtape->label, rstfile->filenum);
188 rstfile->next = curtape->files;
190 if(curmatch->filenum < 1) curtape->isafile = 1;
191 else curtape->isafile = 0;
192 curtape->files = rstfile;
197 find_result_t *rstfile = alloc(sizeof(find_result_t));
198 needed_tape_t *newtape =
199 alloc(sizeof(needed_tape_t));
200 memcpy(rstfile, curmatch, sizeof(find_result_t));
201 rstfile->next = NULL;
202 newtape->files = rstfile;
203 if(curmatch->filenum < 1) newtape->isafile = 1;
204 else newtape->isafile = 0;
205 newtape->label = curmatch->label;
207 needed_tapes->prev->next = newtape;
208 newtape->prev = needed_tapes->prev;
209 needed_tapes->prev = newtape;
212 needed_tapes = newtape;
213 needed_tapes->prev = needed_tapes;
215 newtape->next = NULL;
218 // free_find_result(rstfile);
220 } /* if(!havetape) */
222 } /* for(curmatch = matches ... */
223 } /* for(me = match_list ... */
226 fprintf(stderr, "No matching dumps found\n");
230 /* stick that list in a structure that librestore will understand */
231 for(curtape = needed_tapes; curtape; curtape = curtape->next) {
232 find_result_t *curfind = NULL;
233 for(curfind = curtape->files; curfind; curfind = curfind->next) {
234 tapes = append_to_tapelist(tapes, curtape->label,
235 curfind->filenum, curtape->isafile);
239 fprintf(stderr, "%d tape(s) needed for restoration\n", numtapes);
247 * Parses command line, then loops through all files on tape, restoring
248 * files that match the command line criteria.
254 match_list_t *match_list = NULL;
255 match_list_t *me = NULL;
257 char *config_name = NULL;
258 char *conffile = NULL;
259 tapelist_t *needed_tapes = NULL;
262 rst_flags_t *rst_flags;
263 struct passwd *pwent;
265 for(fd = 3; fd < FD_SETSIZE; fd++) {
267 * Make sure nobody spoofs us with a lot of extra open files
268 * that would cause an open we do to get a very high file
269 * descriptor, which in turn might be used as an index into
270 * an array (e.g. an fd_set).
275 set_pname("amfetchdump");
279 /* we'd rather not run as root */
281 if(client_uid == (uid_t) -1 && (pwent = getpwnam(CLIENT_LOGIN)) != NULL) {
282 client_uid = pwent->pw_uid;
283 client_gid = pwent->pw_gid;
287 if(client_uid == (uid_t) -1) {
288 error("error [cannot find user %s in passwd file]\n", CLIENT_LOGIN);
291 initgroups(CLIENT_LOGIN, client_gid);
296 #endif /* FORCE_USERID */
298 /* Don't die when child closes pipe */
299 signal(SIGPIPE, SIG_IGN);
301 erroutput_type = ERR_INTERACTIVE;
305 if(argc <= 1) usage();
307 rst_flags = new_rst_flags();
308 rst_flags->wait_tape_prompt = 1;
311 while( (opt = getopt(argc, argv, "alht:scCpb:nwi:d:o:")) != -1) {
313 case 'b': rst_flags->compress = 1; break;
314 rst_flags->blocksize = strtol(optarg, &e, 10);
315 if(*e == 'k' || *e == 'K') {
316 rst_flags->blocksize *= 1024;
317 } else if(*e == 'm' || *e == 'M') {
318 rst_flags->blocksize *= 1024 * 1024;
319 } else if(*e != '\0') {
320 error("invalid blocksize value \"%s\"", optarg);
322 if(rst_flags->blocksize < DISK_BLOCK_BYTES) {
323 error("minimum block size is %dk", DISK_BLOCK_BYTES / 1024);
326 case 'c': rst_flags->compress = 1; break;
327 case 'o': rst_flags->restore_dir = stralloc(optarg) ; break;
328 case 'd': rst_flags->alt_tapedev = stralloc(optarg) ; break;
330 rst_flags->compress = 1;
331 rst_flags->comp_type = COMPRESS_BEST_OPT;
333 case 'p': rst_flags->pipe_to_fd = fileno(stdout); break;
334 case 's': rst_flags->fsf = 0; break;
335 case 'l': rst_flags->leave_comp = 1; break;
336 case 'i': rst_flags->inventory_log = stralloc(optarg); break;
337 case 'n': rst_flags->inline_assemble = 0; break;
338 case 'w': rst_flags->delay_assemble = 1; break;
339 case 'a': rst_flags->wait_tape_prompt = 0; break;
340 case 'h': rst_flags->headers = 1; break;
346 /* Check some flags that affect inventorying */
347 if(rst_flags->inventory_log){
348 if(rst_flags->inline_assemble) rst_flags->delay_assemble = 1;
349 rst_flags->inline_assemble = 0;
350 rst_flags->leave_comp = 1;
351 if(rst_flags->compress){
352 error("Cannot force compression when doing inventory/search");
354 fprintf(stderr, "Doing inventory/search, dumps will not be uncompressed or assembled on-the-fly.\n");
357 if(rst_flags->delay_assemble){
358 fprintf(stderr, "Using -w, split dumpfiles will *not* be automatically uncompressed.\n");
362 /* make sure our options all make sense otherwise */
363 if(check_rst_flags(rst_flags) == -1) usage();
365 config_name = argv[optind++];
366 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
367 conffile = stralloc2(config_dir, CONFFILE_NAME);
368 if (read_conffile(conffile)) {
369 error("errors processing config file \"%s\"", conffile);
374 if((argc - optind) < 1 && !rst_flags->inventory_log){
375 fprintf(stderr, "Not enough arguments\n\n");
379 #define ARG_GET_HOST 0
380 #define ARG_GET_DISK 1
381 #define ARG_GET_DATE 2
382 #define ARG_GET_LEVL 3
384 arg_state = ARG_GET_HOST;
385 while(optind < argc) {
389 * New host/disk/date/level set, so allocate a match_list.
391 me = alloc(sizeof(*me));
392 me->hostname = argv[optind++];
396 me->next = match_list;
398 if(me->hostname[0] != '\0'
399 && (errstr=validate_regexp(me->hostname)) != NULL) {
400 fprintf(stderr, "%s: bad hostname regex \"%s\": %s\n",
401 get_pname(), me->hostname, errstr);
404 arg_state = ARG_GET_DISK;
407 me->diskname = argv[optind++];
408 if(me->diskname[0] != '\0'
409 && (errstr=validate_regexp(me->diskname)) != NULL) {
410 fprintf(stderr, "%s: bad diskname regex \"%s\": %s\n",
411 get_pname(), me->diskname, errstr);
414 arg_state = ARG_GET_DATE;
417 me->datestamp = argv[optind++];
418 if(me->datestamp[0] != '\0'
419 && (errstr=validate_regexp(me->datestamp)) != NULL) {
420 fprintf(stderr, "%s: bad datestamp regex \"%s\": %s\n",
421 get_pname(), me->datestamp, errstr);
424 arg_state = ARG_GET_LEVL;
427 me->level = argv[optind++];
428 if(me->level[0] != '\0'
429 && (errstr=validate_regexp(me->level)) != NULL) {
430 fprintf(stderr, "%s: bad level regex \"%s\": %s\n",
431 get_pname(), me->level, errstr);
437 /* XXX I don't think this can happen */
438 if(match_list == NULL && !rst_flags->inventory_log) {
439 match_list = alloc(sizeof(*match_list));
440 match_list->hostname = "";
441 match_list->diskname = "";
442 match_list->datestamp = "";
443 match_list->level = "";
444 match_list->next = NULL;
448 * We've been told explicitly to go and search through the tapes the hard
451 if(rst_flags->inventory_log){
452 fprintf(stderr, "Beginning tape-by-tape search.\n");
453 search_tapes(stderr, 1, NULL, match_list, rst_flags, NULL);
458 /* Decide what tapes we'll need */
459 needed_tapes = list_needed_tapes(match_list);
461 parent_pid = getpid();
463 get_lock = lock_logfile(); /* config is loaded, should be ok here */
464 search_tapes(NULL, 1, needed_tapes, match_list, rst_flags, NULL);
467 free_match_list(match_list);
469 if(rst_flags->inline_assemble || rst_flags->delay_assemble)
470 flush_open_outputs(1, NULL);
471 else flush_open_outputs(0, NULL);
473 free_rst_flags(rst_flags);
481 if(parent_pid == getpid()) {
482 if(get_lock) unlink(rst_conf_logfile);