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 Kern Sibbald.
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);
52 extern int accurate_cmd(JCR *jcr);
54 /* Forward referenced functions */
55 static int backup_cmd(JCR *jcr);
56 static int bootstrap_cmd(JCR *jcr);
57 static int cancel_cmd(JCR *jcr);
58 static int setdebug_cmd(JCR *jcr);
59 static int estimate_cmd(JCR *jcr);
60 static int hello_cmd(JCR *jcr);
61 static int job_cmd(JCR *jcr);
62 static int fileset_cmd(JCR *jcr);
63 static int level_cmd(JCR *jcr);
64 static int verify_cmd(JCR *jcr);
65 static int restore_cmd(JCR *jcr);
66 static int storage_cmd(JCR *jcr);
67 static int session_cmd(JCR *jcr);
68 static int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd);
69 static void filed_free_jcr(JCR *jcr);
70 static int open_sd_read_session(JCR *jcr);
71 static int send_bootstrap_file(JCR *jcr);
72 static int runscript_cmd(JCR *jcr);
73 static int runbefore_cmd(JCR *jcr);
74 static int runafter_cmd(JCR *jcr);
75 static int runbeforenow_cmd(JCR *jcr);
76 static void set_options(findFOPTS *fo, const char *opts);
79 /* Exported functions */
84 int monitoraccess; /* specify if monitors have access to this function */
88 * The following are the recognized commands from the Director.
90 static struct s_cmds cmds[] = {
91 {"backup", backup_cmd, 0},
92 {"cancel", cancel_cmd, 0},
93 {"setdebug=", setdebug_cmd, 0},
94 {"estimate", estimate_cmd, 0},
95 {"Hello", hello_cmd, 1},
96 {"fileset", fileset_cmd, 0},
97 {"JobId=", job_cmd, 0},
98 {"level = ", level_cmd, 0},
99 {"restore", restore_cmd, 0},
100 {"session", session_cmd, 0},
101 {"status", status_cmd, 1},
102 {".status", qstatus_cmd, 1},
103 {"storage ", storage_cmd, 0},
104 {"verify", verify_cmd, 0},
105 {"bootstrap", bootstrap_cmd, 0},
106 {"RunBeforeNow", runbeforenow_cmd, 0},
107 {"RunBeforeJob", runbefore_cmd, 0},
108 {"RunAfterJob", runafter_cmd, 0},
109 {"Run", runscript_cmd, 0},
110 {"accurate", accurate_cmd, 0},
111 {NULL, NULL} /* list terminator */
114 /* Commands received from director that need scanning */
115 static char jobcmd[] = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
116 static char storaddr[] = "storage address=%s port=%d ssl=%d";
117 static char sessioncmd[] = "session %127s %ld %ld %ld %ld %ld %ld\n";
118 static char restorecmd[] = "restore replace=%c prelinks=%d where=%s\n";
119 static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
120 static char restorecmdR[] = "restore replace=%c prelinks=%d regexwhere=%s\n";
121 static char verifycmd[] = "verify level=%30s";
122 static char estimatecmd[] = "estimate listing=%d";
123 static char runbefore[] = "RunBeforeJob %s";
124 static char runafter[] = "RunAfterJob %s";
125 static char runscript[] = "Run OnSuccess=%d OnFailure=%d AbortOnError=%d When=%d Command=%s";
127 /* Responses sent to Director */
128 static char errmsg[] = "2999 Invalid command\n";
129 static char no_auth[] = "2998 No Authorization\n";
130 static char invalid_cmd[] = "2997 Invalid command for a Director with Monitor directive enabled.\n";
131 static char OKinc[] = "2000 OK include\n";
132 static char OKest[] = "2000 OK estimate files=%u bytes=%s\n";
133 static char OKlevel[] = "2000 OK level\n";
134 static char OKbackup[] = "2000 OK backup\n";
135 static char OKbootstrap[] = "2000 OK bootstrap\n";
136 static char OKverify[] = "2000 OK verify\n";
137 static char OKrestore[] = "2000 OK restore\n";
138 static char OKsession[] = "2000 OK session\n";
139 static char OKstore[] = "2000 OK storage\n";
140 static char OKjob[] = "2000 OK Job %s (%s) %s,%s,%s";
141 static char OKsetdebug[] = "2000 OK setdebug=%d\n";
142 static char BADjob[] = "2901 Bad Job\n";
143 static char EndJob[] = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s"
144 " JobBytes=%s Errors=%u VSS=%d Encrypt=%d\n";
145 static char OKRunBefore[] = "2000 OK RunBefore\n";
146 static char OKRunBeforeNow[] = "2000 OK RunBeforeNow\n";
147 static char OKRunAfter[] = "2000 OK RunAfter\n";
148 static char OKRunScript[] = "2000 OK RunScript\n";
149 static char BADcmd[] = "2902 Bad %s\n";
152 /* Responses received from Storage Daemon */
153 static char OK_end[] = "3000 OK end\n";
154 static char OK_close[] = "3000 OK close Status = %d\n";
155 static char OK_open[] = "3000 OK open ticket = %d\n";
156 static char OK_data[] = "3000 OK data\n";
157 static char OK_append[] = "3000 OK append data\n";
158 static char OKSDbootstrap[]= "3000 OK bootstrap\n";
161 /* Commands sent to Storage Daemon */
162 static char append_open[] = "append open session\n";
163 static char append_data[] = "append data %d\n";
164 static char append_end[] = "append end session %d\n";
165 static char append_close[] = "append close session %d\n";
166 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
167 static char read_data[] = "read data %d\n";
168 static char read_close[] = "read close session %d\n";
171 * Accept requests from a Director
173 * NOTE! We are running as a separate thread
175 * Send output one line
176 * at a time followed by a zero length transmission.
178 * Return when the connection is terminated or there
181 * Basic task here is:
182 * Authenticate Director (during Hello command).
183 * Accept commands one at a time from the Director
186 * Concerning ClientRunBefore/After, the sequence of events
187 * is rather critical. If they are not done in the right
188 * order one can easily get FD->SD timeouts if the script
191 * The current sequence of events is:
192 * 1. Dir starts job with FD
193 * 2. Dir connects to SD
194 * 3. Dir connects to FD
195 * 4. FD connects to SD
196 * 5. FD gets/runs ClientRunBeforeJob and sends ClientRunAfterJob
197 * 6. Dir sends include/exclude
198 * 7. FD sends data to SD
199 * 8. SD/FD disconnects while SD despools data and attributes (optionnal)
200 * 9. FD runs ClientRunAfterJob
203 void *handle_client_request(void *dirp)
208 BSOCK *dir = (BSOCK *)dirp;
210 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
211 jcr->dir_bsock = dir;
212 jcr->ff = init_find_files();
213 jcr->start_time = time(NULL);
214 jcr->RunScripts = New(alist(10, not_owned_by_alist));
215 jcr->last_fname = get_pool_memory(PM_FNAME);
216 jcr->last_fname[0] = 0;
217 jcr->client_name = get_memory(strlen(my_name) + 1);
218 pm_strcpy(jcr->client_name, my_name);
219 jcr->crypto.pki_sign = me->pki_sign;
220 jcr->crypto.pki_encrypt = me->pki_encrypt;
221 jcr->crypto.pki_keypair = me->pki_keypair;
222 jcr->crypto.pki_signers = me->pki_signers;
223 jcr->crypto.pki_recipients = me->pki_recipients;
225 enable_backup_privileges(NULL, 1 /* ignore_errors */);
227 /**********FIXME******* add command handler error code */
229 for (quit=false; !quit;) {
231 if (dir->recv() < 0) {
232 break; /* connection terminated */
234 dir->msg[dir->msglen] = 0;
235 Dmsg1(100, "<dird: %s", dir->msg);
237 for (i=0; cmds[i].cmd; i++) {
238 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
239 found = true; /* indicate command found */
240 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
242 dir->signal(BNET_EOD);
245 if ((jcr->authenticated) && (!cmds[i].monitoraccess) && (jcr->director->monitor)) {
246 Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
247 dir->fsend(invalid_cmd);
248 dir->signal(BNET_EOD);
251 Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
252 if (!cmds[i].func(jcr)) { /* do command */
253 quit = true; /* error or fully terminated, get out */
254 Dmsg1(100, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
259 if (!found) { /* command not found */
266 /* Inform Storage daemon that we are done */
267 if (jcr->store_bsock) {
268 jcr->store_bsock->signal(BNET_TERMINATE);
271 /* Run the after job */
272 run_scripts(jcr, jcr->RunScripts, "ClientAfterJob");
274 if (jcr->JobId) { /* send EndJob if running a job */
275 char ed1[50], ed2[50];
276 /* Send termination status back to Dir */
277 dir->fsend(EndJob, jcr->JobStatus, jcr->JobFiles,
278 edit_uint64(jcr->ReadBytes, ed1),
279 edit_uint64(jcr->JobBytes, ed2), jcr->Errors, jcr->VSS,
280 jcr->crypto.pki_encrypt);
281 Dmsg1(110, "End FD msg: %s\n", dir->msg);
284 generate_daemon_event(jcr, "JobEnd");
285 generate_plugin_event(jcr, bEventJobEnd);
287 dequeue_messages(jcr); /* send any queued messages */
289 /* Inform Director that we are done */
290 dir->signal(BNET_TERMINATE);
292 free_plugins(jcr); /* release instantiated plugins */
294 /* Clean up fileset */
295 FF_PKT *ff = jcr->ff;
296 findFILESET *fileset = ff->fileset;
299 /* Delete FileSet Include lists */
300 for (i=0; i<fileset->include_list.size(); i++) {
301 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
302 for (j=0; j<incexe->opts_list.size(); j++) {
303 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
304 for (k=0; k<fo->regex.size(); k++) {
305 regfree((regex_t *)fo->regex.get(k));
307 for (k=0; k<fo->regexdir.size(); k++) {
308 regfree((regex_t *)fo->regexdir.get(k));
310 for (k=0; k<fo->regexfile.size(); k++) {
311 regfree((regex_t *)fo->regexfile.get(k));
314 fo->regexdir.destroy();
315 fo->regexfile.destroy();
317 fo->wilddir.destroy();
318 fo->wildfile.destroy();
319 fo->wildbase.destroy();
321 fo->fstype.destroy();
322 fo->drivetype.destroy();
323 if (fo->ignoredir != NULL) {
327 incexe->opts_list.destroy();
328 incexe->name_list.destroy();
329 incexe->plugin_list.destroy();
331 fileset->include_list.destroy();
333 /* Delete FileSet Exclude lists */
334 for (i=0; i<fileset->exclude_list.size(); i++) {
335 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
336 for (j=0; j<incexe->opts_list.size(); j++) {
337 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
339 fo->regexdir.destroy();
340 fo->regexfile.destroy();
342 fo->wilddir.destroy();
343 fo->wildfile.destroy();
344 fo->wildbase.destroy();
346 fo->fstype.destroy();
347 fo->drivetype.destroy();
349 incexe->opts_list.destroy();
350 incexe->name_list.destroy();
351 incexe->plugin_list.destroy();
353 fileset->exclude_list.destroy();
357 Dmsg0(100, "Calling term_find_files\n");
358 term_find_files(jcr->ff);
360 Dmsg0(100, "Done with term_find_files\n");
361 free_jcr(jcr); /* destroy JCR record */
362 Dmsg0(100, "Done with free_jcr\n");
368 * Hello from Director he must identify himself and provide his
371 static int hello_cmd(JCR *jcr)
373 Dmsg0(120, "Calling Authenticate\n");
374 if (!authenticate_director(jcr)) {
377 Dmsg0(120, "OK Authenticate\n");
378 jcr->authenticated = true;
385 static int cancel_cmd(JCR *jcr)
387 BSOCK *dir = jcr->dir_bsock;
388 char Job[MAX_NAME_LENGTH];
391 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
392 if (!(cjcr=get_jcr_by_full_name(Job))) {
393 dir->fsend(_("2901 Job %s not found.\n"), Job);
395 if (cjcr->store_bsock) {
396 cjcr->store_bsock->set_timed_out();
397 cjcr->store_bsock->set_terminated();
398 pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
400 generate_plugin_event(cjcr, bEventCancelCommand, NULL);
401 set_jcr_job_status(cjcr, JS_Canceled);
403 dir->fsend(_("2001 Job %s marked to be canceled.\n"), Job);
406 dir->fsend(_("2902 Error scanning cancel command.\n"));
408 dir->signal(BNET_EOD);
414 * Set debug level as requested by the Director
417 static int setdebug_cmd(JCR *jcr)
419 BSOCK *dir = jcr->dir_bsock;
420 int level, trace_flag;
422 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
423 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
424 pm_strcpy(jcr->errmsg, dir->msg);
425 dir->fsend(_("2991 Bad setdebug command: %s\n"), jcr->errmsg);
429 set_trace(trace_flag);
430 return dir->fsend(OKsetdebug, level);
434 static int estimate_cmd(JCR *jcr)
436 BSOCK *dir = jcr->dir_bsock;
439 if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
440 pm_strcpy(jcr->errmsg, dir->msg);
441 Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
442 dir->fsend(_("2992 Bad estimate command.\n"));
446 dir->fsend(OKest, jcr->num_files_examined,
447 edit_uint64_with_commas(jcr->JobBytes, ed2));
448 dir->signal(BNET_EOD);
453 * Get JobId and Storage Daemon Authorization key from Director
455 static int job_cmd(JCR *jcr)
457 BSOCK *dir = jcr->dir_bsock;
458 POOLMEM *sd_auth_key;
460 sd_auth_key = get_memory(dir->msglen);
461 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
462 &jcr->VolSessionId, &jcr->VolSessionTime,
464 pm_strcpy(jcr->errmsg, dir->msg);
465 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
467 free_pool_memory(sd_auth_key);
470 jcr->sd_auth_key = bstrdup(sd_auth_key);
471 free_pool_memory(sd_auth_key);
472 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
473 Mmsg(jcr->errmsg, "JobId=%d Job=%s", jcr->JobId, jcr->Job);
474 new_plugins(jcr); /* instantiate plugins for this jcr */
475 generate_plugin_event(jcr, bEventJobStart, (void *)jcr->errmsg);
476 return dir->fsend(OKjob, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER);
479 static int runbefore_cmd(JCR *jcr)
482 BSOCK *dir = jcr->dir_bsock;
483 POOLMEM *cmd = get_memory(dir->msglen+1);
486 Dmsg1(100, "runbefore_cmd: %s", dir->msg);
487 if (sscanf(dir->msg, runbefore, cmd) != 1) {
488 pm_strcpy(jcr->errmsg, dir->msg);
489 Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
490 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
496 /* Run the command now */
497 script = new_runscript();
498 script->set_command(cmd);
499 script->when = SCRIPT_Before;
500 ok = script->run(jcr, "ClientRunBeforeJob");
501 free_runscript(script);
505 dir->fsend(OKRunBefore);
508 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
513 static int runbeforenow_cmd(JCR *jcr)
515 BSOCK *dir = jcr->dir_bsock;
517 run_scripts(jcr, jcr->RunScripts, "ClientBeforeJob");
518 if (job_canceled(jcr)) {
519 dir->fsend(_("2905 Bad RunBeforeNow command.\n"));
520 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: FAILED\n");
523 dir->fsend(OKRunBeforeNow);
524 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: OK\n");
529 static int runafter_cmd(JCR *jcr)
531 BSOCK *dir = jcr->dir_bsock;
532 POOLMEM *msg = get_memory(dir->msglen+1);
535 Dmsg1(100, "runafter_cmd: %s", dir->msg);
536 if (sscanf(dir->msg, runafter, msg) != 1) {
537 pm_strcpy(jcr->errmsg, dir->msg);
538 Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
539 dir->fsend(_("2905 Bad RunAfterJob command.\n"));
545 cmd = new_runscript();
546 cmd->set_command(msg);
547 cmd->on_success = true;
548 cmd->on_failure = false;
549 cmd->when = SCRIPT_After;
551 jcr->RunScripts->append(cmd);
553 free_pool_memory(msg);
554 return dir->fsend(OKRunAfter);
557 static int runscript_cmd(JCR *jcr)
559 BSOCK *dir = jcr->dir_bsock;
560 POOLMEM *msg = get_memory(dir->msglen+1);
561 int on_success, on_failure, fail_on_error;
563 RUNSCRIPT *cmd = new_runscript() ;
565 Dmsg1(100, "runscript_cmd: '%s'\n", dir->msg);
566 /* Note, we cannot sscanf into bools */
567 if (sscanf(dir->msg, runscript, &on_success,
572 pm_strcpy(jcr->errmsg, dir->msg);
573 Jmsg1(jcr, M_FATAL, 0, _("Bad RunScript command: %s\n"), jcr->errmsg);
574 dir->fsend(_("2905 Bad RunScript command.\n"));
579 cmd->on_success = on_success;
580 cmd->on_failure = on_failure;
581 cmd->fail_on_error = fail_on_error;
584 cmd->set_command(msg);
586 jcr->RunScripts->append(cmd);
588 free_pool_memory(msg);
589 return dir->fsend(OKRunScript);
593 static bool init_fileset(JCR *jcr)
596 findFILESET *fileset;
605 fileset = (findFILESET *)malloc(sizeof(findFILESET));
606 memset(fileset, 0, sizeof(findFILESET));
607 ff->fileset = fileset;
608 fileset->state = state_none;
609 fileset->include_list.init(1, true);
610 fileset->exclude_list.init(1, true);
614 static findFOPTS *start_options(FF_PKT *ff)
616 int state = ff->fileset->state;
617 findINCEXE *incexe = ff->fileset->incexe;
619 if (state != state_options) {
620 ff->fileset->state = state_options;
621 findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS));
622 memset(fo, 0, sizeof(findFOPTS));
623 fo->regex.init(1, true);
624 fo->regexdir.init(1, true);
625 fo->regexfile.init(1, true);
626 fo->wild.init(1, true);
627 fo->wilddir.init(1, true);
628 fo->wildfile.init(1, true);
629 fo->wildbase.init(1, true);
630 fo->base.init(1, true);
631 fo->fstype.init(1, true);
632 fo->drivetype.init(1, true);
633 incexe->current_opts = fo;
634 incexe->opts_list.append(fo);
636 return incexe->current_opts;
641 * Add fname to include/exclude fileset list. First check for
642 * | and < and if necessary perform command.
644 static void add_file_to_fileset(JCR *jcr, const char *fname, findFILESET *fileset,
659 p++; /* skip over | */
660 fn = get_pool_memory(PM_FNAME);
661 fn = edit_job_codes(jcr, fn, p, "");
662 bpipe = open_bpipe(fn, 0, "r");
665 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
667 free_pool_memory(fn);
670 free_pool_memory(fn);
671 while (fgets(buf, sizeof(buf), bpipe->rfd)) {
672 strip_trailing_junk(buf);
674 fileset->incexe->name_list.append(new_dlistString(buf));
676 fileset->incexe->plugin_list.append(new_dlistString(buf));
679 if ((stat=close_bpipe(bpipe)) != 0) {
681 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. stat=%d: ERR=%s\n"),
682 p, be.code(stat), be.bstrerror(stat));
687 Dmsg1(100, "Doing < of '%s' include on client.\n", p + 1);
688 p++; /* skip over < */
689 if ((ffd = fopen(p, "rb")) == NULL) {
691 Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"),
695 while (fgets(buf, sizeof(buf), ffd)) {
696 strip_trailing_junk(buf);
697 Dmsg1(100, "%s\n", buf);
699 fileset->incexe->name_list.append(new_dlistString(buf));
701 fileset->incexe->plugin_list.append(new_dlistString(buf));
708 fileset->incexe->name_list.append(new_dlistString(fname));
710 fileset->incexe->plugin_list.append(new_dlistString(fname));
717 static void add_fileset(JCR *jcr, const char *item)
719 FF_PKT *ff = jcr->ff;
720 findFILESET *fileset = ff->fileset;
721 int state = fileset->state;
722 findFOPTS *current_opts;
724 /* Get code, optional subcode, and position item past the dividing space */
725 Dmsg1(100, "%s\n", item);
730 int subcode = ' '; /* A space is always a valid subcode */
731 if (item[0] != '\0' && item[0] != ' ') {
739 /* Skip all lines we receive after an error */
740 if (state == state_error) {
741 Dmsg0(100, "State=error return\n");
746 * The switch tests the code for validity.
747 * The subcode is always good if it is a space, otherwise we must confirm.
748 * We set state to state_error first assuming the subcode is invalid,
749 * requiring state to be set in cases below that handle subcodes.
751 if (subcode != ' ') {
753 Dmsg0(100, "Set state=error or double code.\n");
758 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
759 memset(fileset->incexe, 0, sizeof(findINCEXE));
760 fileset->incexe->opts_list.init(1, true);
761 fileset->incexe->name_list.init(); /* for dlist; was 1,true for alist */
762 fileset->incexe->plugin_list.init();
763 fileset->include_list.append(fileset->incexe);
767 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
768 memset(fileset->incexe, 0, sizeof(findINCEXE));
769 fileset->incexe->opts_list.init(1, true);
770 fileset->incexe->name_list.init();
771 fileset->incexe->plugin_list.init();
772 fileset->exclude_list.append(fileset->incexe);
778 /* File item to include or exclude list */
779 state = state_include;
780 add_file_to_fileset(jcr, item, fileset, true);
783 /* Plugin item to include list */
784 state = state_include;
785 add_file_to_fileset(jcr, item, fileset, false);
788 current_opts = start_options(ff);
792 preg = (regex_t *)malloc(sizeof(regex_t));
793 if (current_opts->flags & FO_IGNORECASE) {
794 rc = regcomp(preg, item, REG_EXTENDED|REG_ICASE);
796 rc = regcomp(preg, item, REG_EXTENDED);
799 regerror(rc, preg, prbuf, sizeof(prbuf));
802 Jmsg(jcr, M_FATAL, 0, _("REGEX %s compile error. ERR=%s\n"), item, prbuf);
806 state = state_options;
807 if (subcode == ' ') {
808 current_opts->regex.append(preg);
809 } else if (subcode == 'D') {
810 current_opts->regexdir.append(preg);
811 } else if (subcode == 'F') {
812 current_opts->regexfile.append(preg);
818 current_opts = start_options(ff);
819 current_opts->base.append(bstrdup(item));
820 state = state_options;
823 current_opts = start_options(ff);
824 state = state_options;
825 if (subcode == ' ') {
826 current_opts->fstype.append(bstrdup(item));
827 } else if (subcode == 'D') {
828 current_opts->drivetype.append(bstrdup(item));
834 current_opts = start_options(ff);
835 state = state_options;
836 if (subcode == ' ') {
837 current_opts->wild.append(bstrdup(item));
838 } else if (subcode == 'D') {
839 current_opts->wilddir.append(bstrdup(item));
840 } else if (subcode == 'F') {
841 current_opts->wildfile.append(bstrdup(item));
842 } else if (subcode == 'B') {
843 current_opts->wildbase.append(bstrdup(item));
849 current_opts = start_options(ff);
850 set_options(current_opts, item);
851 state = state_options;
854 current_opts = start_options(ff);
855 current_opts->ignoredir = bstrdup(item);
856 state = state_options;
859 current_opts = start_options(ff);
860 // current_opts->reader = bstrdup(item);
861 state = state_options;
864 current_opts = start_options(ff);
865 // current_opts->writer = bstrdup(item);
866 state = state_options;
869 Jmsg(jcr, M_FATAL, 0, _("Invalid FileSet command: %s\n"), item);
873 ff->fileset->state = state;
876 static bool term_fileset(JCR *jcr)
878 FF_PKT *ff = jcr->ff;
880 #ifdef xxx_DEBUG_CODE
881 findFILESET *fileset = ff->fileset;
884 for (i=0; i<fileset->include_list.size(); i++) {
885 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
887 for (j=0; j<incexe->opts_list.size(); j++) {
888 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
889 for (k=0; k<fo->regex.size(); k++) {
890 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
892 for (k=0; k<fo->regexdir.size(); k++) {
893 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
895 for (k=0; k<fo->regexfile.size(); k++) {
896 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
898 for (k=0; k<fo->wild.size(); k++) {
899 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
901 for (k=0; k<fo->wilddir.size(); k++) {
902 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
904 for (k=0; k<fo->wildfile.size(); k++) {
905 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
907 for (k=0; k<fo->wildbase.size(); k++) {
908 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
910 for (k=0; k<fo->base.size(); k++) {
911 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
913 for (k=0; k<fo->fstype.size(); k++) {
914 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
916 for (k=0; k<fo->drivetype.size(); k++) {
917 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
920 Dmsg1(400, "Z %s\n", fo->ignoredir);
924 foreach_dlist(node, &incexe->name_list) {
925 Dmsg1(400, "F %s\n", node->c_str());
927 foreach_dlist(node, &incexe->plugin_list) {
928 Dmsg1(400, "P %s\n", node->c_str());
931 for (i=0; i<fileset->exclude_list.size(); i++) {
932 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
934 for (j=0; j<incexe->opts_list.size(); j++) {
935 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
936 for (k=0; k<fo->regex.size(); k++) {
937 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
939 for (k=0; k<fo->regexdir.size(); k++) {
940 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
942 for (k=0; k<fo->regexfile.size(); k++) {
943 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
945 for (k=0; k<fo->wild.size(); k++) {
946 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
948 for (k=0; k<fo->wilddir.size(); k++) {
949 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
951 for (k=0; k<fo->wildfile.size(); k++) {
952 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
954 for (k=0; k<fo->wildbase.size(); k++) {
955 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
957 for (k=0; k<fo->base.size(); k++) {
958 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
960 for (k=0; k<fo->fstype.size(); k++) {
961 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
963 for (k=0; k<fo->drivetype.size(); k++) {
964 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
968 foreach_dlist(node, incexe->name_list) {
969 Dmsg1(400, "F %s\n", node->c_str());
971 foreach_dlist(node, &incexe->plugin_list) {
972 Dmsg1(400, "P %s\n", node->c_str());
976 return ff->fileset->state != state_error;
981 * As an optimization, we should do this during
982 * "compile" time in filed/job.c, and keep only a bit mask
983 * and the Verify options.
985 static void set_options(findFOPTS *fo, const char *opts)
991 // Commented out as it is not backward compatible - KES
993 // fo->flags |= FO_IGNORECASE; /* always ignorecase under windows */
996 for (p=opts; *p; p++) {
998 case 'a': /* alway replace */
999 case '0': /* no option */
1002 fo->flags |= FO_EXCLUDE;
1005 fo->flags |= FO_MULTIFS;
1007 case 'h': /* no recursion */
1008 fo->flags |= FO_NO_RECURSION;
1010 case 'H': /* no hard link handling */
1011 fo->flags |= FO_NO_HARDLINK;
1014 fo->flags |= FO_IGNORECASE;
1017 fo->flags |= FO_MD5;
1020 fo->flags |= FO_NOREPLACE;
1022 case 'p': /* use portable data format */
1023 fo->flags |= FO_PORTABLE;
1025 case 'R': /* Resource forks and Finder Info */
1026 fo->flags |= FO_HFSPLUS;
1027 case 'r': /* read fifo */
1028 fo->flags |= FO_READFIFO;
1033 fo->flags |= FO_SHA1;
1038 fo->flags |= FO_SHA256;
1042 fo->flags |= FO_SHA512;
1048 * If 2 or 3 is seen here, SHA2 is not configured, so
1049 * eat the option, and drop back to SHA-1.
1051 if (p[1] == '2' || p[1] == '3') {
1054 fo->flags |= FO_SHA1;
1059 fo->flags |= FO_SPARSE;
1062 fo->flags |= FO_MTIMEONLY;
1065 fo->flags |= FO_KEEPATIME;
1068 fo->flags |= FO_ACL;
1070 case 'V': /* verify options */
1071 /* Copy Verify Options */
1072 for (j=0; *p && *p != ':'; p++) {
1073 fo->VerifyOpts[j] = *p;
1074 if (j < (int)sizeof(fo->VerifyOpts) - 1) {
1078 fo->VerifyOpts[j] = 0;
1080 case 'C': /* accurate options */
1081 /* Copy Accurate Options */
1082 for (j=0; *p && *p != ':'; p++) {
1083 fo->AccurateOpts[j] = *p;
1084 if (j < (int)sizeof(fo->AccurateOpts) - 1) {
1088 fo->AccurateOpts[j] = 0;
1090 case 'P': /* strip path */
1093 for (j=0; *p && *p != ':'; p++) {
1095 if (j < (int)sizeof(strip) - 1) {
1100 fo->strip_path = atoi(strip);
1101 fo->flags |= FO_STRIPPATH;
1102 Dmsg2(100, "strip=%s strip_path=%d\n", strip, fo->strip_path);
1105 fo->flags |= FO_IF_NEWER;
1108 fo->flags |= FO_ENHANCEDWILD;
1110 case 'Z': /* gzip compression */
1111 fo->flags |= FO_GZIP;
1112 fo->GZIP_level = *++p - '0';
1115 fo->flags |= FO_NOATIME;
1118 fo->flags |= FO_CHKCHANGES;
1121 fo->flags |= FO_HONOR_NODUMP;
1124 fo->flags |= FO_XATTR;
1127 Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *p);
1135 * Director is passing his Fileset
1137 static int fileset_cmd(JCR *jcr)
1139 BSOCK *dir = jcr->dir_bsock;
1141 #if defined(WIN32_VSS)
1144 sscanf(dir->msg, "fileset vss=%d", &vss);
1148 if (!init_fileset(jcr)) {
1151 while (dir->recv() >= 0) {
1152 strip_trailing_junk(dir->msg);
1153 Dmsg1(500, "Fileset: %s\n", dir->msg);
1154 add_fileset(jcr, dir->msg);
1156 if (!term_fileset(jcr)) {
1159 return dir->fsend(OKinc);
1162 static void free_bootstrap(JCR *jcr)
1164 if (jcr->RestoreBootstrap) {
1165 unlink(jcr->RestoreBootstrap);
1166 free_pool_memory(jcr->RestoreBootstrap);
1167 jcr->RestoreBootstrap = NULL;
1172 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1173 static uint32_t bsr_uniq = 0;
1176 * The Director sends us the bootstrap file, which
1177 * we will in turn pass to the SD.
1179 static int bootstrap_cmd(JCR *jcr)
1181 BSOCK *dir = jcr->dir_bsock;
1182 POOLMEM *fname = get_pool_memory(PM_FNAME);
1185 free_bootstrap(jcr);
1188 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1189 jcr->Job, bsr_uniq);
1191 Dmsg1(400, "bootstrap=%s\n", fname);
1192 jcr->RestoreBootstrap = fname;
1193 bs = fopen(fname, "a+b"); /* create file */
1196 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
1197 jcr->RestoreBootstrap, be.bstrerror());
1199 * Suck up what he is sending to us so that he will then
1200 * read our error message.
1202 while (dir->recv() >= 0)
1204 free_bootstrap(jcr);
1205 set_jcr_job_status(jcr, JS_ErrorTerminated);
1209 while (dir->recv() >= 0) {
1210 Dmsg1(200, "filed<dird: bootstrap: %s", dir->msg);
1211 fputs(dir->msg, bs);
1215 * Note, do not free the bootstrap yet -- it needs to be
1218 return dir->fsend(OKbootstrap);
1223 * Get backup level from Director
1226 static int level_cmd(JCR *jcr)
1228 BSOCK *dir = jcr->dir_bsock;
1229 POOLMEM *level, *buf = NULL;
1232 level = get_memory(dir->msglen+1);
1233 Dmsg1(100, "level_cmd: %s", dir->msg);
1234 if (strstr(dir->msg, "accurate")) {
1235 jcr->accurate = true;
1237 if (sscanf(dir->msg, "level = %s ", level) != 1) {
1240 /* Base backup requested? */
1241 if (strcmp(level, "base") == 0) {
1242 jcr->set_JobLevel(L_BASE);
1243 /* Full backup requested? */
1244 } else if (strcmp(level, "full") == 0) {
1245 jcr->set_JobLevel(L_FULL);
1246 } else if (strstr(level, "differential")) {
1247 jcr->set_JobLevel(L_DIFFERENTIAL);
1250 } else if (strstr(level, "incremental")) {
1251 jcr->set_JobLevel(L_INCREMENTAL);
1255 * We get his UTC since time, then sync the clocks and correct it
1256 * to agree with our clock.
1258 } else if (strcmp(level, "since_utime") == 0) {
1259 buf = get_memory(dir->msglen+1);
1260 utime_t since_time, adj;
1261 btime_t his_time, bt_start, rt=0, bt_adj=0;
1262 if (jcr->get_JobLevel() == L_NONE) {
1263 jcr->set_JobLevel(L_SINCE); /* if no other job level set, do it now */
1265 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
1266 buf, &mtime_only) != 2) {
1269 since_time = str_to_uint64(buf); /* this is the since time */
1270 Dmsg1(100, "since_time=%lld\n", since_time);
1271 char ed1[50], ed2[50];
1273 * Sync clocks by polling him for the time. We take
1274 * 10 samples of his time throwing out the first two.
1276 for (int i=0; i<10; i++) {
1277 bt_start = get_current_btime();
1278 dir->signal(BNET_BTIME); /* poll for time */
1279 if (dir->recv() <= 0) { /* get response */
1282 if (sscanf(dir->msg, "btime %s", buf) != 1) {
1285 if (i < 2) { /* toss first two results */
1288 his_time = str_to_uint64(buf);
1289 rt = get_current_btime() - bt_start; /* compute round trip time */
1290 Dmsg2(100, "Dirtime=%s FDtime=%s\n", edit_uint64(his_time, ed1),
1291 edit_uint64(bt_start, ed2));
1292 bt_adj += bt_start - his_time - rt/2;
1293 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1296 bt_adj = bt_adj / 8; /* compute average time */
1297 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1298 adj = btime_to_utime(bt_adj);
1299 since_time += adj; /* adjust for clock difference */
1300 /* Don't notify if time within 3 seconds */
1301 if (adj > 3 || adj < -3) {
1303 if (adj > 600 || adj < -600) {
1308 Jmsg(jcr, type, 0, _("DIR and FD clocks differ by %d seconds, FD automatically compensating.\n"), adj);
1310 dir->signal(BNET_EOD);
1312 Dmsg2(100, "adj = %d since_time=%lld\n", (int)adj, since_time);
1313 jcr->incremental = 1; /* set incremental or decremental backup */
1314 jcr->mtime = since_time; /* set since time */
1315 generate_plugin_event(jcr, bEventSince, (void *)(time_t)jcr->mtime);
1317 Jmsg1(jcr, M_FATAL, 0, _("Unknown backup level: %s\n"), level);
1325 generate_plugin_event(jcr, bEventLevel, (void *)jcr->get_JobLevel());
1326 return dir->fsend(OKlevel);
1329 pm_strcpy(jcr->errmsg, dir->msg);
1330 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
1339 * Get session parameters from Director -- this is for a Restore command
1341 static int session_cmd(JCR *jcr)
1343 BSOCK *dir = jcr->dir_bsock;
1345 Dmsg1(100, "SessionCmd: %s", dir->msg);
1346 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
1347 &jcr->VolSessionId, &jcr->VolSessionTime,
1348 &jcr->StartFile, &jcr->EndFile,
1349 &jcr->StartBlock, &jcr->EndBlock) != 7) {
1350 pm_strcpy(jcr->errmsg, dir->msg);
1351 Jmsg(jcr, M_FATAL, 0, _("Bad session command: %s"), jcr->errmsg);
1355 return dir->fsend(OKsession);
1359 * Get address of storage daemon from Director
1362 static int storage_cmd(JCR *jcr)
1364 int stored_port; /* storage daemon port */
1365 int enable_ssl; /* enable ssl to sd */
1366 BSOCK *dir = jcr->dir_bsock;
1367 BSOCK *sd; /* storage daemon bsock */
1369 Dmsg1(100, "StorageCmd: %s", dir->msg);
1370 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
1371 pm_strcpy(jcr->errmsg, dir->msg);
1372 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
1375 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
1376 /* Open command communications with Storage daemon */
1377 /* Try to connect for 1 hour at 10 second intervals */
1378 sd = bnet_connect(jcr, 10, (int)me->SDConnectTimeout, me->heartbeat_interval,
1379 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1);
1381 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
1382 jcr->stored_addr, stored_port);
1383 Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
1384 jcr->stored_addr, stored_port);
1387 Dmsg0(110, "Connection OK to SD.\n");
1389 jcr->store_bsock = sd;
1391 sd->fsend("Hello Start Job %s\n", jcr->Job);
1392 if (!authenticate_storagedaemon(jcr)) {
1393 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
1396 Dmsg0(110, "Authenticated with SD.\n");
1398 /* Send OK to Director */
1399 return dir->fsend(OKstore);
1402 dir->fsend(BADcmd, "storage");
1411 static int backup_cmd(JCR *jcr)
1413 BSOCK *dir = jcr->dir_bsock;
1414 BSOCK *sd = jcr->store_bsock;
1418 #if defined(WIN32_VSS)
1419 // capture state here, if client is backed up by multiple directors
1420 // and one enables vss and the other does not then enable_vss can change
1421 // between here and where its evaluated after the job completes.
1422 jcr->VSS = g_pVSSClient && enable_vss;
1424 /* Run only one at a time */
1429 set_jcr_job_status(jcr, JS_Blocked);
1430 jcr->set_JobType(JT_BACKUP);
1431 Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
1434 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
1435 dir->fsend(BADcmd, "backup");
1439 dir->fsend(OKbackup);
1440 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1443 * Send Append Open Session to Storage daemon
1445 sd->fsend(append_open);
1446 Dmsg1(110, ">stored: %s", sd->msg);
1448 * Expect to receive back the Ticket number
1450 if (bget_msg(sd) >= 0) {
1451 Dmsg1(110, "<stored: %s", sd->msg);
1452 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1453 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
1456 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
1458 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
1463 * Send Append data command to Storage daemon
1465 sd->fsend(append_data, jcr->Ticket);
1466 Dmsg1(110, ">stored: %s", sd->msg);
1469 * Expect to get OK data
1471 Dmsg1(110, "<stored: %s", sd->msg);
1472 if (!response(jcr, sd, OK_data, "Append Data")) {
1476 generate_daemon_event(jcr, "JobStart");
1477 generate_plugin_event(jcr, bEventStartBackupJob);
1479 #if defined(WIN32_VSS)
1480 /* START VSS ON WIN32 */
1482 if (g_pVSSClient->InitializeForBackup()) {
1483 /* tell vss which drives to snapshot */
1484 char szWinDriveLetters[27];
1485 if (get_win32_driveletters(jcr->ff, szWinDriveLetters)) {
1486 Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
1487 if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {
1488 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed.\n"));
1491 /* tell user if snapshot creation of a specific drive failed */
1493 for (i=0; i < (int)strlen(szWinDriveLetters); i++) {
1494 if (islower(szWinDriveLetters[i])) {
1495 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed. VSS support is disabled on this drive.\n"), szWinDriveLetters[i]);
1499 /* inform user about writer states */
1500 for (i=0; i < (int)g_pVSSClient->GetWriterCount(); i++)
1501 if (g_pVSSClient->GetWriterState(i) < 1) {
1502 Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PrepareForBackup): %s\n"), g_pVSSClient->GetWriterInfo(i));
1507 Jmsg(jcr, M_INFO, 0, _("No drive letters found for generating VSS snapshots.\n"));
1511 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled. ERR=%s\n"), be.bstrerror());
1513 run_scripts(jcr, jcr->RunScripts, "ClientAfterVSS");
1518 * Send Files to Storage daemon
1520 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
1521 if (!blast_data_to_storage_daemon(jcr, NULL)) {
1522 set_jcr_job_status(jcr, JS_ErrorTerminated);
1523 bnet_suppress_error_messages(sd, 1);
1524 bget_msg(sd); /* Read final response from append_data */
1525 Dmsg0(110, "Error in blast_data.\n");
1527 set_jcr_job_status(jcr, JS_Terminated);
1529 if (jcr->JobStatus != JS_Terminated) {
1530 bnet_suppress_error_messages(sd, 1);
1531 goto cleanup; /* bail out now */
1534 * Expect to get response to append_data from Storage daemon
1536 if (!response(jcr, sd, OK_append, "Append Data")) {
1537 set_jcr_job_status(jcr, JS_ErrorTerminated);
1542 * Send Append End Data to Storage daemon
1544 sd->fsend(append_end, jcr->Ticket);
1546 if (!response(jcr, sd, OK_end, "Append End")) {
1547 set_jcr_job_status(jcr, JS_ErrorTerminated);
1552 * Send Append Close to Storage daemon
1554 sd->fsend(append_close, jcr->Ticket);
1555 while (bget_msg(sd) >= 0) { /* stop on signal or error */
1556 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
1558 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
1562 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
1565 if (SDJobStatus != JS_Terminated) {
1566 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
1572 #if defined(WIN32_VSS)
1573 /* STOP VSS ON WIN32 */
1574 /* tell vss to close the backup session */
1576 if (g_pVSSClient->CloseBackup()) {
1577 /* inform user about writer states */
1578 for (int i=0; i<(int)g_pVSSClient->GetWriterCount(); i++) {
1579 int msg_type = M_INFO;
1580 if (g_pVSSClient->GetWriterState(i) < 1) {
1581 msg_type = M_WARNING;
1584 Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
1591 generate_plugin_event(jcr, bEventEndBackupJob);
1592 return 0; /* return and stop command loop */
1596 * Do a Verify for Director
1599 static int verify_cmd(JCR *jcr)
1601 BSOCK *dir = jcr->dir_bsock;
1602 BSOCK *sd = jcr->store_bsock;
1605 jcr->set_JobType(JT_VERIFY);
1606 if (sscanf(dir->msg, verifycmd, level) != 1) {
1607 dir->fsend(_("2994 Bad verify command: %s\n"), dir->msg);
1611 if (strcasecmp(level, "init") == 0) {
1612 jcr->set_JobLevel(L_VERIFY_INIT);
1613 } else if (strcasecmp(level, "catalog") == 0){
1614 jcr->set_JobLevel(L_VERIFY_CATALOG);
1615 } else if (strcasecmp(level, "volume") == 0){
1616 jcr->set_JobLevel(L_VERIFY_VOLUME_TO_CATALOG);
1617 } else if (strcasecmp(level, "data") == 0){
1618 jcr->set_JobLevel(L_VERIFY_DATA);
1619 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
1620 jcr->set_JobLevel(L_VERIFY_DISK_TO_CATALOG);
1622 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1626 dir->fsend(OKverify);
1628 generate_daemon_event(jcr, "JobStart");
1629 generate_plugin_event(jcr, bEventLevel, (void *)jcr->get_JobLevel());
1630 generate_plugin_event(jcr, bEventStartVerifyJob);
1632 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1634 switch (jcr->get_JobLevel()) {
1636 case L_VERIFY_CATALOG:
1639 case L_VERIFY_VOLUME_TO_CATALOG:
1640 if (!open_sd_read_session(jcr)) {
1643 start_dir_heartbeat(jcr);
1644 do_verify_volume(jcr);
1645 stop_dir_heartbeat(jcr);
1647 * Send Close session command to Storage daemon
1649 sd->fsend(read_close, jcr->Ticket);
1650 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1652 /* ****FIXME**** check response */
1653 bget_msg(sd); /* get OK */
1655 /* Inform Storage daemon that we are done */
1656 sd->signal(BNET_TERMINATE);
1659 case L_VERIFY_DISK_TO_CATALOG:
1663 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1667 dir->signal(BNET_EOD);
1668 generate_plugin_event(jcr, bEventEndVerifyJob);
1669 return 0; /* return and terminate command loop */
1673 * Do a Restore for Director
1676 static int restore_cmd(JCR *jcr)
1678 BSOCK *dir = jcr->dir_bsock;
1679 BSOCK *sd = jcr->store_bsock;
1681 bool use_regexwhere=false;
1686 * Scan WHERE (base directory for restore) from command
1688 Dmsg0(150, "restore command\n");
1689 /* Pickup where string */
1690 args = get_memory(dir->msglen+1);
1693 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, args) != 3) {
1694 if (sscanf(dir->msg, restorecmdR, &replace, &prefix_links, args) != 3){
1695 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
1696 pm_strcpy(jcr->errmsg, dir->msg);
1697 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
1702 use_regexwhere = true;
1704 /* Turn / into nothing */
1705 if (IsPathSeparator(args[0]) && args[1] == '\0') {
1709 Dmsg2(150, "Got replace %c, where=%s\n", replace, args);
1710 unbash_spaces(args);
1712 if (use_regexwhere) {
1713 jcr->where_bregexp = get_bregexps(args);
1714 if (!jcr->where_bregexp) {
1715 Jmsg(jcr, M_FATAL, 0, _("Bad where regexp. where=%s\n"), args);
1716 free_pool_memory(args);
1720 jcr->where = bstrdup(args);
1723 free_pool_memory(args);
1724 jcr->replace = replace;
1725 jcr->prefix_links = prefix_links;
1727 dir->fsend(OKrestore);
1728 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1730 jcr->set_JobType(JT_RESTORE);
1732 set_jcr_job_status(jcr, JS_Blocked);
1734 if (!open_sd_read_session(jcr)) {
1735 set_jcr_job_status(jcr, JS_ErrorTerminated);
1739 set_jcr_job_status(jcr, JS_Running);
1742 * Do restore of files and data
1744 start_dir_heartbeat(jcr);
1745 generate_daemon_event(jcr, "JobStart");
1746 generate_plugin_event(jcr, bEventStartRestoreJob);
1748 stop_dir_heartbeat(jcr);
1750 set_jcr_job_status(jcr, JS_Terminated);
1751 if (jcr->JobStatus != JS_Terminated) {
1752 bnet_suppress_error_messages(sd, 1);
1756 * Send Close session command to Storage daemon
1758 sd->fsend(read_close, jcr->Ticket);
1759 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1761 bget_msg(sd); /* get OK */
1763 /* Inform Storage daemon that we are done */
1764 sd->signal(BNET_TERMINATE);
1769 set_jcr_job_status(jcr, JS_ErrorTerminated);
1772 Dmsg0(130, "Done in job.c\n");
1773 generate_plugin_event(jcr, bEventEndRestoreJob);
1774 return 0; /* return and terminate command loop */
1777 static int open_sd_read_session(JCR *jcr)
1779 BSOCK *sd = jcr->store_bsock;
1782 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
1785 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
1786 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
1787 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
1789 * Open Read Session with Storage daemon
1791 sd->fsend(read_open, "DummyVolume",
1792 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
1793 jcr->StartBlock, jcr->EndBlock);
1794 Dmsg1(110, ">stored: %s", sd->msg);
1799 if (bget_msg(sd) >= 0) {
1800 Dmsg1(110, "bfiled<stored: %s", sd->msg);
1801 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1802 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
1805 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
1807 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
1811 if (!send_bootstrap_file(jcr)) {
1816 * Start read of data with Storage daemon
1818 sd->fsend(read_data, jcr->Ticket);
1819 Dmsg1(110, ">stored: %s", sd->msg);
1824 if (!response(jcr, sd, OK_data, "Read Data")) {
1831 * Destroy the Job Control Record and associated
1832 * resources (sockets).
1834 static void filed_free_jcr(JCR *jcr)
1836 if (jcr->store_bsock) {
1837 jcr->store_bsock->close();
1839 free_bootstrap(jcr);
1840 if (jcr->last_fname) {
1841 free_pool_memory(jcr->last_fname);
1843 free_runscripts(jcr->RunScripts);
1844 delete jcr->RunScripts;
1846 if (jcr->JobId != 0)
1847 write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
1853 * Get response from Storage daemon to a command we
1854 * sent. Check that the response is OK.
1856 * Returns: 0 on failure
1859 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
1864 if (bget_msg(sd) > 0) {
1865 Dmsg0(110, sd->msg);
1866 if (strcmp(sd->msg, resp) == 0) {
1870 if (job_canceled(jcr)) {
1871 return 0; /* if canceled avoid useless error messages */
1873 if (is_bnet_error(sd)) {
1874 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
1875 cmd, bnet_strerror(sd));
1877 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
1878 cmd, resp, sd->msg);
1883 static int send_bootstrap_file(JCR *jcr)
1887 BSOCK *sd = jcr->store_bsock;
1888 const char *bootstrap = "bootstrap\n";
1891 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
1892 if (!jcr->RestoreBootstrap) {
1895 bs = fopen(jcr->RestoreBootstrap, "rb");
1898 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
1899 jcr->RestoreBootstrap, be.bstrerror());
1900 set_jcr_job_status(jcr, JS_ErrorTerminated);
1903 sd->msglen = pm_strcpy(sd->msg, bootstrap);
1905 while (fgets(buf, sizeof(buf), bs)) {
1906 sd->msglen = Mmsg(sd->msg, "%s", buf);
1909 sd->signal(BNET_EOD);
1911 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
1912 set_jcr_job_status(jcr, JS_ErrorTerminated);
1918 free_bootstrap(jcr);