]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/restore.c
prefix links code in Dir
[bacula/bacula] / bacula / src / dird / restore.c
1 /*
2  *
3  *   Bacula Director -- restore.c -- responsible for restoring files
4  *
5  *     Kern Sibbald, November MM
6  *
7  *    This routine is run as a separate thread.  
8  * 
9  * Current implementation is Catalog verification only (i.e. no
10  *  verification versus tape).
11  *
12  *  Basic tasks done here:
13  *     Open DB
14  *     Open Message Channel with Storage daemon to tell him a job will be starting.
15  *     Open connection with File daemon and pass him commands
16  *       to do the restore.
17  *     Update the DB according to what files where restored????
18  *
19  *   Version $Id$
20  */
21
22 /*
23    Copyright (C) 2000-2003 Kern Sibbald and John Walker
24
25    This program is free software; you can redistribute it and/or
26    modify it under the terms of the GNU General Public License as
27    published by the Free Software Foundation; either version 2 of
28    the License, or (at your option) any later version.
29
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33    General Public License for more details.
34
35    You should have received a copy of the GNU General Public
36    License along with this program; if not, write to the Free
37    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38    MA 02111-1307, USA.
39
40  */
41
42 #include "bacula.h"
43 #include "dird.h"
44
45 /* Commands sent to File daemon */
46 static char restorecmd[]   = "restore replace=%c where=%s\n";
47 static char storaddr[]     = "storage address=%s port=%d\n";
48 static char sessioncmd[]   = "session %s %ld %ld %ld %ld %ld %ld\n";  
49
50 /* Responses received from File daemon */
51 static char OKrestore[]   = "2000 OK restore\n";
52 static char OKstore[]     = "2000 OK storage\n";
53 static char OKsession[]   = "2000 OK session\n";
54 static char OKbootstrap[] = "2000 OK bootstrap\n";
55 static char EndRestore[]  = "2800 End Job TermCode=%d JobFiles=%u JobBytes=%" lld "\n";
56
57 /* Forward referenced functions */
58 static void restore_cleanup(JCR *jcr, int status);
59 static int send_bootstrap_file(JCR *jcr);
60
61 /* External functions */
62
63 /* 
64  * Do a restore of the specified files
65  *    
66  *  Returns:  0 on failure
67  *            1 on success
68  */
69 int do_restore(JCR *jcr) 
70 {
71    BSOCK   *fd;
72    JOB_DBR rjr;                       /* restore job record */
73    int ok = FALSE;
74
75
76    if (!get_or_create_client_record(jcr)) {
77       restore_cleanup(jcr, JS_ErrorTerminated);
78       return 0;
79    }
80
81    memset(&rjr, 0, sizeof(rjr));
82    jcr->jr.Level = 'F';            /* Full restore */
83    jcr->jr.StartTime = jcr->start_time;
84    if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
85       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
86       restore_cleanup(jcr, JS_ErrorTerminated);
87       return 0;
88    }
89    Dmsg0(20, "Updated job start record\n");
90    jcr->fname = (char *) get_pool_memory(PM_FNAME);
91
92    Dmsg1(20, "RestoreJobId=%d\n", jcr->job->RestoreJobId);
93
94    /* 
95     * The following code is kept temporarily for compatibility.
96     * It is the predecessor to the Bootstrap file.
97     */
98    if (!jcr->RestoreBootstrap) {
99       /*
100        * Find Job Record for Files to be restored
101        */
102       if (jcr->RestoreJobId != 0) {
103          rjr.JobId = jcr->RestoreJobId;     /* specified by UA */
104       } else {
105          rjr.JobId = jcr->job->RestoreJobId; /* specified by Job Resource */
106       }
107       if (!db_get_job_record(jcr, jcr->db, &rjr)) {
108          Jmsg2(jcr, M_FATAL, 0, _("Cannot get job record id=%d %s"), rjr.JobId,
109             db_strerror(jcr->db));
110          restore_cleanup(jcr, JS_ErrorTerminated);
111          return 0;
112       }
113
114       /*
115        * Now find the Volumes we will need for the Restore
116        */
117       jcr->VolumeName[0] = 0;
118       if (!db_get_job_volume_names(jcr, jcr->db, rjr.JobId, &jcr->VolumeName) ||
119            jcr->VolumeName[0] == 0) {
120          Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for restore Job %d. %s"), 
121             rjr.JobId, db_strerror(jcr->db));
122          restore_cleanup(jcr, JS_ErrorTerminated);
123          return 0;
124       }
125       Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
126    }
127       
128
129    /* Print Job Start message */
130    Jmsg(jcr, M_INFO, 0, _("Start Restore Job %s\n"), jcr->Job);
131
132    /*
133     * Open a message channel connection with the Storage
134     * daemon. This is to let him know that our client
135     * will be contacting him for a backup  session.
136     *
137     */
138    Dmsg0(10, "Open connection with storage daemon\n");
139    set_jcr_job_status(jcr, JS_Blocked);
140    /*
141     * Start conversation with Storage daemon  
142     */
143    if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
144       restore_cleanup(jcr, JS_ErrorTerminated);
145       return 0;
146    }
147    /*
148     * Now start a job with the Storage daemon
149     */
150    if (!start_storage_daemon_job(jcr)) {
151       restore_cleanup(jcr, JS_ErrorTerminated);
152       return 0;
153    }
154    /*
155     * Now start a Storage daemon message thread
156     */
157    if (!start_storage_daemon_message_thread(jcr)) {
158       restore_cleanup(jcr, JS_ErrorTerminated);
159       return 0;
160    }
161    Dmsg0(50, "Storage daemon connection OK\n");
162
163    /* 
164     * Start conversation with File daemon  
165     */
166    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
167       restore_cleanup(jcr, JS_ErrorTerminated);
168       return 0;
169    }
170
171    fd = jcr->file_bsock;
172    set_jcr_job_status(jcr, JS_Running);
173
174    if (!send_include_list(jcr)) {
175       restore_cleanup(jcr, JS_ErrorTerminated);
176       return 0;
177    }
178
179    if (!send_exclude_list(jcr)) {
180       restore_cleanup(jcr, JS_ErrorTerminated);
181       return 0;
182    }
183
184
185    /* 
186     * send Storage daemon address to the File daemon,
187     *   then wait for File daemon to make connection
188     *   with Storage daemon.
189     */
190    set_jcr_job_status(jcr, JS_Blocked);
191    if (jcr->store->SDDport == 0) {
192       jcr->store->SDDport = jcr->store->SDport;
193    }
194    bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
195    Dmsg1(6, "dird>filed: %s\n", fd->msg);
196    if (!response(fd, OKstore, "Storage")) {
197       restore_cleanup(jcr, JS_ErrorTerminated);
198       return 0;
199    }
200    set_jcr_job_status(jcr, JS_Running);
201
202    /* 
203     * Send the bootstrap file -- what Volumes/files to restore
204     */
205    if (!send_bootstrap_file(jcr)) {
206       restore_cleanup(jcr, JS_ErrorTerminated);
207       return 0;
208    }
209
210    /* 
211     * The following code is deprecated   
212     */
213    if (!jcr->RestoreBootstrap) {
214       /*
215        * Pass the VolSessionId, VolSessionTime, Start and
216        * end File and Blocks on the session command.
217        */
218       bnet_fsend(fd, sessioncmd, 
219                 jcr->VolumeName,
220                 rjr.VolSessionId, rjr.VolSessionTime, 
221                 rjr.StartFile, rjr.EndFile, rjr.StartBlock, 
222                 rjr.EndBlock);
223       if (!response(fd, OKsession, "Session")) {
224          restore_cleanup(jcr, JS_ErrorTerminated);
225          return 0;
226       }
227    }
228
229    /* Send restore command */
230    char replace, *where;
231
232    if (jcr->replace != 0) {
233       replace = jcr->replace;
234    } else if (jcr->job->replace != 0) {
235       replace = jcr->job->replace;
236    } else {
237       replace = REPLACE_ALWAYS;       /* always replace */
238    }
239    if (jcr->RestoreWhere) {
240       where = jcr->RestoreWhere;      /* override */
241    } else if (jcr->job->RestoreWhere) {
242       where = jcr->job->RestoreWhere; /* no override take from job */
243    } else {
244       where = "";                     /* None */
245    }
246    bash_spaces(where);
247    bnet_fsend(fd, restorecmd, replace, where);
248    unbash_spaces(where);
249
250    if (!response(fd, OKrestore, "Restore")) {
251       restore_cleanup(jcr, JS_ErrorTerminated);
252       return 0;
253    }
254
255    /* Wait for Job Termination */
256    Dmsg0(20, "wait for job termination\n");
257    while (bget_msg(fd, 0) >= 0) {
258       Dmsg1(100, "dird<filed: %s\n", fd->msg);
259       if (sscanf(fd->msg, EndRestore, &jcr->FDJobStatus, &jcr->JobFiles,
260           &jcr->JobBytes) == 3) {
261          ok = TRUE;
262       }
263    }
264
265    restore_cleanup(jcr, ok?jcr->FDJobStatus:JS_ErrorTerminated);
266
267    return 1;
268 }
269
270 /*
271  * Release resources allocated during restore.
272  *
273  */
274 static void restore_cleanup(JCR *jcr, int TermCode)
275 {
276    char sdt[MAX_TIME_LENGTH], edt[MAX_TIME_LENGTH];
277    char ec1[30], ec2[30];
278    char term_code[100];
279    char *term_msg;
280    int msg_type;
281    double kbps;
282
283    Dmsg0(20, "In restore_cleanup\n");
284    set_jcr_job_status(jcr, TermCode);
285
286    update_job_end_record(jcr);
287
288    msg_type = M_INFO;                 /* by default INFO message */
289    switch (TermCode) {
290       case JS_Terminated:
291          term_msg = _("Restore OK");
292          break;
293       case JS_FatalError:
294       case JS_ErrorTerminated:
295          term_msg = _("*** Restore Error ***"); 
296          msg_type = M_ERROR;          /* Generate error message */
297          if (jcr->store_bsock) {
298             bnet_sig(jcr->store_bsock, BNET_TERMINATE);
299             pthread_cancel(jcr->SD_msg_chan);
300          }
301          break;
302       case JS_Cancelled:
303          term_msg = _("Restore Cancelled");
304          if (jcr->store_bsock) {
305             bnet_sig(jcr->store_bsock, BNET_TERMINATE);
306             pthread_cancel(jcr->SD_msg_chan);
307          }
308          break;
309       default:
310          term_msg = term_code;
311          sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
312          break;
313    }
314    bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
315    bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
316    kbps = (double)jcr->jr.JobBytes / (1000 * (jcr->jr.EndTime - jcr->jr.StartTime));
317
318    Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n\
319 JobId:                  %d\n\
320 Job:                    %s\n\
321 Client:                 %s\n\
322 Start time:             %s\n\
323 End time:               %s\n\
324 Files Restored:         %s\n\
325 Bytes Restored:         %s\n\
326 Rate:                   %.1f KB/s\n\
327 Termination:            %s\n\n"),
328         edt,
329         jcr->jr.JobId,
330         jcr->jr.Job,
331         jcr->client->hdr.name,
332         sdt,
333         edt,
334         edit_uint64_with_commas((uint64_t)jcr->jr.JobFiles, ec1),
335         edit_uint64_with_commas(jcr->jr.JobBytes, ec2),
336         (float)kbps,
337         term_msg);
338
339    Dmsg0(20, "Leaving restore_cleanup\n");
340 }
341
342 static int send_bootstrap_file(JCR *jcr)
343 {
344    FILE *bs;
345    char buf[1000];
346    BSOCK *fd = jcr->file_bsock;
347    char *bootstrap = "bootstrap\n";
348
349    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
350    if (!jcr->RestoreBootstrap) {
351       return 1;
352    }
353    bs = fopen(jcr->RestoreBootstrap, "r");
354    if (!bs) {
355       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
356          jcr->RestoreBootstrap, strerror(errno));
357       set_jcr_job_status(jcr, JS_ErrorTerminated);
358       return 0;
359    }
360    strcpy(fd->msg, bootstrap);  
361    fd->msglen = strlen(fd->msg);
362    bnet_send(fd);
363    while (fgets(buf, sizeof(buf), bs)) {
364       fd->msglen = Mmsg(&fd->msg, "%s", buf);
365       bnet_send(fd);       
366    }
367    bnet_sig(fd, BNET_EOD);
368    fclose(bs);
369    if (!response(fd, OKbootstrap, "Bootstrap")) {
370       set_jcr_job_status(jcr, JS_ErrorTerminated);
371       return 0;
372    }
373    return 1;
374 }