Imported Upstream version 2.6.0
[debian/amanda] / restore-src / amfetchdump.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amfetchdump.c,v 1.16 2006/08/24 01:57:15 paddy_s Exp $
28  *
29  * retrieves specific dumps from a set of amanda tapes
30  */
31
32 #include "amanda.h"
33 #include "fileheader.h"
34 #include "util.h"
35 #include "restore.h"
36 #include "diskfile.h"
37 #include "tapefile.h"
38 #include "find.h"
39 #include "changer.h"
40 #include "logfile.h"
41 #include "cmdline.h"
42
43 #define CREAT_MODE      0640
44
45 extern char *rst_conf_logfile;
46 extern char *config_dir;
47 int get_lock = 0;
48
49 typedef struct needed_tapes_s {
50     char *label;
51     int isafile;
52     find_result_t *files;
53     struct needed_tapes_s *next;
54     struct needed_tapes_s *prev;
55 } needed_tape_t;
56
57 /* local functions */
58
59 tapelist_t *list_needed_tapes(GSList *dumpspecs);
60 void usage(void);
61 int main(int argc, char **argv);
62
63 /* exit routine */
64 static pid_t parent_pid = -1;
65 static void cleanup(void);
66
67
68 /*
69  * Print usage message and terminate.
70  */
71
72 void
73 usage(void)
74 {
75     g_fprintf(stderr, _("Usage: amfetchdump [options] config hostname [diskname [datestamp [level [hostname [diskname [datestamp [level ... ]]]]]]] [-o configoption]*\n\n"));
76     g_fprintf(stderr, _("Goes and grabs a dump from tape, moving tapes around and assembling parts as\n"));
77     g_fprintf(stderr, _("necessary.  Files are restored to the current directory, unless otherwise\nspecified.\n\n"));
78     g_fprintf(stderr, _("  -p Pipe exactly *one* complete dumpfile to stdout, instead of to disk.\n"));
79     g_fprintf(stderr, _("  -O <output dir> Restore files to this directory.\n"));
80     g_fprintf(stderr, _("  -d <device> Force restoration from a particular tape device.\n"));
81     g_fprintf(stderr, _("  -c Compress output, fastest method available.\n"));
82     g_fprintf(stderr, _("  -C Compress output, best filesize method available.\n"));
83     g_fprintf(stderr, _("  -l Leave dumps (un)compressed, whichever way they were originally on tape.\n"));
84     g_fprintf(stderr, _("  -a Assume all tapes are available via changer, do not prompt for initial load.\n"));
85     g_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"));
86     g_fprintf(stderr, _("  -w Wait to put split dumps together until all chunks have been restored.\n"));
87     g_fprintf(stderr, _("  -n Do not reassemble split dumpfiles.\n"));
88     g_fprintf(stderr, _("  -k Skip the rewind/label read when reading a new tape.\n"));
89     g_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"));
90     g_fprintf(stderr, _("  -b <blocksize> Force a particular block size (default is 32kb).\n"));
91     exit(1);
92 }
93
94 /*
95  * Build the list of tapes we'll be wanting, and include data about the
96  * files we want from said tapes while we're at it (the whole find_result
97  * should do fine)
98  */
99 tapelist_t *
100 list_needed_tapes(
101     GSList *    dumpspecs)
102 {
103     needed_tape_t *needed_tapes = NULL, *curtape = NULL;
104     disklist_t diskqp;
105     dumpspec_t *ds = NULL;
106     find_result_t *alldumps = NULL;
107     tapelist_t *tapes = NULL;
108     int numtapes = 0;
109     char *conf_diskfile, *conf_tapelist;
110
111     /* For disks and tape lists */
112     conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
113     if(read_diskfile(conf_diskfile, &diskqp) != 0) {
114         error(_("could not load disklist \"%s\""), conf_diskfile);
115         /*NOTREACHED*/
116     }
117     amfree(conf_diskfile);
118
119     conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
120     if(read_tapelist(conf_tapelist)) {
121         error(_("could not load tapelist \"%s\""), conf_tapelist);
122         /*NOTREACHED*/
123     }
124     amfree(conf_tapelist);
125
126     /* Grab a find_output_t of all logged dumps */
127     alldumps = find_dump(&diskqp);
128     free_disklist(&diskqp);
129     if(alldumps == NULL){
130         g_fprintf(stderr, _("No dump records found\n"));
131         exit(1);
132     }
133
134     /* Compare all known dumps to our match list, note what we'll need */
135     while (dumpspecs) {
136         find_result_t *curmatch = NULL; 
137         find_result_t *matches = NULL;  
138         ds = (dumpspec_t *)dumpspecs->data;
139
140         matches = dumps_match(alldumps, ds->host, ds->disk,
141                                  ds->datestamp, ds->level, 1);
142         sort_find_result("Dhklp", &matches);
143         for(curmatch = matches; curmatch; curmatch = curmatch->next){
144             int havetape = 0;
145             int have_part = 0;
146             if(strcmp("OK", curmatch->status)){
147                 g_fprintf(stderr,_("Dump %s %s %s %d had status '%s', skipping\n"),
148                                  curmatch->timestamp, curmatch->hostname,
149                                  curmatch->diskname, curmatch->level,
150                                  curmatch->status);
151                 continue;
152             }
153             /* check if we already have that part */
154             for(curtape = needed_tapes; curtape; curtape = curtape->next) {
155                 find_result_t *rsttemp = NULL;
156                 for(rsttemp = curtape->files;
157                     rsttemp;
158                     rsttemp=rsttemp->next) {
159                     if (strcmp(rsttemp->partnum, curmatch->partnum) == 0)
160                         have_part = 1;
161                 }
162             }
163             if (have_part)
164                 continue;
165             for(curtape = needed_tapes; curtape; curtape = curtape->next) {
166                 if(!strcmp(curtape->label, curmatch->label)){
167                     find_result_t *rsttemp = NULL;
168                     find_result_t *rstfile = alloc(SIZEOF(find_result_t));
169                     int keep = 1;
170
171                     memcpy(rstfile, curmatch, SIZEOF(find_result_t));
172
173                     havetape = 1;
174
175                     for(rsttemp = curtape->files;
176                             rsttemp;
177                             rsttemp=rsttemp->next){
178                         if(rstfile->filenum == rsttemp->filenum){
179                             g_fprintf(stderr, _("Seeing multiple entries for tape "
180                                    "%s file %lld, using most recent\n"),
181                                     curtape->label,
182                                     (long long)rstfile->filenum);
183                             keep = 0;
184                         }
185                     }
186                     if(!keep){
187                         amfree(rstfile);
188                         break;
189                     }
190                     rstfile->next = curtape->files;
191
192                     if(curmatch->filenum < 1) curtape->isafile = 1;
193                     else curtape->isafile = 0;
194                     curtape->files = rstfile;
195                     break;
196                 }
197             }
198             if(!havetape){
199                 find_result_t *rstfile = alloc(SIZEOF(find_result_t));
200                 needed_tape_t *newtape =
201                                           alloc(SIZEOF(needed_tape_t));
202                 memcpy(rstfile, curmatch, SIZEOF(find_result_t));
203                 rstfile->next = NULL;
204                 newtape->files = rstfile;
205                 if(curmatch->filenum < 1) newtape->isafile = 1;
206                 else newtape->isafile = 0;
207                 newtape->label = curmatch->label;
208                 if(needed_tapes){
209                     needed_tapes->prev->next = newtape;
210                     newtape->prev = needed_tapes->prev;
211                     needed_tapes->prev = newtape;
212                 }
213                 else{
214                     needed_tapes = newtape;
215                     needed_tapes->prev = needed_tapes;
216                 }
217                 newtape->next = NULL;
218                 numtapes++;
219 #if 0
220 //              free_find_result(rstfile);
221 #endif
222             } /* if(!havetape) */
223
224         } /* for(curmatch = matches ... */
225         dumpspecs = dumpspecs->next;
226     } /* while (dumpspecs) */
227
228     if(numtapes == 0){
229       g_fprintf(stderr, _("No matching dumps found\n"));
230       exit(1);
231       /* NOTREACHED */
232     }
233
234     /* stick that list in a structure that librestore will understand */
235     for(curtape = needed_tapes; curtape; curtape = curtape->next) {
236         find_result_t *curfind = NULL;
237         for(curfind = curtape->files; curfind; curfind = curfind->next) {
238             tapes = append_to_tapelist(tapes, curtape->label,
239                                        curfind->filenum, -1, curtape->isafile);
240         }
241     }
242
243     g_fprintf(stderr, _("%d tape(s) needed for restoration\n"), numtapes);
244     return(tapes);
245 }
246
247
248 /*
249  * Parses command line, then loops through all files on tape, restoring
250  * files that match the command line criteria.
251  */
252
253 int
254 main(
255     int         argc,
256     char **     argv)
257 {
258     extern int optind;
259     int opt;
260     GSList *dumpspecs = NULL;
261     int fd;
262     tapelist_t *needed_tapes = NULL;
263     char *e;
264     rst_flags_t *rst_flags;
265     int minimum_arguments;
266     config_overwrites_t *cfg_ovr = NULL;
267
268     /*
269      * Configure program for internationalization:
270      *   1) Only set the message locale for now.
271      *   2) Set textdomain for all amanda related programs to "amanda"
272      *      We don't want to be forced to support dozens of message catalogs.
273      */  
274     setlocale(LC_MESSAGES, "C");
275     textdomain("amanda"); 
276
277     for(fd = 3; fd < (int)FD_SETSIZE; fd++) {
278         /*
279          * Make sure nobody spoofs us with a lot of extra open files
280          * that would cause a successful open to get a very high file
281          * descriptor, which in turn might be used as an index into
282          * an array (e.g. an fd_set).
283          */
284         close(fd);
285     }
286
287     set_pname("amfetchdump");
288
289     /* Don't die when child closes pipe */
290     signal(SIGPIPE, SIG_IGN);
291
292     dbopen(DBG_SUBDIR_SERVER);
293
294     erroutput_type = ERR_INTERACTIVE;
295     error_exit_status = 2;
296
297     rst_flags = new_rst_flags();
298     rst_flags->wait_tape_prompt = 1;
299
300     /* handle options */
301     cfg_ovr = new_config_overwrites(argc/2);
302     while( (opt = getopt(argc, argv, "alht:scCpb:nwi:d:O:o:")) != -1) {
303         switch(opt) {
304         case 'b':
305             rst_flags->blocksize = (ssize_t)strtol(optarg, &e, 10);
306             if(*e == 'k' || *e == 'K') {
307                 rst_flags->blocksize *= 1024;
308             } else if(*e == 'm' || *e == 'M') {
309                 rst_flags->blocksize *= 1024 * 1024;
310             } else if(*e != '\0') {
311                 error(_("invalid blocksize value \"%s\""), optarg);
312                 /*NOTREACHED*/
313             }
314             if(rst_flags->blocksize < DISK_BLOCK_BYTES) {
315                 error(_("minimum block size is %dk"), DISK_BLOCK_BYTES / 1024);
316                 /*NOTREACHED*/
317             }
318             break;
319         case 'c': rst_flags->compress = 1; break;
320         case 'O': rst_flags->restore_dir = stralloc(optarg) ; break;
321         case 'd': rst_flags->alt_tapedev = stralloc(optarg) ; break;
322         case 'C':
323             rst_flags->compress = 1;
324             rst_flags->comp_type = COMPRESS_BEST_OPT;
325             break;
326         case 'p': rst_flags->pipe_to_fd = STDOUT_FILENO; break;
327         case 's': rst_flags->fsf = (off_t)0; break;
328         case 'l': rst_flags->leave_comp = 1; break;
329         case 'i': rst_flags->inventory_log = stralloc(optarg); break;
330         case 'n': rst_flags->inline_assemble = 0; break;
331         case 'w': rst_flags->delay_assemble = 1; break;
332         case 'a': rst_flags->wait_tape_prompt = 0; break;
333         case 'h': rst_flags->headers = 1; break;
334         case 'o': add_config_overwrite_opt(cfg_ovr, optarg); break;
335         default:
336             usage();
337             /*NOTREACHED*/
338         }
339     }
340
341     /* Check some flags that affect inventorying */
342     if(rst_flags->inventory_log){
343         if(rst_flags->inline_assemble) rst_flags->delay_assemble = 1;
344         rst_flags->inline_assemble = 0;
345         rst_flags->leave_comp = 1;
346         if(rst_flags->compress){
347             error(_("Cannot force compression when doing inventory/search"));
348             /*NOTREACHED*/
349         }
350         g_fprintf(stderr, _("Doing inventory/search, dumps will not be uncompressed or assembled on-the-fly.\n"));
351     }
352     else{
353         if(rst_flags->delay_assemble){
354             g_fprintf(stderr, _("Using -w, split dumpfiles will *not* be automatically uncompressed.\n"));
355         }
356     }
357
358     /* make sure our options all make sense otherwise */
359     if(check_rst_flags(rst_flags) == -1) {
360         usage();
361         /*NOTREACHED*/
362     }
363
364     if (rst_flags->inventory_log) {
365         minimum_arguments = 1;
366     } else {
367         minimum_arguments = 2;
368     }
369  
370     if(argc - optind < minimum_arguments) {
371         usage();
372         /*NOTREACHED*/
373     }
374
375     config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_FATAL, argv[optind++]);
376     apply_config_overwrites(cfg_ovr);
377
378     check_running_as(RUNNING_AS_DUMPUSER);
379
380     dbrename(config_name, DBG_SUBDIR_SERVER);
381
382     dumpspecs = cmdline_parse_dumpspecs(argc - optind, argv + optind,
383                                         CMDLINE_PARSE_DATESTAMP |
384                                         CMDLINE_PARSE_LEVEL |
385                                         CMDLINE_EMPTY_TO_WILDCARD);
386
387     /*
388      * We've been told explicitly to go and search through the tapes the hard
389      * way.
390      */
391     if(rst_flags->inventory_log){
392         g_fprintf(stderr, _("Beginning tape-by-tape search.\n"));
393         search_tapes(stderr, stdin, rst_flags->alt_tapedev == NULL,
394                      NULL, dumpspecs, rst_flags, NULL);
395         exit(0);
396     }
397
398
399     /* Decide what tapes we'll need */
400     needed_tapes = list_needed_tapes(dumpspecs);
401
402     parent_pid = getpid();
403     atexit(cleanup);
404     get_lock = lock_logfile(); /* config is loaded, should be ok here */
405     if(get_lock == 0) {
406         error(_("%s exists: amdump or amflush is already running, or you must run amcleanup"), rst_conf_logfile);
407     }
408     search_tapes(NULL, stdin, rst_flags->alt_tapedev == NULL,
409                  needed_tapes, dumpspecs, rst_flags, NULL);
410     cleanup();
411
412     dumpspec_list_free(dumpspecs);
413
414     if(rst_flags->inline_assemble || rst_flags->delay_assemble)
415         flush_open_outputs(1, NULL);
416     else flush_open_outputs(0, NULL);
417
418     free_rst_flags(rst_flags);
419
420     return(0);
421 }
422
423 static void
424 cleanup(void)
425 {
426     if(parent_pid == getpid()) {
427         if(get_lock) unlink(rst_conf_logfile);
428     }
429 }