2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation plus additions
11 that are listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Bacula File Daemon Job processing
31 * Kern Sibbald, October MM
40 #if defined(WIN32_VSS)
43 static pthread_mutex_t vss_mutex = PTHREAD_MUTEX_INITIALIZER;
44 static int enable_vss;
47 extern CLIENT *me; /* our client resource */
49 /* Imported functions */
50 extern int status_cmd(JCR *jcr);
51 extern int qstatus_cmd(JCR *jcr);
53 /* Forward referenced functions */
54 static int backup_cmd(JCR *jcr);
55 static int bootstrap_cmd(JCR *jcr);
56 static int cancel_cmd(JCR *jcr);
57 static int setdebug_cmd(JCR *jcr);
58 static int estimate_cmd(JCR *jcr);
59 static int hello_cmd(JCR *jcr);
60 static int job_cmd(JCR *jcr);
61 static int fileset_cmd(JCR *jcr);
62 static int level_cmd(JCR *jcr);
63 static int verify_cmd(JCR *jcr);
64 static int restore_cmd(JCR *jcr);
65 static int storage_cmd(JCR *jcr);
66 static int session_cmd(JCR *jcr);
67 static int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd);
68 static void filed_free_jcr(JCR *jcr);
69 static int open_sd_read_session(JCR *jcr);
70 static int send_bootstrap_file(JCR *jcr);
71 static int runscript_cmd(JCR *jcr);
72 static int runbefore_cmd(JCR *jcr);
73 static int runafter_cmd(JCR *jcr);
74 static int runbeforenow_cmd(JCR *jcr);
75 static void set_options(findFOPTS *fo, const char *opts);
78 /* Exported functions */
83 int monitoraccess; /* specify if monitors have access to this function */
87 * The following are the recognized commands from the Director.
89 static struct s_cmds cmds[] = {
90 {"backup", backup_cmd, 0},
91 {"cancel", cancel_cmd, 0},
92 {"setdebug=", setdebug_cmd, 0},
93 {"estimate", estimate_cmd, 0},
94 {"Hello", hello_cmd, 1},
95 {"fileset", fileset_cmd, 0},
96 {"JobId=", job_cmd, 0},
97 {"level = ", level_cmd, 0},
98 {"restore", restore_cmd, 0},
99 {"session", session_cmd, 0},
100 {"status", status_cmd, 1},
101 {".status", qstatus_cmd, 1},
102 {"storage ", storage_cmd, 0},
103 {"verify", verify_cmd, 0},
104 {"bootstrap", bootstrap_cmd, 0},
105 {"RunBeforeNow", runbeforenow_cmd, 0},
106 {"RunBeforeJob", runbefore_cmd, 0},
107 {"RunAfterJob", runafter_cmd, 0},
108 {"Run", runscript_cmd, 0},
109 {NULL, NULL} /* list terminator */
112 /* Commands received from director that need scanning */
113 static char jobcmd[] = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
114 static char storaddr[] = "storage address=%s port=%d ssl=%d";
115 static char sessioncmd[] = "session %127s %ld %ld %ld %ld %ld %ld\n";
116 static char restorecmd[] = "restore replace=%c prelinks=%d where=%s\n";
117 static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
118 static char restorecmdR[] = "restore replace=%c prelinks=%d regexwhere=%s\n";
119 static char verifycmd[] = "verify level=%30s";
120 static char estimatecmd[] = "estimate listing=%d";
121 static char runbefore[] = "RunBeforeJob %s";
122 static char runafter[] = "RunAfterJob %s";
123 static char runscript[] = "Run OnSuccess=%u OnFailure=%u AbortOnError=%u When=%u Command=%s";
125 /* Responses sent to Director */
126 static char errmsg[] = "2999 Invalid command\n";
127 static char no_auth[] = "2998 No Authorization\n";
128 static char invalid_cmd[] = "2997 Invalid command for a Director with Monitor directive enabled.\n";
129 static char OKinc[] = "2000 OK include\n";
130 static char OKest[] = "2000 OK estimate files=%u bytes=%s\n";
131 static char OKlevel[] = "2000 OK level\n";
132 static char OKbackup[] = "2000 OK backup\n";
133 static char OKbootstrap[] = "2000 OK bootstrap\n";
134 static char OKverify[] = "2000 OK verify\n";
135 static char OKrestore[] = "2000 OK restore\n";
136 static char OKsession[] = "2000 OK session\n";
137 static char OKstore[] = "2000 OK storage\n";
138 static char OKjob[] = "2000 OK Job %s (%s) %s,%s,%s";
139 static char OKsetdebug[] = "2000 OK setdebug=%d\n";
140 static char BADjob[] = "2901 Bad Job\n";
141 static char EndJob[] = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s"
142 " JobBytes=%s Errors=%u VSS=%d Encrypt=%d\n";
143 static char OKRunBefore[] = "2000 OK RunBefore\n";
144 static char OKRunBeforeNow[] = "2000 OK RunBeforeNow\n";
145 static char OKRunAfter[] = "2000 OK RunAfter\n";
146 static char OKRunScript[] = "2000 OK RunScript\n";
149 /* Responses received from Storage Daemon */
150 static char OK_end[] = "3000 OK end\n";
151 static char OK_close[] = "3000 OK close Status = %d\n";
152 static char OK_open[] = "3000 OK open ticket = %d\n";
153 static char OK_data[] = "3000 OK data\n";
154 static char OK_append[] = "3000 OK append data\n";
155 static char OKSDbootstrap[]= "3000 OK bootstrap\n";
158 /* Commands sent to Storage Daemon */
159 static char append_open[] = "append open session\n";
160 static char append_data[] = "append data %d\n";
161 static char append_end[] = "append end session %d\n";
162 static char append_close[] = "append close session %d\n";
163 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
164 static char read_data[] = "read data %d\n";
165 static char read_close[] = "read close session %d\n";
168 * Accept requests from a Director
170 * NOTE! We are running as a separate thread
172 * Send output one line
173 * at a time followed by a zero length transmission.
175 * Return when the connection is terminated or there
178 * Basic task here is:
179 * Authenticate Director (during Hello command).
180 * Accept commands one at a time from the Director
183 * Concerning ClientRunBefore/After, the sequence of events
184 * is rather critical. If they are not done in the right
185 * order one can easily get FD->SD timeouts if the script
188 * The current sequence of events is:
189 * 1. Dir starts job with FD
190 * 2. Dir connects to SD
191 * 3. Dir connects to FD
192 * 4. FD connects to SD
193 * 5. FD gets/runs ClientRunBeforeJob and sends ClientRunAfterJob
194 * 6. Dir sends include/exclude
195 * 7. FD sends data to SD
196 * 8. SD/FD disconnects while SD despools data and attributes (optionnal)
197 * 9. FD runs ClientRunAfterJob
200 void *handle_client_request(void *dirp)
205 BSOCK *dir = (BSOCK *)dirp;
207 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
208 jcr->dir_bsock = dir;
209 jcr->ff = init_find_files();
210 jcr->start_time = time(NULL);
211 jcr->RunScripts = New(alist(10, not_owned_by_alist));
212 jcr->last_fname = get_pool_memory(PM_FNAME);
213 jcr->last_fname[0] = 0;
214 jcr->client_name = get_memory(strlen(my_name) + 1);
215 pm_strcpy(jcr->client_name, my_name);
216 jcr->pki_sign = me->pki_sign;
217 jcr->pki_encrypt = me->pki_encrypt;
218 jcr->pki_keypair = me->pki_keypair;
219 jcr->pki_signers = me->pki_signers;
220 jcr->pki_recipients = me->pki_recipients;
222 enable_backup_privileges(NULL, 1 /* ignore_errors */);
224 /**********FIXME******* add command handler error code */
226 for (quit=false; !quit;) {
229 if (bnet_recv(dir) < 0) {
230 break; /* connection terminated */
232 dir->msg[dir->msglen] = 0;
233 Dmsg1(100, "<dird: %s", dir->msg);
235 for (i=0; cmds[i].cmd; i++) {
236 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
237 found = true; /* indicate command found */
238 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
239 bnet_fsend(dir, no_auth);
240 bnet_sig(dir, BNET_EOD);
243 if ((jcr->authenticated) && (!cmds[i].monitoraccess) && (jcr->director->monitor)) {
244 Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
245 bnet_fsend(dir, invalid_cmd);
246 bnet_sig(dir, BNET_EOD);
249 Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
250 if (!cmds[i].func(jcr)) { /* do command */
251 quit = true; /* error or fully terminated, get out */
252 Dmsg1(20, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
257 if (!found) { /* command not found */
258 bnet_fsend(dir, errmsg);
264 /* Inform Storage daemon that we are done */
265 if (jcr->store_bsock) {
266 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
269 /* Run the after job */
270 run_scripts(jcr, jcr->RunScripts, "ClientAfterJob");
272 generate_daemon_event(jcr, "JobEnd");
274 dequeue_messages(jcr); /* send any queued messages */
276 /* Inform Director that we are done */
277 bnet_sig(dir, BNET_TERMINATE);
279 /* Clean up fileset */
280 FF_PKT *ff = jcr->ff;
281 findFILESET *fileset = ff->fileset;
284 /* Delete FileSet Include lists */
285 for (i=0; i<fileset->include_list.size(); i++) {
286 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
287 for (j=0; j<incexe->opts_list.size(); j++) {
288 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
289 for (k=0; k<fo->regex.size(); k++) {
290 regfree((regex_t *)fo->regex.get(k));
293 fo->regexdir.destroy();
294 fo->regexfile.destroy();
296 fo->wilddir.destroy();
297 fo->wildfile.destroy();
298 fo->wildbase.destroy();
300 fo->fstype.destroy();
301 fo->drivetype.destroy();
309 incexe->opts_list.destroy();
310 incexe->name_list.destroy();
312 fileset->include_list.destroy();
314 /* Delete FileSet Exclude lists */
315 for (i=0; i<fileset->exclude_list.size(); i++) {
316 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
317 for (j=0; j<incexe->opts_list.size(); j++) {
318 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
320 fo->regexdir.destroy();
321 fo->regexfile.destroy();
323 fo->wilddir.destroy();
324 fo->wildfile.destroy();
325 fo->wildbase.destroy();
327 fo->fstype.destroy();
328 fo->drivetype.destroy();
330 incexe->opts_list.destroy();
331 incexe->name_list.destroy();
333 fileset->exclude_list.destroy();
337 Dmsg0(100, "Calling term_find_files\n");
338 term_find_files(jcr->ff);
340 Dmsg0(100, "Done with term_find_files\n");
341 free_jcr(jcr); /* destroy JCR record */
342 Dmsg0(100, "Done with free_jcr\n");
347 * Hello from Director he must identify himself and provide his
350 static int hello_cmd(JCR *jcr)
352 Dmsg0(120, "Calling Authenticate\n");
353 if (!authenticate_director(jcr)) {
356 Dmsg0(120, "OK Authenticate\n");
357 jcr->authenticated = true;
364 static int cancel_cmd(JCR *jcr)
366 BSOCK *dir = jcr->dir_bsock;
367 char Job[MAX_NAME_LENGTH];
370 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
371 if (!(cjcr=get_jcr_by_full_name(Job))) {
372 bnet_fsend(dir, _("2901 Job %s not found.\n"), Job);
374 if (cjcr->store_bsock) {
375 cjcr->store_bsock->timed_out = 1;
376 cjcr->store_bsock->terminated = 1;
377 pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
379 set_jcr_job_status(cjcr, JS_Canceled);
381 bnet_fsend(dir, _("2001 Job %s marked to be canceled.\n"), Job);
384 bnet_fsend(dir, _("2902 Error scanning cancel command.\n"));
386 bnet_sig(dir, BNET_EOD);
392 * Set debug level as requested by the Director
395 static int setdebug_cmd(JCR *jcr)
397 BSOCK *dir = jcr->dir_bsock;
398 int level, trace_flag;
400 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
401 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
402 pm_strcpy(jcr->errmsg, dir->msg);
403 bnet_fsend(dir, _("2991 Bad setdebug command: %s\n"), jcr->errmsg);
407 set_trace(trace_flag);
408 return bnet_fsend(dir, OKsetdebug, level);
412 static int estimate_cmd(JCR *jcr)
414 BSOCK *dir = jcr->dir_bsock;
417 if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
418 pm_strcpy(jcr->errmsg, dir->msg);
419 Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
420 bnet_fsend(dir, _("2992 Bad estimate command.\n"));
424 bnet_fsend(dir, OKest, jcr->num_files_examined,
425 edit_uint64_with_commas(jcr->JobBytes, ed2));
426 bnet_sig(dir, BNET_EOD);
431 * Get JobId and Storage Daemon Authorization key from Director
433 static int job_cmd(JCR *jcr)
435 BSOCK *dir = jcr->dir_bsock;
436 POOLMEM *sd_auth_key;
438 sd_auth_key = get_memory(dir->msglen);
439 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
440 &jcr->VolSessionId, &jcr->VolSessionTime,
442 pm_strcpy(jcr->errmsg, dir->msg);
443 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
444 bnet_fsend(dir, BADjob);
445 free_pool_memory(sd_auth_key);
448 jcr->sd_auth_key = bstrdup(sd_auth_key);
449 free_pool_memory(sd_auth_key);
450 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
451 return bnet_fsend(dir, OKjob, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER);
454 static int runbefore_cmd(JCR *jcr)
457 BSOCK *dir = jcr->dir_bsock;
458 POOLMEM *cmd = get_memory(dir->msglen+1);
461 Dmsg1(100, "runbefore_cmd: %s", dir->msg);
462 if (sscanf(dir->msg, runbefore, cmd) != 1) {
463 pm_strcpy(jcr->errmsg, dir->msg);
464 Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
465 bnet_fsend(dir, _("2905 Bad RunBeforeJob command.\n"));
471 /* Run the command now */
472 script = new_runscript();
473 script->set_command(cmd);
474 script->when = SCRIPT_Before;
475 ok = script->run(jcr, "ClientRunBeforeJob");
476 free_runscript(script);
480 bnet_fsend(dir, OKRunBefore);
483 bnet_fsend(dir, _("2905 Bad RunBeforeJob command.\n"));
488 static int runbeforenow_cmd(JCR *jcr)
490 BSOCK *dir = jcr->dir_bsock;
492 run_scripts(jcr, jcr->RunScripts, "ClientBeforeJob");
493 if (job_canceled(jcr)) {
494 return bnet_fsend(dir, _("2905 Bad RunBeforeNow command.\n"));
496 return bnet_fsend(dir, OKRunBeforeNow);
500 static int runafter_cmd(JCR *jcr)
502 BSOCK *dir = jcr->dir_bsock;
503 POOLMEM *msg = get_memory(dir->msglen+1);
506 Dmsg1(100, "runafter_cmd: %s", dir->msg);
507 if (sscanf(dir->msg, runafter, msg) != 1) {
508 pm_strcpy(jcr->errmsg, dir->msg);
509 Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
510 bnet_fsend(dir, _("2905 Bad RunAfterJob command.\n"));
516 cmd = new_runscript();
517 cmd->set_command(msg);
518 cmd->on_success = true;
519 cmd->on_failure = false;
520 cmd->when = SCRIPT_After;
522 jcr->RunScripts->append(cmd);
524 free_pool_memory(msg);
525 return bnet_fsend(dir, OKRunAfter);
528 static int runscript_cmd(JCR *jcr)
530 BSOCK *dir = jcr->dir_bsock;
531 POOLMEM *msg = get_memory(dir->msglen+1);
533 RUNSCRIPT *cmd = new_runscript() ;
535 Dmsg1(100, "runscript_cmd: '%s'\n", dir->msg);
536 if (sscanf(dir->msg, runscript, &cmd->on_success,
538 &cmd->abort_on_error,
541 pm_strcpy(jcr->errmsg, dir->msg);
542 Jmsg1(jcr, M_FATAL, 0, _("Bad RunScript command: %s\n"), jcr->errmsg);
543 bnet_fsend(dir, _("2905 Bad RunScript command.\n"));
550 cmd->set_command(msg);
552 jcr->RunScripts->append(cmd);
554 free_pool_memory(msg);
555 return bnet_fsend(dir, OKRunScript);
559 static bool init_fileset(JCR *jcr)
562 findFILESET *fileset;
571 fileset = (findFILESET *)malloc(sizeof(findFILESET));
572 memset(fileset, 0, sizeof(findFILESET));
573 ff->fileset = fileset;
574 fileset->state = state_none;
575 fileset->include_list.init(1, true);
576 fileset->exclude_list.init(1, true);
580 static findFOPTS *start_options(FF_PKT *ff)
582 int state = ff->fileset->state;
583 findINCEXE *incexe = ff->fileset->incexe;
585 if (state != state_options) {
586 ff->fileset->state = state_options;
587 findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS));
588 memset(fo, 0, sizeof(findFOPTS));
589 fo->regex.init(1, true);
590 fo->regexdir.init(1, true);
591 fo->regexfile.init(1, true);
592 fo->wild.init(1, true);
593 fo->wilddir.init(1, true);
594 fo->wildfile.init(1, true);
595 fo->wildbase.init(1, true);
596 fo->base.init(1, true);
597 fo->fstype.init(1, true);
598 fo->drivetype.init(1, true);
599 incexe->current_opts = fo;
600 incexe->opts_list.append(fo);
602 return incexe->current_opts;
607 * Add fname to include/exclude fileset list. First check for
608 * | and < and if necessary perform command.
610 static void add_file_to_fileset(JCR *jcr, const char *fname, findFILESET *fileset)
624 p++; /* skip over | */
625 fn = get_pool_memory(PM_FNAME);
626 fn = edit_job_codes(jcr, fn, p, "");
627 bpipe = open_bpipe(fn, 0, "r");
630 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
632 free_pool_memory(fn);
635 free_pool_memory(fn);
636 while (fgets(buf, sizeof(buf), bpipe->rfd)) {
637 strip_trailing_junk(buf);
638 fileset->incexe->name_list.append(new_dlistString(buf));
640 if ((stat=close_bpipe(bpipe)) != 0) {
642 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. stat=%d: ERR=%s\n"),
643 p, be.code(stat), be.strerror(stat));
648 Dmsg0(100, "Doing < include on client.\n");
649 p++; /* skip over < */
650 if ((ffd = fopen(p, "rb")) == NULL) {
652 Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"),
656 while (fgets(buf, sizeof(buf), ffd)) {
657 strip_trailing_junk(buf);
658 Dmsg1(100, "%s\n", buf);
659 fileset->incexe->name_list.append(new_dlistString(buf));
664 fileset->incexe->name_list.append(new_dlistString(fname));
670 static void add_fileset(JCR *jcr, const char *item)
672 FF_PKT *ff = jcr->ff;
673 findFILESET *fileset = ff->fileset;
674 int state = fileset->state;
675 findFOPTS *current_opts;
677 /* Get code, optional subcode, and position item past the dividing space */
678 Dmsg1(100, "%s\n", item);
683 int subcode = ' '; /* A space is always a valid subcode */
684 if (item[0] != '\0' && item[0] != ' ') {
692 /* Skip all lines we receive after an error */
693 if (state == state_error) {
694 Dmsg0(100, "State=error return\n");
699 * The switch tests the code for validity.
700 * The subcode is always good if it is a space, otherwise we must confirm.
701 * We set state to state_error first assuming the subcode is invalid,
702 * requiring state to be set in cases below that handle subcodes.
704 if (subcode != ' ') {
706 Dmsg0(100, "Set state=error\n");
711 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
712 memset(fileset->incexe, 0, sizeof(findINCEXE));
713 fileset->incexe->opts_list.init(1, true);
714 fileset->incexe->name_list.init(); /* for dlist; was 1,true for alist */
715 fileset->include_list.append(fileset->incexe);
719 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
720 memset(fileset->incexe, 0, sizeof(findINCEXE));
721 fileset->incexe->opts_list.init(1, true);
722 fileset->incexe->name_list.init();
723 fileset->exclude_list.append(fileset->incexe);
729 /* File item to either include/include list */
730 state = state_include;
731 add_file_to_fileset(jcr, item, fileset);
734 current_opts = start_options(ff);
738 preg = (regex_t *)malloc(sizeof(regex_t));
739 if (current_opts->flags & FO_IGNORECASE) {
740 rc = regcomp(preg, item, REG_EXTENDED|REG_ICASE);
742 rc = regcomp(preg, item, REG_EXTENDED);
745 regerror(rc, preg, prbuf, sizeof(prbuf));
748 Jmsg(jcr, M_FATAL, 0, _("REGEX %s compile error. ERR=%s\n"), item, prbuf);
752 state = state_options;
753 if (subcode == ' ') {
754 current_opts->regex.append(preg);
755 } else if (subcode == 'D') {
756 current_opts->regexdir.append(preg);
757 } else if (subcode == 'F') {
758 current_opts->regexfile.append(preg);
764 current_opts = start_options(ff);
765 current_opts->base.append(bstrdup(item));
766 state = state_options;
769 current_opts = start_options(ff);
770 state = state_options;
771 if (subcode == ' ') {
772 current_opts->fstype.append(bstrdup(item));
773 } else if (subcode == 'D') {
774 current_opts->drivetype.append(bstrdup(item));
780 current_opts = start_options(ff);
781 state = state_options;
782 if (subcode == ' ') {
783 current_opts->wild.append(bstrdup(item));
784 } else if (subcode == 'D') {
785 current_opts->wilddir.append(bstrdup(item));
786 } else if (subcode == 'F') {
787 current_opts->wildfile.append(bstrdup(item));
788 } else if (subcode == 'B') {
789 current_opts->wildbase.append(bstrdup(item));
795 current_opts = start_options(ff);
796 set_options(current_opts, item);
797 state = state_options;
800 current_opts = start_options(ff);
801 current_opts->reader = bstrdup(item);
802 state = state_options;
805 current_opts = start_options(ff);
806 current_opts->writer = bstrdup(item);
807 state = state_options;
810 Jmsg(jcr, M_FATAL, 0, _("Invalid FileSet command: %s\n"), item);
814 ff->fileset->state = state;
817 static bool term_fileset(JCR *jcr)
819 FF_PKT *ff = jcr->ff;
822 findFILESET *fileset = ff->fileset;
825 for (i=0; i<fileset->include_list.size(); i++) {
826 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
828 for (j=0; j<incexe->opts_list.size(); j++) {
829 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
830 for (k=0; k<fo->regex.size(); k++) {
831 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
833 for (k=0; k<fo->regexdir.size(); k++) {
834 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
836 for (k=0; k<fo->regexfile.size(); k++) {
837 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
839 for (k=0; k<fo->wild.size(); k++) {
840 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
842 for (k=0; k<fo->wilddir.size(); k++) {
843 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
845 for (k=0; k<fo->wildfile.size(); k++) {
846 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
848 for (k=0; k<fo->wildbase.size(); k++) {
849 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
851 for (k=0; k<fo->base.size(); k++) {
852 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
854 for (k=0; k<fo->fstype.size(); k++) {
855 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
857 for (k=0; k<fo->drivetype.size(); k++) {
858 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
861 Dmsg1(400, "D %s\n", fo->reader);
864 Dmsg1(400, "T %s\n", fo->writer);
868 foreach_dlist(node, &incexe->name_list) {
869 Dmsg1(400, "F %s\n", node->c_str());
872 for (i=0; i<fileset->exclude_list.size(); i++) {
873 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
875 for (j=0; j<incexe->opts_list.size(); j++) {
876 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
877 for (k=0; k<fo->regex.size(); k++) {
878 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
880 for (k=0; k<fo->regexdir.size(); k++) {
881 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
883 for (k=0; k<fo->regexfile.size(); k++) {
884 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
886 for (k=0; k<fo->wild.size(); k++) {
887 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
889 for (k=0; k<fo->wilddir.size(); k++) {
890 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
892 for (k=0; k<fo->wildfile.size(); k++) {
893 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
895 for (k=0; k<fo->wildbase.size(); k++) {
896 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
898 for (k=0; k<fo->base.size(); k++) {
899 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
901 for (k=0; k<fo->fstype.size(); k++) {
902 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
904 for (k=0; k<fo->drivetype.size(); k++) {
905 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
909 foreach_dlist(node, incexe->name_list) {
910 Dmsg1(400, "F %s\n", node->c_str());
914 return ff->fileset->state != state_error;
919 * As an optimization, we should do this during
920 * "compile" time in filed/job.c, and keep only a bit mask
921 * and the Verify options.
923 static void set_options(findFOPTS *fo, const char *opts)
929 for (p=opts; *p; p++) {
931 case 'a': /* alway replace */
932 case '0': /* no option */
935 fo->flags |= FO_EXCLUDE;
938 fo->flags |= FO_MULTIFS;
940 case 'h': /* no recursion */
941 fo->flags |= FO_NO_RECURSION;
943 case 'H': /* no hard link handling */
944 fo->flags |= FO_NO_HARDLINK;
947 fo->flags |= FO_IGNORECASE;
953 fo->flags |= FO_NOREPLACE;
955 case 'p': /* use portable data format */
956 fo->flags |= FO_PORTABLE;
958 case 'R': /* Resource forks and Finder Info */
959 fo->flags |= FO_HFSPLUS;
960 case 'r': /* read fifo */
961 fo->flags |= FO_READFIFO;
966 fo->flags |= FO_SHA1;
971 fo->flags |= FO_SHA256;
975 fo->flags |= FO_SHA512;
981 * If 2 or 3 is seen here, SHA2 is not configured, so
982 * eat the option, and drop back to SHA-1.
984 if (p[1] == '2' || p[1] == '3') {
987 fo->flags |= FO_SHA1;
992 fo->flags |= FO_SPARSE;
995 fo->flags |= FO_MTIMEONLY;
998 fo->flags |= FO_KEEPATIME;
1001 fo->flags |= FO_ACL;
1003 case 'V': /* verify options */
1004 /* Copy Verify Options */
1005 for (j=0; *p && *p != ':'; p++) {
1006 fo->VerifyOpts[j] = *p;
1007 if (j < (int)sizeof(fo->VerifyOpts) - 1) {
1011 fo->VerifyOpts[j] = 0;
1013 case 'P': /* strip path */
1015 for (j=0; *p && *p != ':'; p++) {
1017 if (j < (int)sizeof(strip) - 1) {
1022 fo->strip_path = atoi(strip);
1023 fo->flags |= FO_STRIPPATH;
1026 fo->flags |= FO_IF_NEWER;
1029 fo->flags |= FO_ENHANCEDWILD;
1031 case 'Z': /* gzip compression */
1032 fo->flags |= FO_GZIP;
1033 fo->GZIP_level = *++p - '0';
1036 fo->flags |= FO_NOATIME;
1039 fo->flags |= FO_CHKCHANGES;
1042 Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *p);
1050 * Director is passing his Fileset
1052 static int fileset_cmd(JCR *jcr)
1054 BSOCK *dir = jcr->dir_bsock;
1056 #if defined(WIN32_VSS)
1059 sscanf(dir->msg, "fileset vss=%d", &vss);
1063 if (!init_fileset(jcr)) {
1066 while (bnet_recv(dir) >= 0) {
1067 strip_trailing_junk(dir->msg);
1068 Dmsg1(500, "Fileset: %s\n", dir->msg);
1069 add_fileset(jcr, dir->msg);
1071 if (!term_fileset(jcr)) {
1074 return bnet_fsend(dir, OKinc);
1077 static void free_bootstrap(JCR *jcr)
1079 if (jcr->RestoreBootstrap) {
1080 unlink(jcr->RestoreBootstrap);
1081 free_pool_memory(jcr->RestoreBootstrap);
1082 jcr->RestoreBootstrap = NULL;
1087 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1088 static uint32_t bsr_uniq = 0;
1091 * The Director sends us the bootstrap file, which
1092 * we will in turn pass to the SD.
1094 static int bootstrap_cmd(JCR *jcr)
1096 BSOCK *dir = jcr->dir_bsock;
1097 POOLMEM *fname = get_pool_memory(PM_FNAME);
1100 free_bootstrap(jcr);
1103 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1104 jcr->Job, bsr_uniq);
1106 Dmsg1(400, "bootstrap=%s\n", fname);
1107 jcr->RestoreBootstrap = fname;
1108 bs = fopen(fname, "a+b"); /* create file */
1111 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
1112 jcr->RestoreBootstrap, be.strerror());
1114 * Suck up what he is sending to us so that he will then
1115 * read our error message.
1117 while (bnet_recv(dir) >= 0)
1119 free_bootstrap(jcr);
1120 set_jcr_job_status(jcr, JS_ErrorTerminated);
1124 while (bnet_recv(dir) >= 0) {
1125 Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
1126 fputs(dir->msg, bs);
1130 * Note, do not free the bootstrap yet -- it needs to be
1133 return bnet_fsend(dir, OKbootstrap);
1138 * Get backup level from Director
1141 static int level_cmd(JCR *jcr)
1143 BSOCK *dir = jcr->dir_bsock;
1144 POOLMEM *level, *buf = NULL;
1147 level = get_memory(dir->msglen+1);
1148 Dmsg1(110, "level_cmd: %s", dir->msg);
1149 if (sscanf(dir->msg, "level = %s ", level) != 1) {
1152 /* Base backup requested? */
1153 if (strcmp(level, "base") == 0) {
1154 jcr->JobLevel = L_BASE;
1155 /* Full backup requested? */
1156 } else if (strcmp(level, "full") == 0) {
1157 jcr->JobLevel = L_FULL;
1158 } else if (strcmp(level, "differential") == 0) {
1159 jcr->JobLevel = L_DIFFERENTIAL;
1162 } else if (strcmp(level, "incremental") == 0) {
1163 jcr->JobLevel = L_INCREMENTAL;
1167 * We get his UTC since time, then sync the clocks and correct it
1168 * to agree with our clock.
1170 } else if (strcmp(level, "since_utime") == 0) {
1171 buf = get_memory(dir->msglen+1);
1172 utime_t since_time, adj;
1173 btime_t his_time, bt_start, rt=0, bt_adj=0;
1174 if (jcr->JobLevel == L_NONE) {
1175 jcr->JobLevel = L_SINCE; /* if no other job level set, do it now */
1177 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
1178 buf, &mtime_only) != 2) {
1181 since_time = str_to_uint64(buf); /* this is the since time */
1182 Dmsg1(100, "since_time=%d\n", (int)since_time);
1183 char ed1[50], ed2[50];
1185 * Sync clocks by polling him for the time. We take
1186 * 10 samples of his time throwing out the first two.
1188 for (int i=0; i<10; i++) {
1189 bt_start = get_current_btime();
1190 bnet_sig(dir, BNET_BTIME); /* poll for time */
1191 if (bnet_recv(dir) <= 0) { /* get response */
1194 if (sscanf(dir->msg, "btime %s", buf) != 1) {
1197 if (i < 2) { /* toss first two results */
1200 his_time = str_to_uint64(buf);
1201 rt = get_current_btime() - bt_start; /* compute round trip time */
1202 Dmsg2(100, "Dirtime=%s FDtime=%s\n", edit_uint64(his_time, ed1),
1203 edit_uint64(bt_start, ed2));
1204 bt_adj += bt_start - his_time - rt/2;
1205 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1208 bt_adj = bt_adj / 8; /* compute average time */
1209 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1210 adj = btime_to_utime(bt_adj);
1211 since_time += adj; /* adjust for clock difference */
1213 Jmsg(jcr, M_INFO, 0, _("DIR and FD clocks differ by %d seconds, FD automatically adjusting.\n"), adj);
1215 bnet_sig(dir, BNET_EOD);
1217 Dmsg2(100, "adj = %d since_time=%d\n", (int)adj, (int)since_time);
1218 jcr->incremental = 1; /* set incremental or decremental backup */
1219 jcr->mtime = (time_t)since_time; /* set since time */
1221 Jmsg1(jcr, M_FATAL, 0, _("Unknown backup level: %s\n"), level);
1229 return bnet_fsend(dir, OKlevel);
1232 pm_strcpy(jcr->errmsg, dir->msg);
1233 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
1242 * Get session parameters from Director -- this is for a Restore command
1244 static int session_cmd(JCR *jcr)
1246 BSOCK *dir = jcr->dir_bsock;
1248 Dmsg1(100, "SessionCmd: %s", dir->msg);
1249 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
1250 &jcr->VolSessionId, &jcr->VolSessionTime,
1251 &jcr->StartFile, &jcr->EndFile,
1252 &jcr->StartBlock, &jcr->EndBlock) != 7) {
1253 pm_strcpy(jcr->errmsg, dir->msg);
1254 Jmsg(jcr, M_FATAL, 0, _("Bad session command: %s"), jcr->errmsg);
1258 return bnet_fsend(dir, OKsession);
1262 * Get address of storage daemon from Director
1265 static int storage_cmd(JCR *jcr)
1267 int stored_port; /* storage daemon port */
1268 int enable_ssl; /* enable ssl to sd */
1269 BSOCK *dir = jcr->dir_bsock;
1270 BSOCK *sd; /* storage daemon bsock */
1272 Dmsg1(100, "StorageCmd: %s", dir->msg);
1273 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
1274 pm_strcpy(jcr->errmsg, dir->msg);
1275 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
1278 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
1279 /* Open command communications with Storage daemon */
1280 /* Try to connect for 1 hour at 10 second intervals */
1281 sd = bnet_connect(jcr, 10, (int)me->SDConnectTimeout, me->heartbeat_interval,
1282 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1);
1284 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
1285 jcr->stored_addr, stored_port);
1286 Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
1287 jcr->stored_addr, stored_port);
1290 Dmsg0(110, "Connection OK to SD.\n");
1292 jcr->store_bsock = sd;
1294 bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
1295 if (!authenticate_storagedaemon(jcr)) {
1296 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
1299 Dmsg0(110, "Authenticated with SD.\n");
1301 /* Send OK to Director */
1302 return bnet_fsend(dir, OKstore);
1309 static int backup_cmd(JCR *jcr)
1311 BSOCK *dir = jcr->dir_bsock;
1312 BSOCK *sd = jcr->store_bsock;
1315 char ed1[50], ed2[50];
1316 bool bDoVSS = false;
1318 #if defined(WIN32_VSS)
1319 // capture state here, if client is backed up by multiple directors
1320 // and one enables vss and the other does not then enable_vss can change
1321 // between here and where its evaluated after the job completes.
1322 bDoVSS = g_pVSSClient && enable_vss;
1324 /* Run only one at a time */
1329 set_jcr_job_status(jcr, JS_Blocked);
1330 jcr->JobType = JT_BACKUP;
1331 Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
1334 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
1338 bnet_fsend(dir, OKbackup);
1339 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1342 * Send Append Open Session to Storage daemon
1344 bnet_fsend(sd, append_open);
1345 Dmsg1(110, ">stored: %s", sd->msg);
1347 * Expect to receive back the Ticket number
1349 if (bget_msg(sd) >= 0) {
1350 Dmsg1(110, "<stored: %s", sd->msg);
1351 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1352 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
1355 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
1357 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
1362 * Send Append data command to Storage daemon
1364 bnet_fsend(sd, append_data, jcr->Ticket);
1365 Dmsg1(110, ">stored: %s", sd->msg);
1368 * Expect to get OK data
1370 Dmsg1(110, "<stored: %s", sd->msg);
1371 if (!response(jcr, sd, OK_data, "Append Data")) {
1375 generate_daemon_event(jcr, "JobStart");
1377 #if defined(WIN32_VSS)
1378 /* START VSS ON WIN 32 */
1380 if (g_pVSSClient->InitializeForBackup()) {
1381 /* tell vss which drives to snapshot */
1382 char szWinDriveLetters[27];
1383 if (get_win32_driveletters(jcr->ff, szWinDriveLetters)) {
1384 Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
1385 if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {
1386 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed.\n"));
1389 /* tell user if snapshot creation of a specific drive failed */
1391 for (i=0; i < (int)strlen(szWinDriveLetters); i++) {
1392 if (islower(szWinDriveLetters[i])) {
1393 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed. VSS support is disabled on this drive.\n"), szWinDriveLetters[i]);
1397 /* inform user about writer states */
1398 for (i=0; i < (int)g_pVSSClient->GetWriterCount(); i++)
1399 if (g_pVSSClient->GetWriterState(i) < 1) {
1400 Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PrepareForBackup): %s\n"), g_pVSSClient->GetWriterInfo(i));
1405 Jmsg(jcr, M_INFO, 0, _("No drive letters found for generating VSS snapshots.\n"));
1409 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled. ERR=%s\n"), be.strerror());
1415 * Send Files to Storage daemon
1417 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
1418 if (!blast_data_to_storage_daemon(jcr, NULL)) {
1419 set_jcr_job_status(jcr, JS_ErrorTerminated);
1420 bnet_suppress_error_messages(sd, 1);
1421 bget_msg(sd); /* Read final response from append_data */
1422 Dmsg0(110, "Error in blast_data.\n");
1424 set_jcr_job_status(jcr, JS_Terminated);
1426 if (jcr->JobStatus != JS_Terminated) {
1427 bnet_suppress_error_messages(sd, 1);
1428 goto cleanup; /* bail out now */
1431 * Expect to get response to append_data from Storage daemon
1433 if (!response(jcr, sd, OK_append, "Append Data")) {
1434 set_jcr_job_status(jcr, JS_ErrorTerminated);
1439 * Send Append End Data to Storage daemon
1441 bnet_fsend(sd, append_end, jcr->Ticket);
1443 if (!response(jcr, sd, OK_end, "Append End")) {
1444 set_jcr_job_status(jcr, JS_ErrorTerminated);
1449 * Send Append Close to Storage daemon
1451 bnet_fsend(sd, append_close, jcr->Ticket);
1452 while (bget_msg(sd) >= 0) { /* stop on signal or error */
1453 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
1455 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
1459 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
1462 if (SDJobStatus != JS_Terminated) {
1463 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
1469 #if defined(WIN32_VSS)
1470 /* STOP VSS ON WIN 32 */
1471 /* tell vss to close the backup session */
1473 if (g_pVSSClient->CloseBackup()) {
1474 /* inform user about writer states */
1475 for (int i=0; i<(int)g_pVSSClient->GetWriterCount(); i++) {
1476 int msg_type = M_INFO;
1477 if (g_pVSSClient->GetWriterState(i) < 1) {
1478 msg_type = M_WARNING;
1481 Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
1488 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1489 edit_uint64(jcr->ReadBytes, ed1),
1490 edit_uint64(jcr->JobBytes, ed2), jcr->Errors, (int)bDoVSS,
1492 Dmsg1(110, "End FD msg: %s\n", dir->msg);
1494 return 0; /* return and stop command loop */
1498 * Do a Verify for Director
1501 static int verify_cmd(JCR *jcr)
1503 BSOCK *dir = jcr->dir_bsock;
1504 BSOCK *sd = jcr->store_bsock;
1505 char level[100], ed1[50], ed2[50];
1507 jcr->JobType = JT_VERIFY;
1508 if (sscanf(dir->msg, verifycmd, level) != 1) {
1509 bnet_fsend(dir, _("2994 Bad verify command: %s\n"), dir->msg);
1513 if (strcasecmp(level, "init") == 0) {
1514 jcr->JobLevel = L_VERIFY_INIT;
1515 } else if (strcasecmp(level, "catalog") == 0){
1516 jcr->JobLevel = L_VERIFY_CATALOG;
1517 } else if (strcasecmp(level, "volume") == 0){
1518 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
1519 } else if (strcasecmp(level, "data") == 0){
1520 jcr->JobLevel = L_VERIFY_DATA;
1521 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
1522 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
1524 bnet_fsend(dir, _("2994 Bad verify level: %s\n"), dir->msg);
1528 bnet_fsend(dir, OKverify);
1530 generate_daemon_event(jcr, "JobStart");
1532 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1534 switch (jcr->JobLevel) {
1536 case L_VERIFY_CATALOG:
1539 case L_VERIFY_VOLUME_TO_CATALOG:
1540 if (!open_sd_read_session(jcr)) {
1543 start_dir_heartbeat(jcr);
1544 do_verify_volume(jcr);
1545 stop_dir_heartbeat(jcr);
1547 * Send Close session command to Storage daemon
1549 bnet_fsend(sd, read_close, jcr->Ticket);
1550 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1552 /* ****FIXME**** check response */
1553 bget_msg(sd); /* get OK */
1555 /* Inform Storage daemon that we are done */
1556 bnet_sig(sd, BNET_TERMINATE);
1559 case L_VERIFY_DISK_TO_CATALOG:
1563 bnet_fsend(dir, _("2994 Bad verify level: %s\n"), dir->msg);
1567 bnet_sig(dir, BNET_EOD);
1569 /* Send termination status back to Dir */
1570 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1571 edit_uint64(jcr->ReadBytes, ed1),
1572 edit_uint64(jcr->JobBytes, ed2), jcr->Errors, 0,
1574 Dmsg1(110, "End FD msg: %s\n", dir->msg);
1576 /* Inform Director that we are done */
1577 bnet_sig(dir, BNET_TERMINATE);
1578 return 0; /* return and terminate command loop */
1582 * Do a Restore for Director
1585 static int restore_cmd(JCR *jcr)
1587 BSOCK *dir = jcr->dir_bsock;
1588 BSOCK *sd = jcr->store_bsock;
1590 bool use_regexwhere=false;
1593 char ed1[50], ed2[50];
1596 * Scan WHERE (base directory for restore) from command
1598 Dmsg0(150, "restore command\n");
1599 /* Pickup where string */
1600 args = get_memory(dir->msglen+1);
1603 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, args) != 3) {
1604 if (sscanf(dir->msg, restorecmdR, &replace, &prefix_links, args) != 3){
1605 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
1606 pm_strcpy(jcr->errmsg, dir->msg);
1607 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
1612 use_regexwhere = true;
1614 /* Turn / into nothing */
1615 if (IsPathSeparator(args[0]) && args[1] == '\0') {
1619 Dmsg2(150, "Got replace %c, where=%s\n", replace, args);
1620 unbash_spaces(args);
1622 if (use_regexwhere) {
1623 jcr->where_bregexp = get_bregexps(args);
1624 if (!jcr->where_bregexp) {
1625 Jmsg(jcr, M_FATAL, 0, _("Bad where regexp. where=%s\n"), args);
1626 free_pool_memory(args);
1630 jcr->where = bstrdup(args);
1633 free_pool_memory(args);
1634 jcr->replace = replace;
1635 jcr->prefix_links = prefix_links;
1637 bnet_fsend(dir, OKrestore);
1638 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1640 jcr->JobType = JT_RESTORE;
1642 set_jcr_job_status(jcr, JS_Blocked);
1644 if (!open_sd_read_session(jcr)) {
1645 set_jcr_job_status(jcr, JS_ErrorTerminated);
1649 set_jcr_job_status(jcr, JS_Running);
1652 * Do restore of files and data
1654 start_dir_heartbeat(jcr);
1655 generate_daemon_event(jcr, "JobStart");
1657 stop_dir_heartbeat(jcr);
1659 set_jcr_job_status(jcr, JS_Terminated);
1660 if (jcr->JobStatus != JS_Terminated) {
1661 bnet_suppress_error_messages(sd, 1);
1665 * Send Close session command to Storage daemon
1667 bnet_fsend(sd, read_close, jcr->Ticket);
1668 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1670 bget_msg(sd); /* get OK */
1672 /* Inform Storage daemon that we are done */
1673 bnet_sig(sd, BNET_TERMINATE);
1678 set_jcr_job_status(jcr, JS_ErrorTerminated);
1680 /* Send termination status back to Dir */
1681 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1682 edit_uint64(jcr->ReadBytes, ed1),
1683 edit_uint64(jcr->JobBytes, ed2), jcr->Errors, 0,
1685 Dmsg1(110, "End FD msg: %s\n", dir->msg);
1687 /* Inform Director that we are done */
1688 bnet_sig(dir, BNET_TERMINATE);
1690 Dmsg0(130, "Done in job.c\n");
1691 return 0; /* return and terminate command loop */
1694 static int open_sd_read_session(JCR *jcr)
1696 BSOCK *sd = jcr->store_bsock;
1699 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
1702 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
1703 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
1704 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
1706 * Open Read Session with Storage daemon
1708 bnet_fsend(sd, read_open, "DummyVolume",
1709 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
1710 jcr->StartBlock, jcr->EndBlock);
1711 Dmsg1(110, ">stored: %s", sd->msg);
1716 if (bget_msg(sd) >= 0) {
1717 Dmsg1(110, "bfiled<stored: %s", sd->msg);
1718 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1719 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
1722 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
1724 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
1728 if (!send_bootstrap_file(jcr)) {
1733 * Start read of data with Storage daemon
1735 bnet_fsend(sd, read_data, jcr->Ticket);
1736 Dmsg1(110, ">stored: %s", sd->msg);
1741 if (!response(jcr, sd, OK_data, "Read Data")) {
1748 * Destroy the Job Control Record and associated
1749 * resources (sockets).
1751 static void filed_free_jcr(JCR *jcr)
1753 if (jcr->store_bsock) {
1754 bnet_close(jcr->store_bsock);
1756 free_bootstrap(jcr);
1757 if (jcr->last_fname) {
1758 free_pool_memory(jcr->last_fname);
1760 free_runscripts(jcr->RunScripts);
1761 delete jcr->RunScripts;
1767 * Get response from Storage daemon to a command we
1768 * sent. Check that the response is OK.
1770 * Returns: 0 on failure
1773 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
1778 if (bget_msg(sd) > 0) {
1779 Dmsg0(110, sd->msg);
1780 if (strcmp(sd->msg, resp) == 0) {
1784 if (job_canceled(jcr)) {
1785 return 0; /* if canceled avoid useless error messages */
1787 if (is_bnet_error(sd)) {
1788 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
1789 cmd, bnet_strerror(sd));
1791 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
1792 cmd, resp, sd->msg);
1797 static int send_bootstrap_file(JCR *jcr)
1801 BSOCK *sd = jcr->store_bsock;
1802 const char *bootstrap = "bootstrap\n";
1805 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
1806 if (!jcr->RestoreBootstrap) {
1809 bs = fopen(jcr->RestoreBootstrap, "rb");
1812 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
1813 jcr->RestoreBootstrap, be.strerror());
1814 set_jcr_job_status(jcr, JS_ErrorTerminated);
1817 sd->msglen = pm_strcpy(sd->msg, bootstrap);
1819 while (fgets(buf, sizeof(buf), bs)) {
1820 sd->msglen = Mmsg(sd->msg, "%s", buf);
1823 bnet_sig(sd, BNET_EOD);
1825 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
1826 set_jcr_job_status(jcr, JS_ErrorTerminated);
1832 free_bootstrap(jcr);