]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/restore.c
This commit was manufactured by cvs2svn to create tag
[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-2003 Kern Sibbald and John Walker
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    if (!get_or_create_client_record(jcr)) {
71       restore_cleanup(jcr, JS_ErrorTerminated);
72       return 0;
73    }
74
75    memset(&rjr, 0, sizeof(rjr));
76    jcr->jr.Level = L_FULL;         /* Full restore */
77    jcr->jr.StartTime = jcr->start_time;
78    if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
79       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
80       restore_cleanup(jcr, JS_ErrorTerminated);
81       return 0;
82    }
83    Dmsg0(20, "Updated job start record\n");
84    jcr->fname = (char *) get_pool_memory(PM_FNAME);
85
86    Dmsg1(20, "RestoreJobId=%d\n", jcr->job->RestoreJobId);
87
88    /* 
89     * The following code is kept temporarily for compatibility.
90     * It is the predecessor to the Bootstrap file.
91     *   DEPRECATED
92     */
93    if (!jcr->RestoreBootstrap) {
94       /*
95        * Find Job Record for Files to be restored
96        */
97       if (jcr->RestoreJobId != 0) {
98          rjr.JobId = jcr->RestoreJobId;     /* specified by UA */
99       } else {
100          rjr.JobId = jcr->job->RestoreJobId; /* specified by Job Resource */
101       }
102       if (!db_get_job_record(jcr, jcr->db, &rjr)) {
103          Jmsg2(jcr, M_FATAL, 0, _("Cannot get job record id=%d %s"), rjr.JobId,
104             db_strerror(jcr->db));
105          restore_cleanup(jcr, JS_ErrorTerminated);
106          return 0;
107       }
108
109       /*
110        * Now find the Volumes we will need for the Restore
111        */
112       jcr->VolumeName[0] = 0;
113       if (!db_get_job_volume_names(jcr, jcr->db, rjr.JobId, &jcr->VolumeName) ||
114            jcr->VolumeName[0] == 0) {
115          Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for restore Job %d. %s"), 
116             rjr.JobId, db_strerror(jcr->db));
117          restore_cleanup(jcr, JS_ErrorTerminated);
118          return 0;
119       }
120       Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
121    }
122       
123
124    /* Print Job Start message */
125    Jmsg(jcr, M_INFO, 0, _("Start Restore Job %s\n"), jcr->Job);
126
127    /*
128     * Open a message channel connection with the Storage
129     * daemon. This is to let him know that our client
130     * will be contacting him for a backup  session.
131     *
132     */
133    Dmsg0(10, "Open connection with storage daemon\n");
134    set_jcr_job_status(jcr, JS_WaitSD);
135    /*
136     * Start conversation with Storage daemon  
137     */
138    if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
139       restore_cleanup(jcr, JS_ErrorTerminated);
140       return 0;
141    }
142    /*
143     * Now start a job with the Storage daemon
144     */
145    if (!start_storage_daemon_job(jcr)) {
146       restore_cleanup(jcr, JS_ErrorTerminated);
147       return 0;
148    }
149    /*
150     * Now start a Storage daemon message thread
151     */
152    if (!start_storage_daemon_message_thread(jcr)) {
153       restore_cleanup(jcr, JS_ErrorTerminated);
154       return 0;
155    }
156    Dmsg0(50, "Storage daemon connection OK\n");
157
158    /* 
159     * Start conversation with File daemon  
160     */
161    set_jcr_job_status(jcr, JS_WaitFD);
162    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
163       restore_cleanup(jcr, JS_ErrorTerminated);
164       return 0;
165    }
166
167    fd = jcr->file_bsock;
168    set_jcr_job_status(jcr, JS_Running);
169
170    if (!send_include_list(jcr)) {
171       restore_cleanup(jcr, JS_ErrorTerminated);
172       return 0;
173    }
174
175    if (!send_exclude_list(jcr)) {
176       restore_cleanup(jcr, JS_ErrorTerminated);
177       return 0;
178    }
179
180    /* 
181     * send Storage daemon address to the File daemon,
182     *   then wait for File daemon to make connection
183     *   with Storage daemon.
184     */
185    if (jcr->store->SDDport == 0) {
186       jcr->store->SDDport = jcr->store->SDport;
187    }
188    bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
189    Dmsg1(6, "dird>filed: %s\n", fd->msg);
190    if (!response(jcr, fd, OKstore, "Storage", DISPLAY_ERROR)) {
191       restore_cleanup(jcr, JS_ErrorTerminated);
192       return 0;
193    }
194
195    /* 
196     * Send the bootstrap file -- what Volumes/files to restore
197     */
198    if (!send_bootstrap_file(jcr)) {
199       restore_cleanup(jcr, JS_ErrorTerminated);
200       return 0;
201    }
202
203    /* 
204     * The following code is deprecated   
205     */
206    if (!jcr->RestoreBootstrap) {
207       /*
208        * Pass the VolSessionId, VolSessionTime, Start and
209        * end File and Blocks on the session command.
210        */
211       bnet_fsend(fd, sessioncmd, 
212                 jcr->VolumeName,
213                 rjr.VolSessionId, rjr.VolSessionTime, 
214                 rjr.StartFile, rjr.EndFile, rjr.StartBlock, 
215                 rjr.EndBlock);
216       if (!response(jcr, fd, OKsession, "Session", DISPLAY_ERROR)) {
217          restore_cleanup(jcr, JS_ErrorTerminated);
218          return 0;
219       }
220    }
221
222    if (!send_run_before_and_after_commands(jcr)) {
223       restore_cleanup(jcr, JS_ErrorTerminated);
224       return 0;
225    }
226
227    /* Send restore command */
228    char replace, *where;
229
230    if (jcr->replace != 0) {
231       replace = jcr->replace;
232    } else if (jcr->job->replace != 0) {
233       replace = jcr->job->replace;
234    } else {
235       replace = REPLACE_ALWAYS;       /* always replace */
236    }
237    if (jcr->where) {
238       where = jcr->where;             /* override */
239    } else if (jcr->job->RestoreWhere) {
240       where = jcr->job->RestoreWhere; /* no override take from job */
241    } else {
242       where = "";                     /* None */
243    }
244    jcr->prefix_links = jcr->job->PrefixLinks;
245    bash_spaces(where);
246    bnet_fsend(fd, restorecmd, replace, jcr->prefix_links, where);
247    unbash_spaces(where);
248
249    if (!response(jcr, fd, OKrestore, "Restore", DISPLAY_ERROR)) {
250       restore_cleanup(jcr, JS_ErrorTerminated);
251       return 0;
252    }
253
254    /* Wait for Job Termination */
255    int stat = wait_for_job_termination(jcr);
256    restore_cleanup(jcr, stat);
257
258    return 1;
259 }
260
261 /*
262  * Release resources allocated during restore.
263  *
264  */
265 static void restore_cleanup(JCR *jcr, int TermCode)
266 {
267    char sdt[MAX_TIME_LENGTH], edt[MAX_TIME_LENGTH];
268    char ec1[30], ec2[30];
269    char term_code[100], fd_term_msg[100], sd_term_msg[100];
270    char *term_msg;
271    int msg_type;
272    double kbps;
273
274    Dmsg0(20, "In restore_cleanup\n");
275    set_jcr_job_status(jcr, TermCode);
276
277    update_job_end_record(jcr);
278
279    msg_type = M_INFO;                 /* by default INFO message */
280    switch (TermCode) {
281    case JS_Terminated:
282       term_msg = _("Restore OK");
283       break;
284    case JS_FatalError:
285    case JS_ErrorTerminated:
286       term_msg = _("*** Restore Error ***"); 
287       msg_type = M_ERROR;          /* Generate error message */
288       if (jcr->store_bsock) {
289          bnet_sig(jcr->store_bsock, BNET_TERMINATE);
290          if (jcr->SD_msg_chan) {
291             pthread_cancel(jcr->SD_msg_chan);
292          }
293       }
294       break;
295    case JS_Canceled:
296       term_msg = _("Restore Canceled");
297       if (jcr->store_bsock) {
298          bnet_sig(jcr->store_bsock, BNET_TERMINATE);
299          if (jcr->SD_msg_chan) {
300             pthread_cancel(jcr->SD_msg_chan);
301          }
302       }
303       break;
304    default:
305       term_msg = term_code;
306       sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
307       break;
308    }
309    bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
310    bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
311    if (jcr->jr.EndTime - jcr->jr.StartTime > 0) {
312       kbps = (double)jcr->jr.JobBytes / (1000 * (jcr->jr.EndTime - jcr->jr.StartTime));
313    } else {
314       kbps = 0;
315    }
316    if (kbps < 0.05) {
317       kbps = 0;
318    }
319
320    jobstatus_to_ascii(jcr->FDJobStatus, fd_term_msg, sizeof(fd_term_msg));
321    jobstatus_to_ascii(jcr->SDJobStatus, sd_term_msg, sizeof(sd_term_msg));
322
323    Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n\
324 JobId:                  %d\n\
325 Job:                    %s\n\
326 Client:                 %s\n\
327 Start time:             %s\n\
328 End time:               %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->jr.JobFiles, ec1),
343         edit_uint64_with_commas(jcr->jr.JobBytes, ec2),
344         (float)kbps,
345         jcr->Errors,
346         fd_term_msg,
347         sd_term_msg,
348         term_msg);
349
350    Dmsg0(20, "Leaving restore_cleanup\n");
351 }