2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2009 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.
30 * Bacula Director Job processing routines
32 * Kern Sibbald, October MM
40 /* Forward referenced subroutines */
41 static void *job_thread(void *arg);
42 static void job_monitor_watchdog(watchdog_t *self);
43 static void job_monitor_destructor(watchdog_t *self);
44 static bool job_check_maxwaittime(JCR *jcr);
45 static bool job_check_maxruntime(JCR *jcr);
46 static bool job_check_maxschedruntime(JCR *jcr);
48 /* Imported subroutines */
49 extern void term_scheduler();
50 extern void term_ua_server();
52 /* Imported variables */
56 void init_job_server(int max_workers)
61 if ((stat = jobq_init(&job_queue, max_workers, job_thread)) != 0) {
63 Emsg1(M_ABORT, 0, _("Could not init job queue: ERR=%s\n"), be.bstrerror(stat));
66 wd->callback = job_monitor_watchdog;
67 wd->destructor = job_monitor_destructor;
70 wd->data = new_control_jcr("*JobMonitor*", JT_SYSTEM);
71 register_watchdog(wd);
74 void term_job_server()
76 jobq_destroy(&job_queue); /* ignore any errors */
80 * Run a job -- typically called by the scheduler, but may also
81 * be called by the UA (Console program).
83 * Returns: 0 on failure
87 JobId_t run_job(JCR *jcr)
91 Dmsg0(200, "Add jrc to work queue\n");
92 /* Queue the job to be run */
93 if ((stat = jobq_add(&job_queue, jcr)) != 0) {
95 Jmsg(jcr, M_FATAL, 0, _("Could not add job queue: ERR=%s\n"), be.bstrerror(stat));
103 bool setup_job(JCR *jcr)
108 sm_check(__FILE__, __LINE__, true);
109 init_msg(jcr, jcr->messages);
111 /* Initialize termination condition variable */
112 if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
114 Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
118 jcr->term_wait_inited = true;
120 create_unique_job_name(jcr, jcr->job->name());
121 jcr->setJobStatus(JS_Created);
127 Dmsg0(100, "Open database\n");
128 jcr->db=db_init(jcr, jcr->catalog->db_driver, jcr->catalog->db_name,
129 jcr->catalog->db_user,
130 jcr->catalog->db_password, jcr->catalog->db_address,
131 jcr->catalog->db_port, jcr->catalog->db_socket,
132 jcr->catalog->mult_db_connections);
133 if (!jcr->db || !db_open_database(jcr, jcr->db)) {
134 Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
135 jcr->catalog->db_name);
137 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
138 db_close_database(jcr, jcr->db);
142 Dmsg0(150, "DB opened\n");
144 jcr->comment = get_pool_memory(PM_MESSAGE);
145 *jcr->comment = '\0';
148 jcr->fname = get_pool_memory(PM_FNAME);
150 if (!jcr->pool_source) {
151 jcr->pool_source = get_pool_memory(PM_MESSAGE);
152 pm_strcpy(jcr->pool_source, _("unknown source"));
155 if (jcr->JobReads()) {
156 if (!jcr->rpool_source) {
157 jcr->rpool_source = get_pool_memory(PM_MESSAGE);
158 pm_strcpy(jcr->rpool_source, _("unknown source"));
165 init_jcr_job_record(jcr);
166 if (!get_or_create_client_record(jcr)) {
170 if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
171 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
174 jcr->JobId = jcr->jr.JobId;
175 Dmsg4(100, "Created job record JobId=%d Name=%s Type=%c Level=%c\n",
176 jcr->JobId, jcr->Job, jcr->jr.JobType, jcr->jr.JobLevel);
178 generate_daemon_event(jcr, "JobStart");
179 new_plugins(jcr); /* instantiate plugins for this jcr */
180 generate_plugin_event(jcr, bEventJobStart);
182 if (job_canceled(jcr)) {
186 if (jcr->JobReads() && !jcr->rstorage) {
187 if (jcr->job->storage) {
188 copy_rwstorage(jcr, jcr->job->storage, _("Job resource"));
190 copy_rwstorage(jcr, jcr->job->pool->storage, _("Pool resource"));
193 if (!jcr->JobReads()) {
198 * Now, do pre-run stuff, like setting job level (Inc/diff, ...)
199 * this allows us to setup a proper job start record for restarting
200 * in case of later errors.
202 switch (jcr->getJobType()) {
204 if (!do_backup_init(jcr)) {
205 backup_cleanup(jcr, JS_ErrorTerminated);
210 if (!do_verify_init(jcr)) {
211 verify_cleanup(jcr, JS_ErrorTerminated);
216 if (!do_restore_init(jcr)) {
217 restore_cleanup(jcr, JS_ErrorTerminated);
222 if (!do_admin_init(jcr)) {
223 admin_cleanup(jcr, JS_ErrorTerminated);
229 if (!do_migration_init(jcr)) {
230 migration_cleanup(jcr, JS_ErrorTerminated);
235 Pmsg1(0, _("Unimplemented job type: %d\n"), jcr->getJobType());
236 jcr->setJobStatus(JS_ErrorTerminated);
240 generate_job_event(jcr, "JobInit");
241 generate_plugin_event(jcr, bEventJobInit);
249 void update_job_end(JCR *jcr, int TermCode)
251 dequeue_messages(jcr); /* display any queued messages */
252 jcr->setJobStatus(TermCode);
253 update_job_end_record(jcr);
257 * This is the engine called by jobq.c:jobq_add() when we were pulled
258 * from the work queue.
259 * At this point, we are running in our own thread and all
260 * necessary resources are allocated -- see jobq.c
262 static void *job_thread(void *arg)
264 JCR *jcr = (JCR *)arg;
266 pthread_detach(pthread_self());
269 Dmsg0(200, "=====Start Job=========\n");
270 jcr->setJobStatus(JS_Running); /* this will be set only if no error */
271 jcr->start_time = time(NULL); /* set the real start time */
272 jcr->jr.StartTime = jcr->start_time;
274 if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
275 (utime_t)(jcr->start_time - jcr->sched_time)) {
276 jcr->setJobStatus(JS_Canceled);
277 Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
280 if (job_check_maxschedruntime(jcr)) {
281 jcr->setJobStatus(JS_Canceled);
282 Jmsg(jcr, M_FATAL, 0, _("Job canceled because max sched run time exceeded.\n"));
285 /* TODO : check if it is used somewhere */
286 if (jcr->job->RunScripts == NULL) {
287 Dmsg0(200, "Warning, job->RunScripts is empty\n");
288 jcr->job->RunScripts = New(alist(10, not_owned_by_alist));
291 if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
292 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
295 /* Run any script BeforeJob on dird */
296 run_scripts(jcr, jcr->job->RunScripts, "BeforeJob");
299 * We re-update the job start record so that the start
300 * time is set after the run before job. This avoids
301 * that any files created by the run before job will
302 * be saved twice. They will be backed up in the current
303 * job, but not in the next one unless they are changed.
304 * Without this, they will be backed up in this job and
305 * in the next job run because in that case, their date
306 * is after the start of this run.
308 jcr->start_time = time(NULL);
309 jcr->jr.StartTime = jcr->start_time;
310 if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
311 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
313 generate_job_event(jcr, "JobRun");
314 generate_plugin_event(jcr, bEventJobRun);
316 switch (jcr->getJobType()) {
318 if (!job_canceled(jcr) && do_backup(jcr)) {
321 backup_cleanup(jcr, JS_ErrorTerminated);
325 if (!job_canceled(jcr) && do_verify(jcr)) {
328 verify_cleanup(jcr, JS_ErrorTerminated);
332 if (!job_canceled(jcr) && do_restore(jcr)) {
335 restore_cleanup(jcr, JS_ErrorTerminated);
339 if (!job_canceled(jcr) && do_admin(jcr)) {
342 admin_cleanup(jcr, JS_ErrorTerminated);
347 if (!job_canceled(jcr) && do_migration(jcr)) {
350 migration_cleanup(jcr, JS_ErrorTerminated);
354 Pmsg1(0, _("Unimplemented job type: %d\n"), jcr->getJobType());
358 run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
360 /* Send off any queued messages */
361 if (jcr->msg_queue && jcr->msg_queue->size() > 0) {
362 dequeue_messages(jcr);
365 generate_daemon_event(jcr, "JobEnd");
366 generate_plugin_event(jcr, bEventJobEnd);
367 Dmsg1(50, "======== End Job stat=%c ==========\n", jcr->JobStatus);
368 sm_check(__FILE__, __LINE__, true);
374 * Cancel a job -- typically called by the UA (Console program), but may also
375 * be called by the job watchdog.
377 * Returns: true if cancel appears to be successful
378 * false on failure. Message sent to ua->jcr.
380 bool cancel_job(UAContext *ua, JCR *jcr)
384 int32_t old_status = jcr->JobStatus;
386 jcr->setJobStatus(JS_Canceled);
388 switch (old_status) {
391 case JS_WaitClientRes:
392 case JS_WaitStoreRes:
393 case JS_WaitPriority:
395 case JS_WaitStartTime:
396 ua->info_msg(_("JobId %s, Job %s marked to be canceled.\n"),
397 edit_uint64(jcr->JobId, ed1), jcr->Job);
398 jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
402 /* Cancel File daemon */
403 if (jcr->file_bsock) {
404 ua->jcr->client = jcr->client;
405 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
406 ua->error_msg(_("Failed to connect to File daemon.\n"));
409 Dmsg0(200, "Connected to file daemon\n");
410 fd = ua->jcr->file_bsock;
411 fd->fsend("cancel Job=%s\n", jcr->Job);
412 while (fd->recv() >= 0) {
413 ua->send_msg("%s", fd->msg);
415 fd->signal(BNET_TERMINATE);
417 ua->jcr->file_bsock = NULL;
420 /* Cancel Storage daemon */
421 if (jcr->store_bsock) {
422 if (!ua->jcr->wstorage) {
424 copy_wstorage(ua->jcr, jcr->rstorage, _("Job resource"));
426 copy_wstorage(ua->jcr, jcr->wstorage, _("Job resource"));
431 store.store = jcr->rstore;
433 store.store = jcr->wstore;
435 set_wstorage(ua->jcr, &store);
438 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
439 ua->error_msg(_("Failed to connect to Storage daemon.\n"));
442 Dmsg0(200, "Connected to storage daemon\n");
443 sd = ua->jcr->store_bsock;
444 sd->fsend("cancel Job=%s\n", jcr->Job);
445 while (sd->recv() >= 0) {
446 ua->send_msg("%s", sd->msg);
448 sd->signal(BNET_TERMINATE);
450 ua->jcr->store_bsock = NULL;
458 void cancel_storage_daemon_job(JCR *jcr)
460 if (jcr->sd_canceled) {
461 return; /* cancel only once */
464 UAContext *ua = new_ua_context(jcr);
465 JCR *control_jcr = new_control_jcr("*JobCancel*", JT_SYSTEM);
468 ua->jcr = control_jcr;
469 if (jcr->store_bsock) {
470 if (!ua->jcr->wstorage) {
472 copy_wstorage(ua->jcr, jcr->rstorage, _("Job resource"));
474 copy_wstorage(ua->jcr, jcr->wstorage, _("Job resource"));
479 store.store = jcr->rstore;
481 store.store = jcr->wstore;
483 set_wstorage(ua->jcr, &store);
486 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
489 Dmsg0(200, "Connected to storage daemon\n");
490 sd = ua->jcr->store_bsock;
491 sd->fsend("cancel Job=%s\n", jcr->Job);
492 while (sd->recv() >= 0) {
494 sd->signal(BNET_TERMINATE);
496 ua->jcr->store_bsock = NULL;
497 jcr->sd_canceled = true;
500 free_jcr(control_jcr);
504 static void job_monitor_destructor(watchdog_t *self)
506 JCR *control_jcr = (JCR *)self->data;
508 free_jcr(control_jcr);
511 static void job_monitor_watchdog(watchdog_t *self)
513 JCR *control_jcr, *jcr;
515 control_jcr = (JCR *)self->data;
518 Dmsg1(800, "job_monitor_watchdog %p called\n", self);
523 if (jcr->JobId == 0 || job_canceled(jcr) || jcr->no_maxtime) {
524 Dmsg2(800, "Skipping JCR=%p Job=%s\n", jcr, jcr->Job);
528 /* check MaxWaitTime */
529 if (job_check_maxwaittime(jcr)) {
530 jcr->setJobStatus(JS_Canceled);
531 Qmsg(jcr, M_FATAL, 0, _("Max wait time exceeded. Job canceled.\n"));
533 /* check MaxRunTime */
534 } else if (job_check_maxruntime(jcr)) {
535 jcr->setJobStatus(JS_Canceled);
536 Qmsg(jcr, M_FATAL, 0, _("Max run time exceeded. Job canceled.\n"));
538 /* check MaxRunSchedTime */
539 } else if (job_check_maxschedruntime(jcr)) {
540 jcr->setJobStatus(JS_Canceled);
541 Qmsg(jcr, M_FATAL, 0, _("Max sched run time exceeded. Job canceled.\n"));
546 Dmsg3(800, "Cancelling JCR %p jobid %d (%s)\n", jcr, jcr->JobId, jcr->Job);
547 UAContext *ua = new_ua_context(jcr);
548 ua->jcr = control_jcr;
551 Dmsg2(800, "Have cancelled JCR %p Job=%d\n", jcr, jcr->JobId);
555 /* Keep reference counts correct */
560 * Check if the maxwaittime has expired and it is possible
563 static bool job_check_maxwaittime(JCR *jcr)
569 if (!job_waiting(jcr)) {
573 if (jcr->wait_time) {
574 current = watchdog_time - jcr->wait_time;
577 Dmsg2(200, "check maxwaittime %u >= %u\n",
578 current + jcr->wait_time_sum, job->MaxWaitTime);
579 if (job->MaxWaitTime != 0 &&
580 (current + jcr->wait_time_sum) >= job->MaxWaitTime) {
588 * Check if maxruntime has expired and if the job can be
591 static bool job_check_maxruntime(JCR *jcr)
597 if (job_canceled(jcr) || jcr->JobStatus == JS_Created) {
600 if (jcr->job->MaxRunTime == 0 && job->FullMaxRunTime == 0 &&
601 job->IncMaxRunTime == 0 && job->DiffMaxRunTime == 0) {
604 run_time = watchdog_time - jcr->start_time;
605 Dmsg7(200, "check_maxruntime %llu-%u=%llu >= %llu|%llu|%llu|%llu\n",
606 watchdog_time, jcr->start_time, run_time, job->MaxRunTime, job->FullMaxRunTime,
607 job->IncMaxRunTime, job->DiffMaxRunTime);
609 if (jcr->getJobLevel() == L_FULL && job->FullMaxRunTime != 0 &&
610 run_time >= job->FullMaxRunTime) {
611 Dmsg0(200, "check_maxwaittime: FullMaxcancel\n");
613 } else if (jcr->getJobLevel() == L_DIFFERENTIAL && job->DiffMaxRunTime != 0 &&
614 run_time >= job->DiffMaxRunTime) {
615 Dmsg0(200, "check_maxwaittime: DiffMaxcancel\n");
617 } else if (jcr->getJobLevel() == L_INCREMENTAL && job->IncMaxRunTime != 0 &&
618 run_time >= job->IncMaxRunTime) {
619 Dmsg0(200, "check_maxwaittime: IncMaxcancel\n");
621 } else if (job->MaxRunTime > 0 && run_time >= job->MaxRunTime) {
622 Dmsg0(200, "check_maxwaittime: Maxcancel\n");
630 * Check if MaxRunSchedTime has expired and if the job can be
633 static bool job_check_maxschedruntime(JCR *jcr)
635 if (jcr->job->MaxRunSchedTime == 0 || job_canceled(jcr)) {
638 if ((watchdog_time - jcr->sched_time) < jcr->job->MaxRunSchedTime) {
639 Dmsg3(200, "Job %p (%s) with MaxRunSchedTime %d not expired\n",
640 jcr, jcr->Job, jcr->job->MaxRunSchedTime);
648 * Get or create a Pool record with the given name.
649 * Returns: 0 on error
652 DBId_t get_or_create_pool_record(JCR *jcr, char *pool_name)
656 memset(&pr, 0, sizeof(pr));
657 bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
658 Dmsg1(110, "get_or_create_pool=%s\n", pool_name);
660 while (!db_get_pool_record(jcr, jcr->db, &pr)) { /* get by Name */
661 /* Try to create the pool */
662 if (create_pool(jcr, jcr->db, jcr->pool, POOL_OP_CREATE) < 0) {
663 Jmsg(jcr, M_FATAL, 0, _("Pool \"%s\" not in database. ERR=%s"), pr.Name,
664 db_strerror(jcr->db));
667 Jmsg(jcr, M_INFO, 0, _("Created database record for Pool \"%s\".\n"), pr.Name);
674 * Check for duplicate jobs.
675 * Returns: true if current job should continue
676 * false if current job should terminate
678 bool allow_duplicate_job(JCR *jcr)
681 JCR *djcr; /* possible duplicate */
683 if (job->AllowDuplicateJobs) {
686 if (!job->AllowHigherDuplicates) {
688 if (jcr == djcr || djcr->JobId == 0) {
689 continue; /* do not cancel this job or consoles */
691 if (strcmp(job->name(), djcr->job->name()) == 0) {
692 bool cancel_queued = false;
693 if (job->DuplicateJobProximity > 0) {
694 utime_t now = (utime_t)time(NULL);
695 if ((now - djcr->start_time) > job->DuplicateJobProximity) {
696 continue; /* not really a duplicate */
700 /* If CancelQueuedDuplicates is set do so only if job is queued */
701 if (job->CancelQueuedDuplicates) {
702 switch (djcr->JobStatus) {
705 case JS_WaitClientRes:
706 case JS_WaitStoreRes:
707 case JS_WaitPriority:
709 case JS_WaitStartTime:
710 cancel_queued = true;
716 if (cancel_queued || job->CancelRunningDuplicates) {
717 UAContext *ua = new_ua_context(djcr);
718 Jmsg(jcr, M_INFO, 0, _("Cancelling duplicate JobId=%d.\n"), djcr->JobId);
720 cancel_job(ua, djcr);
722 Dmsg2(800, "Have cancelled JCR %p JobId=%d\n", djcr, djcr->JobId);
724 /* Zap current job */
725 Jmsg(jcr, M_FATAL, 0, _("JobId %d already running. Duplicate job not allowed.\n"),
728 break; /* did our work, get out */
736 void apply_pool_overrides(JCR *jcr)
738 bool pool_override = false;
740 if (jcr->run_pool_override) {
741 pm_strcpy(jcr->pool_source, _("Run pool override"));
744 * Apply any level related Pool selections
746 switch (jcr->getJobLevel()) {
748 if (jcr->full_pool) {
749 jcr->pool = jcr->full_pool;
750 pool_override = true;
751 if (jcr->run_full_pool_override) {
752 pm_strcpy(jcr->pool_source, _("Run FullPool override"));
754 pm_strcpy(jcr->pool_source, _("Job FullPool override"));
760 jcr->pool = jcr->inc_pool;
761 pool_override = true;
762 if (jcr->run_inc_pool_override) {
763 pm_strcpy(jcr->pool_source, _("Run IncPool override"));
765 pm_strcpy(jcr->pool_source, _("Job IncPool override"));
770 if (jcr->diff_pool) {
771 jcr->pool = jcr->diff_pool;
772 pool_override = true;
773 if (jcr->run_diff_pool_override) {
774 pm_strcpy(jcr->pool_source, _("Run DiffPool override"));
776 pm_strcpy(jcr->pool_source, _("Job DiffPool override"));
781 /* Update catalog if pool overridden */
782 if (pool_override && jcr->pool->catalog) {
783 jcr->catalog = jcr->pool->catalog;
784 pm_strcpy(jcr->catalog_source, _("Pool resource"));
790 * Get or create a Client record for this Job
792 bool get_or_create_client_record(JCR *jcr)
796 memset(&cr, 0, sizeof(cr));
797 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
798 cr.AutoPrune = jcr->client->AutoPrune;
799 cr.FileRetention = jcr->client->FileRetention;
800 cr.JobRetention = jcr->client->JobRetention;
801 if (!jcr->client_name) {
802 jcr->client_name = get_pool_memory(PM_NAME);
804 pm_strcpy(jcr->client_name, jcr->client->hdr.name);
805 if (!db_create_client_record(jcr, jcr->db, &cr)) {
806 Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. ERR=%s\n"),
807 db_strerror(jcr->db));
810 jcr->jr.ClientId = cr.ClientId;
812 if (!jcr->client_uname) {
813 jcr->client_uname = get_pool_memory(PM_NAME);
815 pm_strcpy(jcr->client_uname, cr.Uname);
817 Dmsg2(100, "Created Client %s record %d\n", jcr->client->hdr.name,
822 bool get_or_create_fileset_record(JCR *jcr)
826 * Get or Create FileSet record
828 memset(&fsr, 0, sizeof(FILESET_DBR));
829 bstrncpy(fsr.FileSet, jcr->fileset->hdr.name, sizeof(fsr.FileSet));
830 if (jcr->fileset->have_MD5) {
831 struct MD5Context md5c;
832 unsigned char digest[MD5HashSize];
833 memcpy(&md5c, &jcr->fileset->md5c, sizeof(md5c));
834 MD5Final(digest, &md5c);
836 * Keep the flag (last arg) set to false otherwise old FileSets will
837 * get new MD5 sums and the user will get Full backups on everything
839 bin_to_base64(fsr.MD5, sizeof(fsr.MD5), (char *)digest, MD5HashSize, false);
840 bstrncpy(jcr->fileset->MD5, fsr.MD5, sizeof(jcr->fileset->MD5));
842 Jmsg(jcr, M_WARNING, 0, _("FileSet MD5 digest not found.\n"));
844 if (!jcr->fileset->ignore_fs_changes ||
845 !db_get_fileset_record(jcr, jcr->db, &fsr)) {
846 if (!db_create_fileset_record(jcr, jcr->db, &fsr)) {
847 Jmsg(jcr, M_ERROR, 0, _("Could not create FileSet \"%s\" record. ERR=%s\n"),
848 fsr.FileSet, db_strerror(jcr->db));
852 jcr->jr.FileSetId = fsr.FileSetId;
853 bstrncpy(jcr->FSCreateTime, fsr.cCreateTime, sizeof(jcr->FSCreateTime));
854 Dmsg2(119, "Created FileSet %s record %u\n", jcr->fileset->hdr.name,
859 void init_jcr_job_record(JCR *jcr)
861 jcr->jr.SchedTime = jcr->sched_time;
862 jcr->jr.StartTime = jcr->start_time;
863 jcr->jr.EndTime = 0; /* perhaps rescheduled, clear it */
864 jcr->jr.JobType = jcr->getJobType();
865 jcr->jr.JobLevel = jcr->getJobLevel();
866 jcr->jr.JobStatus = jcr->JobStatus;
867 jcr->jr.JobId = jcr->JobId;
868 bstrncpy(jcr->jr.Name, jcr->job->name(), sizeof(jcr->jr.Name));
869 bstrncpy(jcr->jr.Job, jcr->Job, sizeof(jcr->jr.Job));
873 * Write status and such in DB
875 void update_job_end_record(JCR *jcr)
877 jcr->jr.EndTime = time(NULL);
878 jcr->end_time = jcr->jr.EndTime;
879 jcr->jr.JobId = jcr->JobId;
880 jcr->jr.JobStatus = jcr->JobStatus;
881 jcr->jr.JobFiles = jcr->JobFiles;
882 jcr->jr.JobBytes = jcr->JobBytes;
883 jcr->jr.ReadBytes = jcr->ReadBytes;
884 jcr->jr.VolSessionId = jcr->VolSessionId;
885 jcr->jr.VolSessionTime = jcr->VolSessionTime;
886 jcr->jr.JobErrors = jcr->JobErrors;
887 jcr->jr.HasBase = jcr->HasBase;
888 if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
889 Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"),
890 db_strerror(jcr->db));
895 * Takes base_name and appends (unique) current
896 * date and time to form unique job name.
898 * Note, the seconds are actually a sequence number. This
899 * permits us to start a maximum fo 59 unique jobs a second, which
900 * should be sufficient.
902 * Returns: unique job name in jcr->Job
903 * date/time in jcr->start_time
905 void create_unique_job_name(JCR *jcr, const char *base_name)
907 /* Job start mutex */
908 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
909 static time_t last_start_time = 0;
911 time_t now = time(NULL);
913 char dt[MAX_TIME_LENGTH];
914 char name[MAX_NAME_LENGTH];
918 /* Guarantee unique start time -- maximum one per second, and
919 * thus unique Job Name
921 P(mutex); /* lock creation of jobs */
923 if (seq > 59) { /* wrap as if it is seconds */
925 while (now == last_start_time) {
926 bmicrosleep(0, 500000);
930 last_start_time = now;
931 V(mutex); /* allow creation of jobs */
932 jcr->start_time = now;
933 /* Form Unique JobName */
934 (void)localtime_r(&now, &tm);
935 /* Use only characters that are permitted in Windows filenames */
936 strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", &tm);
937 len = strlen(dt) + 5; /* dt + .%02d EOS */
938 bstrncpy(name, base_name, sizeof(name));
939 name[sizeof(name)-len] = 0; /* truncate if too long */
940 bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s_%02d", name, dt, seq); /* add date & time */
941 /* Convert spaces into underscores */
942 for (p=jcr->Job; *p; p++) {
947 Dmsg2(100, "JobId=%u created Job=%s\n", jcr->JobId, jcr->Job);
950 /* Called directly from job rescheduling */
951 void dird_free_jcr_pointers(JCR *jcr)
953 if (jcr->sd_auth_key) {
954 free(jcr->sd_auth_key);
955 jcr->sd_auth_key = NULL;
961 if (jcr->file_bsock) {
962 Dmsg0(200, "Close File bsock\n");
963 bnet_close(jcr->file_bsock);
964 jcr->file_bsock = NULL;
966 if (jcr->store_bsock) {
967 Dmsg0(200, "Close Store bsock\n");
968 bnet_close(jcr->store_bsock);
969 jcr->store_bsock = NULL;
972 free_pool_memory(jcr->comment);
976 Dmsg0(200, "Free JCR fname\n");
977 free_pool_memory(jcr->fname);
980 if (jcr->RestoreBootstrap) {
981 free(jcr->RestoreBootstrap);
982 jcr->RestoreBootstrap = NULL;
984 if (jcr->client_uname) {
985 free_pool_memory(jcr->client_uname);
986 jcr->client_uname = NULL;
989 free_pool_memory(jcr->attr);
999 * Free the Job Control Record if no one is still using it.
1000 * Called from main free_jcr() routine in src/lib/jcr.c so
1001 * that we can do our Director specific cleanup of the jcr.
1003 void dird_free_jcr(JCR *jcr)
1005 Dmsg0(200, "Start dird free_jcr\n");
1007 dird_free_jcr_pointers(jcr);
1008 if (jcr->term_wait_inited) {
1009 pthread_cond_destroy(&jcr->term_wait);
1010 jcr->term_wait_inited = false;
1012 if (jcr->db_batch) {
1013 db_close_database(jcr, jcr->db_batch);
1014 jcr->db_batch = NULL;
1015 jcr->batch_started = false;
1018 db_close_database(jcr, jcr->db);
1022 Dmsg0(200, "Free JCR stime\n");
1023 free_pool_memory(jcr->stime);
1027 Dmsg0(200, "Free JCR fname\n");
1028 free_pool_memory(jcr->fname);
1031 if (jcr->pool_source) {
1032 free_pool_memory(jcr->pool_source);
1033 jcr->pool_source = NULL;
1035 if (jcr->catalog_source) {
1036 free_pool_memory(jcr->catalog_source);
1037 jcr->catalog_source = NULL;
1039 if (jcr->rpool_source) {
1040 free_pool_memory(jcr->rpool_source);
1041 jcr->rpool_source = NULL;
1043 if (jcr->wstore_source) {
1044 free_pool_memory(jcr->wstore_source);
1045 jcr->wstore_source = NULL;
1047 if (jcr->rstore_source) {
1048 free_pool_memory(jcr->rstore_source);
1049 jcr->rstore_source = NULL;
1052 /* Delete lists setup to hold storage pointers */
1053 free_rwstorage(jcr);
1055 jcr->job_end_push.destroy();
1057 if (jcr->JobId != 0)
1058 write_state_file(director->working_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
1060 free_plugins(jcr); /* release instantiated plugins */
1062 Dmsg0(200, "End dird free_jcr\n");
1066 * The Job storage definition must be either in the Job record
1067 * or in the Pool record. The Pool record overrides the Job
1070 void get_job_storage(USTORE *store, JOB *job, RUN *run)
1072 if (run && run->pool && run->pool->storage) {
1073 store->store = (STORE *)run->pool->storage->first();
1074 pm_strcpy(store->store_source, _("Run pool override"));
1077 if (run && run->storage) {
1078 store->store = run->storage;
1079 pm_strcpy(store->store_source, _("Run storage override"));
1082 if (job->pool->storage) {
1083 store->store = (STORE *)job->pool->storage->first();
1084 pm_strcpy(store->store_source, _("Pool resource"));
1086 store->store = (STORE *)job->storage->first();
1087 pm_strcpy(store->store_source, _("Job resource"));
1092 * Set some defaults in the JCR necessary to
1093 * run. These items are pulled from the job
1094 * definition as defaults, but can be overridden
1095 * later either by the Run record in the Schedule resource,
1096 * or by the Console program.
1098 void set_jcr_defaults(JCR *jcr, JOB *job)
1101 jcr->set_JobType(job->JobType);
1102 jcr->JobStatus = JS_Created;
1104 switch (jcr->getJobType()) {
1106 jcr->set_JobLevel(L_NONE);
1109 jcr->set_JobLevel(job->JobLevel);
1114 jcr->fname = get_pool_memory(PM_FNAME);
1116 if (!jcr->pool_source) {
1117 jcr->pool_source = get_pool_memory(PM_MESSAGE);
1118 pm_strcpy(jcr->pool_source, _("unknown source"));
1120 if (!jcr->catalog_source) {
1121 jcr->catalog_source = get_pool_memory(PM_MESSAGE);
1122 pm_strcpy(jcr->catalog_source, _("unknown source"));
1125 jcr->JobPriority = job->Priority;
1126 /* Copy storage definitions -- deleted in dir_free_jcr above */
1128 copy_rwstorage(jcr, job->storage, _("Job resource"));
1130 copy_rwstorage(jcr, job->pool->storage, _("Pool resource"));
1132 jcr->client = job->client;
1133 if (!jcr->client_name) {
1134 jcr->client_name = get_pool_memory(PM_NAME);
1136 pm_strcpy(jcr->client_name, jcr->client->hdr.name);
1137 pm_strcpy(jcr->pool_source, _("Job resource"));
1138 jcr->pool = job->pool;
1139 jcr->full_pool = job->full_pool;
1140 jcr->inc_pool = job->inc_pool;
1141 jcr->diff_pool = job->diff_pool;
1142 if (job->pool->catalog) {
1143 jcr->catalog = job->pool->catalog;
1144 pm_strcpy(jcr->catalog_source, _("Pool resource"));
1146 jcr->catalog = job->client->catalog;
1147 pm_strcpy(jcr->catalog_source, _("Client resource"));
1149 jcr->fileset = job->fileset;
1150 jcr->messages = job->messages;
1151 jcr->spool_data = job->spool_data;
1152 jcr->spool_size = job->spool_size;
1153 jcr->write_part_after_job = job->write_part_after_job;
1154 jcr->accurate = job->accurate;
1155 if (jcr->RestoreBootstrap) {
1156 free(jcr->RestoreBootstrap);
1157 jcr->RestoreBootstrap = NULL;
1159 /* This can be overridden by Console program */
1160 if (job->RestoreBootstrap) {
1161 jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
1163 /* This can be overridden by Console program */
1164 jcr->verify_job = job->verify_job;
1165 /* If no default level given, set one */
1166 if (jcr->getJobLevel() == 0) {
1167 switch (jcr->getJobType()) {
1169 jcr->set_JobLevel(L_VERIFY_CATALOG);
1172 jcr->set_JobLevel(L_INCREMENTAL);
1176 jcr->set_JobLevel(L_NONE);
1179 jcr->set_JobLevel(L_FULL);
1186 * Copy the storage definitions from an alist to the JCR
1188 void copy_rwstorage(JCR *jcr, alist *storage, const char *where)
1190 if (jcr->JobReads()) {
1191 copy_rstorage(jcr, storage, where);
1193 copy_wstorage(jcr, storage, where);
1197 /* Set storage override. Releases any previous storage definition */
1198 void set_rwstorage(JCR *jcr, USTORE *store)
1201 Jmsg(jcr, M_FATAL, 0, _("No storage specified.\n"));
1204 if (jcr->JobReads()) {
1205 set_rstorage(jcr, store);
1207 set_wstorage(jcr, store);
1210 void free_rwstorage(JCR *jcr)
1217 * Copy the storage definitions from an alist to the JCR
1219 void copy_rstorage(JCR *jcr, alist *storage, const char *where)
1223 if (jcr->rstorage) {
1224 delete jcr->rstorage;
1226 jcr->rstorage = New(alist(10, not_owned_by_alist));
1227 foreach_alist(st, storage) {
1228 jcr->rstorage->append(st);
1230 if (!jcr->rstore_source) {
1231 jcr->rstore_source = get_pool_memory(PM_MESSAGE);
1233 pm_strcpy(jcr->rstore_source, where);
1234 if (jcr->rstorage) {
1235 jcr->rstore = (STORE *)jcr->rstorage->first();
1241 /* Set storage override. Remove all previous storage */
1242 void set_rstorage(JCR *jcr, USTORE *store)
1246 if (!store->store) {
1249 if (jcr->rstorage) {
1252 if (!jcr->rstorage) {
1253 jcr->rstorage = New(alist(10, not_owned_by_alist));
1255 jcr->rstore = store->store;
1256 if (!jcr->rstore_source) {
1257 jcr->rstore_source = get_pool_memory(PM_MESSAGE);
1259 pm_strcpy(jcr->rstore_source, store->store_source);
1260 foreach_alist(storage, jcr->rstorage) {
1261 if (store->store == storage) {
1265 /* Store not in list, so add it */
1266 jcr->rstorage->prepend(store->store);
1269 void free_rstorage(JCR *jcr)
1271 if (jcr->rstorage) {
1272 delete jcr->rstorage;
1273 jcr->rstorage = NULL;
1279 * Copy the storage definitions from an alist to the JCR
1281 void copy_wstorage(JCR *jcr, alist *storage, const char *where)
1285 if (jcr->wstorage) {
1286 delete jcr->wstorage;
1288 jcr->wstorage = New(alist(10, not_owned_by_alist));
1289 foreach_alist(st, storage) {
1290 Dmsg1(100, "wstorage=%s\n", st->name());
1291 jcr->wstorage->append(st);
1293 if (!jcr->wstore_source) {
1294 jcr->wstore_source = get_pool_memory(PM_MESSAGE);
1296 pm_strcpy(jcr->wstore_source, where);
1297 if (jcr->wstorage) {
1298 jcr->wstore = (STORE *)jcr->wstorage->first();
1299 Dmsg2(100, "wstore=%s where=%s\n", jcr->wstore->name(), jcr->wstore_source);
1305 /* Set storage override. Remove all previous storage */
1306 void set_wstorage(JCR *jcr, USTORE *store)
1310 if (!store->store) {
1313 if (jcr->wstorage) {
1316 if (!jcr->wstorage) {
1317 jcr->wstorage = New(alist(10, not_owned_by_alist));
1319 jcr->wstore = store->store;
1320 if (!jcr->wstore_source) {
1321 jcr->wstore_source = get_pool_memory(PM_MESSAGE);
1323 pm_strcpy(jcr->wstore_source, store->store_source);
1324 Dmsg2(50, "wstore=%s where=%s\n", jcr->wstore->name(), jcr->wstore_source);
1325 foreach_alist(storage, jcr->wstorage) {
1326 if (store->store == storage) {
1330 /* Store not in list, so add it */
1331 jcr->wstorage->prepend(store->store);
1334 void free_wstorage(JCR *jcr)
1336 if (jcr->wstorage) {
1337 delete jcr->wstorage;
1338 jcr->wstorage = NULL;
1343 char *job_code_callback_clones(JCR *jcr, const char* param)
1345 if (param[0] == 'p') {
1346 return jcr->pool->name();
1351 void create_clones(JCR *jcr)
1354 * Fire off any clone jobs (run directives)
1356 Dmsg2(900, "cloned=%d run_cmds=%p\n", jcr->cloned, jcr->job->run_cmds);
1357 if (!jcr->cloned && jcr->job->run_cmds) {
1359 JOB *job = jcr->job;
1360 POOLMEM *cmd = get_pool_memory(PM_FNAME);
1361 UAContext *ua = new_ua_context(jcr);
1363 foreach_alist(runcmd, job->run_cmds) {
1364 cmd = edit_job_codes(jcr, cmd, runcmd, "", job_code_callback_clones);
1365 Mmsg(ua->cmd, "run %s cloned=yes", cmd);
1366 Dmsg1(900, "=============== Clone cmd=%s\n", ua->cmd);
1367 parse_ua_args(ua); /* parse command */
1368 int stat = run_cmd(ua, ua->cmd);
1370 Jmsg(jcr, M_ERROR, 0, _("Could not start clone job: \"%s\".\n"),
1373 Jmsg(jcr, M_INFO, 0, _("Clone JobId %d started.\n"), stat);
1376 free_ua_context(ua);
1377 free_pool_memory(cmd);
1382 * Given: a JobId in jcr->previous_jr.JobId,
1383 * this subroutine writes a bsr file to restore that job.
1384 * Returns: -1 on error
1385 * number of files if OK
1387 int create_restore_bootstrap_file(JCR *jcr)
1393 memset(&rx, 0, sizeof(rx));
1395 rx.JobIds = (char *)"";
1396 rx.bsr->JobId = jcr->previous_jr.JobId;
1397 ua = new_ua_context(jcr);
1398 if (!complete_bsr(ua, rx.bsr)) {
1402 rx.bsr->fi = new_findex();
1403 rx.bsr->fi->findex = 1;
1404 rx.bsr->fi->findex2 = jcr->previous_jr.JobFiles;
1405 jcr->ExpectedFiles = write_bsr_file(ua, rx);
1406 if (jcr->ExpectedFiles == 0) {
1410 free_ua_context(ua);
1412 jcr->needs_sd = true;
1413 return jcr->ExpectedFiles;
1416 free_ua_context(ua);
1421 /* TODO: redirect command ouput to job log */
1422 bool run_console_command(JCR *jcr, const char *cmd)
1426 JCR *ljcr = new_control_jcr("-RunScript-", JT_CONSOLE);
1427 ua = new_ua_context(ljcr);
1428 /* run from runscript and check if commands are autorized */
1429 ua->runscript = true;
1430 Mmsg(ua->cmd, "%s", cmd);
1431 Dmsg1(100, "Console command: %s\n", ua->cmd);
1433 ok= do_a_command(ua);
1434 free_ua_context(ua);