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