]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/restore.c
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / dird / restore.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2000-2015 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /**
21  *   Bacula Director -- restore.c -- responsible for restoring files
22  *
23  *     Written by Kern Sibbald, November MM
24  *
25  *    This routine is run as a separate thread.
26  *
27  * Current implementation is Catalog verification only (i.e. no
28  *  verification versus tape).
29  *
30  *  Basic tasks done here:
31  *     Open DB
32  *     Open Message Channel with Storage daemon to tell him a job will be starting.
33  *     Open connection with File daemon and pass him commands
34  *       to do the restore.
35  *     Update the DB according to what files where restored????
36  *
37  */
38
39
40 #include "bacula.h"
41 #include "dird.h"
42 #include "lib/ini.h"
43
44 /* Commands sent to File daemon */
45 static char restorecmd[]  = "restore %sreplace=%c prelinks=%d where=%s\n";
46 static char restorecmdR[] = "restore %sreplace=%c prelinks=%d regexwhere=%s\n";
47 static char storaddr[]    = "storage address=%s port=%d ssl=%d Authorization=%s\n";
48
49 /* Responses received from File daemon */
50 static char OKrestore[]   = "2000 OK restore\n";
51 static char OKstore[]     = "2000 OK storage\n";
52 static char OKstoreend[]  = "2000 OK storage end\n";
53
54 /* Responses received from the Storage daemon */
55 static char OKbootstrap[] = "3000 OK bootstrap\n";
56
57 static void build_restore_command(JCR *jcr, POOL_MEM &ret)
58 {
59    char replace, *where, *cmd;
60    char empty = '\0';
61    char files[100];
62
63    /* Build the restore command */
64
65    if (jcr->replace != 0) {
66       replace = jcr->replace;
67    } else if (jcr->job->replace != 0) {
68       replace = jcr->job->replace;
69    } else {
70       replace = REPLACE_ALWAYS;       /* always replace */
71    }
72
73    if (jcr->RegexWhere) {
74       where = jcr->RegexWhere;             /* override */
75       cmd = restorecmdR;
76    } else if (jcr->job->RegexWhere) {
77       where = jcr->job->RegexWhere;   /* no override take from job */
78       cmd = restorecmdR;
79
80    } else if (jcr->where) {
81       where = jcr->where;             /* override */
82       cmd = restorecmd;
83    } else if (jcr->job->RestoreWhere) {
84       where = jcr->job->RestoreWhere; /* no override take from job */
85       cmd = restorecmd;
86
87    } else {                           /* nothing was specified */
88       where = ∅                 /* use default */
89       cmd   = restorecmd;
90    }
91
92    jcr->prefix_links = jcr->job->PrefixLinks;
93
94    bash_spaces(where);
95    if (jcr->FDVersion < 7) {
96       Mmsg(ret, cmd, "", replace, jcr->prefix_links, where);
97    } else {
98       snprintf(files, sizeof(files), "files=%d ", jcr->ExpectedFiles);
99       Mmsg(ret, cmd, files, replace, jcr->prefix_links, where);
100    }
101    unbash_spaces(where);
102 }
103
104 struct bootstrap_info
105 {
106    FILE *bs;
107    UAContext *ua;
108    char storage[MAX_NAME_LENGTH+1];
109 };
110
111 #define UA_CMD_SIZE 1000
112
113 /**
114  * Open the bootstrap file and find the first Storage=
115  * Returns ok if able to open
116  * It fills the storage name (should be the first line)
117  * and the file descriptor to the bootstrap file,
118  * it should be used for next operations, and need to be closed
119  * at the end.
120  */
121 static bool open_bootstrap_file(JCR *jcr, bootstrap_info &info)
122 {
123    FILE *bs;
124    UAContext *ua;
125    info.bs = NULL;
126    info.ua = NULL;
127
128    if (!jcr->RestoreBootstrap) {
129       return false;
130    }
131    strncpy(info.storage, jcr->rstore->name(), MAX_NAME_LENGTH);
132
133    bs = fopen(jcr->RestoreBootstrap, "rb");
134    if (!bs) {
135       berrno be;
136       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
137          jcr->RestoreBootstrap, be.bstrerror());
138       jcr->setJobStatus(JS_ErrorTerminated);
139       return false;
140    }
141
142    ua = new_ua_context(jcr);
143    ua->cmd = check_pool_memory_size(ua->cmd, UA_CMD_SIZE+1);
144    while (!fgets(ua->cmd, UA_CMD_SIZE, bs)) {
145       parse_ua_args(ua);
146       if (ua->argc != 1) {
147          continue;
148       }
149       if (!strcasecmp(ua->argk[0], "Storage")) {
150          strncpy(info.storage, ua->argv[0], MAX_NAME_LENGTH);
151          break;
152       }
153    }
154    info.bs = bs;
155    info.ua = ua;
156    fseek(bs, 0, SEEK_SET);      /* return to the top of the file */
157    return true;
158 }
159
160 /**
161  * This function compare the given storage name with the
162  * the current one. We compare the name and the address:port.
163  * Returns true if we use the same storage.
164  */
165 static bool is_on_same_storage(JCR *jcr, char *new_one)
166 {
167    STORE *new_store;
168
169    /* with old FD, we send the whole bootstrap to the storage */
170    if (jcr->FDVersion < 2) {
171       return true;
172    }
173    /* we are in init loop ? shoudn't fail here */
174    if (!*new_one) {
175       return true;
176    }
177    /* same name */
178    if (!strcmp(new_one, jcr->rstore->name())) {
179       return true;
180    }
181    new_store = (STORE *)GetResWithName(R_STORAGE, new_one);
182    if (!new_store) {
183       Jmsg(jcr, M_WARNING, 0,
184            _("Could not get storage resource '%s'.\n"), new_one);
185       /* If not storage found, use last one */
186       return true;
187    }
188    /* if Port and Hostname/IP are same, we are talking to the same
189     * Storage Daemon
190     */
191    if (jcr->rstore->SDport != new_store->SDport ||
192        strcmp(jcr->rstore->address, new_store->address))
193    {
194       return false;
195    }
196    return true;
197 }
198
199 /**
200  * Check if the current line contains Storage="xxx", and compare the
201  * result to the current storage. We use UAContext to analyse the bsr
202  * string.
203  *
204  * Returns true if we need to change the storage, and it set the new
205  * Storage resource name in "storage" arg.
206  */
207 static bool check_for_new_storage(JCR *jcr, bootstrap_info &info)
208 {
209    UAContext *ua = info.ua;
210    parse_ua_args(ua);
211    if (ua->argc != 1) {
212       return false;
213    }
214    if (!strcasecmp(ua->argk[0], "Storage")) {
215       /* Continue if this is a volume from the same storage. */
216       if (is_on_same_storage(jcr, ua->argv[0])) {
217          return false;
218       }
219       /* note the next storage name */
220       strncpy(info.storage, ua->argv[0], MAX_NAME_LENGTH);
221       Dmsg1(5, "Change storage to %s\n", info.storage);
222       return true;
223    }
224    return false;
225 }
226
227 /**
228  * Send bootstrap file to Storage daemon section by section.
229  */
230 static bool send_bootstrap_file(JCR *jcr, BSOCK *sock,
231                                 bootstrap_info &info)
232 {
233    boffset_t pos;
234    const char *bootstrap = "bootstrap\n";
235    UAContext *ua = info.ua;
236    FILE *bs = info.bs;
237
238    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
239    if (!jcr->RestoreBootstrap) {
240       return false;
241    }
242    sock->fsend(bootstrap);
243    pos = ftello(bs);
244    while(fgets(ua->cmd, UA_CMD_SIZE, bs)) {
245       if (check_for_new_storage(jcr, info)) {
246          /* Otherwise, we need to contact another storage daemon.
247           * Reset bs to the beginning of the current segment.
248           */
249          fseeko(bs, pos, SEEK_SET);
250          break;
251       }
252       sock->fsend("%s", ua->cmd);
253       pos = ftello(bs);
254    }
255    sock->signal(BNET_EOD);
256    return true;
257 }
258
259 #define MAX_TRIES 6 * 360   /* 6 hours */
260
261 /**
262  * Change the read storage resource for the current job.
263  */
264 static bool select_rstore(JCR *jcr, bootstrap_info &info)
265 {
266    USTORE ustore;
267    int i;
268
269
270    if (!strcmp(jcr->rstore->name(), info.storage)) {
271       return true;                 /* same SD nothing to change */
272    }
273
274    if (!(ustore.store = (STORE *)GetResWithName(R_STORAGE,info.storage))) {
275       Jmsg(jcr, M_FATAL, 0,
276            _("Could not get storage resource '%s'.\n"), info.storage);
277       jcr->setJobStatus(JS_ErrorTerminated);
278       return false;
279    }
280
281    /*
282     * This releases the store_bsock between calls to the SD.
283     *  I think.
284     */
285    free_bsock(jcr->store_bsock);
286
287    /*
288     * release current read storage and get a new one
289     */
290    dec_read_store(jcr);
291    free_rstorage(jcr);
292    set_rstorage(jcr, &ustore);
293    jcr->setJobStatus(JS_WaitSD);
294    /*
295     * Wait for up to 6 hours to increment read stoage counter
296     */
297    for (i=0; i < MAX_TRIES; i++) {
298       /* try to get read storage counter incremented */
299       if (inc_read_store(jcr)) {
300          jcr->setJobStatus(JS_Running);
301          return true;
302       }
303       bmicrosleep(10, 0);       /* sleep 10 secs */
304       if (job_canceled(jcr)) {
305          free_rstorage(jcr);
306          return false;
307       }
308    }
309    /* Failed to inc_read_store() */
310    free_rstorage(jcr);
311    Jmsg(jcr, M_FATAL, 0,
312       _("Could not acquire read storage lock for \"%s\""), info.storage);
313    return false;
314 }
315
316 /*
317  * Clean the bootstrap_info struct
318  */
319 static void close_bootstrap_file(bootstrap_info &info)
320 {
321    if (info.bs) {
322       fclose(info.bs);
323    }
324    if (info.ua) {
325       free_ua_context(info.ua);
326    }
327 }
328
329 /**
330  * The bootstrap is stored in a file, so open the file, and loop
331  *   through it processing each storage device in turn. If the
332  *   storage is different from the prior one, we open a new connection
333  *   to the new storage and do a restore for that part.
334  * This permits handling multiple storage daemons for a single
335  *   restore.  E.g. your Full is stored on tape, and Incrementals
336  *   on disk.
337  */
338 bool restore_bootstrap(JCR *jcr)
339 {
340    int tls_need = BNET_TLS_NONE;
341    BSOCK *fd = NULL;
342    BSOCK *sd;
343    char *store_address;
344    uint32_t store_port;
345    bool first_time = true;
346    bootstrap_info info;
347    POOL_MEM restore_cmd(PM_MESSAGE);
348    bool ret = false;
349
350
351    /* Open the bootstrap file */
352    if (!open_bootstrap_file(jcr, info)) {
353       goto bail_out;
354    }
355    /* Read the bootstrap file */
356    while (!feof(info.bs)) {
357
358       if (!select_rstore(jcr, info)) {
359          goto bail_out;
360       }
361
362       /**
363        * Open a message channel connection with the Storage
364        * daemon. This is to let him know that our client
365        * will be contacting him for a backup  session.
366        *
367        */
368       Dmsg0(10, "Open connection with storage daemon\n");
369       jcr->setJobStatus(JS_WaitSD);
370       /*
371        * Start conversation with Storage daemon
372        */
373       if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
374          goto bail_out;
375       }
376       sd = jcr->store_bsock;
377       /*
378        * Now start a job with the Storage daemon
379        */
380       if (!start_storage_daemon_job(jcr, jcr->rstorage, NULL)) {
381          goto bail_out;
382       }
383
384       if (first_time) {
385          /*
386           * Start conversation with File daemon
387           */
388          jcr->setJobStatus(JS_WaitFD);
389          jcr->keep_sd_auth_key = true; /* don't clear the sd_auth_key now */
390          if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
391             goto bail_out;
392          }
393          fd = jcr->file_bsock;
394          build_restore_command(jcr, restore_cmd);
395       }
396
397       jcr->setJobStatus(JS_Running);
398
399       /*
400        * Send the bootstrap file -- what Volumes/files to restore
401        */
402       if (!send_bootstrap_file(jcr, sd, info) ||
403           !response(jcr, sd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
404          goto bail_out;
405       }
406
407       if (jcr->sd_calls_client) {
408          /*
409           * SD must call "client" i.e. FD
410           */
411          if (jcr->FDVersion < 10) {
412             Jmsg(jcr, M_FATAL, 0, _("The File daemon does not support SDCallsClient.\n"));
413             goto bail_out;
414          }
415          if (!send_client_addr_to_sd(jcr)) {
416             goto bail_out;
417          }
418          if (!run_storage_and_start_message_thread(jcr, sd)) {
419             goto bail_out;
420          }
421
422          store_address = jcr->rstore->address;  /* dummy */
423          store_port = 0;                        /* flag that SD calls FD */
424
425       } else {
426          /*
427           * Default case where FD must call the SD
428           */
429          if (!run_storage_and_start_message_thread(jcr, sd)) {
430             goto bail_out;
431          }
432
433          /*
434           * send Storage daemon address to the File daemon,
435           *   then wait for File daemon to make connection
436           *   with Storage daemon.
437           */
438          if (jcr->rstore->SDDport == 0) {
439             jcr->rstore->SDDport = jcr->rstore->SDport;
440          }
441
442          store_address = get_storage_address(jcr->client, jcr->rstore);
443          store_port = jcr->rstore->SDDport;
444       }
445
446       /* TLS Requirement */
447       if (jcr->rstore->tls_enable) {
448          if (jcr->rstore->tls_require) {
449             tls_need = BNET_TLS_REQUIRED;
450          } else {
451             tls_need = BNET_TLS_OK;
452          }
453       }
454
455       /*
456        * Send storage address to FD
457        *  if port==0 FD must wait for SD to call it.
458        */
459       fd->fsend(storaddr, store_address, store_port, tls_need, jcr->sd_auth_key);
460       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
461       Dmsg1(6, "dird>filed: %s\n", fd->msg);
462       if (!response(jcr, fd, OKstore, "Storage", DISPLAY_ERROR)) {
463          goto bail_out;
464       }
465
466       /* Declare the job started to start the MaxRunTime check */
467       jcr->setJobStarted();
468
469       /* Only pass "global" commands to the FD once */
470       if (first_time) {
471          first_time = false;
472          if (!send_runscripts_commands(jcr)) {
473             goto bail_out;
474          }
475          if (!send_component_info(jcr)) {
476             Pmsg0(000, "FAIL: Send component info\n");
477             goto bail_out;
478          }
479          if (!send_restore_objects(jcr)) {
480             Pmsg0(000, "FAIL: Send restore objects\n");
481             goto bail_out;
482          }
483       }
484
485       fd->fsend("%s", restore_cmd.c_str());
486       if (!response(jcr, fd, OKrestore, "Restore", DISPLAY_ERROR)) {
487          goto bail_out;
488       }
489
490       if (jcr->FDVersion < 2) { /* Old FD */
491          break;                 /* we do only one loop */
492       } else {
493          if (!response(jcr, fd, OKstoreend, "Store end", DISPLAY_ERROR)) {
494             goto bail_out;
495          }
496          wait_for_storage_daemon_termination(jcr);
497       }
498    } /* the whole boostrap has been send */
499
500    if (fd && jcr->FDVersion >= 2) {
501       fd->fsend("endrestore");
502    }
503
504    ret = true;
505
506 bail_out:
507    close_bootstrap_file(info);
508    return ret;
509 }
510
511 /**
512  * Do a restore of the specified files
513  *
514  *  Returns:  0 on failure
515  *            1 on success
516  */
517 bool do_restore(JCR *jcr)
518 {
519    JOB_DBR rjr;                       /* restore job record */
520    int stat;
521
522    free_wstorage(jcr);                /* we don't write */
523
524    if (!allow_duplicate_job(jcr)) {
525       goto bail_out;
526    }
527
528    memset(&rjr, 0, sizeof(rjr));
529    jcr->jr.JobLevel = L_FULL;         /* Full restore */
530    if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
531       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
532       goto bail_out;
533    }
534    Dmsg0(20, "Updated job start record\n");
535
536    Dmsg1(20, "RestoreJobId=%d\n", jcr->job->RestoreJobId);
537
538    if (!jcr->RestoreBootstrap) {
539       Jmsg(jcr, M_FATAL, 0, _("Cannot restore without a bootstrap file.\n"
540           "You probably ran a restore job directly. All restore jobs must\n"
541           "be run using the restore command.\n"));
542       goto bail_out;
543    }
544
545
546    /* Print Job Start message */
547    Jmsg(jcr, M_INFO, 0, _("Start Restore Job %s\n"), jcr->Job);
548
549    if (jcr->client) {
550       jcr->sd_calls_client = jcr->client->sd_calls_client;
551    }
552
553    /* Read the bootstrap file and do the restore */
554    if (!restore_bootstrap(jcr)) {
555       goto bail_out;
556    }
557
558    /* Wait for Job Termination */
559    stat = wait_for_job_termination(jcr);
560    restore_cleanup(jcr, stat);
561    return true;
562
563 bail_out:
564    restore_cleanup(jcr, JS_ErrorTerminated);
565    return false;
566 }
567
568 bool do_restore_init(JCR *jcr)
569 {
570    free_wstorage(jcr);
571    return true;
572 }
573
574 /**
575  * Release resources allocated during restore.
576  *
577  */
578 void restore_cleanup(JCR *jcr, int TermCode)
579 {
580    char sdt[MAX_TIME_LENGTH], edt[MAX_TIME_LENGTH];
581    char ec1[30], ec2[30], ec3[30];
582    char term_code[100], fd_term_msg[100], sd_term_msg[100];
583    const char *term_msg;
584    int msg_type = M_INFO;
585    double kbps;
586
587    Dmsg0(20, "In restore_cleanup\n");
588    update_job_end(jcr, TermCode);
589
590    if (jcr->component_fd) {
591       fclose(jcr->component_fd);
592       jcr->component_fd = NULL;
593    }
594    if (jcr->component_fname && *jcr->component_fname) {
595       unlink(jcr->component_fname);
596    }
597    free_and_null_pool_memory(jcr->component_fname);
598
599    if (jcr->unlink_bsr && jcr->RestoreBootstrap) {
600       unlink(jcr->RestoreBootstrap);
601       jcr->unlink_bsr = false;
602    }
603
604    if (job_canceled(jcr)) {
605       cancel_storage_daemon_job(jcr);
606    }
607
608    switch (TermCode) {
609    case JS_Terminated:
610       if (jcr->ExpectedFiles > jcr->jr.JobFiles) {
611          term_msg = _("Restore OK -- warning file count mismatch");
612       } else {
613          term_msg = _("Restore OK");
614       }
615       break;
616    case JS_Warnings:
617          term_msg = _("Restore OK -- with warnings");
618          break;
619    case JS_FatalError:
620    case JS_ErrorTerminated:
621       term_msg = _("*** Restore Error ***");
622       msg_type = M_ERROR;          /* Generate error message */
623       if (jcr->store_bsock) {
624          jcr->store_bsock->signal(BNET_TERMINATE);
625          if (jcr->SD_msg_chan_started) {
626             pthread_cancel(jcr->SD_msg_chan);
627          }
628       }
629       break;
630    case JS_Canceled:
631       term_msg = _("Restore Canceled");
632       if (jcr->store_bsock) {
633          jcr->store_bsock->signal(BNET_TERMINATE);
634          if (jcr->SD_msg_chan_started) {
635             pthread_cancel(jcr->SD_msg_chan);
636          }
637       }
638       break;
639    default:
640       term_msg = term_code;
641       sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
642       break;
643    }
644    bstrftimes(sdt, sizeof(sdt), jcr->jr.StartTime);
645    bstrftimes(edt, sizeof(edt), jcr->jr.EndTime);
646    if (jcr->jr.EndTime - jcr->jr.StartTime > 0) {
647       kbps = (double)jcr->jr.JobBytes / (1000 * (jcr->jr.EndTime - jcr->jr.StartTime));
648    } else {
649       kbps = 0;
650    }
651    if (kbps < 0.05) {
652       kbps = 0;
653    }
654
655    jobstatus_to_ascii(jcr->FDJobStatus, fd_term_msg, sizeof(fd_term_msg));
656    jobstatus_to_ascii(jcr->SDJobStatus, sd_term_msg, sizeof(sd_term_msg));
657
658    Jmsg(jcr, msg_type, 0, _("%s %s %s (%s):\n"
659 "  Build OS:               %s %s %s\n"
660 "  JobId:                  %d\n"
661 "  Job:                    %s\n"
662 "  Restore Client:         %s\n"
663 "  Start time:             %s\n"
664 "  End time:               %s\n"
665 "  Files Expected:         %s\n"
666 "  Files Restored:         %s\n"
667 "  Bytes Restored:         %s\n"
668 "  Rate:                   %.1f KB/s\n"
669 "  FD Errors:              %d\n"
670 "  FD termination status:  %s\n"
671 "  SD termination status:  %s\n"
672 "  Termination:            %s\n\n"),
673         BACULA, my_name, VERSION, LSMDATE,
674         HOST_OS, DISTNAME, DISTVER,
675         jcr->jr.JobId,
676         jcr->jr.Job,
677         jcr->client->name(),
678         sdt,
679         edt,
680         edit_uint64_with_commas((uint64_t)jcr->ExpectedFiles, ec1),
681         edit_uint64_with_commas((uint64_t)jcr->jr.JobFiles, ec2),
682         edit_uint64_with_commas(jcr->jr.JobBytes, ec3),
683         (float)kbps,
684         jcr->JobErrors,
685         fd_term_msg,
686         sd_term_msg,
687         term_msg);
688
689    Dmsg0(20, "Leaving restore_cleanup\n");
690 }