2 * Bacula Director -- restore.c -- responsible for restoring files
4 * Kern Sibbald, November MM
6 * This routine is run as a separate thread.
8 * Current implementation is Catalog verification only (i.e. no
9 * verification versus tape).
11 * Basic tasks done here:
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
16 * Update the DB according to what files where restored????
22 Copyright (C) 2000-2005 Kern Sibbald
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.
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.
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,
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";
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";
55 * Do a restore of the specified files
57 * Returns: 0 on failure
60 bool do_restore(JCR *jcr)
63 JOB_DBR rjr; /* restore job record */
65 memset(&rjr, 0, sizeof(rjr));
66 jcr->jr.JobLevel = L_FULL; /* Full restore */
67 if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
68 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
69 restore_cleanup(jcr, JS_ErrorTerminated);
72 Dmsg0(20, "Updated job start record\n");
74 Dmsg1(20, "RestoreJobId=%d\n", jcr->job->RestoreJobId);
77 * The following code is kept temporarily for compatibility.
78 * It is the predecessor to the Bootstrap file.
81 if (!jcr->RestoreBootstrap) {
83 * Find Job Record for Files to be restored
85 if (jcr->RestoreJobId != 0) {
86 rjr.JobId = jcr->RestoreJobId; /* specified by UA */
88 rjr.JobId = jcr->job->RestoreJobId; /* specified by Job Resource */
90 if (!db_get_job_record(jcr, jcr->db, &rjr)) {
91 Jmsg2(jcr, M_FATAL, 0, _("Cannot get job record id=%d %s"), rjr.JobId,
92 db_strerror(jcr->db));
93 restore_cleanup(jcr, JS_ErrorTerminated);
98 * Now find the Volumes we will need for the Restore
100 jcr->VolumeName[0] = 0;
101 if (!db_get_job_volume_names(jcr, jcr->db, rjr.JobId, &jcr->VolumeName) ||
102 jcr->VolumeName[0] == 0) {
103 Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume names for restore Job %d. %s"),
104 rjr.JobId, db_strerror(jcr->db));
105 restore_cleanup(jcr, JS_ErrorTerminated);
108 Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
112 /* Print Job Start message */
113 Jmsg(jcr, M_INFO, 0, _("Start Restore Job %s\n"), jcr->Job);
116 * Open a message channel connection with the Storage
117 * daemon. This is to let him know that our client
118 * will be contacting him for a backup session.
121 Dmsg0(10, "Open connection with storage daemon\n");
122 set_jcr_job_status(jcr, JS_WaitSD);
124 * Start conversation with Storage daemon
126 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
127 restore_cleanup(jcr, JS_ErrorTerminated);
131 * Now start a job with the Storage daemon
133 if (!start_storage_daemon_job(jcr, jcr->storage, SD_READ)) {
134 restore_cleanup(jcr, JS_ErrorTerminated);
138 * Now start a Storage daemon message thread
140 if (!start_storage_daemon_message_thread(jcr)) {
141 restore_cleanup(jcr, JS_ErrorTerminated);
144 Dmsg0(50, "Storage daemon connection OK\n");
147 * Start conversation with File daemon
149 set_jcr_job_status(jcr, JS_WaitFD);
150 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
151 restore_cleanup(jcr, JS_ErrorTerminated);
155 fd = jcr->file_bsock;
156 set_jcr_job_status(jcr, JS_Running);
158 if (!send_include_list(jcr)) {
159 restore_cleanup(jcr, JS_ErrorTerminated);
163 if (!send_exclude_list(jcr)) {
164 restore_cleanup(jcr, JS_ErrorTerminated);
169 * send Storage daemon address to the File daemon,
170 * then wait for File daemon to make connection
171 * with Storage daemon.
173 if (jcr->store->SDDport == 0) {
174 jcr->store->SDDport = jcr->store->SDport;
176 bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
177 Dmsg1(6, "dird>filed: %s\n", fd->msg);
178 if (!response(jcr, fd, OKstore, "Storage", DISPLAY_ERROR)) {
179 restore_cleanup(jcr, JS_ErrorTerminated);
184 * Send the bootstrap file -- what Volumes/files to restore
186 if (!send_bootstrap_file(jcr)) {
187 restore_cleanup(jcr, JS_ErrorTerminated);
192 * The following code is deprecated
194 if (!jcr->RestoreBootstrap) {
196 * Pass the VolSessionId, VolSessionTime, Start and
197 * end File and Blocks on the session command.
199 bnet_fsend(fd, sessioncmd,
201 rjr.VolSessionId, rjr.VolSessionTime,
202 rjr.StartFile, rjr.EndFile, rjr.StartBlock,
204 if (!response(jcr, fd, OKsession, "Session", DISPLAY_ERROR)) {
205 restore_cleanup(jcr, JS_ErrorTerminated);
210 if (!send_run_before_and_after_commands(jcr)) {
211 restore_cleanup(jcr, JS_ErrorTerminated);
215 /* Send restore command */
216 char replace, *where;
219 if (jcr->replace != 0) {
220 replace = jcr->replace;
221 } else if (jcr->job->replace != 0) {
222 replace = jcr->job->replace;
224 replace = REPLACE_ALWAYS; /* always replace */
227 where = jcr->where; /* override */
228 } else if (jcr->job->RestoreWhere) {
229 where = jcr->job->RestoreWhere; /* no override take from job */
231 where = ∅ /* None */
233 jcr->prefix_links = jcr->job->PrefixLinks;
235 bnet_fsend(fd, restorecmd, replace, jcr->prefix_links, where);
236 unbash_spaces(where);
238 if (!response(jcr, fd, OKrestore, "Restore", DISPLAY_ERROR)) {
239 restore_cleanup(jcr, JS_ErrorTerminated);
243 /* Wait for Job Termination */
244 int stat = wait_for_job_termination(jcr);
245 restore_cleanup(jcr, stat);
250 bool do_restore_init(JCR *jcr)
256 * Release resources allocated during restore.
259 void restore_cleanup(JCR *jcr, int TermCode)
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;
268 Dmsg0(20, "In restore_cleanup\n");
269 dequeue_messages(jcr); /* display any queued messages */
270 set_jcr_job_status(jcr, TermCode);
272 update_job_end_record(jcr);
274 msg_type = M_INFO; /* by default INFO message */
277 if (jcr->ExpectedFiles > jcr->jr.JobFiles) {
278 term_msg = _("Restore OK -- warning file count mismatch");
280 term_msg = _("Restore OK");
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);
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);
304 term_msg = term_code;
305 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
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));
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));
322 Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n"
328 " Files Expected: %s\n"
329 " Files Restored: %s\n"
330 " Bytes Restored: %s\n"
333 " FD termination status: %s\n"
334 " SD termination status: %s\n"
335 " Termination: %s\n\n"),
339 jcr->client->hdr.name,
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),
351 Dmsg0(20, "Leaving restore_cleanup\n");