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-2003 Kern Sibbald and John Walker
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";
54 /* Forward referenced functions */
55 static void restore_cleanup(JCR *jcr, int status);
57 /* External functions */
60 * Do a restore of the specified files
62 * Returns: 0 on failure
65 int do_restore(JCR *jcr)
68 JOB_DBR rjr; /* restore job record */
70 if (!get_or_create_client_record(jcr)) {
71 restore_cleanup(jcr, JS_ErrorTerminated);
75 memset(&rjr, 0, sizeof(rjr));
76 jcr->jr.Level = 'F'; /* 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);
83 Dmsg0(20, "Updated job start record\n");
84 jcr->fname = (char *) get_pool_memory(PM_FNAME);
86 Dmsg1(20, "RestoreJobId=%d\n", jcr->job->RestoreJobId);
89 * The following code is kept temporarily for compatibility.
90 * It is the predecessor to the Bootstrap file.
92 if (!jcr->RestoreBootstrap) {
94 * Find Job Record for Files to be restored
96 if (jcr->RestoreJobId != 0) {
97 rjr.JobId = jcr->RestoreJobId; /* specified by UA */
99 rjr.JobId = jcr->job->RestoreJobId; /* specified by Job Resource */
101 if (!db_get_job_record(jcr, jcr->db, &rjr)) {
102 Jmsg2(jcr, M_FATAL, 0, _("Cannot get job record id=%d %s"), rjr.JobId,
103 db_strerror(jcr->db));
104 restore_cleanup(jcr, JS_ErrorTerminated);
109 * Now find the Volumes we will need for the Restore
111 jcr->VolumeName[0] = 0;
112 if (!db_get_job_volume_names(jcr, jcr->db, rjr.JobId, &jcr->VolumeName) ||
113 jcr->VolumeName[0] == 0) {
114 Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for restore Job %d. %s"),
115 rjr.JobId, db_strerror(jcr->db));
116 restore_cleanup(jcr, JS_ErrorTerminated);
119 Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
123 /* Print Job Start message */
124 Jmsg(jcr, M_INFO, 0, _("Start Restore Job %s\n"), jcr->Job);
127 * Open a message channel connection with the Storage
128 * daemon. This is to let him know that our client
129 * will be contacting him for a backup session.
132 Dmsg0(10, "Open connection with storage daemon\n");
133 set_jcr_job_status(jcr, JS_WaitSD);
135 * Start conversation with Storage daemon
137 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
138 restore_cleanup(jcr, JS_ErrorTerminated);
142 * Now start a job with the Storage daemon
144 if (!start_storage_daemon_job(jcr)) {
145 restore_cleanup(jcr, JS_ErrorTerminated);
149 * Now start a Storage daemon message thread
151 if (!start_storage_daemon_message_thread(jcr)) {
152 restore_cleanup(jcr, JS_ErrorTerminated);
155 Dmsg0(50, "Storage daemon connection OK\n");
158 * Start conversation with File daemon
160 set_jcr_job_status(jcr, JS_WaitFD);
161 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
162 restore_cleanup(jcr, JS_ErrorTerminated);
166 fd = jcr->file_bsock;
167 set_jcr_job_status(jcr, JS_Running);
169 if (!send_include_list(jcr)) {
170 restore_cleanup(jcr, JS_ErrorTerminated);
174 if (!send_exclude_list(jcr)) {
175 restore_cleanup(jcr, JS_ErrorTerminated);
180 * send Storage daemon address to the File daemon,
181 * then wait for File daemon to make connection
182 * with Storage daemon.
184 if (jcr->store->SDDport == 0) {
185 jcr->store->SDDport = jcr->store->SDport;
187 bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
188 Dmsg1(6, "dird>filed: %s\n", fd->msg);
189 if (!response(jcr, fd, OKstore, "Storage", DISPLAY_ERROR)) {
190 restore_cleanup(jcr, JS_ErrorTerminated);
195 * Send the bootstrap file -- what Volumes/files to restore
197 if (!send_bootstrap_file(jcr)) {
198 restore_cleanup(jcr, JS_ErrorTerminated);
203 * The following code is deprecated
205 if (!jcr->RestoreBootstrap) {
207 * Pass the VolSessionId, VolSessionTime, Start and
208 * end File and Blocks on the session command.
210 bnet_fsend(fd, sessioncmd,
212 rjr.VolSessionId, rjr.VolSessionTime,
213 rjr.StartFile, rjr.EndFile, rjr.StartBlock,
215 if (!response(jcr, fd, OKsession, "Session", DISPLAY_ERROR)) {
216 restore_cleanup(jcr, JS_ErrorTerminated);
221 if (!send_run_before_and_after_commands(jcr)) {
222 restore_cleanup(jcr, JS_ErrorTerminated);
226 /* Send restore command */
227 char replace, *where;
229 if (jcr->replace != 0) {
230 replace = jcr->replace;
231 } else if (jcr->job->replace != 0) {
232 replace = jcr->job->replace;
234 replace = REPLACE_ALWAYS; /* always replace */
237 where = jcr->where; /* override */
238 } else if (jcr->job->RestoreWhere) {
239 where = jcr->job->RestoreWhere; /* no override take from job */
241 where = ""; /* None */
243 jcr->prefix_links = jcr->job->PrefixLinks;
245 bnet_fsend(fd, restorecmd, replace, jcr->prefix_links, where);
246 unbash_spaces(where);
248 if (!response(jcr, fd, OKrestore, "Restore", DISPLAY_ERROR)) {
249 restore_cleanup(jcr, JS_ErrorTerminated);
253 /* Wait for Job Termination */
254 int stat = wait_for_job_termination(jcr);
255 restore_cleanup(jcr, stat);
261 * Release resources allocated during restore.
264 static void restore_cleanup(JCR *jcr, int TermCode)
266 char sdt[MAX_TIME_LENGTH], edt[MAX_TIME_LENGTH];
267 char ec1[30], ec2[30];
268 char term_code[100], fd_term_msg[100], sd_term_msg[100];
273 Dmsg0(20, "In restore_cleanup\n");
274 set_jcr_job_status(jcr, TermCode);
276 update_job_end_record(jcr);
278 msg_type = M_INFO; /* by default INFO message */
281 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 pthread_cancel(jcr->SD_msg_chan);
293 term_msg = _("Restore Canceled");
294 if (jcr->store_bsock) {
295 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
296 pthread_cancel(jcr->SD_msg_chan);
300 term_msg = term_code;
301 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
304 bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
305 bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
306 if (jcr->jr.EndTime - jcr->jr.StartTime > 0) {
307 kbps = (double)jcr->jr.JobBytes / (1000 * (jcr->jr.EndTime - jcr->jr.StartTime));
315 jobstatus_to_ascii(jcr->FDJobStatus, fd_term_msg, sizeof(fd_term_msg));
316 jobstatus_to_ascii(jcr->SDJobStatus, sd_term_msg, sizeof(sd_term_msg));
318 Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n\
324 Files Restored: %s\n\
325 Bytes Restored: %s\n\
327 Non-fatal FD Errors: %d\n\
328 FD termination status: %s\n\
329 SD termination status: %s\n\
330 Termination: %s\n\n"),
334 jcr->client->hdr.name,
337 edit_uint64_with_commas((uint64_t)jcr->jr.JobFiles, ec1),
338 edit_uint64_with_commas(jcr->jr.JobBytes, ec2),
345 Dmsg0(20, "Leaving restore_cleanup\n");