2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 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 and included
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=%d OnFailure=%d AbortOnError=%d When=%d 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 new_plugins(jcr); /* instantiate plugins for this jcr */
216 pm_strcpy(jcr->client_name, my_name);
217 jcr->crypto.pki_sign = me->pki_sign;
218 jcr->crypto.pki_encrypt = me->pki_encrypt;
219 jcr->crypto.pki_keypair = me->pki_keypair;
220 jcr->crypto.pki_signers = me->pki_signers;
221 jcr->crypto.pki_recipients = me->pki_recipients;
223 enable_backup_privileges(NULL, 1 /* ignore_errors */);
225 /**********FIXME******* add command handler error code */
227 for (quit=false; !quit;) {
230 if (bnet_recv(dir) < 0) {
231 break; /* connection terminated */
233 dir->msg[dir->msglen] = 0;
234 Dmsg1(100, "<dird: %s", dir->msg);
236 for (i=0; cmds[i].cmd; i++) {
237 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
238 found = true; /* indicate command found */
239 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
240 bnet_fsend(dir, no_auth);
241 bnet_sig(dir, BNET_EOD);
244 if ((jcr->authenticated) && (!cmds[i].monitoraccess) && (jcr->director->monitor)) {
245 Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
246 bnet_fsend(dir, invalid_cmd);
247 bnet_sig(dir, BNET_EOD);
250 Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
251 if (!cmds[i].func(jcr)) { /* do command */
252 quit = true; /* error or fully terminated, get out */
253 Dmsg1(20, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
258 if (!found) { /* command not found */
259 bnet_fsend(dir, errmsg);
265 /* Inform Storage daemon that we are done */
266 if (jcr->store_bsock) {
267 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
270 /* Run the after job */
271 run_scripts(jcr, jcr->RunScripts, "ClientAfterJob");
273 if (jcr->JobId) { /* send EndJob if running a job */
274 char ed1[50], ed2[50];
275 /* Send termination status back to Dir */
276 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
277 edit_uint64(jcr->ReadBytes, ed1),
278 edit_uint64(jcr->JobBytes, ed2), jcr->Errors, jcr->VSS,
279 jcr->crypto.pki_encrypt);
280 Dmsg1(110, "End FD msg: %s\n", dir->msg);
283 generate_daemon_event(jcr, "JobEnd");
284 generate_plugin_event(jcr, bEventJobEnd);
286 dequeue_messages(jcr); /* send any queued messages */
288 /* Inform Director that we are done */
289 dir->signal(BNET_TERMINATE);
291 free_plugins(jcr); /* release instantiated plugins */
293 /* Clean up fileset */
294 FF_PKT *ff = jcr->ff;
295 findFILESET *fileset = ff->fileset;
298 /* Delete FileSet Include lists */
299 for (i=0; i<fileset->include_list.size(); i++) {
300 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
301 for (j=0; j<incexe->opts_list.size(); j++) {
302 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
303 for (k=0; k<fo->regex.size(); k++) {
304 regfree((regex_t *)fo->regex.get(k));
307 fo->regexdir.destroy();
308 fo->regexfile.destroy();
310 fo->wilddir.destroy();
311 fo->wildfile.destroy();
312 fo->wildbase.destroy();
314 fo->fstype.destroy();
315 fo->drivetype.destroy();
317 incexe->opts_list.destroy();
318 incexe->name_list.destroy();
319 incexe->plugin_list.destroy();
321 fileset->include_list.destroy();
323 /* Delete FileSet Exclude lists */
324 for (i=0; i<fileset->exclude_list.size(); i++) {
325 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
326 for (j=0; j<incexe->opts_list.size(); j++) {
327 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
329 fo->regexdir.destroy();
330 fo->regexfile.destroy();
332 fo->wilddir.destroy();
333 fo->wildfile.destroy();
334 fo->wildbase.destroy();
336 fo->fstype.destroy();
337 fo->drivetype.destroy();
339 incexe->opts_list.destroy();
340 incexe->name_list.destroy();
341 incexe->plugin_list.destroy();
343 fileset->exclude_list.destroy();
347 Dmsg0(100, "Calling term_find_files\n");
348 term_find_files(jcr->ff);
350 Dmsg0(100, "Done with term_find_files\n");
351 free_jcr(jcr); /* destroy JCR record */
352 Dmsg0(100, "Done with free_jcr\n");
358 * Hello from Director he must identify himself and provide his
361 static int hello_cmd(JCR *jcr)
363 Dmsg0(120, "Calling Authenticate\n");
364 if (!authenticate_director(jcr)) {
367 Dmsg0(120, "OK Authenticate\n");
368 jcr->authenticated = true;
375 static int cancel_cmd(JCR *jcr)
377 BSOCK *dir = jcr->dir_bsock;
378 char Job[MAX_NAME_LENGTH];
381 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
382 if (!(cjcr=get_jcr_by_full_name(Job))) {
383 dir->fsend(_("2901 Job %s not found.\n"), Job);
385 if (cjcr->store_bsock) {
386 cjcr->store_bsock->set_timed_out();
387 cjcr->store_bsock->set_terminated();
388 pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
390 set_jcr_job_status(cjcr, JS_Canceled);
392 dir->fsend(_("2001 Job %s marked to be canceled.\n"), Job);
395 dir->fsend(_("2902 Error scanning cancel command.\n"));
397 dir->signal(BNET_EOD);
403 * Set debug level as requested by the Director
406 static int setdebug_cmd(JCR *jcr)
408 BSOCK *dir = jcr->dir_bsock;
409 int level, trace_flag;
411 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
412 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
413 pm_strcpy(jcr->errmsg, dir->msg);
414 dir->fsend(_("2991 Bad setdebug command: %s\n"), jcr->errmsg);
418 set_trace(trace_flag);
419 return dir->fsend(OKsetdebug, level);
423 static int estimate_cmd(JCR *jcr)
425 BSOCK *dir = jcr->dir_bsock;
428 if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
429 pm_strcpy(jcr->errmsg, dir->msg);
430 Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
431 dir->fsend(_("2992 Bad estimate command.\n"));
435 dir->fsend(OKest, jcr->num_files_examined,
436 edit_uint64_with_commas(jcr->JobBytes, ed2));
437 dir->signal(BNET_EOD);
442 * Get JobId and Storage Daemon Authorization key from Director
444 static int job_cmd(JCR *jcr)
446 BSOCK *dir = jcr->dir_bsock;
447 POOLMEM *sd_auth_key;
449 sd_auth_key = get_memory(dir->msglen);
450 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
451 &jcr->VolSessionId, &jcr->VolSessionTime,
453 pm_strcpy(jcr->errmsg, dir->msg);
454 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
456 free_pool_memory(sd_auth_key);
459 jcr->sd_auth_key = bstrdup(sd_auth_key);
460 free_pool_memory(sd_auth_key);
461 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
462 Mmsg(jcr->errmsg, "JobId=%d Job=%s", jcr->JobId, jcr->Job);
463 generate_plugin_event(jcr, bEventJobStart, (void *)jcr->errmsg);
464 return dir->fsend(OKjob, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER);
467 static int runbefore_cmd(JCR *jcr)
470 BSOCK *dir = jcr->dir_bsock;
471 POOLMEM *cmd = get_memory(dir->msglen+1);
474 Dmsg1(100, "runbefore_cmd: %s", dir->msg);
475 if (sscanf(dir->msg, runbefore, cmd) != 1) {
476 pm_strcpy(jcr->errmsg, dir->msg);
477 Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
478 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
484 /* Run the command now */
485 script = new_runscript();
486 script->set_command(cmd);
487 script->when = SCRIPT_Before;
488 ok = script->run(jcr, "ClientRunBeforeJob");
489 free_runscript(script);
493 dir->fsend(OKRunBefore);
496 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
501 static int runbeforenow_cmd(JCR *jcr)
503 BSOCK *dir = jcr->dir_bsock;
505 run_scripts(jcr, jcr->RunScripts, "ClientBeforeJob");
506 if (job_canceled(jcr)) {
507 dir->fsend(_("2905 Bad RunBeforeNow command.\n"));
508 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: FAILED\n");
511 dir->fsend(OKRunBeforeNow);
512 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: OK\n");
517 static int runafter_cmd(JCR *jcr)
519 BSOCK *dir = jcr->dir_bsock;
520 POOLMEM *msg = get_memory(dir->msglen+1);
523 Dmsg1(100, "runafter_cmd: %s", dir->msg);
524 if (sscanf(dir->msg, runafter, msg) != 1) {
525 pm_strcpy(jcr->errmsg, dir->msg);
526 Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
527 dir->fsend(_("2905 Bad RunAfterJob command.\n"));
533 cmd = new_runscript();
534 cmd->set_command(msg);
535 cmd->on_success = true;
536 cmd->on_failure = false;
537 cmd->when = SCRIPT_After;
539 jcr->RunScripts->append(cmd);
541 free_pool_memory(msg);
542 return dir->fsend(OKRunAfter);
545 static int runscript_cmd(JCR *jcr)
547 BSOCK *dir = jcr->dir_bsock;
548 POOLMEM *msg = get_memory(dir->msglen+1);
549 int on_success, on_failure, fail_on_error;
551 RUNSCRIPT *cmd = new_runscript() ;
553 Dmsg1(100, "runscript_cmd: '%s'\n", dir->msg);
554 /* Note, we cannot sscanf into bools */
555 if (sscanf(dir->msg, runscript, &on_success,
560 pm_strcpy(jcr->errmsg, dir->msg);
561 Jmsg1(jcr, M_FATAL, 0, _("Bad RunScript command: %s\n"), jcr->errmsg);
562 dir->fsend(_("2905 Bad RunScript command.\n"));
567 cmd->on_success = on_success;
568 cmd->on_failure = on_failure;
569 cmd->fail_on_error = fail_on_error;
572 cmd->set_command(msg);
574 jcr->RunScripts->append(cmd);
576 free_pool_memory(msg);
577 return dir->fsend(OKRunScript);
581 static bool init_fileset(JCR *jcr)
584 findFILESET *fileset;
593 fileset = (findFILESET *)malloc(sizeof(findFILESET));
594 memset(fileset, 0, sizeof(findFILESET));
595 ff->fileset = fileset;
596 fileset->state = state_none;
597 fileset->include_list.init(1, true);
598 fileset->exclude_list.init(1, true);
602 static findFOPTS *start_options(FF_PKT *ff)
604 int state = ff->fileset->state;
605 findINCEXE *incexe = ff->fileset->incexe;
607 if (state != state_options) {
608 ff->fileset->state = state_options;
609 findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS));
610 memset(fo, 0, sizeof(findFOPTS));
611 fo->regex.init(1, true);
612 fo->regexdir.init(1, true);
613 fo->regexfile.init(1, true);
614 fo->wild.init(1, true);
615 fo->wilddir.init(1, true);
616 fo->wildfile.init(1, true);
617 fo->wildbase.init(1, true);
618 fo->base.init(1, true);
619 fo->fstype.init(1, true);
620 fo->drivetype.init(1, true);
621 incexe->current_opts = fo;
622 incexe->opts_list.append(fo);
624 return incexe->current_opts;
629 * Add fname to include/exclude fileset list. First check for
630 * | and < and if necessary perform command.
632 static void add_file_to_fileset(JCR *jcr, const char *fname, findFILESET *fileset,
647 p++; /* skip over | */
648 fn = get_pool_memory(PM_FNAME);
649 fn = edit_job_codes(jcr, fn, p, "");
650 bpipe = open_bpipe(fn, 0, "r");
653 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
655 free_pool_memory(fn);
658 free_pool_memory(fn);
659 while (fgets(buf, sizeof(buf), bpipe->rfd)) {
660 strip_trailing_junk(buf);
662 fileset->incexe->name_list.append(new_dlistString(buf));
664 fileset->incexe->plugin_list.append(new_dlistString(buf));
667 if ((stat=close_bpipe(bpipe)) != 0) {
669 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. stat=%d: ERR=%s\n"),
670 p, be.code(stat), be.bstrerror(stat));
675 Dmsg0(100, "Doing < include on client.\n");
676 p++; /* skip over < */
677 if ((ffd = fopen(p, "rb")) == NULL) {
679 Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"),
683 while (fgets(buf, sizeof(buf), ffd)) {
684 strip_trailing_junk(buf);
685 Dmsg1(100, "%s\n", buf);
687 fileset->incexe->name_list.append(new_dlistString(buf));
689 fileset->incexe->plugin_list.append(new_dlistString(buf));
696 fileset->incexe->name_list.append(new_dlistString(fname));
698 fileset->incexe->plugin_list.append(new_dlistString(fname));
705 static void add_fileset(JCR *jcr, const char *item)
707 FF_PKT *ff = jcr->ff;
708 findFILESET *fileset = ff->fileset;
709 int state = fileset->state;
710 findFOPTS *current_opts;
712 /* Get code, optional subcode, and position item past the dividing space */
713 Dmsg1(100, "%s\n", item);
718 int subcode = ' '; /* A space is always a valid subcode */
719 if (item[0] != '\0' && item[0] != ' ') {
727 /* Skip all lines we receive after an error */
728 if (state == state_error) {
729 Dmsg0(100, "State=error return\n");
734 * The switch tests the code for validity.
735 * The subcode is always good if it is a space, otherwise we must confirm.
736 * We set state to state_error first assuming the subcode is invalid,
737 * requiring state to be set in cases below that handle subcodes.
739 if (subcode != ' ') {
741 Dmsg0(100, "Set state=error\n");
746 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
747 memset(fileset->incexe, 0, sizeof(findINCEXE));
748 fileset->incexe->opts_list.init(1, true);
749 fileset->incexe->name_list.init(); /* for dlist; was 1,true for alist */
750 fileset->incexe->plugin_list.init();
751 fileset->include_list.append(fileset->incexe);
755 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
756 memset(fileset->incexe, 0, sizeof(findINCEXE));
757 fileset->incexe->opts_list.init(1, true);
758 fileset->incexe->name_list.init();
759 fileset->incexe->plugin_list.init();
760 fileset->exclude_list.append(fileset->incexe);
766 /* File item to include or exclude list */
767 state = state_include;
768 add_file_to_fileset(jcr, item, fileset, true);
771 /* Plugin item to include list */
772 state = state_include;
773 add_file_to_fileset(jcr, item, fileset, false);
776 current_opts = start_options(ff);
780 preg = (regex_t *)malloc(sizeof(regex_t));
781 if (current_opts->flags & FO_IGNORECASE) {
782 rc = regcomp(preg, item, REG_EXTENDED|REG_ICASE);
784 rc = regcomp(preg, item, REG_EXTENDED);
787 regerror(rc, preg, prbuf, sizeof(prbuf));
790 Jmsg(jcr, M_FATAL, 0, _("REGEX %s compile error. ERR=%s\n"), item, prbuf);
794 state = state_options;
795 if (subcode == ' ') {
796 current_opts->regex.append(preg);
797 } else if (subcode == 'D') {
798 current_opts->regexdir.append(preg);
799 } else if (subcode == 'F') {
800 current_opts->regexfile.append(preg);
806 current_opts = start_options(ff);
807 current_opts->base.append(bstrdup(item));
808 state = state_options;
811 current_opts = start_options(ff);
812 state = state_options;
813 if (subcode == ' ') {
814 current_opts->fstype.append(bstrdup(item));
815 } else if (subcode == 'D') {
816 current_opts->drivetype.append(bstrdup(item));
822 current_opts = start_options(ff);
823 state = state_options;
824 if (subcode == ' ') {
825 current_opts->wild.append(bstrdup(item));
826 } else if (subcode == 'D') {
827 current_opts->wilddir.append(bstrdup(item));
828 } else if (subcode == 'F') {
829 current_opts->wildfile.append(bstrdup(item));
830 } else if (subcode == 'B') {
831 current_opts->wildbase.append(bstrdup(item));
837 current_opts = start_options(ff);
838 set_options(current_opts, item);
839 state = state_options;
842 current_opts = start_options(ff);
843 // current_opts->reader = bstrdup(item);
844 state = state_options;
847 current_opts = start_options(ff);
848 // current_opts->writer = bstrdup(item);
849 state = state_options;
852 Jmsg(jcr, M_FATAL, 0, _("Invalid FileSet command: %s\n"), item);
856 ff->fileset->state = state;
859 static bool term_fileset(JCR *jcr)
861 FF_PKT *ff = jcr->ff;
863 #ifdef xxx_DEBUG_CODE
864 findFILESET *fileset = ff->fileset;
867 for (i=0; i<fileset->include_list.size(); i++) {
868 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
870 for (j=0; j<incexe->opts_list.size(); j++) {
871 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
872 for (k=0; k<fo->regex.size(); k++) {
873 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
875 for (k=0; k<fo->regexdir.size(); k++) {
876 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
878 for (k=0; k<fo->regexfile.size(); k++) {
879 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
881 for (k=0; k<fo->wild.size(); k++) {
882 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
884 for (k=0; k<fo->wilddir.size(); k++) {
885 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
887 for (k=0; k<fo->wildfile.size(); k++) {
888 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
890 for (k=0; k<fo->wildbase.size(); k++) {
891 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
893 for (k=0; k<fo->base.size(); k++) {
894 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
896 for (k=0; k<fo->fstype.size(); k++) {
897 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
899 for (k=0; k<fo->drivetype.size(); k++) {
900 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
904 foreach_dlist(node, &incexe->name_list) {
905 Dmsg1(400, "F %s\n", node->c_str());
907 foreach_dlist(node, &incexe->plugin_list) {
908 Dmsg1(400, "P %s\n", node->c_str());
911 for (i=0; i<fileset->exclude_list.size(); i++) {
912 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
914 for (j=0; j<incexe->opts_list.size(); j++) {
915 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
916 for (k=0; k<fo->regex.size(); k++) {
917 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
919 for (k=0; k<fo->regexdir.size(); k++) {
920 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
922 for (k=0; k<fo->regexfile.size(); k++) {
923 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
925 for (k=0; k<fo->wild.size(); k++) {
926 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
928 for (k=0; k<fo->wilddir.size(); k++) {
929 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
931 for (k=0; k<fo->wildfile.size(); k++) {
932 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
934 for (k=0; k<fo->wildbase.size(); k++) {
935 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
937 for (k=0; k<fo->base.size(); k++) {
938 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
940 for (k=0; k<fo->fstype.size(); k++) {
941 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
943 for (k=0; k<fo->drivetype.size(); k++) {
944 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
948 foreach_dlist(node, incexe->name_list) {
949 Dmsg1(400, "F %s\n", node->c_str());
951 foreach_dlist(node, &incexe->plugin_list) {
952 Dmsg1(400, "P %s\n", node->c_str());
956 return ff->fileset->state != state_error;
961 * As an optimization, we should do this during
962 * "compile" time in filed/job.c, and keep only a bit mask
963 * and the Verify options.
965 static void set_options(findFOPTS *fo, const char *opts)
971 // Commented out as it is not backward compatible - KES
973 // fo->flags |= FO_IGNORECASE; /* always ignorecase under windows */
976 for (p=opts; *p; p++) {
978 case 'a': /* alway replace */
979 case '0': /* no option */
982 fo->flags |= FO_EXCLUDE;
985 fo->flags |= FO_MULTIFS;
987 case 'h': /* no recursion */
988 fo->flags |= FO_NO_RECURSION;
990 case 'H': /* no hard link handling */
991 fo->flags |= FO_NO_HARDLINK;
994 fo->flags |= FO_IGNORECASE;
1000 fo->flags |= FO_NOREPLACE;
1002 case 'p': /* use portable data format */
1003 fo->flags |= FO_PORTABLE;
1005 case 'R': /* Resource forks and Finder Info */
1006 fo->flags |= FO_HFSPLUS;
1007 case 'r': /* read fifo */
1008 fo->flags |= FO_READFIFO;
1013 fo->flags |= FO_SHA1;
1018 fo->flags |= FO_SHA256;
1022 fo->flags |= FO_SHA512;
1028 * If 2 or 3 is seen here, SHA2 is not configured, so
1029 * eat the option, and drop back to SHA-1.
1031 if (p[1] == '2' || p[1] == '3') {
1034 fo->flags |= FO_SHA1;
1039 fo->flags |= FO_SPARSE;
1042 fo->flags |= FO_MTIMEONLY;
1045 fo->flags |= FO_KEEPATIME;
1048 fo->flags |= FO_ACL;
1050 case 'V': /* verify options */
1051 /* Copy Verify Options */
1052 for (j=0; *p && *p != ':'; p++) {
1053 fo->VerifyOpts[j] = *p;
1054 if (j < (int)sizeof(fo->VerifyOpts) - 1) {
1058 fo->VerifyOpts[j] = 0;
1060 case 'P': /* strip path */
1063 for (j=0; *p && *p != ':'; p++) {
1065 if (j < (int)sizeof(strip) - 1) {
1070 fo->strip_path = atoi(strip);
1071 fo->flags |= FO_STRIPPATH;
1072 Dmsg2(100, "strip=%s strip_path=%d\n", strip, fo->strip_path);
1075 fo->flags |= FO_IF_NEWER;
1078 fo->flags |= FO_ENHANCEDWILD;
1080 case 'Z': /* gzip compression */
1081 fo->flags |= FO_GZIP;
1082 fo->GZIP_level = *++p - '0';
1085 fo->flags |= FO_NOATIME;
1088 fo->flags |= FO_CHKCHANGES;
1091 Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *p);
1099 * Director is passing his Fileset
1101 static int fileset_cmd(JCR *jcr)
1103 BSOCK *dir = jcr->dir_bsock;
1105 #if defined(WIN32_VSS)
1108 sscanf(dir->msg, "fileset vss=%d", &vss);
1112 if (!init_fileset(jcr)) {
1115 while (dir->recv() >= 0) {
1116 strip_trailing_junk(dir->msg);
1117 Dmsg1(500, "Fileset: %s\n", dir->msg);
1118 add_fileset(jcr, dir->msg);
1120 if (!term_fileset(jcr)) {
1123 return dir->fsend(OKinc);
1126 static void free_bootstrap(JCR *jcr)
1128 if (jcr->RestoreBootstrap) {
1129 unlink(jcr->RestoreBootstrap);
1130 free_pool_memory(jcr->RestoreBootstrap);
1131 jcr->RestoreBootstrap = NULL;
1136 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1137 static uint32_t bsr_uniq = 0;
1140 * The Director sends us the bootstrap file, which
1141 * we will in turn pass to the SD.
1143 static int bootstrap_cmd(JCR *jcr)
1145 BSOCK *dir = jcr->dir_bsock;
1146 POOLMEM *fname = get_pool_memory(PM_FNAME);
1149 free_bootstrap(jcr);
1152 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1153 jcr->Job, bsr_uniq);
1155 Dmsg1(400, "bootstrap=%s\n", fname);
1156 jcr->RestoreBootstrap = fname;
1157 bs = fopen(fname, "a+b"); /* create file */
1160 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
1161 jcr->RestoreBootstrap, be.bstrerror());
1163 * Suck up what he is sending to us so that he will then
1164 * read our error message.
1166 while (dir->recv() >= 0)
1168 free_bootstrap(jcr);
1169 set_jcr_job_status(jcr, JS_ErrorTerminated);
1173 while (dir->recv() >= 0) {
1174 Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
1175 fputs(dir->msg, bs);
1179 * Note, do not free the bootstrap yet -- it needs to be
1182 return dir->fsend(OKbootstrap);
1187 * Get backup level from Director
1190 static int level_cmd(JCR *jcr)
1192 BSOCK *dir = jcr->dir_bsock;
1193 POOLMEM *level, *buf = NULL;
1196 level = get_memory(dir->msglen+1);
1197 Dmsg1(110, "level_cmd: %s", dir->msg);
1198 if (sscanf(dir->msg, "level = %s ", level) != 1) {
1201 /* Base backup requested? */
1202 if (strcmp(level, "base") == 0) {
1203 jcr->JobLevel = L_BASE;
1204 /* Full backup requested? */
1205 } else if (strcmp(level, "full") == 0) {
1206 jcr->JobLevel = L_FULL;
1207 } else if (strcmp(level, "differential") == 0) {
1208 jcr->JobLevel = L_DIFFERENTIAL;
1211 } else if (strcmp(level, "incremental") == 0) {
1212 jcr->JobLevel = L_INCREMENTAL;
1216 * We get his UTC since time, then sync the clocks and correct it
1217 * to agree with our clock.
1219 } else if (strcmp(level, "since_utime") == 0) {
1220 buf = get_memory(dir->msglen+1);
1221 utime_t since_time, adj;
1222 btime_t his_time, bt_start, rt=0, bt_adj=0;
1223 if (jcr->JobLevel == L_NONE) {
1224 jcr->JobLevel = L_SINCE; /* if no other job level set, do it now */
1226 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
1227 buf, &mtime_only) != 2) {
1230 since_time = str_to_uint64(buf); /* this is the since time */
1231 Dmsg1(100, "since_time=%d\n", (int)since_time);
1232 char ed1[50], ed2[50];
1234 * Sync clocks by polling him for the time. We take
1235 * 10 samples of his time throwing out the first two.
1237 for (int i=0; i<10; i++) {
1238 bt_start = get_current_btime();
1239 dir->signal(BNET_BTIME); /* poll for time */
1240 if (dir->recv() <= 0) { /* get response */
1243 if (sscanf(dir->msg, "btime %s", buf) != 1) {
1246 if (i < 2) { /* toss first two results */
1249 his_time = str_to_uint64(buf);
1250 rt = get_current_btime() - bt_start; /* compute round trip time */
1251 Dmsg2(100, "Dirtime=%s FDtime=%s\n", edit_uint64(his_time, ed1),
1252 edit_uint64(bt_start, ed2));
1253 bt_adj += bt_start - his_time - rt/2;
1254 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1257 bt_adj = bt_adj / 8; /* compute average time */
1258 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1259 adj = btime_to_utime(bt_adj);
1260 since_time += adj; /* adjust for clock difference */
1261 /* Don't notify if time within 3 seconds */
1262 if (adj > 3 || adj < -3) {
1264 if (adj > 600 || adj < -600) {
1269 Jmsg(jcr, type, 0, _("DIR and FD clocks differ by %d seconds, FD automatically compensating.\n"), adj);
1271 dir->signal(BNET_EOD);
1273 Dmsg2(100, "adj = %d since_time=%d\n", (int)adj, (int)since_time);
1274 jcr->incremental = 1; /* set incremental or decremental backup */
1275 jcr->mtime = (time_t)since_time; /* set since time */
1276 generate_plugin_event(jcr, bEventSince, (void *)jcr->mtime);
1278 Jmsg1(jcr, M_FATAL, 0, _("Unknown backup level: %s\n"), level);
1286 generate_plugin_event(jcr, bEventLevel, (void *)jcr->JobLevel);
1287 return dir->fsend(OKlevel);
1290 pm_strcpy(jcr->errmsg, dir->msg);
1291 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
1300 * Get session parameters from Director -- this is for a Restore command
1302 static int session_cmd(JCR *jcr)
1304 BSOCK *dir = jcr->dir_bsock;
1306 Dmsg1(100, "SessionCmd: %s", dir->msg);
1307 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
1308 &jcr->VolSessionId, &jcr->VolSessionTime,
1309 &jcr->StartFile, &jcr->EndFile,
1310 &jcr->StartBlock, &jcr->EndBlock) != 7) {
1311 pm_strcpy(jcr->errmsg, dir->msg);
1312 Jmsg(jcr, M_FATAL, 0, _("Bad session command: %s"), jcr->errmsg);
1316 return bnet_fsend(dir, OKsession);
1320 * Get address of storage daemon from Director
1323 static int storage_cmd(JCR *jcr)
1325 int stored_port; /* storage daemon port */
1326 int enable_ssl; /* enable ssl to sd */
1327 BSOCK *dir = jcr->dir_bsock;
1328 BSOCK *sd; /* storage daemon bsock */
1330 Dmsg1(100, "StorageCmd: %s", dir->msg);
1331 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
1332 pm_strcpy(jcr->errmsg, dir->msg);
1333 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
1336 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
1337 /* Open command communications with Storage daemon */
1338 /* Try to connect for 1 hour at 10 second intervals */
1339 sd = bnet_connect(jcr, 10, (int)me->SDConnectTimeout, me->heartbeat_interval,
1340 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1);
1342 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
1343 jcr->stored_addr, stored_port);
1344 Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
1345 jcr->stored_addr, stored_port);
1348 Dmsg0(110, "Connection OK to SD.\n");
1350 jcr->store_bsock = sd;
1352 sd->fsend("Hello Start Job %s\n", jcr->Job);
1353 if (!authenticate_storagedaemon(jcr)) {
1354 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
1357 Dmsg0(110, "Authenticated with SD.\n");
1359 /* Send OK to Director */
1360 return dir->fsend(OKstore);
1367 static int backup_cmd(JCR *jcr)
1369 BSOCK *dir = jcr->dir_bsock;
1370 BSOCK *sd = jcr->store_bsock;
1374 #if defined(WIN32_VSS)
1375 // capture state here, if client is backed up by multiple directors
1376 // and one enables vss and the other does not then enable_vss can change
1377 // between here and where its evaluated after the job completes.
1378 jcr->VSS = g_pVSSClient && enable_vss;
1380 /* Run only one at a time */
1385 set_jcr_job_status(jcr, JS_Blocked);
1386 jcr->JobType = JT_BACKUP;
1387 Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
1390 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
1394 dir->fsend(OKbackup);
1395 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1398 * Send Append Open Session to Storage daemon
1400 sd->fsend(append_open);
1401 Dmsg1(110, ">stored: %s", sd->msg);
1403 * Expect to receive back the Ticket number
1405 if (bget_msg(sd) >= 0) {
1406 Dmsg1(110, "<stored: %s", sd->msg);
1407 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1408 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
1411 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
1413 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
1418 * Send Append data command to Storage daemon
1420 sd->fsend(append_data, jcr->Ticket);
1421 Dmsg1(110, ">stored: %s", sd->msg);
1424 * Expect to get OK data
1426 Dmsg1(110, "<stored: %s", sd->msg);
1427 if (!response(jcr, sd, OK_data, "Append Data")) {
1431 generate_daemon_event(jcr, "JobStart");
1432 generate_plugin_event(jcr, bEventBackupStart);
1434 #if defined(WIN32_VSS)
1435 /* START VSS ON WIN32 */
1437 if (g_pVSSClient->InitializeForBackup()) {
1438 /* tell vss which drives to snapshot */
1439 char szWinDriveLetters[27];
1440 if (get_win32_driveletters(jcr->ff, szWinDriveLetters)) {
1441 Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
1442 if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {
1443 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed.\n"));
1446 /* tell user if snapshot creation of a specific drive failed */
1448 for (i=0; i < (int)strlen(szWinDriveLetters); i++) {
1449 if (islower(szWinDriveLetters[i])) {
1450 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed. VSS support is disabled on this drive.\n"), szWinDriveLetters[i]);
1454 /* inform user about writer states */
1455 for (i=0; i < (int)g_pVSSClient->GetWriterCount(); i++)
1456 if (g_pVSSClient->GetWriterState(i) < 1) {
1457 Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PrepareForBackup): %s\n"), g_pVSSClient->GetWriterInfo(i));
1462 Jmsg(jcr, M_INFO, 0, _("No drive letters found for generating VSS snapshots.\n"));
1466 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled. ERR=%s\n"), be.bstrerror());
1472 * Send Files to Storage daemon
1474 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
1475 if (!blast_data_to_storage_daemon(jcr, NULL)) {
1476 set_jcr_job_status(jcr, JS_ErrorTerminated);
1477 bnet_suppress_error_messages(sd, 1);
1478 bget_msg(sd); /* Read final response from append_data */
1479 Dmsg0(110, "Error in blast_data.\n");
1481 set_jcr_job_status(jcr, JS_Terminated);
1483 if (jcr->JobStatus != JS_Terminated) {
1484 bnet_suppress_error_messages(sd, 1);
1485 goto cleanup; /* bail out now */
1488 * Expect to get response to append_data from Storage daemon
1490 if (!response(jcr, sd, OK_append, "Append Data")) {
1491 set_jcr_job_status(jcr, JS_ErrorTerminated);
1496 * Send Append End Data to Storage daemon
1498 sd->fsend(append_end, jcr->Ticket);
1500 if (!response(jcr, sd, OK_end, "Append End")) {
1501 set_jcr_job_status(jcr, JS_ErrorTerminated);
1506 * Send Append Close to Storage daemon
1508 sd->fsend(append_close, jcr->Ticket);
1509 while (bget_msg(sd) >= 0) { /* stop on signal or error */
1510 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
1512 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
1516 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
1519 if (SDJobStatus != JS_Terminated) {
1520 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
1526 #if defined(WIN32_VSS)
1527 /* STOP VSS ON WIN32 */
1528 /* tell vss to close the backup session */
1530 if (g_pVSSClient->CloseBackup()) {
1531 /* inform user about writer states */
1532 for (int i=0; i<(int)g_pVSSClient->GetWriterCount(); i++) {
1533 int msg_type = M_INFO;
1534 if (g_pVSSClient->GetWriterState(i) < 1) {
1535 msg_type = M_WARNING;
1538 Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
1545 generate_plugin_event(jcr, bEventBackupEnd);
1546 return 0; /* return and stop command loop */
1550 * Do a Verify for Director
1553 static int verify_cmd(JCR *jcr)
1555 BSOCK *dir = jcr->dir_bsock;
1556 BSOCK *sd = jcr->store_bsock;
1559 jcr->JobType = JT_VERIFY;
1560 if (sscanf(dir->msg, verifycmd, level) != 1) {
1561 dir->fsend(_("2994 Bad verify command: %s\n"), dir->msg);
1565 if (strcasecmp(level, "init") == 0) {
1566 jcr->JobLevel = L_VERIFY_INIT;
1567 } else if (strcasecmp(level, "catalog") == 0){
1568 jcr->JobLevel = L_VERIFY_CATALOG;
1569 } else if (strcasecmp(level, "volume") == 0){
1570 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
1571 } else if (strcasecmp(level, "data") == 0){
1572 jcr->JobLevel = L_VERIFY_DATA;
1573 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
1574 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
1576 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1580 dir->fsend(OKverify);
1582 generate_daemon_event(jcr, "JobStart");
1583 generate_plugin_event(jcr, bEventLevel, (void *)jcr->JobLevel);
1584 generate_plugin_event(jcr, bEventVerifyStart);
1586 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1588 switch (jcr->JobLevel) {
1590 case L_VERIFY_CATALOG:
1593 case L_VERIFY_VOLUME_TO_CATALOG:
1594 if (!open_sd_read_session(jcr)) {
1597 start_dir_heartbeat(jcr);
1598 do_verify_volume(jcr);
1599 stop_dir_heartbeat(jcr);
1601 * Send Close session command to Storage daemon
1603 sd->fsend(read_close, jcr->Ticket);
1604 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1606 /* ****FIXME**** check response */
1607 bget_msg(sd); /* get OK */
1609 /* Inform Storage daemon that we are done */
1610 sd->signal(BNET_TERMINATE);
1613 case L_VERIFY_DISK_TO_CATALOG:
1617 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1621 dir->signal(BNET_EOD);
1622 generate_plugin_event(jcr, bEventVerifyEnd);
1623 return 0; /* return and terminate command loop */
1627 * Do a Restore for Director
1630 static int restore_cmd(JCR *jcr)
1632 BSOCK *dir = jcr->dir_bsock;
1633 BSOCK *sd = jcr->store_bsock;
1635 bool use_regexwhere=false;
1640 * Scan WHERE (base directory for restore) from command
1642 Dmsg0(150, "restore command\n");
1643 /* Pickup where string */
1644 args = get_memory(dir->msglen+1);
1647 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, args) != 3) {
1648 if (sscanf(dir->msg, restorecmdR, &replace, &prefix_links, args) != 3){
1649 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
1650 pm_strcpy(jcr->errmsg, dir->msg);
1651 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
1656 use_regexwhere = true;
1658 /* Turn / into nothing */
1659 if (IsPathSeparator(args[0]) && args[1] == '\0') {
1663 Dmsg2(150, "Got replace %c, where=%s\n", replace, args);
1664 unbash_spaces(args);
1666 if (use_regexwhere) {
1667 jcr->where_bregexp = get_bregexps(args);
1668 if (!jcr->where_bregexp) {
1669 Jmsg(jcr, M_FATAL, 0, _("Bad where regexp. where=%s\n"), args);
1670 free_pool_memory(args);
1674 jcr->where = bstrdup(args);
1677 free_pool_memory(args);
1678 jcr->replace = replace;
1679 jcr->prefix_links = prefix_links;
1681 dir->fsend(OKrestore);
1682 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1684 jcr->JobType = JT_RESTORE;
1686 set_jcr_job_status(jcr, JS_Blocked);
1688 if (!open_sd_read_session(jcr)) {
1689 set_jcr_job_status(jcr, JS_ErrorTerminated);
1693 set_jcr_job_status(jcr, JS_Running);
1696 * Do restore of files and data
1698 start_dir_heartbeat(jcr);
1699 generate_daemon_event(jcr, "JobStart");
1700 generate_plugin_event(jcr, bEventRestoreStart);
1702 stop_dir_heartbeat(jcr);
1704 set_jcr_job_status(jcr, JS_Terminated);
1705 if (jcr->JobStatus != JS_Terminated) {
1706 bnet_suppress_error_messages(sd, 1);
1710 * Send Close session command to Storage daemon
1712 sd->fsend(read_close, jcr->Ticket);
1713 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1715 bget_msg(sd); /* get OK */
1717 /* Inform Storage daemon that we are done */
1718 sd->signal(BNET_TERMINATE);
1723 set_jcr_job_status(jcr, JS_ErrorTerminated);
1726 Dmsg0(130, "Done in job.c\n");
1727 generate_plugin_event(jcr, bEventRestoreEnd);
1728 return 0; /* return and terminate command loop */
1731 static int open_sd_read_session(JCR *jcr)
1733 BSOCK *sd = jcr->store_bsock;
1736 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
1739 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
1740 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
1741 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
1743 * Open Read Session with Storage daemon
1745 bnet_fsend(sd, read_open, "DummyVolume",
1746 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
1747 jcr->StartBlock, jcr->EndBlock);
1748 Dmsg1(110, ">stored: %s", sd->msg);
1753 if (bget_msg(sd) >= 0) {
1754 Dmsg1(110, "bfiled<stored: %s", sd->msg);
1755 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1756 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
1759 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
1761 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
1765 if (!send_bootstrap_file(jcr)) {
1770 * Start read of data with Storage daemon
1772 bnet_fsend(sd, read_data, jcr->Ticket);
1773 Dmsg1(110, ">stored: %s", sd->msg);
1778 if (!response(jcr, sd, OK_data, "Read Data")) {
1785 * Destroy the Job Control Record and associated
1786 * resources (sockets).
1788 static void filed_free_jcr(JCR *jcr)
1790 if (jcr->store_bsock) {
1791 bnet_close(jcr->store_bsock);
1793 free_bootstrap(jcr);
1794 if (jcr->last_fname) {
1795 free_pool_memory(jcr->last_fname);
1797 free_runscripts(jcr->RunScripts);
1798 delete jcr->RunScripts;
1800 if (jcr->JobId != 0)
1801 write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
1807 * Get response from Storage daemon to a command we
1808 * sent. Check that the response is OK.
1810 * Returns: 0 on failure
1813 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
1818 if (bget_msg(sd) > 0) {
1819 Dmsg0(110, sd->msg);
1820 if (strcmp(sd->msg, resp) == 0) {
1824 if (job_canceled(jcr)) {
1825 return 0; /* if canceled avoid useless error messages */
1827 if (is_bnet_error(sd)) {
1828 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
1829 cmd, bnet_strerror(sd));
1831 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
1832 cmd, resp, sd->msg);
1837 static int send_bootstrap_file(JCR *jcr)
1841 BSOCK *sd = jcr->store_bsock;
1842 const char *bootstrap = "bootstrap\n";
1845 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
1846 if (!jcr->RestoreBootstrap) {
1849 bs = fopen(jcr->RestoreBootstrap, "rb");
1852 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
1853 jcr->RestoreBootstrap, be.bstrerror());
1854 set_jcr_job_status(jcr, JS_ErrorTerminated);
1857 sd->msglen = pm_strcpy(sd->msg, bootstrap);
1859 while (fgets(buf, sizeof(buf), bs)) {
1860 sd->msglen = Mmsg(sd->msg, "%s", buf);
1863 bnet_sig(sd, BNET_EOD);
1865 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
1866 set_jcr_job_status(jcr, JS_ErrorTerminated);
1872 free_bootstrap(jcr);