2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula Director -- fd_cmds.c -- send commands to File daemon
32 * Kern Sibbald, October MM
34 * This routine is run as a separate thread. There may be more
35 * work to be done to make it totally reentrant!!!!
37 * Utility functions for sending info to File Daemon.
38 * These functions are used by both backup and verify.
45 #include "findlib/find.h"
47 const int dbglvl = 400;
49 /* Commands sent to File daemon */
50 static char filesetcmd[] = "fileset%s\n"; /* set full fileset */
51 static char jobcmd[] = "JobId=%s Job=%s SDid=%u SDtime=%u Authorization=%s\n";
52 /* Note, mtime_only is not used here -- implemented as file option */
53 static char levelcmd[] = "level = %s%s mtime_only=%d\n";
54 static char runscript[] = "Run OnSuccess=%u OnFailure=%u AbortOnError=%u When=%u Command=%s\n";
55 static char runbeforenow[]= "RunBeforeNow\n";
57 /* Responses received from File daemon */
58 static char OKinc[] = "2000 OK include\n";
59 static char OKjob[] = "2000 OK Job";
60 static char OKlevel[] = "2000 OK level\n";
61 static char OKRunScript[] = "2000 OK RunScript\n";
62 static char OKRunBeforeNow[] = "2000 OK RunBeforeNow\n";
64 /* Forward referenced functions */
66 /* External functions */
67 extern DIRRES *director;
68 extern int FDConnectTimeout;
74 * Open connection with File daemon.
75 * Try connecting every retry_interval (default 10 sec), and
76 * give up after max_retry_time (default 30 mins).
79 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
86 if (jcr->client->heartbeat_interval) {
87 heart_beat = jcr->client->heartbeat_interval;
89 heart_beat = director->heartbeat_interval;
92 if (!jcr->file_bsock) {
93 fd = bnet_connect(jcr, retry_interval, max_retry_time, heart_beat,
94 _("File daemon"), jcr->client->address,
95 NULL, jcr->client->FDport, verbose);
97 set_jcr_job_status(jcr, JS_ErrorTerminated);
100 Dmsg0(10, "Opened connection with File daemon\n");
102 fd = jcr->file_bsock; /* use existing connection */
104 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
105 jcr->file_bsock = fd;
106 set_jcr_job_status(jcr, JS_Running);
108 if (!authenticate_file_daemon(jcr)) {
109 set_jcr_job_status(jcr, JS_ErrorTerminated);
114 * Now send JobId and authorization key
116 bnet_fsend(fd, jobcmd, edit_int64(jcr->JobId, ed1), jcr->Job, jcr->VolSessionId,
117 jcr->VolSessionTime, jcr->sd_auth_key);
118 if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
119 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
121 Dmsg1(100, ">filed: %s", fd->msg);
122 if (bget_dirmsg(fd) > 0) {
123 Dmsg1(110, "<filed: %s", fd->msg);
124 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
125 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
126 jcr->client->hdr.name, fd->msg);
127 set_jcr_job_status(jcr, JS_ErrorTerminated);
129 } else if (jcr->db) {
131 memset(&cr, 0, sizeof(cr));
132 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
133 cr.AutoPrune = jcr->client->AutoPrune;
134 cr.FileRetention = jcr->client->FileRetention;
135 cr.JobRetention = jcr->client->JobRetention;
136 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
137 if (!db_update_client_record(jcr, jcr->db, &cr)) {
138 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
139 db_strerror(jcr->db));
143 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
145 set_jcr_job_status(jcr, JS_ErrorTerminated);
152 * This subroutine edits the last job start time into a
153 * "since=date/time" buffer that is returned in the
154 * variable since. This is used for display purposes in
155 * the job report. The time in jcr->stime is later
156 * passed to tell the File daemon what to do.
158 void get_level_since_time(JCR *jcr, char *since, int since_len)
164 if (jcr->stime && jcr->stime[0]) {
165 bstrncpy(since, _(", since="), since_len);
166 bstrncat(since, jcr->stime, since_len);
171 jcr->stime = get_pool_memory(PM_MESSAGE);
174 /* Lookup the last FULL backup job to get the time/date for a
175 * differential or incremental save.
177 switch (jcr->JobLevel) {
180 /* Look up start time of last job */
181 jcr->jr.JobId = 0; /* flag for db_find_job_start time */
182 if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
183 /* No job found, so upgrade this one to Full */
184 Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
185 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found in catalog. Doing FULL backup.\n"));
186 bsnprintf(since, since_len, _(" (upgraded from %s)"),
187 level_to_str(jcr->JobLevel));
188 jcr->JobLevel = jcr->jr.JobLevel = L_FULL;
190 if (jcr->job->rerun_failed_levels) {
191 if (db_find_failed_job_since(jcr, jcr->db, &jcr->jr, jcr->stime, JobLevel)) {
192 Jmsg(jcr, M_INFO, 0, _("Prior failed job found in catalog. Upgrading to %s.\n"),
193 level_to_str(JobLevel));
194 bsnprintf(since, since_len, _(" (upgraded from %s)"),
195 level_to_str(jcr->JobLevel));
196 jcr->JobLevel = jcr->jr.JobLevel = JobLevel;
197 jcr->jr.JobId = jcr->JobId;
201 bstrncpy(since, _(", since="), since_len);
202 bstrncat(since, jcr->stime, since_len);
204 jcr->jr.JobId = jcr->JobId;
207 Dmsg2(100, "Level=%c last start time=%s\n", jcr->JobLevel, jcr->stime);
210 static void send_since_time(JCR *jcr)
212 BSOCK *fd = jcr->file_bsock;
216 stime = str_to_utime(jcr->stime);
217 bnet_fsend(fd, levelcmd, NT_("since_utime "), edit_uint64(stime, ed1), 0);
218 while (bget_dirmsg(fd) >= 0) { /* allow him to poll us to sync clocks */
219 Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
225 * Send level command to FD.
226 * Used for backup jobs and estimate command.
228 bool send_level_command(JCR *jcr)
230 BSOCK *fd = jcr->file_bsock;
232 * Send Level command to File daemon
234 switch (jcr->JobLevel) {
236 bnet_fsend(fd, levelcmd, "base", " ", 0);
238 /* L_NONE is the console, sending something off to the FD */
241 bnet_fsend(fd, levelcmd, "full", " ", 0);
244 bnet_fsend(fd, levelcmd, "differential", " ", 0);
245 send_since_time(jcr);
248 bnet_fsend(fd, levelcmd, "incremental", " ", 0);
249 send_since_time(jcr);
253 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
254 jcr->JobLevel, jcr->JobLevel);
257 Dmsg1(120, ">filed: %s", fd->msg);
258 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
265 * Send either an Included or an Excluded list to FD
267 static bool send_fileset(JCR *jcr)
269 FILESET *fileset = jcr->fileset;
270 BSOCK *fd = jcr->file_bsock;
276 num = fileset->num_includes;
278 num = fileset->num_excludes;
280 for (int i=0; i<num; i++) {
290 ie = fileset->include_items[i];
291 bnet_fsend(fd, "I\n");
293 ie = fileset->exclude_items[i];
294 bnet_fsend(fd, "E\n");
296 for (j=0; j<ie->num_opts; j++) {
297 FOPTS *fo = ie->opts_list[j];
298 bnet_fsend(fd, "O %s\n", fo->opts);
300 bool enhanced_wild = false;
301 for (k=0; fo->opts[k]!='\0'; k++) {
302 if (fo->opts[k]=='W') {
303 enhanced_wild = true;
308 for (k=0; k<fo->regex.size(); k++) {
309 bnet_fsend(fd, "R %s\n", fo->regex.get(k));
311 for (k=0; k<fo->regexdir.size(); k++) {
312 bnet_fsend(fd, "RD %s\n", fo->regexdir.get(k));
314 for (k=0; k<fo->regexfile.size(); k++) {
315 bnet_fsend(fd, "RF %s\n", fo->regexfile.get(k));
317 for (k=0; k<fo->wild.size(); k++) {
318 bnet_fsend(fd, "W %s\n", fo->wild.get(k));
320 for (k=0; k<fo->wilddir.size(); k++) {
321 bnet_fsend(fd, "WD %s\n", fo->wilddir.get(k));
323 for (k=0; k<fo->wildfile.size(); k++) {
324 bnet_fsend(fd, "WF %s\n", fo->wildfile.get(k));
326 for (k=0; k<fo->wildbase.size(); k++) {
327 bnet_fsend(fd, "W%c %s\n", enhanced_wild ? 'B' : 'F', fo->wildbase.get(k));
329 for (k=0; k<fo->base.size(); k++) {
330 bnet_fsend(fd, "B %s\n", fo->base.get(k));
332 for (k=0; k<fo->fstype.size(); k++) {
333 bnet_fsend(fd, "X %s\n", fo->fstype.get(k));
335 for (k=0; k<fo->drivetype.size(); k++) {
336 bnet_fsend(fd, "XD %s\n", fo->drivetype.get(k));
339 bnet_fsend(fd, "D %s\n", fo->reader);
342 bnet_fsend(fd, "T %s\n", fo->writer);
344 bnet_fsend(fd, "N\n");
347 for (j=0; j<ie->name_list.size(); j++) {
348 p = (char *)ie->name_list.get(j);
351 p++; /* skip over the | */
352 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
353 bpipe = open_bpipe(fd->msg, 0, "r");
356 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
360 bstrncpy(buf, "F ", sizeof(buf));
361 Dmsg1(500, "Opts=%s\n", buf);
362 optlen = strlen(buf);
363 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
364 fd->msglen = Mmsg(fd->msg, "%s", buf);
365 Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
366 if (!bnet_send(fd)) {
367 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
371 if ((stat=close_bpipe(bpipe)) != 0) {
373 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
374 p, be.bstrerror(stat));
379 p++; /* skip over < */
380 if ((ffd = fopen(p, "rb")) == NULL) {
382 Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
386 bstrncpy(buf, "F ", sizeof(buf));
387 Dmsg1(500, "Opts=%s\n", buf);
388 optlen = strlen(buf);
389 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
390 fd->msglen = Mmsg(fd->msg, "%s", buf);
391 if (!bnet_send(fd)) {
392 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
399 p++; /* skip over \ */
400 /* Note, fall through wanted */
402 pm_strcpy(fd->msg, "F ");
403 fd->msglen = pm_strcat(fd->msg, p);
404 Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
405 if (!bnet_send(fd)) {
406 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
412 bnet_fsend(fd, "N\n");
414 if (!include) { /* If we just did excludes */
415 break; /* all done */
417 include = false; /* Now do excludes */
420 bnet_sig(fd, BNET_EOD); /* end of data */
421 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
427 set_jcr_job_status(jcr, JS_ErrorTerminated);
434 * Send include list to File daemon
436 bool send_include_list(JCR *jcr)
438 BSOCK *fd = jcr->file_bsock;
439 if (jcr->fileset->new_include) {
440 bnet_fsend(fd, filesetcmd, jcr->fileset->enable_vss ? " vss=1" : "");
441 return send_fileset(jcr);
448 * Send exclude list to File daemon
449 * Under the new scheme, the Exclude list
450 * is part of the FileSet sent with the
451 * "include_list" above.
453 bool send_exclude_list(JCR *jcr)
460 * Send bootstrap file if any to the socket given (FD or SD).
461 * This is used for restore, verify VolumeToCatalog, and
464 bool send_bootstrap_file(JCR *jcr, BSOCK *sock)
468 const char *bootstrap = "bootstrap\n";
470 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
471 if (!jcr->RestoreBootstrap) {
474 bs = fopen(jcr->RestoreBootstrap, "rb");
477 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
478 jcr->RestoreBootstrap, be.bstrerror());
479 set_jcr_job_status(jcr, JS_ErrorTerminated);
482 bnet_fsend(sock, bootstrap);
483 while (fgets(buf, sizeof(buf), bs)) {
484 bnet_fsend(sock, "%s", buf);
486 bnet_sig(sock, BNET_EOD);
488 if (jcr->unlink_bsr) {
489 unlink(jcr->RestoreBootstrap);
490 jcr->unlink_bsr = false;
495 /* TODO: drop this with runscript.old_proto in bacula 1.42 */
496 static char runbefore[] = "RunBeforeJob %s\n";
497 static char runafter[] = "RunAfterJob %s\n";
498 static char OKRunBefore[] = "2000 OK RunBefore\n";
499 static char OKRunAfter[] = "2000 OK RunAfter\n";
501 int send_runscript_with_old_proto(JCR *jcr, int when, POOLMEM *msg)
504 Dmsg1(120, "bdird: sending old runcommand to fd '%s'\n",msg);
505 if (when & SCRIPT_Before) {
506 bnet_fsend(jcr->file_bsock, runbefore, msg);
507 ret = response(jcr, jcr->file_bsock, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR);
509 bnet_fsend(jcr->file_bsock, runafter, msg);
510 ret = response(jcr, jcr->file_bsock, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR);
516 * Send RunScripts to File daemon
517 * 1) We send all runscript to FD, they can be executed Before, After, or twice
518 * 2) Then, we send a "RunBeforeNow" command to the FD to tell him to do the
519 * first run_script() call. (ie ClientRunBeforeJob)
521 int send_runscripts_commands(JCR *jcr)
523 POOLMEM *msg = get_pool_memory(PM_FNAME);
524 BSOCK *fd = jcr->file_bsock;
526 bool launch_before_cmd = false;
527 POOLMEM *ehost = get_pool_memory(PM_FNAME);
530 Dmsg0(120, "bdird: sending runscripts to fd\n");
532 foreach_alist(cmd, jcr->job->RunScripts) {
533 if (cmd->can_run_at_level(jcr->JobLevel) && cmd->target) {
534 ehost = edit_job_codes(jcr, ehost, cmd->target, "");
535 Dmsg2(200, "bdird: runscript %s -> %s\n", cmd->target, ehost);
537 if (strcmp(ehost, jcr->client->name()) == 0) {
538 pm_strcpy(msg, cmd->command);
541 Dmsg1(120, "bdird: sending runscripts to fd '%s'\n", cmd->command);
543 /* TODO: remove this with bacula 1.42 */
544 if (cmd->old_proto) {
545 result = send_runscript_with_old_proto(jcr, cmd->when, msg);
548 bnet_fsend(fd, runscript, cmd->on_success,
554 result = response(jcr, fd, OKRunScript, "RunScript", DISPLAY_ERROR);
555 launch_before_cmd = true;
562 /* TODO : we have to play with other client */
565 send command to an other client
571 /* Tell the FD to execute the ClientRunBeforeJob */
572 if (launch_before_cmd) {
573 bnet_fsend(fd, runbeforenow);
574 if (!response(jcr, fd, OKRunBeforeNow, "RunBeforeNow", DISPLAY_ERROR)) {
578 free_pool_memory(msg);
579 free_pool_memory(ehost);
583 Jmsg(jcr, M_FATAL, 0, _("Client \"%s\" RunScript failed.\n"), ehost);
584 free_pool_memory(msg);
585 free_pool_memory(ehost);
592 * Read the attributes from the File daemon for
593 * a Verify job and store them in the catalog.
595 int get_attributes_and_put_in_catalog(JCR *jcr)
600 char digest[MAXSTRING];
602 fd = jcr->file_bsock;
603 jcr->jr.FirstIndex = 1;
605 /* Start transaction allocates jcr->attr and jcr->ar if needed */
606 db_start_transaction(jcr, jcr->db); /* start transaction if not already open */
609 Dmsg0(120, "bdird: waiting to receive file attributes\n");
610 /* Pickup file attributes and digest */
611 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
615 char Digest[MAXSTRING]; /* either Verify opts or MD5/SHA1 digest */
617 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
618 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Digest)) != 3) {
619 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n"
620 "msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
621 set_jcr_job_status(jcr, JS_ErrorTerminated);
625 /* The following three fields were sscanf'ed above so skip them */
626 skip_nonspaces(&p); /* skip FileIndex */
628 skip_nonspaces(&p); /* skip Stream */
630 skip_nonspaces(&p); /* skip Opts_Digest */
631 p++; /* skip space */
632 Dmsg1(dbglvl, "Stream=%d\n", stream);
633 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
634 if (jcr->cached_attribute) {
635 Dmsg3(dbglvl, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname,
637 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
638 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
641 /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
644 *fn++ = *p++; /* copy filename */
646 *fn = *p++; /* term filename and point p to attribs */
647 pm_strcpy(jcr->attr, p); /* save attributes */
649 jcr->FileIndex = file_index;
650 ar->attr = jcr->attr;
651 ar->fname = jcr->fname;
652 ar->FileIndex = file_index;
655 ar->JobId = jcr->JobId;
656 ar->ClientId = jcr->ClientId;
660 ar->DigestType = CRYPTO_DIGEST_NONE;
661 jcr->cached_attribute = true;
663 Dmsg2(dbglvl, "dird<filed: stream=%d %s\n", stream, jcr->fname);
664 Dmsg1(dbglvl, "dird<filed: attr=%s\n", ar->attr);
665 jcr->FileId = ar->FileId;
667 * First, get STREAM_UNIX_ATTRIBUTES and fill ATTR_DBR structure
668 * Next, we CAN have a CRYPTO_DIGEST, so we fill ATTR_DBR with it (or not)
669 * When we get a new STREAM_UNIX_ATTRIBUTES, we known that we can add file to the catalog
670 * At the end, we have to add the last file
672 } else if (crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) {
673 if (jcr->FileIndex != (uint32_t)file_index) {
674 Jmsg3(jcr, M_ERROR, 0, _("%s index %d not same as attributes %d\n"),
675 stream_to_ascii(stream), file_index, jcr->FileIndex);
679 ar->DigestType = crypto_digest_stream_type(stream);
680 db_escape_string(digest, Digest, strlen(Digest));
681 Dmsg4(dbglvl, "stream=%d DigestLen=%d Digest=%s type=%d\n", stream,
682 strlen(digest), digest, ar->DigestType);
684 jcr->jr.JobFiles = jcr->JobFiles = file_index;
685 jcr->jr.LastIndex = file_index;
687 if (is_bnet_error(fd)) {
688 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
692 if (jcr->cached_attribute) {
693 Dmsg3(dbglvl, "Cached attr with digest. Stream=%d fname=%s attr=%s\n", ar->Stream,
694 ar->fname, ar->attr);
695 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
696 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
698 jcr->cached_attribute = false;
700 set_jcr_job_status(jcr, JS_Terminated);