3 * Bacula Director -- restore.c -- responsible for restoring files
5 * Kern Sibbald, November MM
7 * This routine is run as a separate thread.
9 * Current implementation is Catalog verification only (i.e. no
10 * verification versus tape).
12 * Basic tasks done here:
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
17 * Update the DB according to what files where restored????
23 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
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";
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";
57 /* Forward referenced functions */
58 static void restore_cleanup(JCR *jcr, int status);
59 static int send_bootstrap_file(JCR *jcr);
61 /* External functions */
64 * Do a restore of the specified files
66 * Returns: 0 on failure
69 int do_restore(JCR *jcr)
72 JOB_DBR rjr; /* restore job record */
76 if (!get_or_create_client_record(jcr)) {
77 restore_cleanup(jcr, JS_ErrorTerminated);
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);
89 Dmsg0(20, "Updated job start record\n");
90 jcr->fname = (char *) get_pool_memory(PM_FNAME);
92 Dmsg1(20, "RestoreJobId=%d\n", jcr->job->RestoreJobId);
95 * The following code is kept temporarily for compatibility.
96 * It is the predecessor to the Bootstrap file.
98 if (!jcr->RestoreBootstrap) {
100 * Find Job Record for Files to be restored
102 if (jcr->RestoreJobId != 0) {
103 rjr.JobId = jcr->RestoreJobId; /* specified by UA */
105 rjr.JobId = jcr->job->RestoreJobId; /* specified by Job Resource */
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);
115 * Now find the Volumes we will need for the Restore
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);
125 Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
129 /* Print Job Start message */
130 Jmsg(jcr, M_INFO, 0, _("Start Restore Job %s\n"), jcr->Job);
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.
138 Dmsg0(10, "Open connection with storage daemon\n");
139 set_jcr_job_status(jcr, JS_Blocked);
141 * Start conversation with Storage daemon
143 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
144 restore_cleanup(jcr, JS_ErrorTerminated);
148 * Now start a job with the Storage daemon
150 if (!start_storage_daemon_job(jcr)) {
151 restore_cleanup(jcr, JS_ErrorTerminated);
155 * Now start a Storage daemon message thread
157 if (!start_storage_daemon_message_thread(jcr)) {
158 restore_cleanup(jcr, JS_ErrorTerminated);
161 Dmsg0(50, "Storage daemon connection OK\n");
164 * Start conversation with File daemon
166 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
167 restore_cleanup(jcr, JS_ErrorTerminated);
171 fd = jcr->file_bsock;
172 set_jcr_job_status(jcr, JS_Running);
174 if (!send_include_list(jcr)) {
175 restore_cleanup(jcr, JS_ErrorTerminated);
179 if (!send_exclude_list(jcr)) {
180 restore_cleanup(jcr, JS_ErrorTerminated);
186 * send Storage daemon address to the File daemon,
187 * then wait for File daemon to make connection
188 * with Storage daemon.
190 set_jcr_job_status(jcr, JS_Blocked);
191 if (jcr->store->SDDport == 0) {
192 jcr->store->SDDport = jcr->store->SDport;
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);
200 set_jcr_job_status(jcr, JS_Running);
203 * Send the bootstrap file -- what Volumes/files to restore
205 if (!send_bootstrap_file(jcr)) {
206 restore_cleanup(jcr, JS_ErrorTerminated);
211 * The following code is deprecated
213 if (!jcr->RestoreBootstrap) {
215 * Pass the VolSessionId, VolSessionTime, Start and
216 * end File and Blocks on the session command.
218 bnet_fsend(fd, sessioncmd,
220 rjr.VolSessionId, rjr.VolSessionTime,
221 rjr.StartFile, rjr.EndFile, rjr.StartBlock,
223 if (!response(fd, OKsession, "Session")) {
224 restore_cleanup(jcr, JS_ErrorTerminated);
229 /* Send restore command */
230 char replace, *where;
232 if (jcr->replace != 0) {
233 replace = jcr->replace;
234 } else if (jcr->job->replace != 0) {
235 replace = jcr->job->replace;
237 replace = REPLACE_ALWAYS; /* always replace */
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 */
244 where = ""; /* None */
247 bnet_fsend(fd, restorecmd, replace, where);
248 unbash_spaces(where);
250 if (!response(fd, OKrestore, "Restore")) {
251 restore_cleanup(jcr, JS_ErrorTerminated);
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) {
265 restore_cleanup(jcr, ok?jcr->FDJobStatus:JS_ErrorTerminated);
271 * Release resources allocated during restore.
274 static void restore_cleanup(JCR *jcr, int TermCode)
276 char sdt[MAX_TIME_LENGTH], edt[MAX_TIME_LENGTH];
277 char ec1[30], ec2[30];
283 Dmsg0(20, "In restore_cleanup\n");
284 set_jcr_job_status(jcr, TermCode);
286 update_job_end_record(jcr);
288 msg_type = M_INFO; /* by default INFO message */
291 term_msg = _("Restore OK");
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);
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);
310 term_msg = term_code;
311 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
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));
318 Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n\
324 Files Restored: %s\n\
325 Bytes Restored: %s\n\
327 Termination: %s\n\n"),
331 jcr->client->hdr.name,
334 edit_uint64_with_commas((uint64_t)jcr->jr.JobFiles, ec1),
335 edit_uint64_with_commas(jcr->jr.JobBytes, ec2),
339 Dmsg0(20, "Leaving restore_cleanup\n");
342 static int send_bootstrap_file(JCR *jcr)
346 BSOCK *fd = jcr->file_bsock;
347 char *bootstrap = "bootstrap\n";
349 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
350 if (!jcr->RestoreBootstrap) {
353 bs = fopen(jcr->RestoreBootstrap, "r");
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);
360 strcpy(fd->msg, bootstrap);
361 fd->msglen = strlen(fd->msg);
363 while (fgets(buf, sizeof(buf), bs)) {
364 fd->msglen = Mmsg(&fd->msg, "%s", buf);
367 bnet_sig(fd, BNET_EOD);
369 if (!response(fd, OKbootstrap, "Bootstrap")) {
370 set_jcr_job_status(jcr, JS_ErrorTerminated);