3 * Bacula Director -- restore.c -- responsible for restoring files
5 * Kern Sibbald, November MM
7 * This routine is run as a separate thread. There may be more
8 * work to be done to make it totally reentrant!!!!
10 * Current implementation is Catalog verification only (i.e. no
11 * verification versus tape).
13 * Basic tasks done here:
15 * Open Message Channel with Storage daemon to tell him a job will be starting.
16 * Open connection with File daemon and pass him commands
18 * Update the DB according to what files where restored????
24 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
26 This program is free software; you can redistribute it and/or
27 modify it under the terms of the GNU General Public License as
28 published by the Free Software Foundation; either version 2 of
29 the License, or (at your option) any later version.
31 This program is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 General Public License for more details.
36 You should have received a copy of the GNU General Public
37 License along with this program; if not, write to the Free
38 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
46 /* Commands sent to File daemon */
47 static char restorecmd[] = "restore replace=%c where=%s\n";
48 static char storaddr[] = "storage address=%s port=%d\n";
49 static char sessioncmd[] = "session %s %ld %ld %ld %ld %ld %ld\n";
51 /* Responses received from File daemon */
52 static char OKrestore[] = "2000 OK restore\n";
53 static char OKstore[] = "2000 OK storage\n";
54 static char OKsession[] = "2000 OK session\n";
55 static char OKbootstrap[] = "2000 OK bootstrap\n";
56 static char EndRestore[] = "2800 End Job TermCode=%d JobFiles=%u JobBytes=%" lld "\n";
58 /* Forward referenced functions */
59 static void restore_cleanup(JCR *jcr, int status);
60 static int send_bootstrap_file(JCR *jcr);
62 /* External functions */
65 * Do a restore of the specified files
67 * Returns: 0 on failure
70 int do_restore(JCR *jcr)
73 JOB_DBR rjr; /* restore job record */
77 if (!get_or_create_client_record(jcr)) {
78 restore_cleanup(jcr, JS_ErrorTerminated);
82 memset(&rjr, 0, sizeof(rjr));
83 jcr->jr.Level = 'F'; /* Full restore */
84 jcr->jr.StartTime = jcr->start_time;
85 if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
86 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
87 restore_cleanup(jcr, JS_ErrorTerminated);
90 Dmsg0(20, "Updated job start record\n");
91 jcr->fname = (char *) get_pool_memory(PM_FNAME);
93 Dmsg1(20, "RestoreJobId=%d\n", jcr->job->RestoreJobId);
96 * The following code is kept temporarily for compatibility.
97 * It is the predecessor to the Bootstrap file.
99 if (!jcr->RestoreBootstrap) {
101 * Find Job Record for Files to be restored
103 if (jcr->RestoreJobId != 0) {
104 rjr.JobId = jcr->RestoreJobId; /* specified by UA */
106 rjr.JobId = jcr->job->RestoreJobId; /* specified by Job Resource */
108 if (!db_get_job_record(jcr->db, &rjr)) {
109 Jmsg2(jcr, M_FATAL, 0, _("Cannot get job record id=%d %s"), rjr.JobId,
110 db_strerror(jcr->db));
111 restore_cleanup(jcr, JS_ErrorTerminated);
116 * Now find the Volumes we will need for the Restore
118 jcr->VolumeName[0] = 0;
119 if (!db_get_job_volume_names(jcr->db, rjr.JobId, &jcr->VolumeName) ||
120 jcr->VolumeName[0] == 0) {
121 Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for restore Job %d. %s"),
122 rjr.JobId, db_strerror(jcr->db));
123 restore_cleanup(jcr, JS_ErrorTerminated);
126 Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
130 /* Print Job Start message */
131 Jmsg(jcr, M_INFO, 0, _("Start Restore Job %s\n"), jcr->Job);
134 * Open a message channel connection with the Storage
135 * daemon. This is to let him know that our client
136 * will be contacting him for a backup session.
139 Dmsg0(10, "Open connection with storage daemon\n");
140 jcr->JobStatus = JS_Blocked;
142 * Start conversation with Storage daemon
144 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
145 restore_cleanup(jcr, JS_ErrorTerminated);
149 * Now start a job with the Storage daemon
151 if (!start_storage_daemon_job(jcr)) {
152 restore_cleanup(jcr, JS_ErrorTerminated);
156 * Now start a Storage daemon message thread
158 if (!start_storage_daemon_message_thread(jcr)) {
159 restore_cleanup(jcr, JS_ErrorTerminated);
162 Dmsg0(50, "Storage daemon connection OK\n");
165 * Start conversation with File daemon
167 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
168 restore_cleanup(jcr, JS_ErrorTerminated);
172 fd = jcr->file_bsock;
173 jcr->JobStatus = JS_Running;
175 if (!send_include_list(jcr)) {
176 restore_cleanup(jcr, JS_ErrorTerminated);
180 if (!send_exclude_list(jcr)) {
181 restore_cleanup(jcr, JS_ErrorTerminated);
187 * send Storage daemon address to the File daemon,
188 * then wait for File daemon to make connection
189 * with Storage daemon.
191 jcr->JobStatus = JS_Blocked;
192 if (jcr->store->SDDport == 0) {
193 jcr->store->SDDport = jcr->store->SDport;
195 bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
196 Dmsg1(6, "dird>filed: %s\n", fd->msg);
197 if (!response(fd, OKstore, "Storage")) {
198 restore_cleanup(jcr, JS_ErrorTerminated);
201 jcr->JobStatus = JS_Running;
204 * Send the bootstrap file -- what Volumes/files to restore
206 if (!send_bootstrap_file(jcr)) {
207 restore_cleanup(jcr, JS_ErrorTerminated);
212 * The following code is deprecated
214 if (!jcr->RestoreBootstrap) {
216 * Pass the VolSessionId, VolSessionTime, Start and
217 * end File and Blocks on the session command.
219 bnet_fsend(fd, sessioncmd,
221 rjr.VolSessionId, rjr.VolSessionTime,
222 rjr.StartFile, rjr.EndFile, rjr.StartBlock,
224 if (!response(fd, OKsession, "Session")) {
225 restore_cleanup(jcr, JS_ErrorTerminated);
230 /* Send restore command */
231 char replace, *where;
233 if (jcr->job->replace != 0) {
234 replace = jcr->job->replace;
236 replace = 'a'; /* always replace */
238 if (jcr->RestoreWhere) {
239 where = jcr->RestoreWhere; /* override */
240 } else if (jcr->job->RestoreWhere) {
241 where = jcr->job->RestoreWhere; /* no override take from job */
243 where = ""; /* None */
246 bnet_fsend(fd, restorecmd, replace, where);
247 unbash_spaces(where);
249 if (!response(fd, OKrestore, "Restore")) {
250 restore_cleanup(jcr, JS_ErrorTerminated);
254 /* Wait for Job Termination */
255 Dmsg0(20, "wait for job termination\n");
256 while (bget_msg(fd, 0) >= 0) {
257 Dmsg1(100, "dird<filed: %s\n", fd->msg);
258 if (sscanf(fd->msg, EndRestore, &jcr->JobStatus, &jcr->JobFiles,
259 &jcr->JobBytes) == 3) {
264 restore_cleanup(jcr, ok?jcr->JobStatus:JS_ErrorTerminated);
270 * Release resources allocated during restore.
273 static void restore_cleanup(JCR *jcr, int TermCode)
275 char sdt[MAX_TIME_LENGTH], edt[MAX_TIME_LENGTH];
276 char ec1[30], ec2[30];
282 Dmsg0(20, "In restore_cleanup\n");
283 jcr->JobStatus = TermCode;
285 update_job_end_record(jcr);
287 msg_type = M_INFO; /* by default INFO message */
290 term_msg = _("Restore OK");
293 case JS_ErrorTerminated:
294 term_msg = _("*** Restore Error ***");
295 msg_type = M_ERROR; /* Generate error message */
296 if (jcr->store_bsock) {
297 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
298 pthread_cancel(jcr->SD_msg_chan);
302 term_msg = _("Restore Cancelled");
303 if (jcr->store_bsock) {
304 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
305 pthread_cancel(jcr->SD_msg_chan);
309 term_msg = term_code;
310 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
313 bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
314 bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
315 kbps = (double)jcr->jr.JobBytes / (1000 * (jcr->jr.EndTime - jcr->jr.StartTime));
317 Jmsg(jcr, msg_type, 0, _("%s\n\
323 Files Restored: %s\n\
324 Bytes Restored: %s\n\
326 Termination: %s\n\n"),
330 jcr->client->hdr.name,
333 edit_uint64_with_commas(jcr->jr.JobFiles, ec1),
334 edit_uint64_with_commas(jcr->jr.JobBytes, ec2),
338 Dmsg0(20, "Leaving restore_cleanup\n");
341 static int send_bootstrap_file(JCR *jcr)
345 BSOCK *fd = jcr->file_bsock;
346 char *bootstrap = "bootstrap\n";
348 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
349 if (!jcr->RestoreBootstrap) {
352 bs = fopen(jcr->RestoreBootstrap, "r");
354 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
355 jcr->RestoreBootstrap, strerror(errno));
356 jcr->JobStatus = JS_ErrorTerminated;
359 strcpy(fd->msg, bootstrap);
360 fd->msglen = strlen(fd->msg);
362 while (fgets(buf, sizeof(buf), bs)) {
363 fd->msglen = Mmsg(&fd->msg, "%s", buf);
366 bnet_sig(fd, BNET_EOD);
368 if (!response(fd, OKbootstrap, "Bootstrap")) {
369 jcr->JobStatus = JS_ErrorTerminated;