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.16 2006/08/24 01:57:15 paddy_s Exp $
29 * retrieves specific dumps from a set of amanda tapes
33 #include "fileheader.h"
43 #define CREAT_MODE 0640
45 extern char *rst_conf_logfile;
46 extern char *config_dir;
49 typedef struct needed_tapes_s {
53 struct needed_tapes_s *next;
54 struct needed_tapes_s *prev;
59 tapelist_t *list_needed_tapes(GSList *dumpspecs, int only_one);
61 int main(int argc, char **argv);
64 static pid_t parent_pid = -1;
65 static void cleanup(void);
69 * Print usage message and terminate.
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"));
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
104 needed_tape_t *needed_tapes = NULL, *curtape = NULL;
106 find_result_t *alldumps = NULL;
107 find_result_t *curmatch = NULL;
108 find_result_t *matches = NULL;
109 tapelist_t *tapes = NULL;
111 char *conf_diskfile, *conf_tapelist;
113 /* For disks and tape lists */
114 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
115 if(read_diskfile(conf_diskfile, &diskqp) != 0) {
116 error(_("could not load disklist \"%s\""), conf_diskfile);
119 amfree(conf_diskfile);
121 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
122 if(read_tapelist(conf_tapelist)) {
123 error(_("could not load tapelist \"%s\""), conf_tapelist);
126 amfree(conf_tapelist);
128 /* Grab a find_output_t of all logged dumps */
129 alldumps = find_dump(&diskqp);
130 free_disklist(&diskqp);
131 if(alldumps == NULL){
132 g_fprintf(stderr, _("No dump records found\n"));
136 /* Compare all known dumps to our match list, note what we'll need */
137 matches = dumps_match_dumpspecs(alldumps, dumpspecs, 1);
138 sort_find_result("Dhklp", &matches);
139 for(curmatch = matches; curmatch; curmatch = curmatch->next) {
143 /* keep only first dump if only_one */
145 curmatch != matches &&
146 (strcmp(curmatch->hostname, matches->hostname) ||
147 strcmp(curmatch->diskname, matches->diskname) ||
148 strcmp(curmatch->timestamp, matches->timestamp) ||
149 curmatch->level != matches->level)) {
152 if(strcmp("OK", curmatch->status)){
153 g_fprintf(stderr,_("Dump %s %s %s %d had status '%s', skipping\n"),
154 curmatch->timestamp, curmatch->hostname,
155 curmatch->diskname, curmatch->level,
159 /* check if we already have that part */
160 for(curtape = needed_tapes; curtape; curtape = curtape->next) {
161 find_result_t *rsttemp = NULL;
162 for(rsttemp = curtape->files;
164 rsttemp=rsttemp->next) {
165 if (!strcmp(rsttemp->partnum, curmatch->partnum) &&
166 !strcmp(rsttemp->hostname, curmatch->hostname) &&
167 !strcmp(rsttemp->diskname, curmatch->diskname) &&
168 !strcmp(rsttemp->timestamp, curmatch->timestamp) &&
169 rsttemp->level == curmatch->level) {
177 for(curtape = needed_tapes; curtape; curtape = curtape->next) {
178 if (!strcmp(curtape->label, curmatch->label)) {
179 find_result_t *rsttemp = NULL;
180 find_result_t *rstfile;
185 for(rsttemp = curtape->files;
187 rsttemp=rsttemp->next){
188 if(curmatch->filenum == rsttemp->filenum){
189 g_fprintf(stderr, _("Seeing multiple entries for tape "
190 "%s file %lld, using most recent\n"),
192 (long long)curmatch->filenum);
200 rstfile = alloc(SIZEOF(find_result_t));
201 memcpy(rstfile, curmatch, SIZEOF(find_result_t));
202 rstfile->next = curtape->files;
204 if (curmatch->filenum < 1)
205 curtape->isafile = 1;
206 else curtape->isafile = 0;
207 curtape->files = rstfile;
212 find_result_t *rstfile = alloc(SIZEOF(find_result_t));
213 needed_tape_t *newtape = alloc(SIZEOF(needed_tape_t));
214 memcpy(rstfile, curmatch, SIZEOF(find_result_t));
215 rstfile->next = NULL;
216 newtape->files = rstfile;
217 if(curmatch->filenum < 1) newtape->isafile = 1;
218 else newtape->isafile = 0;
219 newtape->label = curmatch->label;
221 needed_tapes->prev->next = newtape;
222 newtape->prev = needed_tapes->prev;
223 needed_tapes->prev = newtape;
225 needed_tapes = newtape;
226 needed_tapes->prev = needed_tapes;
228 newtape->next = NULL;
230 } /* if(!havetape) */
232 } /* for(curmatch = matches ... */
235 g_fprintf(stderr, _("No matching dumps found\n"));
240 /* stick that list in a structure that librestore will understand */
241 for(curtape = needed_tapes; curtape; curtape = curtape->next) {
242 find_result_t *curfind = NULL;
243 for(curfind = curtape->files; curfind; curfind = curfind->next) {
244 tapes = append_to_tapelist(tapes, curtape->label,
245 curfind->filenum, -1, curtape->isafile);
249 g_fprintf(stderr, _("%d tape(s) needed for restoration\n"), numtapes);
255 * Parses command line, then loops through all files on tape, restoring
256 * files that match the command line criteria.
266 GSList *dumpspecs = NULL;
268 tapelist_t *needed_tapes = NULL;
270 rst_flags_t *rst_flags;
271 int minimum_arguments;
272 config_overwrites_t *cfg_ovr = NULL;
275 * Configure program for internationalization:
276 * 1) Only set the message locale for now.
277 * 2) Set textdomain for all amanda related programs to "amanda"
278 * We don't want to be forced to support dozens of message catalogs.
280 setlocale(LC_MESSAGES, "C");
281 textdomain("amanda");
283 for(fd = 3; fd < (int)FD_SETSIZE; fd++) {
285 * Make sure nobody spoofs us with a lot of extra open files
286 * that would cause a successful open to get a very high file
287 * descriptor, which in turn might be used as an index into
288 * an array (e.g. an fd_set).
293 set_pname("amfetchdump");
295 /* Don't die when child closes pipe */
296 signal(SIGPIPE, SIG_IGN);
298 dbopen(DBG_SUBDIR_SERVER);
300 erroutput_type = ERR_INTERACTIVE;
301 error_exit_status = 2;
303 rst_flags = new_rst_flags();
304 rst_flags->wait_tape_prompt = 1;
307 cfg_ovr = new_config_overwrites(argc/2);
308 while( (opt = getopt(argc, argv, "alht:scCpb:nwi:d:O:o:")) != -1) {
311 rst_flags->blocksize = (ssize_t)strtol(optarg, &e, 10);
312 if(*e == 'k' || *e == 'K') {
313 rst_flags->blocksize *= 1024;
314 } else if(*e == 'm' || *e == 'M') {
315 rst_flags->blocksize *= 1024 * 1024;
316 } else if(*e != '\0') {
317 error(_("invalid blocksize value \"%s\""), optarg);
320 if(rst_flags->blocksize < DISK_BLOCK_BYTES) {
321 error(_("minimum block size is %dk"), DISK_BLOCK_BYTES / 1024);
325 case 'c': rst_flags->compress = 1; break;
326 case 'O': rst_flags->restore_dir = stralloc(optarg) ; break;
327 case 'd': rst_flags->alt_tapedev = stralloc(optarg) ; break;
329 rst_flags->compress = 1;
330 rst_flags->comp_type = COMPRESS_BEST_OPT;
332 case 'p': rst_flags->pipe_to_fd = STDOUT_FILENO; break;
333 case 's': rst_flags->fsf = (off_t)0; break;
334 case 'l': rst_flags->leave_comp = 1; break;
335 case 'i': rst_flags->inventory_log = stralloc(optarg); break;
336 case 'n': rst_flags->inline_assemble = 0; break;
337 case 'w': rst_flags->delay_assemble = 1; break;
338 case 'a': rst_flags->wait_tape_prompt = 0; break;
339 case 'h': rst_flags->headers = 1; break;
340 case 'o': add_config_overwrite_opt(cfg_ovr, optarg); break;
347 /* Check some flags that affect inventorying */
348 if(rst_flags->inventory_log){
349 if(rst_flags->inline_assemble) rst_flags->delay_assemble = 1;
350 rst_flags->inline_assemble = 0;
351 rst_flags->leave_comp = 1;
352 if(rst_flags->compress){
353 error(_("Cannot force compression when doing inventory/search"));
356 g_fprintf(stderr, _("Doing inventory/search, dumps will not be uncompressed or assembled on-the-fly.\n"));
359 if(rst_flags->delay_assemble){
360 g_fprintf(stderr, _("Using -w, split dumpfiles will *not* be automatically uncompressed.\n"));
364 /* make sure our options all make sense otherwise */
365 if(check_rst_flags(rst_flags) == -1) {
370 if (rst_flags->inventory_log) {
371 minimum_arguments = 1;
373 minimum_arguments = 2;
376 if(argc - optind < minimum_arguments) {
381 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_FATAL, argv[optind++]);
382 apply_config_overwrites(cfg_ovr);
384 check_running_as(RUNNING_AS_DUMPUSER);
386 dbrename(config_name, DBG_SUBDIR_SERVER);
388 dumpspecs = cmdline_parse_dumpspecs(argc - optind, argv + optind,
389 CMDLINE_PARSE_DATESTAMP |
390 CMDLINE_PARSE_LEVEL |
391 CMDLINE_EMPTY_TO_WILDCARD);
394 * We've been told explicitly to go and search through the tapes the hard
397 if(rst_flags->inventory_log){
398 g_fprintf(stderr, _("Beginning tape-by-tape search.\n"));
399 search_tapes(stderr, stdin, rst_flags->alt_tapedev == NULL,
400 NULL, dumpspecs, rst_flags, NULL);
405 /* Decide what tapes we'll need */
406 needed_tapes = list_needed_tapes(dumpspecs,
407 rst_flags->pipe_to_fd == STDOUT_FILENO);
409 parent_pid = getpid();
411 get_lock = lock_logfile(); /* config is loaded, should be ok here */
413 error(_("%s exists: amdump or amflush is already running, or you must run amcleanup"), rst_conf_logfile);
415 search_tapes(NULL, stdin, rst_flags->alt_tapedev == NULL,
416 needed_tapes, dumpspecs, rst_flags, NULL);
419 dumpspec_list_free(dumpspecs);
421 if(rst_flags->inline_assemble || rst_flags->delay_assemble)
422 flush_open_outputs(1, NULL);
423 else flush_open_outputs(0, NULL);
425 free_rst_flags(rst_flags);
433 if(parent_pid == getpid()) {
434 if(get_lock) unlink(rst_conf_logfile);