2 * Bacula File Daemon Job processing
4 * Kern Sibbald, October MM
10 Copyright (C) 2000-2004 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 extern char my_name[];
33 extern CLIENT *me; /* our client resource */
35 /* Imported functions */
36 extern int status_cmd(JCR *jcr);
38 /* Forward referenced functions */
39 static int backup_cmd(JCR *jcr);
40 static int bootstrap_cmd(JCR *jcr);
41 static int cancel_cmd(JCR *jcr);
42 static int setdebug_cmd(JCR *jcr);
43 static int estimate_cmd(JCR *jcr);
44 static int exclude_cmd(JCR *jcr);
45 static int hello_cmd(JCR *jcr);
46 static int job_cmd(JCR *jcr);
47 static int include_cmd(JCR *jcr);
48 static int level_cmd(JCR *jcr);
49 static int verify_cmd(JCR *jcr);
50 static int restore_cmd(JCR *jcr);
51 static int storage_cmd(JCR *jcr);
52 static int session_cmd(JCR *jcr);
53 static int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd);
54 static void filed_free_jcr(JCR *jcr);
55 static int open_sd_read_session(JCR *jcr);
56 static int send_bootstrap_file(JCR *jcr);
57 static int runbefore_cmd(JCR *jcr);
58 static int runafter_cmd(JCR *jcr);
59 static int run_cmd(JCR *jcr, char *cmd, const char *name);
62 /* Exported functions */
70 * The following are the recognized commands from the Director.
72 static struct s_cmds cmds[] = {
73 {"backup", backup_cmd},
74 {"cancel", cancel_cmd},
75 {"setdebug=", setdebug_cmd},
76 {"estimate", estimate_cmd},
77 {"exclude", exclude_cmd},
79 {"include", include_cmd},
81 {"level = ", level_cmd},
82 {"restore", restore_cmd},
83 {"session", session_cmd},
84 {"status", status_cmd},
85 {"storage ", storage_cmd},
86 {"verify", verify_cmd},
87 {"bootstrap", bootstrap_cmd},
88 {"RunBeforeJob", runbefore_cmd},
89 {"RunAfterJob", runafter_cmd},
90 {NULL, NULL} /* list terminator */
93 /* Commands received from director that need scanning */
94 static char jobcmd[] = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
95 static char storaddr[] = "storage address=%s port=%d ssl=%d\n";
96 static char sessioncmd[] = "session %127s %ld %ld %ld %ld %ld %ld\n";
97 static char restorecmd[] = "restore replace=%c prelinks=%d where=%s\n";
98 static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
99 static char verifycmd[] = "verify level=%30s\n";
100 static char estimatecmd[] = "estimate listing=%d\n";
101 static char runbefore[] = "RunBeforeJob %s\n";
102 static char runafter[] = "RunAfterJob %s\n";
104 /* Responses sent to Director */
105 static char errmsg[] = "2999 Invalid command\n";
106 static char no_auth[] = "2998 No Authorization\n";
107 static char OKinc[] = "2000 OK include\n";
108 static char OKest[] = "2000 OK estimate files=%u bytes=%s\n";
109 static char OKexc[] = "2000 OK exclude\n";
110 static char OKlevel[] = "2000 OK level\n";
111 static char OKbackup[] = "2000 OK backup\n";
112 static char OKbootstrap[] = "2000 OK bootstrap\n";
113 static char OKverify[] = "2000 OK verify\n";
114 static char OKrestore[] = "2000 OK restore\n";
115 static char OKsession[] = "2000 OK session\n";
116 static char OKstore[] = "2000 OK storage\n";
117 static char OKjob[] = "2000 OK Job %s,%s,%s";
118 static char OKsetdebug[] = "2000 OK setdebug=%d\n";
119 static char BADjob[] = "2901 Bad Job\n";
120 static char EndJob[] = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s JobBytes=%s Errors=%u\n";
121 static char OKRunBefore[] = "2000 OK RunBefore\n";
122 static char OKRunAfter[] = "2000 OK RunAfter\n";
124 /* Responses received from Storage Daemon */
125 static char OK_end[] = "3000 OK end\n";
126 static char OK_close[] = "3000 OK close Status = %d\n";
127 static char OK_open[] = "3000 OK open ticket = %d\n";
128 static char OK_data[] = "3000 OK data\n";
129 static char OK_append[] = "3000 OK append data\n";
130 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
133 /* Commands sent to Storage Daemon */
134 static char append_open[] = "append open session\n";
135 static char append_data[] = "append data %d\n";
136 static char append_end[] = "append end session %d\n";
137 static char append_close[] = "append close session %d\n";
138 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
139 static char read_data[] = "read data %d\n";
140 static char read_close[] = "read close session %d\n";
143 * Accept requests from a Director
145 * NOTE! We are running as a separate thread
147 * Send output one line
148 * at a time followed by a zero length transmission.
150 * Return when the connection is terminated or there
153 * Basic task here is:
154 * Authenticate Director (during Hello command).
155 * Accept commands one at a time from the Director
159 void *handle_client_request(void *dirp)
163 BSOCK *dir = (BSOCK *)dirp;
165 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
166 jcr->dir_bsock = dir;
167 jcr->ff = init_find_files();
168 jcr->start_time = time(NULL);
169 jcr->last_fname = get_pool_memory(PM_FNAME);
170 jcr->last_fname[0] = 0;
171 jcr->client_name = get_memory(strlen(my_name) + 1);
172 pm_strcpy(&jcr->client_name, my_name);
174 enable_backup_privileges(NULL, 1 /* ignore_errors */);
176 /**********FIXME******* add command handler error code */
178 for (quit=0; !quit;) {
181 if (bnet_recv(dir) < 0) {
182 break; /* connection terminated */
184 dir->msg[dir->msglen] = 0;
185 Dmsg1(100, "<dird: %s", dir->msg);
187 for (i=0; cmds[i].cmd; i++) {
188 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
189 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
190 bnet_fsend(dir, no_auth);
193 found = TRUE; /* indicate command found */
194 if (!cmds[i].func(jcr)) { /* do command */
195 quit = TRUE; /* error or fully terminated, get out */
196 Dmsg0(20, "Command error or Job done.\n");
201 if (!found) { /* command not found */
202 bnet_fsend(dir, errmsg);
208 /* Inform Storage daemon that we are done */
209 if (jcr->store_bsock) {
210 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
213 if (jcr->RunAfterJob && !job_canceled(jcr)) {
214 run_cmd(jcr, jcr->RunAfterJob, "ClientRunAfterJob");
217 /* Inform Director that we are done */
218 bnet_sig(dir, BNET_TERMINATE);
220 Dmsg0(100, "Calling term_find_files\n");
221 term_find_files((FF_PKT *)jcr->ff);
222 Dmsg0(100, "Done with term_find_files\n");
223 free_jcr(jcr); /* destroy JCR record */
224 Dmsg0(100, "Done with free_jcr\n");
229 * Hello from Director he must identify himself and provide his
232 static int hello_cmd(JCR *jcr)
234 Dmsg0(120, "Calling Authenticate\n");
235 if (!authenticate_director(jcr)) {
238 Dmsg0(120, "OK Authenticate\n");
239 jcr->authenticated = TRUE;
246 static int cancel_cmd(JCR *jcr)
248 BSOCK *dir = jcr->dir_bsock;
249 char Job[MAX_NAME_LENGTH];
252 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
253 if (!(cjcr=get_jcr_by_full_name(Job))) {
254 bnet_fsend(dir, "2901 Job %s not found.\n", Job);
256 if (cjcr->store_bsock) {
258 cjcr->store_bsock->timed_out = 1;
259 cjcr->store_bsock->terminated = 1;
260 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
261 pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
265 set_jcr_job_status(cjcr, JS_Canceled);
267 bnet_fsend(dir, _("2001 Job %s marked to be canceled.\n"), Job);
270 bnet_fsend(dir, _("2902 Error scanning cancel command.\n"));
272 bnet_sig(dir, BNET_EOD);
278 * Set debug level as requested by the Director
281 static int setdebug_cmd(JCR *jcr)
283 BSOCK *dir = jcr->dir_bsock;
284 int level, trace_flag;
286 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
287 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
288 pm_strcpy(&jcr->errmsg, dir->msg);
289 bnet_fsend(dir, "2991 Bad setdebug command: %s\n", jcr->errmsg);
293 set_trace(trace_flag);
294 return bnet_fsend(dir, OKsetdebug, level);
298 static int estimate_cmd(JCR *jcr)
300 BSOCK *dir = jcr->dir_bsock;
303 if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
304 pm_strcpy(&jcr->errmsg, dir->msg);
305 Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
306 bnet_fsend(dir, "2992 Bad estimate command.\n");
310 bnet_fsend(dir, OKest, jcr->num_files_examined,
311 edit_uint64_with_commas(jcr->JobBytes, ed2));
312 bnet_sig(dir, BNET_EOD);
317 * Get JobId and Storage Daemon Authorization key from Director
319 static int job_cmd(JCR *jcr)
321 BSOCK *dir = jcr->dir_bsock;
322 POOLMEM *sd_auth_key;
324 sd_auth_key = get_memory(dir->msglen);
325 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
326 &jcr->VolSessionId, &jcr->VolSessionTime,
328 pm_strcpy(&jcr->errmsg, dir->msg);
329 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
330 bnet_fsend(dir, BADjob);
331 free_pool_memory(sd_auth_key);
334 jcr->sd_auth_key = bstrdup(sd_auth_key);
335 free_pool_memory(sd_auth_key);
336 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
337 return bnet_fsend(dir, OKjob, HOST_OS, DISTNAME, DISTVER);
340 static int runbefore_cmd(JCR *jcr)
343 BSOCK *dir = jcr->dir_bsock;
344 POOLMEM *cmd = get_memory(dir->msglen+1);
346 Dmsg1(100, "runbefore_cmd: %s", dir->msg);
347 if (sscanf(dir->msg, runbefore, cmd) != 1) {
348 pm_strcpy(&jcr->errmsg, dir->msg);
349 Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
350 bnet_fsend(dir, "2905 Bad RunBeforeJob command.\n");
356 /* Run the command now */
357 stat = run_cmd(jcr, cmd, "ClientRunBeforeJob");
360 bnet_fsend(dir, OKRunBefore);
363 bnet_fsend(dir, "2905 Bad RunBeforeJob command.\n");
368 static int runafter_cmd(JCR *jcr)
370 BSOCK *dir = jcr->dir_bsock;
371 POOLMEM *msg = get_memory(dir->msglen+1);
373 Dmsg1(100, "runafter_cmd: %s", dir->msg);
374 if (sscanf(dir->msg, runafter, msg) != 1) {
375 pm_strcpy(&jcr->errmsg, dir->msg);
376 Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
377 bnet_fsend(dir, "2905 Bad RunAfterJob command.\n");
382 if (jcr->RunAfterJob) {
383 free_pool_memory(jcr->RunAfterJob);
385 jcr->RunAfterJob = get_pool_memory(PM_FNAME);
386 pm_strcpy(&jcr->RunAfterJob, msg);
387 free_pool_memory(msg);
388 return bnet_fsend(dir, OKRunAfter);
391 static int run_cmd(JCR *jcr, char *cmd, const char *name)
393 POOLMEM *ecmd = get_pool_memory(PM_FNAME);
396 char line[MAXSTRING];
398 ecmd = edit_job_codes(jcr, ecmd, cmd, "");
399 bpipe = open_bpipe(ecmd, 0, "r");
400 free_pool_memory(ecmd);
401 while (fgets(line, sizeof(line), bpipe->rfd)) {
402 Jmsg(jcr, M_INFO, 0, _("%s: %s"), name, line);
404 status = close_bpipe(bpipe);
406 Jmsg(jcr, M_FATAL, 0, _("%s returned non-zero status=%d\n"), name,
408 set_jcr_job_status(jcr, JS_FatalError);
418 static void add_fname_to_list(JCR *jcr, char *fname, int list)
428 /* Skip leading options -- currently ignored */
429 for (p=fname; *p && *p != ' '; p++)
431 /* Skip spaces, and q points to first space */
432 for (q=NULL; *p && *p == ' '; p++) {
440 p++; /* skip over | */
441 fn = get_pool_memory(PM_FNAME);
442 fn = edit_job_codes(jcr, fn, p, "");
443 bpipe = open_bpipe(fn, 0, "r");
444 free_pool_memory(fn);
446 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
450 /* Copy File options */
451 if (list == INC_LIST) {
452 *q = 0; /* terminate options */
455 optlen = strlen(buf);
459 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
460 strip_trailing_junk(buf);
461 if (list == INC_LIST) {
462 add_fname_to_include_list((FF_PKT *)jcr->ff, 1, buf);
464 add_fname_to_exclude_list((FF_PKT *)jcr->ff, buf);
467 if ((stat=close_bpipe(bpipe)) != 0) {
468 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
469 p, stat, strerror(errno));
474 p++; /* skip over < */
475 if ((ffd = fopen(p, "r")) == NULL) {
476 Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
477 list==INC_LIST?"included":"excluded", p, strerror(errno));
480 /* Copy File options */
481 if (list == INC_LIST) {
482 *q = 0; /* terminate options */
485 optlen = strlen(buf);
489 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
490 strip_trailing_junk(buf);
491 if (list == INC_LIST) {
492 add_fname_to_include_list((FF_PKT *)jcr->ff, 1, buf);
494 add_fname_to_exclude_list((FF_PKT *)jcr->ff, buf);
500 if (list == INC_LIST) {
501 add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
503 add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
511 * Get list of files/directories to include from Director
514 static int include_cmd(JCR *jcr)
516 BSOCK *dir = jcr->dir_bsock;
518 while (bnet_recv(dir) >= 0) {
519 dir->msg[dir->msglen] = 0;
520 strip_trailing_junk(dir->msg);
521 Dmsg1(010, "include file: %s\n", dir->msg);
522 add_fname_to_list(jcr, dir->msg, INC_LIST);
525 return bnet_fsend(dir, OKinc);
529 * Get list of files to exclude from Director
532 static int exclude_cmd(JCR *jcr)
534 BSOCK *dir = jcr->dir_bsock;
536 while (bnet_recv(dir) >= 0) {
537 dir->msg[dir->msglen] = 0;
538 strip_trailing_junk(dir->msg);
539 add_fname_to_list(jcr, dir->msg, EXC_LIST);
540 Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
543 return bnet_fsend(dir, OKexc);
547 static int bootstrap_cmd(JCR *jcr)
549 BSOCK *dir = jcr->dir_bsock;
550 POOLMEM *fname = get_pool_memory(PM_FNAME);
553 if (jcr->RestoreBootstrap) {
554 unlink(jcr->RestoreBootstrap);
555 free_pool_memory(jcr->RestoreBootstrap);
557 Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
559 Dmsg1(400, "bootstrap=%s\n", fname);
560 jcr->RestoreBootstrap = fname;
561 bs = fopen(fname, "a+"); /* create file */
564 * Suck up what he is sending to us so that he will then
565 * read our error message.
567 while (bnet_recv(dir) >= 0)
570 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
571 jcr->RestoreBootstrap, strerror(errno));
572 free_pool_memory(jcr->RestoreBootstrap);
573 jcr->RestoreBootstrap = NULL;
574 set_jcr_job_status(jcr, JS_ErrorTerminated);
578 while (bnet_recv(dir) >= 0) {
579 Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
584 return bnet_fsend(dir, OKbootstrap);
589 * Get backup level from Director
592 static int level_cmd(JCR *jcr)
594 BSOCK *dir = jcr->dir_bsock;
595 POOLMEM *level, *buf = NULL;
600 level = get_memory(dir->msglen+1);
601 Dmsg1(110, "level_cmd: %s", dir->msg);
602 if (sscanf(dir->msg, "level = %s ", level) != 1) {
605 /* Base backup requested? */
606 if (strcmp(level, "base") == 0) {
607 jcr->JobLevel = L_BASE;
608 /* Full backup requested? */
609 } else if (strcmp(level, "full") == 0) {
610 jcr->JobLevel = L_FULL;
612 * Backup requested since <date> <time>
613 * This form is also used for incremental and differential
614 * This code is deprecated. See since_utime for new code.
616 } else if (strcmp(level, "since") == 0) {
617 jcr->JobLevel = L_SINCE;
618 if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d mtime_only=%d",
619 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
620 &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &mtime_only) != 7) {
625 tm.tm_wday = tm.tm_yday = 0;
628 Dmsg2(100, "Got since time: %s mtime_only=%d\n", ctime(&mtime), mtime_only);
629 jcr->incremental = 1; /* set incremental or decremental backup */
630 jcr->mtime = mtime; /* set since time */
632 * We get his UTC since time, then sync the clocks and correct it
633 * to agree with our clock.
635 } else if (strcmp(level, "since_utime") == 0) {
636 buf = get_memory(dir->msglen+1);
637 utime_t since_time, adj;
638 btime_t his_time, bt_start, rt=0, bt_adj=0;
639 jcr->JobLevel = L_SINCE;
640 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
641 buf, &mtime_only) != 2) {
644 since_time = str_to_uint64(buf); /* this is the since time */
645 char ed1[50], ed2[50];
647 * Sync clocks by polling him for the time. We take
648 * 10 samples of his time throwing out the first two.
650 for (int i=0; i<10; i++) {
651 bt_start = get_current_btime();
652 bnet_sig(dir, BNET_BTIME); /* poll for time */
653 if (bnet_recv(dir) <= 0) { /* get response */
656 if (sscanf(dir->msg, "btime %s", buf) != 1) {
659 if (i < 2) { /* toss first two results */
662 his_time = str_to_uint64(buf);
663 rt = get_current_btime() - bt_start; /* compute round trip time */
664 bt_adj -= his_time - bt_start - rt/2;
665 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
668 bt_adj = bt_adj / 8; /* compute average time */
669 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
670 adj = btime_to_utime(bt_adj);
671 since_time += adj; /* adjust for clock difference */
673 Jmsg(jcr, M_INFO, 0, _("Since time adjusted by %d seconds.\n"), adj);
675 bnet_sig(dir, BNET_EOD);
677 Dmsg2(100, "adj = %d since_time=%d\n", (int)adj, (int)since_time);
678 jcr->incremental = 1; /* set incremental or decremental backup */
679 jcr->mtime = since_time; /* set since time */
681 Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
689 return bnet_fsend(dir, OKlevel);
692 pm_strcpy(&jcr->errmsg, dir->msg);
693 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
702 * Get session parameters from Director -- this is for a Restore command
704 static int session_cmd(JCR *jcr)
706 BSOCK *dir = jcr->dir_bsock;
708 Dmsg1(100, "SessionCmd: %s", dir->msg);
709 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
710 &jcr->VolSessionId, &jcr->VolSessionTime,
711 &jcr->StartFile, &jcr->EndFile,
712 &jcr->StartBlock, &jcr->EndBlock) != 7) {
713 pm_strcpy(&jcr->errmsg, dir->msg);
714 Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", jcr->errmsg);
718 return bnet_fsend(dir, OKsession);
722 * Get address of storage daemon from Director
725 static int storage_cmd(JCR *jcr)
727 int stored_port; /* storage daemon port */
728 int enable_ssl; /* enable ssl to sd */
729 BSOCK *dir = jcr->dir_bsock;
730 BSOCK *sd; /* storage daemon bsock */
732 Dmsg1(100, "StorageCmd: %s", dir->msg);
733 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
734 pm_strcpy(&jcr->errmsg, dir->msg);
735 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
738 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
739 /* Open command communications with Storage daemon */
740 /* Try to connect for 1 hour at 10 second intervals */
741 sd = bnet_connect(jcr, 10, me->SDConnectTimeout, _("Storage daemon"),
742 jcr->stored_addr, NULL, stored_port, 1);
744 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
745 jcr->stored_addr, stored_port);
749 jcr->store_bsock = sd;
751 bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
752 if (!authenticate_storagedaemon(jcr)) {
753 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
756 Dmsg0(110, "Authenticated with SD.\n");
758 /* Send OK to Director */
759 return bnet_fsend(dir, OKstore);
764 * Do a backup. For now, we handle only Full and Incremental.
766 static int backup_cmd(JCR *jcr)
768 BSOCK *dir = jcr->dir_bsock;
769 BSOCK *sd = jcr->store_bsock;
772 char ed1[50], ed2[50];
774 set_jcr_job_status(jcr, JS_Blocked);
775 jcr->JobType = JT_BACKUP;
776 Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
779 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
783 bnet_fsend(dir, OKbackup);
784 Dmsg1(110, "bfiled>dird: %s", dir->msg);
787 * Send Append Open Session to Storage daemon
789 bnet_fsend(sd, append_open);
790 Dmsg1(110, ">stored: %s", sd->msg);
792 * Expect to receive back the Ticket number
794 if (bget_msg(sd) >= 0) {
795 Dmsg1(110, "<stored: %s", sd->msg);
796 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
797 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
800 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
802 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
807 * Send Append data command to Storage daemon
809 bnet_fsend(sd, append_data, jcr->Ticket);
810 Dmsg1(110, ">stored: %s", sd->msg);
813 * Expect to get OK data
815 Dmsg1(110, "<stored: %s", sd->msg);
816 if (!response(jcr, sd, OK_data, "Append Data")) {
821 * Send Files to Storage daemon
823 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
824 if (!blast_data_to_storage_daemon(jcr, NULL)) {
825 set_jcr_job_status(jcr, JS_ErrorTerminated);
826 bnet_suppress_error_messages(sd, 1);
828 set_jcr_job_status(jcr, JS_Terminated);
829 if (jcr->JobStatus != JS_Terminated) {
830 bnet_suppress_error_messages(sd, 1);
831 goto cleanup; /* bail out now */
834 * Expect to get response to append_data from Storage daemon
836 if (!response(jcr, sd, OK_append, "Append Data")) {
837 set_jcr_job_status(jcr, JS_ErrorTerminated);
842 * Send Append End Data to Storage daemon
844 bnet_fsend(sd, append_end, jcr->Ticket);
846 if (!response(jcr, sd, OK_end, "Append End")) {
847 set_jcr_job_status(jcr, JS_ErrorTerminated);
852 * Send Append Close to Storage daemon
854 bnet_fsend(sd, append_close, jcr->Ticket);
855 while (bget_msg(sd) >= 0) { /* stop on signal or error */
856 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
858 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
862 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
865 if (SDJobStatus != JS_Terminated) {
866 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
873 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
874 edit_uint64(jcr->ReadBytes, ed1),
875 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
877 return 0; /* return and stop command loop */
881 * Do a Verify for Director
884 static int verify_cmd(JCR *jcr)
886 BSOCK *dir = jcr->dir_bsock;
887 BSOCK *sd = jcr->store_bsock;
888 char level[100], ed1[50], ed2[50];
890 jcr->JobType = JT_VERIFY;
891 if (sscanf(dir->msg, verifycmd, level) != 1) {
892 bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
895 if (strcasecmp(level, "init") == 0) {
896 jcr->JobLevel = L_VERIFY_INIT;
897 } else if (strcasecmp(level, "catalog") == 0){
898 jcr->JobLevel = L_VERIFY_CATALOG;
899 } else if (strcasecmp(level, "volume") == 0){
900 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
901 } else if (strcasecmp(level, "data") == 0){
902 jcr->JobLevel = L_VERIFY_DATA;
903 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
904 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
906 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
910 bnet_fsend(dir, OKverify);
911 Dmsg1(110, "bfiled>dird: %s", dir->msg);
913 switch (jcr->JobLevel) {
915 case L_VERIFY_CATALOG:
918 case L_VERIFY_VOLUME_TO_CATALOG:
919 if (!open_sd_read_session(jcr)) {
922 start_dir_heartbeat(jcr);
923 do_verify_volume(jcr);
924 stop_dir_heartbeat(jcr);
926 * Send Close session command to Storage daemon
928 bnet_fsend(sd, read_close, jcr->Ticket);
929 Dmsg1(130, "bfiled>stored: %s", sd->msg);
931 /* ****FIXME**** check response */
932 bget_msg(sd); /* get OK */
934 /* Inform Storage daemon that we are done */
935 bnet_sig(sd, BNET_TERMINATE);
938 case L_VERIFY_DISK_TO_CATALOG:
942 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
946 bnet_sig(dir, BNET_EOD);
948 /* Send termination status back to Dir */
949 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
950 edit_uint64(jcr->ReadBytes, ed1),
951 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
953 /* Inform Director that we are done */
954 bnet_sig(dir, BNET_TERMINATE);
955 return 0; /* return and terminate command loop */
959 * Do a Restore for Director
962 static int restore_cmd(JCR *jcr)
964 BSOCK *dir = jcr->dir_bsock;
965 BSOCK *sd = jcr->store_bsock;
969 char ed1[50], ed2[50];
972 * Scan WHERE (base directory for restore) from command
974 Dmsg0(150, "restore command\n");
975 /* Pickup where string */
976 where = get_memory(dir->msglen+1);
979 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, where) != 3) {
980 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
981 pm_strcpy(&jcr->errmsg, dir->msg);
982 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
987 /* Turn / into nothing */
988 if (where[0] == '/' && where[1] == 0) {
992 Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
993 unbash_spaces(where);
994 jcr->where = bstrdup(where);
995 free_pool_memory(where);
996 jcr->replace = replace;
997 jcr->prefix_links = prefix_links;
999 bnet_fsend(dir, OKrestore);
1000 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1002 jcr->JobType = JT_RESTORE;
1004 set_jcr_job_status(jcr, JS_Blocked);
1005 if (!open_sd_read_session(jcr)) {
1006 set_jcr_job_status(jcr, JS_ErrorTerminated);
1010 set_jcr_job_status(jcr, JS_Running);
1013 * Do restore of files and data
1015 start_dir_heartbeat(jcr);
1017 stop_dir_heartbeat(jcr);
1019 set_jcr_job_status(jcr, JS_Terminated);
1020 if (jcr->JobStatus != JS_Terminated) {
1021 bnet_suppress_error_messages(sd, 1);
1025 * Send Close session command to Storage daemon
1027 bnet_fsend(sd, read_close, jcr->Ticket);
1028 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1030 bget_msg(sd); /* get OK */
1032 /* Inform Storage daemon that we are done */
1033 bnet_sig(sd, BNET_TERMINATE);
1038 set_jcr_job_status(jcr, JS_ErrorTerminated);
1040 /* Send termination status back to Dir */
1041 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1042 edit_uint64(jcr->ReadBytes, ed1),
1043 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
1045 /* Inform Director that we are done */
1046 bnet_sig(dir, BNET_TERMINATE);
1048 Dmsg0(130, "Done in job.c\n");
1049 return 0; /* return and terminate command loop */
1052 static int open_sd_read_session(JCR *jcr)
1054 BSOCK *sd = jcr->store_bsock;
1057 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
1060 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
1061 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
1062 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
1064 * Open Read Session with Storage daemon
1066 bnet_fsend(sd, read_open, jcr->VolumeName,
1067 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
1068 jcr->StartBlock, jcr->EndBlock);
1069 Dmsg1(110, ">stored: %s", sd->msg);
1074 if (bget_msg(sd) >= 0) {
1075 Dmsg1(110, "bfiled<stored: %s", sd->msg);
1076 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1077 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
1080 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
1082 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
1086 if (!send_bootstrap_file(jcr)) {
1091 * Start read of data with Storage daemon
1093 bnet_fsend(sd, read_data, jcr->Ticket);
1094 Dmsg1(110, ">stored: %s", sd->msg);
1099 if (!response(jcr, sd, OK_data, "Read Data")) {
1106 * Destroy the Job Control Record and associated
1107 * resources (sockets).
1109 static void filed_free_jcr(JCR *jcr)
1111 if (jcr->store_bsock) {
1112 bnet_close(jcr->store_bsock);
1114 if (jcr->RestoreBootstrap) {
1115 unlink(jcr->RestoreBootstrap);
1116 free_pool_memory(jcr->RestoreBootstrap);
1117 jcr->RestoreBootstrap = NULL;
1119 if (jcr->last_fname) {
1120 free_pool_memory(jcr->last_fname);
1122 if (jcr->RunAfterJob) {
1123 free_pool_memory(jcr->RunAfterJob);
1129 * Get response from Storage daemon to a command we
1130 * sent. Check that the response is OK.
1132 * Returns: 0 on failure
1135 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
1140 if (bget_msg(sd) > 0) {
1141 Dmsg0(110, sd->msg);
1142 if (strcmp(sd->msg, resp) == 0) {
1146 if (job_canceled(jcr)) {
1147 return 0; /* if canceled avoid useless error messages */
1149 if (is_bnet_error(sd)) {
1150 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
1151 cmd, bnet_strerror(sd));
1153 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
1154 cmd, resp, sd->msg);
1159 static int send_bootstrap_file(JCR *jcr)
1163 BSOCK *sd = jcr->store_bsock;
1164 char *bootstrap = "bootstrap\n";
1167 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
1168 if (!jcr->RestoreBootstrap) {
1171 bs = fopen(jcr->RestoreBootstrap, "r");
1173 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
1174 jcr->RestoreBootstrap, strerror(errno));
1175 set_jcr_job_status(jcr, JS_ErrorTerminated);
1178 pm_strcpy(&sd->msg, bootstrap);
1179 sd->msglen = strlen(sd->msg);
1181 while (fgets(buf, sizeof(buf), bs)) {
1182 sd->msglen = Mmsg(&sd->msg, "%s", buf);
1185 bnet_sig(sd, BNET_EOD);
1187 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
1188 set_jcr_job_status(jcr, JS_ErrorTerminated);
1194 if (jcr->RestoreBootstrap) {
1195 unlink(jcr->RestoreBootstrap);
1196 free_pool_memory(jcr->RestoreBootstrap);
1197 jcr->RestoreBootstrap = NULL;