2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2010 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
38 #if defined(WIN32_VSS)
41 static pthread_mutex_t vss_mutex = PTHREAD_MUTEX_INITIALIZER;
42 static int enable_vss = 0;
46 * As Windows saves ACLs as part of the standard backup stream
47 * we just pretend here that is has implicit acl support.
49 #if defined(HAVE_ACL) || defined(HAVE_WIN32)
50 const bool have_acl = true;
52 const bool have_acl = false;
55 #if defined(HAVE_XATTR)
56 const bool have_xattr = true;
58 const bool have_xattr = false;
61 extern CLIENT *me; /* our client resource */
63 /* Imported functions */
64 extern int status_cmd(JCR *jcr);
65 extern int qstatus_cmd(JCR *jcr);
66 extern int accurate_cmd(JCR *jcr);
68 /* Forward referenced functions */
69 static int backup_cmd(JCR *jcr);
70 static int bootstrap_cmd(JCR *jcr);
71 static int cancel_cmd(JCR *jcr);
72 static int setdebug_cmd(JCR *jcr);
73 static int estimate_cmd(JCR *jcr);
74 static int hello_cmd(JCR *jcr);
75 static int job_cmd(JCR *jcr);
76 static int fileset_cmd(JCR *jcr);
77 static int level_cmd(JCR *jcr);
78 static int verify_cmd(JCR *jcr);
79 static int restore_cmd(JCR *jcr);
80 static int end_restore_cmd(JCR *jcr);
81 static int storage_cmd(JCR *jcr);
82 static int session_cmd(JCR *jcr);
83 static int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd);
84 static void filed_free_jcr(JCR *jcr);
85 static int open_sd_read_session(JCR *jcr);
86 static int send_bootstrap_file(JCR *jcr);
87 static int runscript_cmd(JCR *jcr);
88 static int runbefore_cmd(JCR *jcr);
89 static int runafter_cmd(JCR *jcr);
90 static int runbeforenow_cmd(JCR *jcr);
91 static void set_options(findFOPTS *fo, const char *opts);
92 static void set_storage_auth_key(JCR *jcr, char *key);
94 /* Exported functions */
99 int monitoraccess; /* specify if monitors have access to this function */
103 * The following are the recognized commands from the Director.
105 static struct s_cmds cmds[] = {
106 {"backup", backup_cmd, 0},
107 {"cancel", cancel_cmd, 0},
108 {"setdebug=", setdebug_cmd, 0},
109 {"estimate", estimate_cmd, 0},
110 {"Hello", hello_cmd, 1},
111 {"fileset", fileset_cmd, 0},
112 {"JobId=", job_cmd, 0},
113 {"level = ", level_cmd, 0},
114 {"restore", restore_cmd, 0},
115 {"endrestore", end_restore_cmd, 0},
116 {"session", session_cmd, 0},
117 {"status", status_cmd, 1},
118 {".status", qstatus_cmd, 1},
119 {"storage ", storage_cmd, 0},
120 {"verify", verify_cmd, 0},
121 {"bootstrap", bootstrap_cmd, 0},
122 {"RunBeforeNow", runbeforenow_cmd, 0},
123 {"RunBeforeJob", runbefore_cmd, 0},
124 {"RunAfterJob", runafter_cmd, 0},
125 {"Run", runscript_cmd, 0},
126 {"accurate", accurate_cmd, 0},
127 {NULL, NULL} /* list terminator */
130 /* Commands received from director that need scanning */
131 static char jobcmd[] = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
132 static char storaddr[] = "storage address=%s port=%d ssl=%d Authorization=%100s";
133 static char storaddr_v1[] = "storage address=%s port=%d ssl=%d";
134 static char sessioncmd[] = "session %127s %ld %ld %ld %ld %ld %ld\n";
135 static char restorecmd[] = "restore replace=%c prelinks=%d where=%s\n";
136 static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
137 static char restorecmdR[] = "restore replace=%c prelinks=%d regexwhere=%s\n";
138 static char verifycmd[] = "verify level=%30s";
139 static char estimatecmd[] = "estimate listing=%d";
140 static char runbefore[] = "RunBeforeJob %s";
141 static char runafter[] = "RunAfterJob %s";
142 static char runscript[] = "Run OnSuccess=%d OnFailure=%d AbortOnError=%d When=%d Command=%s";
144 /* Responses sent to Director */
145 static char errmsg[] = "2999 Invalid command\n";
146 static char no_auth[] = "2998 No Authorization\n";
147 static char invalid_cmd[] = "2997 Invalid command for a Director with Monitor directive enabled.\n";
148 static char OKinc[] = "2000 OK include\n";
149 static char OKest[] = "2000 OK estimate files=%s bytes=%s\n";
150 static char OKlevel[] = "2000 OK level\n";
151 static char OKbackup[] = "2000 OK backup\n";
152 static char OKbootstrap[] = "2000 OK bootstrap\n";
153 static char OKverify[] = "2000 OK verify\n";
154 static char OKrestore[] = "2000 OK restore\n";
155 static char OKsession[] = "2000 OK session\n";
156 static char OKstore[] = "2000 OK storage\n";
157 static char OKstoreend[] = "2000 OK storage end\n";
158 static char OKjob[] = "2000 OK Job %s (%s) %s,%s,%s";
159 static char OKsetdebug[] = "2000 OK setdebug=%d\n";
160 static char BADjob[] = "2901 Bad Job\n";
161 static char EndJob[] = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s"
162 " JobBytes=%s Errors=%u VSS=%d Encrypt=%d\n";
163 static char OKRunBefore[] = "2000 OK RunBefore\n";
164 static char OKRunBeforeNow[] = "2000 OK RunBeforeNow\n";
165 static char OKRunAfter[] = "2000 OK RunAfter\n";
166 static char OKRunScript[] = "2000 OK RunScript\n";
167 static char BADcmd[] = "2902 Bad %s\n";
170 /* Responses received from Storage Daemon */
171 static char OK_end[] = "3000 OK end\n";
172 static char OK_close[] = "3000 OK close Status = %d\n";
173 static char OK_open[] = "3000 OK open ticket = %d\n";
174 static char OK_data[] = "3000 OK data\n";
175 static char OK_append[] = "3000 OK append data\n";
176 static char OKSDbootstrap[]= "3000 OK bootstrap\n";
179 /* Commands sent to Storage Daemon */
180 static char append_open[] = "append open session\n";
181 static char append_data[] = "append data %d\n";
182 static char append_end[] = "append end session %d\n";
183 static char append_close[] = "append close session %d\n";
184 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
185 static char read_data[] = "read data %d\n";
186 static char read_close[] = "read close session %d\n";
189 * Accept requests from a Director
191 * NOTE! We are running as a separate thread
193 * Send output one line
194 * at a time followed by a zero length transmission.
196 * Return when the connection is terminated or there
199 * Basic task here is:
200 * Authenticate Director (during Hello command).
201 * Accept commands one at a time from the Director
204 * Concerning ClientRunBefore/After, the sequence of events
205 * is rather critical. If they are not done in the right
206 * order one can easily get FD->SD timeouts if the script
209 * The current sequence of events is:
210 * 1. Dir starts job with FD
211 * 2. Dir connects to SD
212 * 3. Dir connects to FD
213 * 4. FD connects to SD
214 * 5. FD gets/runs ClientRunBeforeJob and sends ClientRunAfterJob
215 * 6. Dir sends include/exclude
216 * 7. FD sends data to SD
217 * 8. SD/FD disconnects while SD despools data and attributes (optional)
218 * 9. FD runs ClientRunAfterJob
221 void *handle_client_request(void *dirp)
226 BSOCK *dir = (BSOCK *)dirp;
227 const char jobname[12] = "*Director*";
230 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
231 jcr->dir_bsock = dir;
232 jcr->ff = init_find_files();
233 // save_cwd.save(jcr);
234 jcr->start_time = time(NULL);
235 jcr->RunScripts = New(alist(10, not_owned_by_alist));
236 jcr->last_fname = get_pool_memory(PM_FNAME);
237 jcr->last_fname[0] = 0;
238 jcr->client_name = get_memory(strlen(my_name) + 1);
239 pm_strcpy(jcr->client_name, my_name);
240 bstrncpy(jcr->Job, jobname, sizeof(jobname)); /* dummy */
241 jcr->crypto.pki_sign = me->pki_sign;
242 jcr->crypto.pki_encrypt = me->pki_encrypt;
243 jcr->crypto.pki_keypair = me->pki_keypair;
244 jcr->crypto.pki_signers = me->pki_signers;
245 jcr->crypto.pki_recipients = me->pki_recipients;
247 enable_backup_privileges(NULL, 1 /* ignore_errors */);
249 /**********FIXME******* add command handler error code */
251 for (quit=false; !quit;) {
253 if (dir->recv() < 0) {
254 break; /* connection terminated */
256 dir->msg[dir->msglen] = 0;
257 Dmsg1(100, "<dird: %s", dir->msg);
259 for (i=0; cmds[i].cmd; i++) {
260 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
261 found = true; /* indicate command found */
262 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
264 dir->signal(BNET_EOD);
267 if ((jcr->authenticated) && (!cmds[i].monitoraccess) && (jcr->director->monitor)) {
268 Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
269 dir->fsend(invalid_cmd);
270 dir->signal(BNET_EOD);
273 Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
274 if (!cmds[i].func(jcr)) { /* do command */
275 quit = true; /* error or fully terminated, get out */
276 Dmsg1(100, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
281 if (!found) { /* command not found */
288 /* Inform Storage daemon that we are done */
289 if (jcr->store_bsock) {
290 jcr->store_bsock->signal(BNET_TERMINATE);
293 /* Run the after job */
294 run_scripts(jcr, jcr->RunScripts, "ClientAfterJob");
296 if (jcr->JobId) { /* send EndJob if running a job */
297 char ed1[50], ed2[50];
298 /* Send termination status back to Dir */
299 dir->fsend(EndJob, jcr->JobStatus, jcr->JobFiles,
300 edit_uint64(jcr->ReadBytes, ed1),
301 edit_uint64(jcr->JobBytes, ed2), jcr->JobErrors, jcr->VSS,
302 jcr->crypto.pki_encrypt);
303 Dmsg1(110, "End FD msg: %s\n", dir->msg);
306 generate_daemon_event(jcr, "JobEnd");
307 generate_plugin_event(jcr, bEventJobEnd);
309 dequeue_messages(jcr); /* send any queued messages */
311 /* Inform Director that we are done */
312 dir->signal(BNET_TERMINATE);
314 free_plugins(jcr); /* release instantiated plugins */
316 /* Clean up fileset */
317 FF_PKT *ff = jcr->ff;
318 findFILESET *fileset = ff->fileset;
321 /* Delete FileSet Include lists */
322 for (i=0; i<fileset->include_list.size(); i++) {
323 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
324 for (j=0; j<incexe->opts_list.size(); j++) {
325 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
326 for (k=0; k<fo->regex.size(); k++) {
327 regfree((regex_t *)fo->regex.get(k));
329 for (k=0; k<fo->regexdir.size(); k++) {
330 regfree((regex_t *)fo->regexdir.get(k));
332 for (k=0; k<fo->regexfile.size(); k++) {
333 regfree((regex_t *)fo->regexfile.get(k));
336 fo->regexdir.destroy();
337 fo->regexfile.destroy();
339 fo->wilddir.destroy();
340 fo->wildfile.destroy();
341 fo->wildbase.destroy();
343 fo->fstype.destroy();
344 fo->drivetype.destroy();
346 incexe->opts_list.destroy();
347 incexe->name_list.destroy();
348 incexe->plugin_list.destroy();
349 if (incexe->ignoredir) {
350 free(incexe->ignoredir);
353 fileset->include_list.destroy();
355 /* Delete FileSet Exclude lists */
356 for (i=0; i<fileset->exclude_list.size(); i++) {
357 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
358 for (j=0; j<incexe->opts_list.size(); j++) {
359 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
361 fo->regexdir.destroy();
362 fo->regexfile.destroy();
364 fo->wilddir.destroy();
365 fo->wildfile.destroy();
366 fo->wildbase.destroy();
368 fo->fstype.destroy();
369 fo->drivetype.destroy();
371 incexe->opts_list.destroy();
372 incexe->name_list.destroy();
373 incexe->plugin_list.destroy();
374 if (incexe->ignoredir) {
375 free(incexe->ignoredir);
378 fileset->exclude_list.destroy();
382 Dmsg0(100, "Calling term_find_files\n");
383 term_find_files(jcr->ff);
384 // save_cwd.restore(jcr);
385 // save_cwd.release();
387 Dmsg0(100, "Done with term_find_files\n");
388 free_jcr(jcr); /* destroy JCR record */
389 Dmsg0(100, "Done with free_jcr\n");
395 * Hello from Director he must identify himself and provide his
398 static int hello_cmd(JCR *jcr)
400 Dmsg0(120, "Calling Authenticate\n");
401 if (!authenticate_director(jcr)) {
404 Dmsg0(120, "OK Authenticate\n");
405 jcr->authenticated = true;
412 static int cancel_cmd(JCR *jcr)
414 BSOCK *dir = jcr->dir_bsock;
415 char Job[MAX_NAME_LENGTH];
418 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
419 if (!(cjcr=get_jcr_by_full_name(Job))) {
420 dir->fsend(_("2901 Job %s not found.\n"), Job);
422 if (cjcr->store_bsock) {
423 cjcr->store_bsock->set_timed_out();
424 cjcr->store_bsock->set_terminated();
425 pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
427 generate_plugin_event(cjcr, bEventCancelCommand, NULL);
428 set_jcr_job_status(cjcr, JS_Canceled);
430 dir->fsend(_("2001 Job %s marked to be canceled.\n"), Job);
433 dir->fsend(_("2902 Error scanning cancel command.\n"));
435 dir->signal(BNET_EOD);
441 * Set debug level as requested by the Director
444 static int setdebug_cmd(JCR *jcr)
446 BSOCK *dir = jcr->dir_bsock;
447 int level, trace_flag;
449 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
450 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
451 pm_strcpy(jcr->errmsg, dir->msg);
452 dir->fsend(_("2991 Bad setdebug command: %s\n"), jcr->errmsg);
456 set_trace(trace_flag);
457 return dir->fsend(OKsetdebug, level);
461 static int estimate_cmd(JCR *jcr)
463 BSOCK *dir = jcr->dir_bsock;
464 char ed1[50], ed2[50];
466 if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
467 pm_strcpy(jcr->errmsg, dir->msg);
468 Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
469 dir->fsend(_("2992 Bad estimate command.\n"));
473 dir->fsend(OKest, edit_uint64_with_commas(jcr->num_files_examined, ed1),
474 edit_uint64_with_commas(jcr->JobBytes, ed2));
475 dir->signal(BNET_EOD);
480 * Get JobId and Storage Daemon Authorization key from Director
482 static int job_cmd(JCR *jcr)
484 BSOCK *dir = jcr->dir_bsock;
485 POOL_MEM sd_auth_key(PM_MESSAGE);
486 sd_auth_key.check_size(dir->msglen);
488 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
489 &jcr->VolSessionId, &jcr->VolSessionTime,
490 sd_auth_key.c_str()) != 5) {
491 pm_strcpy(jcr->errmsg, dir->msg);
492 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
496 set_storage_auth_key(jcr, sd_auth_key.c_str());
497 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
498 Mmsg(jcr->errmsg, "JobId=%d Job=%s", jcr->JobId, jcr->Job);
499 new_plugins(jcr); /* instantiate plugins for this jcr */
500 generate_plugin_event(jcr, bEventJobStart, (void *)jcr->errmsg);
501 return dir->fsend(OKjob, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER);
504 static int runbefore_cmd(JCR *jcr)
507 BSOCK *dir = jcr->dir_bsock;
508 POOLMEM *cmd = get_memory(dir->msglen+1);
511 Dmsg1(100, "runbefore_cmd: %s", dir->msg);
512 if (sscanf(dir->msg, runbefore, cmd) != 1) {
513 pm_strcpy(jcr->errmsg, dir->msg);
514 Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
515 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
521 /* Run the command now */
522 script = new_runscript();
523 script->set_command(cmd);
524 script->when = SCRIPT_Before;
525 ok = script->run(jcr, "ClientRunBeforeJob");
526 free_runscript(script);
530 dir->fsend(OKRunBefore);
533 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
538 static int runbeforenow_cmd(JCR *jcr)
540 BSOCK *dir = jcr->dir_bsock;
542 run_scripts(jcr, jcr->RunScripts, "ClientBeforeJob");
543 if (job_canceled(jcr)) {
544 dir->fsend(_("2905 Bad RunBeforeNow command.\n"));
545 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: FAILED\n");
548 dir->fsend(OKRunBeforeNow);
549 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: OK\n");
554 static int runafter_cmd(JCR *jcr)
556 BSOCK *dir = jcr->dir_bsock;
557 POOLMEM *msg = get_memory(dir->msglen+1);
560 Dmsg1(100, "runafter_cmd: %s", dir->msg);
561 if (sscanf(dir->msg, runafter, msg) != 1) {
562 pm_strcpy(jcr->errmsg, dir->msg);
563 Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
564 dir->fsend(_("2905 Bad RunAfterJob command.\n"));
570 cmd = new_runscript();
571 cmd->set_command(msg);
572 cmd->on_success = true;
573 cmd->on_failure = false;
574 cmd->when = SCRIPT_After;
576 jcr->RunScripts->append(cmd);
578 free_pool_memory(msg);
579 return dir->fsend(OKRunAfter);
582 static int runscript_cmd(JCR *jcr)
584 BSOCK *dir = jcr->dir_bsock;
585 POOLMEM *msg = get_memory(dir->msglen+1);
586 int on_success, on_failure, fail_on_error;
588 RUNSCRIPT *cmd = new_runscript() ;
590 Dmsg1(100, "runscript_cmd: '%s'\n", dir->msg);
591 /* Note, we cannot sscanf into bools */
592 if (sscanf(dir->msg, runscript, &on_success,
597 pm_strcpy(jcr->errmsg, dir->msg);
598 Jmsg1(jcr, M_FATAL, 0, _("Bad RunScript command: %s\n"), jcr->errmsg);
599 dir->fsend(_("2905 Bad RunScript command.\n"));
604 cmd->on_success = on_success;
605 cmd->on_failure = on_failure;
606 cmd->fail_on_error = fail_on_error;
609 cmd->set_command(msg);
611 jcr->RunScripts->append(cmd);
613 free_pool_memory(msg);
614 return dir->fsend(OKRunScript);
618 static bool init_fileset(JCR *jcr)
621 findFILESET *fileset;
630 fileset = (findFILESET *)malloc(sizeof(findFILESET));
631 memset(fileset, 0, sizeof(findFILESET));
632 ff->fileset = fileset;
633 fileset->state = state_none;
634 fileset->include_list.init(1, true);
635 fileset->exclude_list.init(1, true);
639 static findFOPTS *start_options(FF_PKT *ff)
641 int state = ff->fileset->state;
642 findINCEXE *incexe = ff->fileset->incexe;
644 if (state != state_options) {
645 ff->fileset->state = state_options;
646 findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS));
647 memset(fo, 0, sizeof(findFOPTS));
648 fo->regex.init(1, true);
649 fo->regexdir.init(1, true);
650 fo->regexfile.init(1, true);
651 fo->wild.init(1, true);
652 fo->wilddir.init(1, true);
653 fo->wildfile.init(1, true);
654 fo->wildbase.init(1, true);
655 fo->base.init(1, true);
656 fo->fstype.init(1, true);
657 fo->drivetype.init(1, true);
658 incexe->current_opts = fo;
659 incexe->opts_list.append(fo);
661 return incexe->current_opts;
666 * Add fname to include/exclude fileset list. First check for
667 * | and < and if necessary perform command.
669 void add_file_to_fileset(JCR *jcr, const char *fname, findFILESET *fileset,
684 p++; /* skip over | */
685 fn = get_pool_memory(PM_FNAME);
686 fn = edit_job_codes(jcr, fn, p, "");
687 bpipe = open_bpipe(fn, 0, "r");
690 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
692 free_pool_memory(fn);
695 free_pool_memory(fn);
696 while (fgets(buf, sizeof(buf), bpipe->rfd)) {
697 strip_trailing_junk(buf);
699 fileset->incexe->name_list.append(new_dlistString(buf));
701 fileset->incexe->plugin_list.append(new_dlistString(buf));
704 if ((stat=close_bpipe(bpipe)) != 0) {
706 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. stat=%d: ERR=%s\n"),
707 p, be.code(stat), be.bstrerror(stat));
712 Dmsg1(100, "Doing < of '%s' include on client.\n", p + 1);
713 p++; /* skip over < */
714 if ((ffd = fopen(p, "rb")) == NULL) {
716 Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"),
720 while (fgets(buf, sizeof(buf), ffd)) {
721 strip_trailing_junk(buf);
722 Dmsg1(100, "%s\n", buf);
724 fileset->incexe->name_list.append(new_dlistString(buf));
726 fileset->incexe->plugin_list.append(new_dlistString(buf));
733 fileset->incexe->name_list.append(new_dlistString(fname));
735 if (me->plugin_directory) {
736 fileset->incexe->plugin_list.append(new_dlistString(fname));
738 Jmsg(jcr, M_FATAL, 0, _("Plugin Directory not defined. Cannot use plugin: \"%\"\n"),
746 findFILESET *new_exclude(JCR *jcr)
748 FF_PKT *ff = jcr->ff;
749 findFILESET *fileset = ff->fileset;
752 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
753 memset(fileset->incexe, 0, sizeof(findINCEXE));
754 fileset->incexe->opts_list.init(1, true);
755 fileset->incexe->name_list.init();
756 fileset->incexe->plugin_list.init();
757 fileset->exclude_list.append(fileset->incexe);
762 static void add_fileset(JCR *jcr, const char *item)
764 FF_PKT *ff = jcr->ff;
765 findFILESET *fileset = ff->fileset;
766 int state = fileset->state;
767 findFOPTS *current_opts;
769 /* Get code, optional subcode, and position item past the dividing space */
770 Dmsg1(100, "%s\n", item);
775 int subcode = ' '; /* A space is always a valid subcode */
776 if (item[0] != '\0' && item[0] != ' ') {
784 /* Skip all lines we receive after an error */
785 if (state == state_error) {
786 Dmsg0(100, "State=error return\n");
791 * The switch tests the code for validity.
792 * The subcode is always good if it is a space, otherwise we must confirm.
793 * We set state to state_error first assuming the subcode is invalid,
794 * requiring state to be set in cases below that handle subcodes.
796 if (subcode != ' ') {
798 Dmsg0(100, "Set state=error or double code.\n");
803 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
804 memset(fileset->incexe, 0, sizeof(findINCEXE));
805 fileset->incexe->opts_list.init(1, true);
806 fileset->incexe->name_list.init(); /* for dlist; was 1,true for alist */
807 fileset->incexe->plugin_list.init();
808 fileset->include_list.append(fileset->incexe);
811 fileset = new_exclude(jcr);
817 /* File item to include or exclude list */
818 state = state_include;
819 add_file_to_fileset(jcr, item, fileset, true);
822 /* Plugin item to include list */
823 state = state_include;
824 add_file_to_fileset(jcr, item, fileset, false);
827 current_opts = start_options(ff);
831 preg = (regex_t *)malloc(sizeof(regex_t));
832 if (current_opts->flags & FO_IGNORECASE) {
833 rc = regcomp(preg, item, REG_EXTENDED|REG_ICASE);
835 rc = regcomp(preg, item, REG_EXTENDED);
838 regerror(rc, preg, prbuf, sizeof(prbuf));
841 Jmsg(jcr, M_FATAL, 0, _("REGEX %s compile error. ERR=%s\n"), item, prbuf);
845 state = state_options;
846 if (subcode == ' ') {
847 current_opts->regex.append(preg);
848 } else if (subcode == 'D') {
849 current_opts->regexdir.append(preg);
850 } else if (subcode == 'F') {
851 current_opts->regexfile.append(preg);
857 current_opts = start_options(ff);
858 current_opts->base.append(bstrdup(item));
859 state = state_options;
862 current_opts = start_options(ff);
863 state = state_options;
864 if (subcode == ' ') {
865 current_opts->fstype.append(bstrdup(item));
866 } else if (subcode == 'D') {
867 current_opts->drivetype.append(bstrdup(item));
873 current_opts = start_options(ff);
874 state = state_options;
875 if (subcode == ' ') {
876 current_opts->wild.append(bstrdup(item));
877 } else if (subcode == 'D') {
878 current_opts->wilddir.append(bstrdup(item));
879 } else if (subcode == 'F') {
880 current_opts->wildfile.append(bstrdup(item));
881 } else if (subcode == 'B') {
882 current_opts->wildbase.append(bstrdup(item));
888 current_opts = start_options(ff);
889 set_options(current_opts, item);
890 state = state_options;
893 state = state_include;
894 fileset->incexe->ignoredir = bstrdup(item);
897 current_opts = start_options(ff);
898 // current_opts->reader = bstrdup(item);
899 state = state_options;
902 current_opts = start_options(ff);
903 // current_opts->writer = bstrdup(item);
904 state = state_options;
907 Jmsg(jcr, M_FATAL, 0, _("Invalid FileSet command: %s\n"), item);
911 ff->fileset->state = state;
914 static bool term_fileset(JCR *jcr)
916 FF_PKT *ff = jcr->ff;
918 #ifdef xxx_DEBUG_CODE
919 findFILESET *fileset = ff->fileset;
922 for (i=0; i<fileset->include_list.size(); i++) {
923 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
925 for (j=0; j<incexe->opts_list.size(); j++) {
926 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
927 for (k=0; k<fo->regex.size(); k++) {
928 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
930 for (k=0; k<fo->regexdir.size(); k++) {
931 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
933 for (k=0; k<fo->regexfile.size(); k++) {
934 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
936 for (k=0; k<fo->wild.size(); k++) {
937 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
939 for (k=0; k<fo->wilddir.size(); k++) {
940 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
942 for (k=0; k<fo->wildfile.size(); k++) {
943 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
945 for (k=0; k<fo->wildbase.size(); k++) {
946 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
948 for (k=0; k<fo->base.size(); k++) {
949 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
951 for (k=0; k<fo->fstype.size(); k++) {
952 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
954 for (k=0; k<fo->drivetype.size(); k++) {
955 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
958 if (incexe->ignoredir) {
959 Dmsg1(400, "Z %s\n", incexe->ignoredir);
962 foreach_dlist(node, &incexe->name_list) {
963 Dmsg1(400, "F %s\n", node->c_str());
965 foreach_dlist(node, &incexe->plugin_list) {
966 Dmsg1(400, "P %s\n", node->c_str());
969 for (i=0; i<fileset->exclude_list.size(); i++) {
970 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
972 for (j=0; j<incexe->opts_list.size(); j++) {
973 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
974 for (k=0; k<fo->regex.size(); k++) {
975 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
977 for (k=0; k<fo->regexdir.size(); k++) {
978 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
980 for (k=0; k<fo->regexfile.size(); k++) {
981 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
983 for (k=0; k<fo->wild.size(); k++) {
984 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
986 for (k=0; k<fo->wilddir.size(); k++) {
987 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
989 for (k=0; k<fo->wildfile.size(); k++) {
990 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
992 for (k=0; k<fo->wildbase.size(); k++) {
993 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
995 for (k=0; k<fo->base.size(); k++) {
996 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
998 for (k=0; k<fo->fstype.size(); k++) {
999 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
1001 for (k=0; k<fo->drivetype.size(); k++) {
1002 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
1006 foreach_dlist(node, incexe->name_list) {
1007 Dmsg1(400, "F %s\n", node->c_str());
1009 foreach_dlist(node, &incexe->plugin_list) {
1010 Dmsg1(400, "P %s\n", node->c_str());
1014 return ff->fileset->state != state_error;
1019 * As an optimization, we should do this during
1020 * "compile" time in filed/job.c, and keep only a bit mask
1021 * and the Verify options.
1023 static void set_options(findFOPTS *fo, const char *opts)
1029 // Commented out as it is not backward compatible - KES
1031 // fo->flags |= FO_IGNORECASE; /* always ignorecase under windows */
1034 for (p=opts; *p; p++) {
1036 case 'a': /* alway replace */
1037 case '0': /* no option */
1040 fo->flags |= FO_EXCLUDE;
1043 fo->flags |= FO_MULTIFS;
1045 case 'h': /* no recursion */
1046 fo->flags |= FO_NO_RECURSION;
1048 case 'H': /* no hard link handling */
1049 fo->flags |= FO_NO_HARDLINK;
1052 fo->flags |= FO_IGNORECASE;
1055 fo->flags |= FO_MD5;
1058 fo->flags |= FO_NOREPLACE;
1060 case 'p': /* use portable data format */
1061 fo->flags |= FO_PORTABLE;
1063 case 'R': /* Resource forks and Finder Info */
1064 fo->flags |= FO_HFSPLUS;
1065 case 'r': /* read fifo */
1066 fo->flags |= FO_READFIFO;
1071 fo->flags |= FO_SHA1;
1076 fo->flags |= FO_SHA256;
1080 fo->flags |= FO_SHA512;
1086 * If 2 or 3 is seen here, SHA2 is not configured, so
1087 * eat the option, and drop back to SHA-1.
1089 if (p[1] == '2' || p[1] == '3') {
1092 fo->flags |= FO_SHA1;
1097 fo->flags |= FO_SPARSE;
1100 fo->flags |= FO_MTIMEONLY;
1103 fo->flags |= FO_KEEPATIME;
1106 fo->flags |= FO_ACL;
1108 case 'V': /* verify options */
1109 /* Copy Verify Options */
1110 for (j=0; *p && *p != ':'; p++) {
1111 fo->VerifyOpts[j] = *p;
1112 if (j < (int)sizeof(fo->VerifyOpts) - 1) {
1116 fo->VerifyOpts[j] = 0;
1118 case 'C': /* accurate options */
1119 /* Copy Accurate Options */
1120 for (j=0; *p && *p != ':'; p++) {
1121 fo->AccurateOpts[j] = *p;
1122 if (j < (int)sizeof(fo->AccurateOpts) - 1) {
1126 fo->AccurateOpts[j] = 0;
1128 case 'J': /* Basejob options */
1129 /* Copy BaseJob Options */
1130 for (j=0; *p && *p != ':'; p++) {
1131 fo->BaseJobOpts[j] = *p;
1132 if (j < (int)sizeof(fo->BaseJobOpts) - 1) {
1136 fo->BaseJobOpts[j] = 0;
1138 case 'P': /* strip path */
1141 for (j=0; *p && *p != ':'; p++) {
1143 if (j < (int)sizeof(strip) - 1) {
1148 fo->strip_path = atoi(strip);
1149 fo->flags |= FO_STRIPPATH;
1150 Dmsg2(100, "strip=%s strip_path=%d\n", strip, fo->strip_path);
1153 fo->flags |= FO_IF_NEWER;
1156 fo->flags |= FO_ENHANCEDWILD;
1158 case 'Z': /* gzip compression */
1159 fo->flags |= FO_GZIP;
1160 fo->GZIP_level = *++p - '0';
1163 fo->flags |= FO_NOATIME;
1166 fo->flags |= FO_CHKCHANGES;
1169 fo->flags |= FO_HONOR_NODUMP;
1172 fo->flags |= FO_XATTR;
1175 Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *p);
1183 * Director is passing his Fileset
1185 static int fileset_cmd(JCR *jcr)
1187 BSOCK *dir = jcr->dir_bsock;
1189 #if defined(WIN32_VSS)
1192 sscanf(dir->msg, "fileset vss=%d", &vss);
1196 if (!init_fileset(jcr)) {
1199 while (dir->recv() >= 0) {
1200 strip_trailing_junk(dir->msg);
1201 Dmsg1(500, "Fileset: %s\n", dir->msg);
1202 add_fileset(jcr, dir->msg);
1204 if (!term_fileset(jcr)) {
1207 return dir->fsend(OKinc);
1210 static void free_bootstrap(JCR *jcr)
1212 if (jcr->RestoreBootstrap) {
1213 unlink(jcr->RestoreBootstrap);
1214 free_pool_memory(jcr->RestoreBootstrap);
1215 jcr->RestoreBootstrap = NULL;
1220 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1221 static uint32_t bsr_uniq = 0;
1224 * The Director sends us the bootstrap file, which
1225 * we will in turn pass to the SD.
1226 * Deprecated. The bsr is now sent directly from the
1227 * Director to the SD.
1229 static int bootstrap_cmd(JCR *jcr)
1231 BSOCK *dir = jcr->dir_bsock;
1232 POOLMEM *fname = get_pool_memory(PM_FNAME);
1235 free_bootstrap(jcr);
1238 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1239 jcr->Job, bsr_uniq);
1241 Dmsg1(400, "bootstrap=%s\n", fname);
1242 jcr->RestoreBootstrap = fname;
1243 bs = fopen(fname, "a+b"); /* create file */
1246 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
1247 jcr->RestoreBootstrap, be.bstrerror());
1249 * Suck up what he is sending to us so that he will then
1250 * read our error message.
1252 while (dir->recv() >= 0)
1254 free_bootstrap(jcr);
1255 set_jcr_job_status(jcr, JS_ErrorTerminated);
1259 while (dir->recv() >= 0) {
1260 Dmsg1(200, "filed<dird: bootstrap: %s", dir->msg);
1261 fputs(dir->msg, bs);
1265 * Note, do not free the bootstrap yet -- it needs to be
1268 return dir->fsend(OKbootstrap);
1273 * Get backup level from Director
1276 static int level_cmd(JCR *jcr)
1278 BSOCK *dir = jcr->dir_bsock;
1279 POOLMEM *level, *buf = NULL;
1282 level = get_memory(dir->msglen+1);
1283 Dmsg1(100, "level_cmd: %s", dir->msg);
1285 /* keep compatibility with older directors */
1286 if (strstr(dir->msg, "accurate")) {
1287 jcr->accurate = true;
1289 if (sscanf(dir->msg, "level = %s ", level) != 1) {
1292 /* Base backup requested? */
1293 if (strcmp(level, "base") == 0) {
1294 jcr->set_JobLevel(L_BASE);
1295 /* Full backup requested? */
1296 } else if (strcmp(level, "full") == 0) {
1297 jcr->set_JobLevel(L_FULL);
1298 } else if (strstr(level, "differential")) {
1299 jcr->set_JobLevel(L_DIFFERENTIAL);
1302 } else if (strstr(level, "incremental")) {
1303 jcr->set_JobLevel(L_INCREMENTAL);
1307 * We get his UTC since time, then sync the clocks and correct it
1308 * to agree with our clock.
1310 } else if (strcmp(level, "since_utime") == 0) {
1311 buf = get_memory(dir->msglen+1);
1312 utime_t since_time, adj;
1313 btime_t his_time, bt_start, rt=0, bt_adj=0;
1314 if (jcr->getJobLevel() == L_NONE) {
1315 jcr->set_JobLevel(L_SINCE); /* if no other job level set, do it now */
1317 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
1318 buf, &mtime_only) != 2) {
1321 since_time = str_to_uint64(buf); /* this is the since time */
1322 Dmsg1(100, "since_time=%lld\n", since_time);
1323 char ed1[50], ed2[50];
1325 * Sync clocks by polling him for the time. We take
1326 * 10 samples of his time throwing out the first two.
1328 for (int i=0; i<10; i++) {
1329 bt_start = get_current_btime();
1330 dir->signal(BNET_BTIME); /* poll for time */
1331 if (dir->recv() <= 0) { /* get response */
1334 if (sscanf(dir->msg, "btime %s", buf) != 1) {
1337 if (i < 2) { /* toss first two results */
1340 his_time = str_to_uint64(buf);
1341 rt = get_current_btime() - bt_start; /* compute round trip time */
1342 Dmsg2(100, "Dirtime=%s FDtime=%s\n", edit_uint64(his_time, ed1),
1343 edit_uint64(bt_start, ed2));
1344 bt_adj += bt_start - his_time - rt/2;
1345 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1348 bt_adj = bt_adj / 8; /* compute average time */
1349 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1350 adj = btime_to_utime(bt_adj);
1351 since_time += adj; /* adjust for clock difference */
1352 /* Don't notify if time within 3 seconds */
1353 if (adj > 3 || adj < -3) {
1355 if (adj > 600 || adj < -600) {
1360 Jmsg(jcr, type, 0, _("DIR and FD clocks differ by %lld seconds, FD automatically compensating.\n"), adj);
1362 dir->signal(BNET_EOD);
1364 Dmsg2(100, "adj=%lld since_time=%lld\n", adj, since_time);
1365 jcr->incremental = 1; /* set incremental or decremental backup */
1366 jcr->mtime = since_time; /* set since time */
1367 generate_plugin_event(jcr, bEventSince, (void *)(time_t)jcr->mtime);
1369 Jmsg1(jcr, M_FATAL, 0, _("Unknown backup level: %s\n"), level);
1377 generate_plugin_event(jcr, bEventLevel, (void *)jcr->getJobLevel());
1378 return dir->fsend(OKlevel);
1381 pm_strcpy(jcr->errmsg, dir->msg);
1382 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
1391 * Get session parameters from Director -- this is for a Restore command
1392 * This is deprecated. It is now passed via the bsr.
1394 static int session_cmd(JCR *jcr)
1396 BSOCK *dir = jcr->dir_bsock;
1398 Dmsg1(100, "SessionCmd: %s", dir->msg);
1399 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
1400 &jcr->VolSessionId, &jcr->VolSessionTime,
1401 &jcr->StartFile, &jcr->EndFile,
1402 &jcr->StartBlock, &jcr->EndBlock) != 7) {
1403 pm_strcpy(jcr->errmsg, dir->msg);
1404 Jmsg(jcr, M_FATAL, 0, _("Bad session command: %s"), jcr->errmsg);
1408 return dir->fsend(OKsession);
1411 static void set_storage_auth_key(JCR *jcr, char *key)
1413 /* if no key don't update anything */
1418 /* We can be contacting multiple storage daemons.
1419 * So, make sure that any old jcr->store_bsock is cleaned up.
1421 if (jcr->store_bsock) {
1422 jcr->store_bsock->destroy();
1423 jcr->store_bsock = NULL;
1427 * We can be contacting multiple storage daemons.
1428 * So, make sure that any old jcr->sd_auth_key is cleaned up.
1430 if (jcr->sd_auth_key) {
1431 /* If we already have a Authorization key, director can do multi
1434 Dmsg0(5, "set multi_restore=true\n");
1435 jcr->multi_restore = true;
1436 bfree(jcr->sd_auth_key);
1439 jcr->sd_auth_key = bstrdup(key);
1443 * Get address of storage daemon from Director
1446 static int storage_cmd(JCR *jcr)
1448 int stored_port; /* storage daemon port */
1449 int enable_ssl; /* enable ssl to sd */
1450 POOL_MEM sd_auth_key(PM_MESSAGE);
1451 BSOCK *dir = jcr->dir_bsock;
1452 BSOCK *sd = new_bsock(); /* storage daemon bsock */
1455 Dmsg1(100, "StorageCmd: %s", dir->msg);
1456 sd_auth_key.check_size(dir->msglen);
1457 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port,
1458 &enable_ssl, sd_auth_key.c_str()) != 4) {
1459 if (sscanf(dir->msg, storaddr_v1, &jcr->stored_addr,
1460 &stored_port, &enable_ssl) != 3) {
1461 pm_strcpy(jcr->errmsg, dir->msg);
1462 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
1467 set_storage_auth_key(jcr, sd_auth_key.c_str());
1469 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port,
1471 /* Open command communications with Storage daemon */
1472 /* Try to connect for 1 hour at 10 second intervals */
1474 sd->set_source_address(me->FDsrc_addr);
1475 if (!sd->connect(jcr, 10, (int)me->SDConnectTimeout, me->heartbeat_interval,
1476 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1)) {
1482 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
1483 jcr->stored_addr, stored_port);
1484 Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
1485 jcr->stored_addr, stored_port);
1488 Dmsg0(110, "Connection OK to SD.\n");
1490 jcr->store_bsock = sd;
1492 sd->fsend("Hello Start Job %s\n", jcr->Job);
1493 if (!authenticate_storagedaemon(jcr)) {
1494 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
1497 Dmsg0(110, "Authenticated with SD.\n");
1499 /* Send OK to Director */
1500 return dir->fsend(OKstore);
1503 dir->fsend(BADcmd, "storage");
1512 static int backup_cmd(JCR *jcr)
1514 BSOCK *dir = jcr->dir_bsock;
1515 BSOCK *sd = jcr->store_bsock;
1519 #if defined(WIN32_VSS)
1520 // capture state here, if client is backed up by multiple directors
1521 // and one enables vss and the other does not then enable_vss can change
1522 // between here and where its evaluated after the job completes.
1523 jcr->VSS = g_pVSSClient && enable_vss;
1525 /* Run only one at a time */
1531 * Validate some options given to the backup make sense for the compiled in
1532 * options of this filed.
1534 if (jcr->ff->flags & FO_ACL && !have_acl) {
1535 Jmsg(jcr, M_FATAL, 0, _("ACL support not configured for your machine.\n"));
1538 if (jcr->ff->flags & FO_XATTR && !have_xattr) {
1539 Jmsg(jcr, M_FATAL, 0, _("XATTR support not configured for your machine.\n"));
1543 set_jcr_job_status(jcr, JS_Blocked);
1544 jcr->set_JobType(JT_BACKUP);
1545 Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
1548 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
1549 dir->fsend(BADcmd, "backup");
1553 dir->fsend(OKbackup);
1554 Dmsg1(110, "filed>dird: %s", dir->msg);
1557 * Send Append Open Session to Storage daemon
1559 sd->fsend(append_open);
1560 Dmsg1(110, ">stored: %s", sd->msg);
1562 * Expect to receive back the Ticket number
1564 if (bget_msg(sd) >= 0) {
1565 Dmsg1(110, "<stored: %s", sd->msg);
1566 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1567 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
1570 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
1572 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
1577 * Send Append data command to Storage daemon
1579 sd->fsend(append_data, jcr->Ticket);
1580 Dmsg1(110, ">stored: %s", sd->msg);
1583 * Expect to get OK data
1585 Dmsg1(110, "<stored: %s", sd->msg);
1586 if (!response(jcr, sd, OK_data, "Append Data")) {
1590 generate_daemon_event(jcr, "JobStart");
1591 generate_plugin_event(jcr, bEventStartBackupJob);
1593 #if defined(WIN32_VSS)
1594 /* START VSS ON WIN32 */
1596 if (g_pVSSClient->InitializeForBackup(jcr)) {
1597 /* tell vss which drives to snapshot */
1598 char szWinDriveLetters[27];
1599 if (get_win32_driveletters(jcr->ff, szWinDriveLetters)) {
1600 Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
1601 if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {
1602 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed.\n"));
1605 /* tell user if snapshot creation of a specific drive failed */
1607 for (i=0; i < (int)strlen(szWinDriveLetters); i++) {
1608 if (islower(szWinDriveLetters[i])) {
1609 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed. VSS support is disabled on this drive.\n"), szWinDriveLetters[i]);
1613 /* inform user about writer states */
1614 for (i=0; i < (int)g_pVSSClient->GetWriterCount(); i++)
1615 if (g_pVSSClient->GetWriterState(i) < 1) {
1616 Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PrepareForBackup): %s\n"), g_pVSSClient->GetWriterInfo(i));
1621 Jmsg(jcr, M_INFO, 0, _("No drive letters found for generating VSS snapshots.\n"));
1625 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled. ERR=%s\n"), be.bstrerror());
1627 run_scripts(jcr, jcr->RunScripts, "ClientAfterVSS");
1632 * Send Files to Storage daemon
1634 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
1635 if (!blast_data_to_storage_daemon(jcr, NULL)) {
1636 set_jcr_job_status(jcr, JS_ErrorTerminated);
1637 bnet_suppress_error_messages(sd, 1);
1638 bget_msg(sd); /* Read final response from append_data */
1639 Dmsg0(110, "Error in blast_data.\n");
1641 set_jcr_job_status(jcr, JS_Terminated);
1642 /* Note, the above set status will not override an error */
1643 if (!(jcr->JobStatus == JS_Terminated || jcr->JobStatus == JS_Warnings)) {
1644 bnet_suppress_error_messages(sd, 1);
1645 goto cleanup; /* bail out now */
1648 * Expect to get response to append_data from Storage daemon
1650 if (!response(jcr, sd, OK_append, "Append Data")) {
1651 set_jcr_job_status(jcr, JS_ErrorTerminated);
1656 * Send Append End Data to Storage daemon
1658 sd->fsend(append_end, jcr->Ticket);
1660 if (!response(jcr, sd, OK_end, "Append End")) {
1661 set_jcr_job_status(jcr, JS_ErrorTerminated);
1666 * Send Append Close to Storage daemon
1668 sd->fsend(append_close, jcr->Ticket);
1669 while (bget_msg(sd) >= 0) { /* stop on signal or error */
1670 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
1672 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
1676 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
1679 if (!(SDJobStatus == JS_Terminated || SDJobStatus == JS_Warnings)) {
1680 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
1686 #if defined(WIN32_VSS)
1687 /* STOP VSS ON WIN32 */
1688 /* tell vss to close the backup session */
1690 if (g_pVSSClient->CloseBackup()) {
1691 /* inform user about writer states */
1692 for (int i=0; i<(int)g_pVSSClient->GetWriterCount(); i++) {
1693 int msg_type = M_INFO;
1694 if (g_pVSSClient->GetWriterState(i) < 1) {
1695 msg_type = M_WARNING;
1698 Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
1701 Win32ConvCleanupCache();
1706 generate_plugin_event(jcr, bEventEndBackupJob);
1707 return 0; /* return and stop command loop */
1711 * Do a Verify for Director
1714 static int verify_cmd(JCR *jcr)
1716 BSOCK *dir = jcr->dir_bsock;
1717 BSOCK *sd = jcr->store_bsock;
1720 jcr->set_JobType(JT_VERIFY);
1721 if (sscanf(dir->msg, verifycmd, level) != 1) {
1722 dir->fsend(_("2994 Bad verify command: %s\n"), dir->msg);
1726 if (strcasecmp(level, "init") == 0) {
1727 jcr->set_JobLevel(L_VERIFY_INIT);
1728 } else if (strcasecmp(level, "catalog") == 0){
1729 jcr->set_JobLevel(L_VERIFY_CATALOG);
1730 } else if (strcasecmp(level, "volume") == 0){
1731 jcr->set_JobLevel(L_VERIFY_VOLUME_TO_CATALOG);
1732 } else if (strcasecmp(level, "data") == 0){
1733 jcr->set_JobLevel(L_VERIFY_DATA);
1734 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
1735 jcr->set_JobLevel(L_VERIFY_DISK_TO_CATALOG);
1737 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1741 dir->fsend(OKverify);
1743 generate_daemon_event(jcr, "JobStart");
1744 generate_plugin_event(jcr, bEventLevel, (void *)jcr->getJobLevel());
1745 generate_plugin_event(jcr, bEventStartVerifyJob);
1747 Dmsg1(110, "filed>dird: %s", dir->msg);
1749 switch (jcr->getJobLevel()) {
1751 case L_VERIFY_CATALOG:
1754 case L_VERIFY_VOLUME_TO_CATALOG:
1755 if (!open_sd_read_session(jcr)) {
1758 start_dir_heartbeat(jcr);
1759 do_verify_volume(jcr);
1760 stop_dir_heartbeat(jcr);
1762 * Send Close session command to Storage daemon
1764 sd->fsend(read_close, jcr->Ticket);
1765 Dmsg1(130, "filed>stored: %s", sd->msg);
1767 /* ****FIXME**** check response */
1768 bget_msg(sd); /* get OK */
1770 /* Inform Storage daemon that we are done */
1771 sd->signal(BNET_TERMINATE);
1774 case L_VERIFY_DISK_TO_CATALOG:
1778 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1782 dir->signal(BNET_EOD);
1783 generate_plugin_event(jcr, bEventEndVerifyJob);
1784 return 0; /* return and terminate command loop */
1788 * Do a Restore for Director
1791 static int restore_cmd(JCR *jcr)
1793 BSOCK *dir = jcr->dir_bsock;
1794 BSOCK *sd = jcr->store_bsock;
1796 bool use_regexwhere=false;
1801 * Scan WHERE (base directory for restore) from command
1803 Dmsg0(150, "restore command\n");
1804 /* Pickup where string */
1805 args = get_memory(dir->msglen+1);
1808 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, args) != 3) {
1809 if (sscanf(dir->msg, restorecmdR, &replace, &prefix_links, args) != 3){
1810 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
1811 pm_strcpy(jcr->errmsg, dir->msg);
1812 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
1817 use_regexwhere = true;
1819 /* Turn / into nothing */
1820 if (IsPathSeparator(args[0]) && args[1] == '\0') {
1824 Dmsg2(150, "Got replace %c, where=%s\n", replace, args);
1825 unbash_spaces(args);
1827 if (use_regexwhere) {
1828 jcr->where_bregexp = get_bregexps(args);
1829 if (!jcr->where_bregexp) {
1830 Jmsg(jcr, M_FATAL, 0, _("Bad where regexp. where=%s\n"), args);
1831 free_pool_memory(args);
1835 jcr->where = bstrdup(args);
1838 free_pool_memory(args);
1839 jcr->replace = replace;
1840 jcr->prefix_links = prefix_links;
1842 dir->fsend(OKrestore);
1843 Dmsg1(110, "filed>dird: %s", dir->msg);
1845 jcr->set_JobType(JT_RESTORE);
1847 set_jcr_job_status(jcr, JS_Blocked);
1849 if (!open_sd_read_session(jcr)) {
1850 set_jcr_job_status(jcr, JS_ErrorTerminated);
1854 set_jcr_job_status(jcr, JS_Running);
1857 * Do restore of files and data
1859 start_dir_heartbeat(jcr);
1860 generate_daemon_event(jcr, "JobStart");
1861 generate_plugin_event(jcr, bEventStartRestoreJob);
1863 stop_dir_heartbeat(jcr);
1865 set_jcr_job_status(jcr, JS_Terminated);
1866 if (jcr->JobStatus != JS_Terminated) {
1867 bnet_suppress_error_messages(sd, 1);
1871 * Send Close session command to Storage daemon
1873 sd->fsend(read_close, jcr->Ticket);
1874 Dmsg1(130, "filed>stored: %s", sd->msg);
1876 bget_msg(sd); /* get OK */
1878 /* Inform Storage daemon that we are done */
1879 sd->signal(BNET_TERMINATE);
1882 bfree_and_null(jcr->where);
1884 if (jcr->JobErrors) {
1885 set_jcr_job_status(jcr, JS_ErrorTerminated);
1888 Dmsg0(130, "Done in job.c\n");
1891 if (jcr->multi_restore) {
1892 dir->fsend(OKstoreend);
1893 ret = 1; /* we continue the loop, waiting for next part */
1895 end_restore_cmd(jcr);
1896 ret = 0; /* we stop here */
1902 static int end_restore_cmd(JCR *jcr)
1904 Dmsg0(5, "end_restore_cmd\n");
1905 generate_plugin_event(jcr, bEventEndRestoreJob);
1906 return 0; /* return and terminate command loop */
1909 static int open_sd_read_session(JCR *jcr)
1911 BSOCK *sd = jcr->store_bsock;
1914 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
1917 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
1918 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
1919 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
1921 * Open Read Session with Storage daemon
1923 sd->fsend(read_open, "DummyVolume",
1924 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
1925 jcr->StartBlock, jcr->EndBlock);
1926 Dmsg1(110, ">stored: %s", sd->msg);
1931 if (bget_msg(sd) >= 0) {
1932 Dmsg1(110, "filed<stored: %s", sd->msg);
1933 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1934 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
1937 Dmsg1(110, "filed: got Ticket=%d\n", jcr->Ticket);
1939 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
1943 if (!send_bootstrap_file(jcr)) {
1948 * Start read of data with Storage daemon
1950 sd->fsend(read_data, jcr->Ticket);
1951 Dmsg1(110, ">stored: %s", sd->msg);
1956 if (!response(jcr, sd, OK_data, "Read Data")) {
1963 * Destroy the Job Control Record and associated
1964 * resources (sockets).
1966 static void filed_free_jcr(JCR *jcr)
1968 if (jcr->store_bsock) {
1969 jcr->store_bsock->close();
1971 free_bootstrap(jcr);
1972 if (jcr->last_fname) {
1973 free_pool_memory(jcr->last_fname);
1975 free_runscripts(jcr->RunScripts);
1976 delete jcr->RunScripts;
1978 if (jcr->JobId != 0)
1979 write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
1985 * Get response from Storage daemon to a command we
1986 * sent. Check that the response is OK.
1988 * Returns: 0 on failure
1991 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
1996 if (bget_msg(sd) > 0) {
1997 Dmsg0(110, sd->msg);
1998 if (strcmp(sd->msg, resp) == 0) {
2002 if (job_canceled(jcr)) {
2003 return 0; /* if canceled avoid useless error messages */
2005 if (is_bnet_error(sd)) {
2006 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
2007 cmd, bnet_strerror(sd));
2009 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
2010 cmd, resp, sd->msg);
2015 static int send_bootstrap_file(JCR *jcr)
2019 BSOCK *sd = jcr->store_bsock;
2020 const char *bootstrap = "bootstrap\n";
2023 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
2024 if (!jcr->RestoreBootstrap) {
2027 bs = fopen(jcr->RestoreBootstrap, "rb");
2030 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
2031 jcr->RestoreBootstrap, be.bstrerror());
2032 set_jcr_job_status(jcr, JS_ErrorTerminated);
2035 sd->msglen = pm_strcpy(sd->msg, bootstrap);
2037 while (fgets(buf, sizeof(buf), bs)) {
2038 sd->msglen = Mmsg(sd->msg, "%s", buf);
2041 sd->signal(BNET_EOD);
2043 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
2044 set_jcr_job_status(jcr, JS_ErrorTerminated);
2050 free_bootstrap(jcr);