]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/restore.c
ebl use regexwhere instead of rwhere
[bacula/bacula] / bacula / src / dird / restore.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation plus additions
11    that are listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *   Bacula Director -- restore.c -- responsible for restoring files
30  *
31  *     Kern Sibbald, November MM
32  *
33  *    This routine is run as a separate thread.
34  *
35  * Current implementation is Catalog verification only (i.e. no
36  *  verification versus tape).
37  *
38  *  Basic tasks done here:
39  *     Open DB
40  *     Open Message Channel with Storage daemon to tell him a job will be starting.
41  *     Open connection with File daemon and pass him commands
42  *       to do the restore.
43  *     Update the DB according to what files where restored????
44  *
45  *   Version $Id$
46  */
47
48
49 #include "bacula.h"
50 #include "dird.h"
51
52 /* Commands sent to File daemon */
53 static char restorecmd[]        = "restore replace=%c prelinks=%d where=%s\n";
54 static char restorecmdR[] = "restore replace=%c prelinks=%d regexwhere=%s\n";
55 static char storaddr[]   = "storage address=%s port=%d ssl=0\n";
56
57 /* Responses received from File daemon */
58 static char OKrestore[]   = "2000 OK restore\n";
59 static char OKstore[]     = "2000 OK storage\n";
60 static char OKbootstrap[] = "2000 OK bootstrap\n";
61
62 /*
63  * Do a restore of the specified files
64  *
65  *  Returns:  0 on failure
66  *            1 on success
67  */
68 bool do_restore(JCR *jcr)
69 {
70    BSOCK   *fd;
71    JOB_DBR rjr;                       /* restore job record */
72
73    free_wstorage(jcr);                /* we don't write */
74
75    memset(&rjr, 0, sizeof(rjr));
76    jcr->jr.JobLevel = L_FULL;         /* Full restore */
77    if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
78       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
79       restore_cleanup(jcr, JS_ErrorTerminated);
80       return false;
81    }
82    Dmsg0(20, "Updated job start record\n");
83
84    Dmsg1(20, "RestoreJobId=%d\n", jcr->job->RestoreJobId);
85
86    if (!jcr->RestoreBootstrap) {
87       Jmsg0(jcr, M_FATAL, 0, _("Cannot restore without a bootstrap file.\n"
88           "You probably ran a restore job directly. All restore jobs must\n"
89           "be run using the restore command.\n"));
90       restore_cleanup(jcr, JS_ErrorTerminated);
91       return false;
92    }
93
94
95    /* Print Job Start message */
96    Jmsg(jcr, M_INFO, 0, _("Start Restore Job %s\n"), jcr->Job);
97
98    /*
99     * Open a message channel connection with the Storage
100     * daemon. This is to let him know that our client
101     * will be contacting him for a backup  session.
102     *
103     */
104    Dmsg0(10, "Open connection with storage daemon\n");
105    set_jcr_job_status(jcr, JS_WaitSD);
106    /*
107     * Start conversation with Storage daemon
108     */
109    if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
110       restore_cleanup(jcr, JS_ErrorTerminated);
111       return false;
112    }
113    /*
114     * Now start a job with the Storage daemon
115     */
116    if (!start_storage_daemon_job(jcr, jcr->rstorage, NULL)) {
117       restore_cleanup(jcr, JS_ErrorTerminated);
118       return false;
119    }
120    if (!bnet_fsend(jcr->store_bsock, "run")) {
121       return false;
122    }
123    /*
124     * Now start a Storage daemon message thread
125     */
126    if (!start_storage_daemon_message_thread(jcr)) {
127       restore_cleanup(jcr, JS_ErrorTerminated);
128       return false;
129    }
130    Dmsg0(50, "Storage daemon connection OK\n");
131
132
133    /*
134     * Start conversation with File daemon
135     */
136    set_jcr_job_status(jcr, JS_WaitFD);
137    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
138       restore_cleanup(jcr, JS_ErrorTerminated);
139       return false;
140    }
141
142    fd = jcr->file_bsock;
143    set_jcr_job_status(jcr, JS_Running);
144
145    /*
146     * send Storage daemon address to the File daemon,
147     *   then wait for File daemon to make connection
148     *   with Storage daemon.
149     */
150    if (jcr->rstore->SDDport == 0) {
151       jcr->rstore->SDDport = jcr->rstore->SDport;
152    }
153    bnet_fsend(fd, storaddr, jcr->rstore->address, jcr->rstore->SDDport);
154    Dmsg1(6, "dird>filed: %s\n", fd->msg);
155    if (!response(jcr, fd, OKstore, "Storage", DISPLAY_ERROR)) {
156       restore_cleanup(jcr, JS_ErrorTerminated);
157       return false;
158    }
159
160    /*
161     * Send the bootstrap file -- what Volumes/files to restore
162     */
163    if (!send_bootstrap_file(jcr, fd) ||
164        !response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
165       restore_cleanup(jcr, JS_ErrorTerminated);
166       return false;
167    }
168
169
170    if (!send_runscripts_commands(jcr)) {
171       restore_cleanup(jcr, JS_ErrorTerminated);
172       return false;
173    }
174
175    /* Send restore command */
176    char replace, *where, *cmd;
177    char empty = '\0';
178
179    if (jcr->replace != 0) {
180       replace = jcr->replace;
181    } else if (jcr->job->replace != 0) {
182       replace = jcr->job->replace;
183    } else {
184       replace = REPLACE_ALWAYS;       /* always replace */
185    }
186    if (jcr->where) {
187       where = jcr->where;             /* override */
188    } else if (jcr->job->RestoreWhere) {
189       where = jcr->job->RestoreWhere; /* no override take from job */
190    } else {
191       where = ∅                 /* None */
192    }
193    
194    jcr->prefix_links = jcr->job->PrefixLinks;
195
196    if (jcr->where_use_regexp) {
197       cmd = restorecmdR;
198    } else {
199       cmd = restorecmd;
200    }
201
202    bash_spaces(where);
203    bnet_fsend(fd, cmd, replace, jcr->prefix_links, where);
204    unbash_spaces(where);
205
206    if (!response(jcr, fd, OKrestore, "Restore", DISPLAY_ERROR)) {
207       restore_cleanup(jcr, JS_ErrorTerminated);
208       return false;
209    }
210
211    /* Wait for Job Termination */
212    int stat = wait_for_job_termination(jcr);
213    restore_cleanup(jcr, stat);
214
215    return true;
216 }
217
218 bool do_restore_init(JCR *jcr) 
219 {
220    free_wstorage(jcr);
221    return true;
222 }
223
224 /*
225  * Release resources allocated during restore.
226  *
227  */
228 void restore_cleanup(JCR *jcr, int TermCode)
229 {
230    char sdt[MAX_TIME_LENGTH], edt[MAX_TIME_LENGTH];
231    char ec1[30], ec2[30], ec3[30];
232    char term_code[100], fd_term_msg[100], sd_term_msg[100];
233    const char *term_msg;
234    int msg_type = M_INFO;
235    double kbps;
236
237    Dmsg0(20, "In restore_cleanup\n");
238    update_job_end(jcr, TermCode);
239
240    if (jcr->unlink_bsr && jcr->RestoreBootstrap) {
241       unlink(jcr->RestoreBootstrap);
242       jcr->unlink_bsr = false;
243    }
244
245    switch (TermCode) {
246    case JS_Terminated:
247       if (jcr->ExpectedFiles > jcr->jr.JobFiles) {
248          term_msg = _("Restore OK -- warning file count mismatch");
249       } else {
250          term_msg = _("Restore OK");
251       }
252       break;
253    case JS_FatalError:
254    case JS_ErrorTerminated:
255       term_msg = _("*** Restore Error ***");
256       msg_type = M_ERROR;          /* Generate error message */
257       if (jcr->store_bsock) {
258          bnet_sig(jcr->store_bsock, BNET_TERMINATE);
259          if (jcr->SD_msg_chan) {
260             pthread_cancel(jcr->SD_msg_chan);
261          }
262       }
263       break;
264    case JS_Canceled:
265       term_msg = _("Restore Canceled");
266       if (jcr->store_bsock) {
267          bnet_sig(jcr->store_bsock, BNET_TERMINATE);
268          if (jcr->SD_msg_chan) {
269             pthread_cancel(jcr->SD_msg_chan);
270          }
271       }
272       break;
273    default:
274       term_msg = term_code;
275       sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
276       break;
277    }
278    bstrftimes(sdt, sizeof(sdt), jcr->jr.StartTime);
279    bstrftimes(edt, sizeof(edt), jcr->jr.EndTime);
280    if (jcr->jr.EndTime - jcr->jr.StartTime > 0) {
281       kbps = (double)jcr->jr.JobBytes / (1000 * (jcr->jr.EndTime - jcr->jr.StartTime));
282    } else {
283       kbps = 0;
284    }
285    if (kbps < 0.05) {
286       kbps = 0;
287    }
288
289    jobstatus_to_ascii(jcr->FDJobStatus, fd_term_msg, sizeof(fd_term_msg));
290    jobstatus_to_ascii(jcr->SDJobStatus, sd_term_msg, sizeof(sd_term_msg));
291
292    Jmsg(jcr, msg_type, 0, _("Bacula %s %s (%s): %s\n"
293 "  Build OS:               %s %s %s\n"
294 "  JobId:                  %d\n"
295 "  Job:                    %s\n"
296 "  Client:                 %s\n"
297 "  Start time:             %s\n"
298 "  End time:               %s\n"
299 "  Files Expected:         %s\n"
300 "  Files Restored:         %s\n"
301 "  Bytes Restored:         %s\n"
302 "  Rate:                   %.1f KB/s\n"
303 "  FD Errors:              %d\n"
304 "  FD termination status:  %s\n"
305 "  SD termination status:  %s\n"
306 "  Termination:            %s\n\n"),
307         my_name, VERSION, LSMDATE, edt,
308         HOST_OS, DISTNAME, DISTVER,
309         jcr->jr.JobId,
310         jcr->jr.Job,
311         jcr->client->hdr.name,
312         sdt,
313         edt,
314         edit_uint64_with_commas((uint64_t)jcr->ExpectedFiles, ec1),
315         edit_uint64_with_commas((uint64_t)jcr->jr.JobFiles, ec2),
316         edit_uint64_with_commas(jcr->jr.JobBytes, ec3),
317         (float)kbps,
318         jcr->Errors,
319         fd_term_msg,
320         sd_term_msg,
321         term_msg);
322
323    Dmsg0(20, "Leaving restore_cleanup\n");
324 }