3 * Bacula Director -- User Agent Commands
5 * Kern Sibbald, September MM
11 Copyright (C) 2000-2004 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 /* Imported subroutines */
35 /* Imported variables */
38 extern struct s_res resources[];
39 extern char my_name[];
40 extern jobq_t job_queue; /* job queue */
43 extern char *list_pool;
45 /* Imported functions */
46 extern int status_cmd(UAContext *ua, const char *cmd);
47 extern int list_cmd(UAContext *ua, const char *cmd);
48 extern int llist_cmd(UAContext *ua, const char *cmd);
49 extern int show_cmd(UAContext *ua, const char *cmd);
50 extern int messagescmd(UAContext *ua, const char *cmd);
51 extern int autodisplay_cmd(UAContext *ua, const char *cmd);
52 extern int gui_cmd(UAContext *ua, const char *cmd);
53 extern int sqlquerycmd(UAContext *ua, const char *cmd);
54 extern int querycmd(UAContext *ua, const char *cmd);
55 extern int run_cmd(UAContext *ua, const char *cmd);
56 extern int retentioncmd(UAContext *ua, const char *cmd);
57 extern int prunecmd(UAContext *ua, const char *cmd);
58 extern int purgecmd(UAContext *ua, const char *cmd);
59 extern int restore_cmd(UAContext *ua, const char *cmd);
60 extern int label_cmd(UAContext *ua, const char *cmd);
61 extern int relabel_cmd(UAContext *ua, const char *cmd);
62 extern int update_slots(UAContext *ua); /* ua_label.c */
64 /* Forward referenced functions */
65 static int add_cmd(UAContext *ua, const char *cmd);
66 static int create_cmd(UAContext *ua, const char *cmd);
67 static int cancel_cmd(UAContext *ua, const char *cmd);
68 static int setdebug_cmd(UAContext *ua, const char *cmd);
69 static int trace_cmd(UAContext *ua, const char *cmd);
70 static int var_cmd(UAContext *ua, const char *cmd);
71 static int estimate_cmd(UAContext *ua, const char *cmd);
72 static int help_cmd(UAContext *ua, const char *cmd);
73 static int delete_cmd(UAContext *ua, const char *cmd);
74 static int use_cmd(UAContext *ua, const char *cmd);
75 static int unmount_cmd(UAContext *ua, const char *cmd);
76 static int version_cmd(UAContext *ua, const char *cmd);
77 static int automount_cmd(UAContext *ua, const char *cmd);
78 static int time_cmd(UAContext *ua, const char *cmd);
79 static int reload_cmd(UAContext *ua, const char *cmd);
80 static int update_volume(UAContext *ua);
81 static int update_pool(UAContext *ua);
82 static int delete_volume(UAContext *ua);
83 static int delete_pool(UAContext *ua);
84 static void delete_job(UAContext *ua);
85 static int mount_cmd(UAContext *ua, const char *cmd);
86 static int release_cmd(UAContext *ua, const char *cmd);
87 static int update_cmd(UAContext *ua, const char *cmd);
88 static int wait_cmd(UAContext *ua, const char *cmd);
89 static int setip_cmd(UAContext *ua, const char *cmd);
90 static int python_cmd(UAContext *ua, const char *cmd);
91 static void do_job_delete(UAContext *ua, JobId_t JobId);
92 static void delete_job_id_range(UAContext *ua, char *tok);
94 int qhelp_cmd(UAContext *ua, const char *cmd);
95 int quit_cmd(UAContext *ua, const char *cmd);
98 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; };
99 static struct cmdstruct commands[] = {
100 { N_("add"), add_cmd, _("add media to a pool")},
101 { N_("autodisplay"), autodisplay_cmd, _("autodisplay [on/off] -- console messages")},
102 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
103 { N_("cancel"), cancel_cmd, _("cancel job=nnn -- cancel a job")},
104 { N_("create"), create_cmd, _("create DB Pool from resource")},
105 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
106 { N_("estimate"), estimate_cmd, _("performs FileSet estimate, listing gives full listing")},
107 { N_("exit"), quit_cmd, _("exit = quit")},
108 { N_("gui"), gui_cmd, _("gui [on/off] -- non-interactive gui mode")},
109 { N_("help"), help_cmd, _("print this command")},
110 { N_("list"), list_cmd, _("list [pools | jobs | jobtotals | media <pool> | files jobid=<nn>]; from catalog")},
111 { N_("label"), label_cmd, _("label a tape")},
112 { N_("llist"), llist_cmd, _("full or long list like list command")},
113 { N_("messages"), messagescmd, _("messages")},
114 { N_("mount"), mount_cmd, _("mount <storage-name>")},
115 { N_("prune"), prunecmd, _("prune expired records from catalog")},
116 { N_("purge"), purgecmd, _("purge records from catalog")},
117 { N_("python"), python_cmd, _("python control commands")},
118 { N_("quit"), quit_cmd, _("quit")},
119 { N_("query"), querycmd, _("query catalog")},
120 { N_("restore"), restore_cmd, _("restore files")},
121 { N_("relabel"), relabel_cmd, _("relabel a tape")},
122 { N_("release"), release_cmd, _("release <storage-name>")},
123 { N_("reload"), reload_cmd, _("reload conf file")},
124 { N_("run"), run_cmd, _("run <job-name>")},
125 { N_("status"), status_cmd, _("status [storage | client]=<name>")},
126 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
127 { N_("setip"), setip_cmd, _("sets new client address -- if authorized")},
128 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
129 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
130 { N_("time"), time_cmd, _("print current time")},
131 { N_("trace"), trace_cmd, _("turn on/off trace to file")},
132 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
133 { N_("umount"), unmount_cmd, _("umount <storage-name> for old-time Unix guys")},
134 { N_("update"), update_cmd, _("update Volume or Pool")},
135 { N_("use"), use_cmd, _("use catalog xxx")},
136 { N_("var"), var_cmd, _("does variable expansion")},
137 { N_("version"), version_cmd, _("print Director version")},
138 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
140 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
143 * Execute a command from the UA
145 int do_a_command(UAContext *ua, const char *cmd)
153 Dmsg1(200, "Command: %s\n", ua->UA_sock->msg);
158 len = strlen(ua->argk[0]);
159 for (i=0; i<comsize; i++) { /* search for command */
160 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
161 if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
164 stat = (*commands[i].func)(ua, cmd); /* go execute command */
170 bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
176 * This is a common routine used to stuff the Pool DB record defaults
177 * into the Media DB record just before creating a media (Volume)
180 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
182 mr->PoolId = pr->PoolId;
183 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
184 mr->Recycle = pr->Recycle;
185 mr->VolRetention = pr->VolRetention;
186 mr->VolUseDuration = pr->VolUseDuration;
187 mr->MaxVolJobs = pr->MaxVolJobs;
188 mr->MaxVolFiles = pr->MaxVolFiles;
189 mr->MaxVolBytes = pr->MaxVolBytes;
194 * Add Volumes to an existing Pool
196 static int add_cmd(UAContext *ua, const char *cmd)
200 int num, i, max, startnum;
202 char name[MAX_NAME_LENGTH];
204 int Slot = 0, InChanger = 0;
207 "You probably don't want to be using this command since it\n"
208 "creates database records without labeling the Volumes.\n"
209 "You probably want to use the \"label\" command.\n\n"));
215 memset(&pr, 0, sizeof(pr));
216 memset(&mr, 0, sizeof(mr));
218 if (!get_pool_dbr(ua, &pr)) {
222 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
223 pr.MaxVols, pr.PoolType);
225 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
226 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
228 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
231 pr.MaxVols = ua->pint32_val;
236 if ((store = get_storage_resource(ua, 0)) != NULL) {
237 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
238 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
242 if (pr.MaxVols == 0) {
245 max = pr.MaxVols - pr.NumVols;
249 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
250 if (!get_pint(ua, buf)) {
253 num = ua->pint32_val;
254 if (num < 0 || num > max) {
255 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
262 if (!get_cmd(ua, _("Enter Volume name: "))) {
266 if (!get_cmd(ua, _("Enter base volume name: "))) {
270 /* Don't allow | in Volume name because it is the volume separator character */
271 if (!is_volume_name_legal(ua, ua->cmd)) {
274 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
275 bsendmsg(ua, _("Volume name too long.\n"));
278 if (strlen(ua->cmd) == 0) {
279 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
283 bstrncpy(name, ua->cmd, sizeof(name));
285 strcat(name, "%04d");
288 if (!get_pint(ua, _("Enter the starting number: "))) {
291 startnum = ua->pint32_val;
293 bsendmsg(ua, _("Start number must be greater than zero.\n"));
303 if (store && store->autochanger) {
304 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
307 Slot = ua->pint32_val;
308 if (!get_yesno(ua, _("InChanger? yes/no: "))) {
311 InChanger = ua->pint32_val;
314 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
315 for (i=startnum; i < num+startnum; i++) {
316 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
318 mr.InChanger = InChanger;
319 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
320 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
321 bsendmsg(ua, "%s", db_strerror(ua->db));
325 first_id = mr.PoolId;
329 Dmsg0(200, "Update pool record.\n");
330 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
331 bsendmsg(ua, "%s", db_strerror(ua->db));
334 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
340 * Turn auto mount on/off
345 int automount_cmd(UAContext *ua, const char *cmd)
350 if (!get_cmd(ua, _("Turn on or off? "))) {
358 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
366 static int cancel_cmd(UAContext *ua, const char *cmd)
371 char JobName[MAX_NAME_LENGTH];
377 for (i=1; i<ua->argc; i++) {
378 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
383 JobId = str_to_int64(ua->argv[i]);
384 if (!(jcr=get_jcr_by_id(JobId))) {
385 bsendmsg(ua, _("JobId %d is not running.\n"), JobId);
389 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
393 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
394 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
400 /* If we still do not have a jcr,
401 * throw up a list and ask the user to select one.
404 /* Count Jobs running */
407 if (jcr->JobId == 0) { /* this is us */
408 free_locked_jcr(jcr);
411 free_locked_jcr(jcr);
417 bsendmsg(ua, _("No Jobs running.\n"));
420 start_prompt(ua, _("Select Job:\n"));
423 if (jcr->JobId == 0) { /* this is us */
424 free_locked_jcr(jcr);
427 add_prompt(ua, jcr->Job);
428 free_locked_jcr(jcr);
432 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
436 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
440 /* NOTE! This increments the ref_count */
441 jcr = get_jcr_by_full_name(JobName);
443 bsendmsg(ua, _("Job %s not found.\n"), JobName);
448 ret = cancel_job(ua, jcr);
455 * This is a common routine to create or update a
456 * Pool DB base record from a Pool Resource. We handle
457 * the setting of MaxVols and NumVols slightly differently
458 * depending on if we are creating the Pool or we are
459 * simply bringing it into agreement with the resource (updage).
461 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
463 strcpy(pr->PoolType, pool->pool_type);
464 if (op == POOL_OP_CREATE) {
465 pr->MaxVols = pool->max_volumes;
467 } else { /* update pool */
468 if (pr->MaxVols != pool->max_volumes) {
469 pr->MaxVols = pool->max_volumes;
471 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
472 pr->MaxVols = pr->NumVols;
475 pr->UseOnce = pool->use_volume_once;
476 pr->UseCatalog = pool->use_catalog;
477 pr->AcceptAnyVolume = pool->accept_any_volume;
478 pr->Recycle = pool->Recycle;
479 pr->VolRetention = pool->VolRetention;
480 pr->VolUseDuration = pool->VolUseDuration;
481 pr->MaxVolJobs = pool->MaxVolJobs;
482 pr->MaxVolFiles = pool->MaxVolFiles;
483 pr->MaxVolBytes = pool->MaxVolBytes;
484 pr->AutoPrune = pool->AutoPrune;
485 pr->Recycle = pool->Recycle;
486 if (pool->label_format) {
487 strcpy(pr->LabelFormat, pool->label_format);
489 strcpy(pr->LabelFormat, "*"); /* none */
495 * Create a pool record from a given Pool resource
496 * Also called from backup.c
497 * Returns: -1 on error
498 * 0 record already exists
502 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
506 memset(&pr, 0, sizeof(POOL_DBR));
508 strcpy(pr.Name, pool->hdr.name);
510 if (db_get_pool_record(jcr, db, &pr)) {
512 if (op == POOL_OP_UPDATE) { /* update request */
513 set_pooldbr_from_poolres(&pr, pool, op);
514 db_update_pool_record(jcr, db, &pr);
516 return 0; /* exists */
519 set_pooldbr_from_poolres(&pr, pool, op);
521 if (!db_create_pool_record(jcr, db, &pr)) {
522 return -1; /* error */
530 * Create a Pool Record in the database.
531 * It is always created from the Resource record.
533 static int create_cmd(UAContext *ua, const char *cmd)
541 pool = get_pool_resource(ua);
546 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
548 bsendmsg(ua, _("Error: Pool %s already exists.\n"
549 "Use update to change it.\n"), pool->hdr.name);
553 bsendmsg(ua, "%s", db_strerror(ua->db));
559 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
564 extern DIRRES *director;
567 * Python control command
568 * python restart (restarts interpreter)
570 static int python_cmd(UAContext *ua, const char *cmd)
572 if (strcasecmp(ua->argk[1], _("restart")) == 0) {
573 term_python_interpreter();
574 init_python_interpreter(director->hdr.name, director->scripts_directory ?
575 director->scripts_directory : ".");
576 bsendmsg(ua, _("Python interpreter restarted.\n"));
578 bsendmsg(ua, _("Nothing done.\n"));
585 * Set a new address in a Client resource. We do this only
586 * if the Console name is the same as the Client name
587 * and the Console can access the client.
589 static int setip_cmd(UAContext *ua, const char *cmd)
593 if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
594 bsendmsg(ua, _("Illegal command from this console.\n"));
598 client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
601 bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
604 if (client->address) {
605 free(client->address);
607 /* MA Bug 6 remove ifdef */
608 sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf));
609 client->address = bstrdup(buf);
610 bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
611 client->hdr.name, client->address);
619 * Update a Pool Record in the database.
620 * It is always updated from the Resource record.
622 * update pool=<pool-name>
623 * updates pool from Pool resource
624 * update media pool=<pool-name> volume=<volume-name>
625 * changes pool info for volume
626 * update slots [scan=...]
627 * updates autochanger slots
629 static int update_cmd(UAContext *ua, const char *cmd)
631 static const char *kw[] = {
633 N_("volume"), /* 1 */
642 switch (find_arg_keyword(ua, kw)) {
657 start_prompt(ua, _("Update choice:\n"));
658 add_prompt(ua, _("Volume parameters"));
659 add_prompt(ua, _("Pool from resource"));
660 add_prompt(ua, _("Slots from autochanger"));
661 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
677 static void update_volstatus(UAContext *ua, const char *val, MEDIA_DBR *mr)
679 POOLMEM *query = get_pool_memory(PM_MESSAGE);
693 for (i=0; kw[i]; i++) {
694 if (strcasecmp(val, kw[i]) == 0) {
700 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
702 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
703 Mmsg(query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
704 mr->VolStatus, mr->MediaId);
705 if (!db_sql_query(ua->db, query, NULL, NULL)) {
706 bsendmsg(ua, "%s", db_strerror(ua->db));
708 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
711 free_pool_memory(query);
714 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
718 if (!duration_to_utime(val, &mr->VolRetention)) {
719 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
722 query = get_pool_memory(PM_MESSAGE);
723 Mmsg(query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
724 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
725 if (!db_sql_query(ua->db, query, NULL, NULL)) {
726 bsendmsg(ua, "%s", db_strerror(ua->db));
728 bsendmsg(ua, _("New retention period is: %s\n"),
729 edit_utime(mr->VolRetention, ed1, sizeof(ed1)));
731 free_pool_memory(query);
734 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
739 if (!duration_to_utime(val, &mr->VolUseDuration)) {
740 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
743 query = get_pool_memory(PM_MESSAGE);
744 Mmsg(query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
745 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
746 if (!db_sql_query(ua->db, query, NULL, NULL)) {
747 bsendmsg(ua, "%s", db_strerror(ua->db));
749 bsendmsg(ua, _("New use duration is: %s\n"),
750 edit_utime(mr->VolUseDuration, ed1, sizeof(ed1)));
752 free_pool_memory(query);
755 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
757 POOLMEM *query = get_pool_memory(PM_MESSAGE);
758 Mmsg(query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
760 if (!db_sql_query(ua->db, query, NULL, NULL)) {
761 bsendmsg(ua, "%s", db_strerror(ua->db));
763 bsendmsg(ua, _("New max jobs is: %s\n"), val);
765 free_pool_memory(query);
768 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
770 POOLMEM *query = get_pool_memory(PM_MESSAGE);
771 Mmsg(query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
773 if (!db_sql_query(ua->db, query, NULL, NULL)) {
774 bsendmsg(ua, "%s", db_strerror(ua->db));
776 bsendmsg(ua, _("New max files is: %s\n"), val);
778 free_pool_memory(query);
781 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
787 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
788 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
791 query = get_pool_memory(PM_MESSAGE);
792 Mmsg(query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
793 edit_uint64(maxbytes, ed1), mr->MediaId);
794 if (!db_sql_query(ua->db, query, NULL, NULL)) {
795 bsendmsg(ua, "%s", db_strerror(ua->db));
797 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
799 free_pool_memory(query);
802 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
806 if (strcasecmp(val, _("yes")) == 0) {
808 } else if (strcasecmp(val, _("no")) == 0) {
811 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
814 query = get_pool_memory(PM_MESSAGE);
815 Mmsg(query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
816 recycle, mr->MediaId);
817 if (!db_sql_query(ua->db, query, NULL, NULL)) {
818 bsendmsg(ua, "%s", db_strerror(ua->db));
820 bsendmsg(ua, _("New Recycle flag is: %s\n"),
821 mr->Recycle==1?_("yes"):_("no"));
823 free_pool_memory(query);
826 /* Modify the Pool in which this Volume is located */
827 static void update_vol_pool(UAContext *ua, char *val, MEDIA_DBR *mr, POOL_DBR *opr)
832 memset(&pr, 0, sizeof(pr));
833 bstrncpy(pr.Name, val, sizeof(pr.Name));
834 if (!get_pool_dbr(ua, &pr)) {
837 mr->PoolId = pr.PoolId; /* set new PoolId */
840 query = get_pool_memory(PM_MESSAGE);
842 Mmsg(query, "UPDATE Media SET PoolId=%d WHERE MediaId=%u",
843 mr->PoolId, mr->MediaId);
844 if (!db_sql_query(ua->db, query, NULL, NULL)) {
845 bsendmsg(ua, "%s", db_strerror(ua->db));
847 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
849 if (!db_update_pool_record(ua->jcr, ua->db, opr)) {
850 bsendmsg(ua, "%s", db_strerror(ua->db));
853 if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
854 bsendmsg(ua, "%s", db_strerror(ua->db));
856 db_make_inchanger_unique(ua->jcr, ua->db, mr);
859 free_pool_memory(query);
863 * Refresh the Volume information from the Pool record
865 static void update_volfrompool(UAContext *ua, MEDIA_DBR *mr)
869 memset(&pr, 0, sizeof(pr));
870 pr.PoolId = mr->PoolId;
871 if (!db_get_pool_record(ua->jcr, ua->db, &pr) ||
872 !acl_access_ok(ua, Pool_ACL, pr.Name)) {
875 set_pool_dbr_defaults_in_media_dbr(mr, &pr);
876 if (!db_update_media_defaults(ua->jcr, ua->db, mr)) {
877 bsendmsg(ua, _("Error updating Volume record: ERR=%s"), db_strerror(ua->db));
879 bsendmsg(ua, _("Volume defaults updated from \"%s\" Pool record.\n"),
885 * Refresh the Volume information from the Pool record
888 static void update_all_vols_from_pool(UAContext *ua)
893 memset(&pr, 0, sizeof(pr));
894 memset(&mr, 0, sizeof(mr));
895 if (!get_pool_dbr(ua, &pr)) {
898 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
899 mr.PoolId = pr.PoolId;
900 if (!db_update_media_defaults(ua->jcr, ua->db, &mr)) {
901 bsendmsg(ua, _("Error updating Volume records: ERR=%s"), db_strerror(ua->db));
903 bsendmsg(ua, _("All Volume defaults updated from Pool record.\n"));
909 * Update a media record -- allows you to change the
910 * Volume status. E.g. if you want Bacula to stop
911 * writing on the volume, set it to anything other
914 static int update_volume(UAContext *ua)
922 N_("VolStatus"), /* 0 */
923 N_("VolRetention"), /* 1 */
924 N_("VolUse"), /* 2 */
925 N_("MaxVolJobs"), /* 3 */
926 N_("MaxVolFiles"), /* 4 */
927 N_("MaxVolBytes"), /* 5 */
928 N_("Recycle"), /* 6 */
930 N_("FromPool"), /* 8 */
931 N_("AllFromPool"), /* 9 */
934 for (int i=0; kw[i]; i++) {
937 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
938 if (i != 9 && !select_media_dbr(ua, &mr)) {
943 update_volstatus(ua, ua->argv[j], &mr);
946 update_volretention(ua, ua->argv[j], &mr);
949 update_voluseduration(ua, ua->argv[j], &mr);
952 update_volmaxjobs(ua, ua->argv[j], &mr);
955 update_volmaxfiles(ua, ua->argv[j], &mr);
958 update_volmaxbytes(ua, ua->argv[j], &mr);
961 update_volrecycle(ua, ua->argv[j], &mr);
964 memset(&pr, 0, sizeof(POOL_DBR));
965 pr.PoolId = mr.PoolId;
966 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
967 bsendmsg(ua, "%s", db_strerror(ua->db));
970 update_vol_pool(ua, ua->argv[j], &mr, &pr);
973 update_volfrompool(ua, &mr);
976 update_all_vols_from_pool(ua);
984 if (!select_media_dbr(ua, &mr)) {
987 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
988 start_prompt(ua, _("Parameters to modify:\n"));
989 add_prompt(ua, _("Volume Status"));
990 add_prompt(ua, _("Volume Retention Period"));
991 add_prompt(ua, _("Volume Use Duration"));
992 add_prompt(ua, _("Maximum Volume Jobs"));
993 add_prompt(ua, _("Maximum Volume Files"));
994 add_prompt(ua, _("Maximum Volume Bytes"));
995 add_prompt(ua, _("Recycle Flag"));
996 add_prompt(ua, _("Slot"));
997 add_prompt(ua, _("InChanger Flag"));
998 add_prompt(ua, _("Volume Files"));
999 add_prompt(ua, _("Pool"));
1000 add_prompt(ua, _("Volume from Pool"));
1001 add_prompt(ua, _("All Volumes from Pool"));
1002 add_prompt(ua, _("Done"));
1003 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
1004 case 0: /* Volume Status */
1005 /* Modify Volume Status */
1006 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
1007 start_prompt(ua, _("Possible Values are:\n"));
1008 add_prompt(ua, "Append"); /* Better not translate these as */
1009 add_prompt(ua, "Archive"); /* They are known in the database code */
1010 add_prompt(ua, "Disabled");
1011 add_prompt(ua, "Full");
1012 add_prompt(ua, "Used");
1013 add_prompt(ua, "Cleaning");
1014 if (strcmp(mr.VolStatus, "Purged") == 0) {
1015 add_prompt(ua, "Recycle");
1017 add_prompt(ua, "Read-Only");
1018 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
1021 update_volstatus(ua, ua->cmd, &mr);
1023 case 1: /* Retention */
1024 bsendmsg(ua, _("Current retention period is: %s\n"),
1025 edit_utime(mr.VolRetention, ed1, sizeof(ed1)));
1026 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
1029 update_volretention(ua, ua->cmd, &mr);
1032 case 2: /* Use Duration */
1033 bsendmsg(ua, _("Current use duration is: %s\n"),
1034 edit_utime(mr.VolUseDuration, ed1, sizeof(ed1)));
1035 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
1038 update_voluseduration(ua, ua->cmd, &mr);
1041 case 3: /* Max Jobs */
1042 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
1043 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
1046 update_volmaxjobs(ua, ua->cmd, &mr);
1049 case 4: /* Max Files */
1050 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
1051 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
1054 update_volmaxfiles(ua, ua->cmd, &mr);
1057 case 5: /* Max Bytes */
1058 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
1059 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
1062 update_volmaxbytes(ua, ua->cmd, &mr);
1066 case 6: /* Recycle */
1067 bsendmsg(ua, _("Current recycle flag is: %s\n"),
1068 mr.Recycle==1?_("yes"):_("no"));
1069 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
1072 update_volrecycle(ua, ua->cmd, &mr);
1078 memset(&pr, 0, sizeof(POOL_DBR));
1079 pr.PoolId = mr.PoolId;
1080 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1081 bsendmsg(ua, "%s", db_strerror(ua->db));
1084 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
1085 if (!get_pint(ua, _("Enter new Slot: "))) {
1088 Slot = ua->pint32_val;
1089 if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
1090 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
1096 * Make sure to use db_update... rather than doing this directly,
1097 * so that any Slot is handled correctly.
1099 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1100 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1102 bsendmsg(ua, _("New Slot is: %d\n"), mr.Slot);
1106 case 8: /* InChanger */
1107 bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
1108 if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
1111 mr.InChanger = ua->pint32_val;
1113 * Make sure to use db_update... rather than doing this directly,
1114 * so that any Slot is handled correctly.
1116 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1117 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1119 bsendmsg(ua, _("New InChanger flag is: %d\n"), mr.InChanger);
1124 case 9: /* Volume Files */
1126 bsendmsg(ua, _("Warning changing Volume Files can result\n"
1127 "in loss of data on your Volume\n\n"));
1128 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1129 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1132 VolFiles = ua->pint32_val;
1133 if (VolFiles != (int)(mr.VolFiles + 1)) {
1134 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1135 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1139 query = get_pool_memory(PM_MESSAGE);
1140 Mmsg(query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1141 VolFiles, mr.MediaId);
1142 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1143 bsendmsg(ua, "%s", db_strerror(ua->db));
1145 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1147 free_pool_memory(query);
1150 case 10: /* Volume's Pool */
1151 memset(&pr, 0, sizeof(POOL_DBR));
1152 pr.PoolId = mr.PoolId;
1153 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1154 bsendmsg(ua, "%s", db_strerror(ua->db));
1157 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1158 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1161 update_vol_pool(ua, ua->cmd, &mr, &pr);
1165 update_volfrompool(ua, &mr);
1168 update_all_vols_from_pool(ua);
1170 default: /* Done or error */
1171 bsendmsg(ua, "Selection done.\n");
1179 * Update pool record -- pull info from current POOL resource
1181 static int update_pool(UAContext *ua)
1188 pool = get_pool_resource(ua);
1193 memset(&pr, 0, sizeof(pr));
1194 strcpy(pr.Name, pool->hdr.name);
1195 if (!get_pool_dbr(ua, &pr)) {
1199 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1201 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1203 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1204 id, db_strerror(ua->db));
1206 query = get_pool_memory(PM_MESSAGE);
1207 Mmsg(query, list_pool, pr.PoolId);
1208 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1209 free_pool_memory(query);
1210 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1215 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
1221 /* Try connecting for up to 15 seconds */
1222 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1223 store->hdr.name, store->address, store->SDport);
1224 if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
1225 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1228 Dmsg0(120, _("Connected to storage daemon\n"));
1229 sd = jcr->store_bsock;
1230 bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
1231 if (bnet_recv(sd) >= 0) {
1232 bsendmsg(ua, "%s", sd->msg);
1234 bnet_sig(sd, BNET_TERMINATE);
1236 jcr->store_bsock = NULL;
1240 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
1244 /* Connect to File daemon */
1246 ua->jcr->client = client;
1247 /* Try to connect for 15 seconds */
1248 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1249 client->hdr.name, client->address, client->FDport);
1250 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1251 bsendmsg(ua, _("Failed to connect to Client.\n"));
1254 Dmsg0(120, "Connected to file daemon\n");
1255 fd = ua->jcr->file_bsock;
1256 bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
1257 if (bnet_recv(fd) >= 0) {
1258 bsendmsg(ua, "%s", fd->msg);
1260 bnet_sig(fd, BNET_TERMINATE);
1262 ua->jcr->file_bsock = NULL;
1267 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
1269 STORE *store, **unique_store;
1270 CLIENT *client, **unique_client;
1274 debug_level = level;
1276 /* Count Storage items */
1279 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1281 unique_store = (STORE **) malloc(i * sizeof(STORE));
1282 /* Find Unique Storage address/port */
1283 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1285 unique_store[i++] = store;
1286 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1288 for (j=0; j<i; j++) {
1289 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1290 unique_store[j]->SDport == store->SDport) {
1296 unique_store[i++] = store;
1297 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1302 /* Call each unique Storage daemon */
1303 for (j=0; j<i; j++) {
1304 do_storage_setdebug(ua, unique_store[j], level, trace_flag);
1308 /* Count Client items */
1311 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1313 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1314 /* Find Unique Client address/port */
1315 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1317 unique_client[i++] = client;
1318 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1320 for (j=0; j<i; j++) {
1321 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1322 unique_client[j]->FDport == client->FDport) {
1328 unique_client[i++] = client;
1329 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1334 /* Call each unique File daemon */
1335 for (j=0; j<i; j++) {
1336 do_client_setdebug(ua, unique_client[j], level, trace_flag);
1338 free(unique_client);
1342 * setdebug level=nn all trace=1/0
1344 static int setdebug_cmd(UAContext *ua, const char *cmd)
1349 int trace_flag = -1;
1355 Dmsg1(120, "setdebug:%s:\n", cmd);
1358 i = find_arg_with_value(ua, _("level"));
1360 level = atoi(ua->argv[i]);
1363 if (!get_pint(ua, _("Enter new debug level: "))) {
1366 level = ua->pint32_val;
1369 /* Look for trace flag. -1 => not change */
1370 i = find_arg_with_value(ua, _("trace"));
1372 trace_flag = atoi(ua->argv[i]);
1373 if (trace_flag > 0) {
1378 /* General debug? */
1379 for (i=1; i<ua->argc; i++) {
1380 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1381 do_all_setdebug(ua, level, trace_flag);
1384 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1385 strcasecmp(ua->argk[i], _("director")) == 0) {
1386 debug_level = level;
1387 set_trace(trace_flag);
1390 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1391 strcasecmp(ua->argk[i], _("fd")) == 0) {
1394 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1396 do_client_setdebug(ua, client, level, trace_flag);
1400 client = select_client_resource(ua);
1402 do_client_setdebug(ua, client, level, trace_flag);
1407 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1408 strcasecmp(ua->argk[i], _("storage")) == 0 ||
1409 strcasecmp(ua->argk[i], _("sd")) == 0) {
1412 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1414 do_storage_setdebug(ua, store, level, trace_flag);
1418 store = get_storage_resource(ua, 0);
1420 do_storage_setdebug(ua, store, level, trace_flag);
1426 * We didn't find an appropriate keyword above, so
1429 start_prompt(ua, _("Available daemons are: \n"));
1430 add_prompt(ua, _("Director"));
1431 add_prompt(ua, _("Storage"));
1432 add_prompt(ua, _("Client"));
1433 add_prompt(ua, _("All"));
1434 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1435 case 0: /* Director */
1436 debug_level = level;
1437 set_trace(trace_flag);
1440 store = get_storage_resource(ua, 0);
1442 do_storage_setdebug(ua, store, level, trace_flag);
1446 client = select_client_resource(ua);
1448 do_client_setdebug(ua, client, level, trace_flag);
1452 do_all_setdebug(ua, level, trace_flag);
1461 * Turn debug tracing to file on/off
1463 static int trace_cmd(UAContext *ua, const char *cmd)
1467 if (ua->argc != 2) {
1468 if (!get_cmd(ua, _("Turn on or off? "))) {
1473 onoff = ua->argk[1];
1476 set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
1481 static int var_cmd(UAContext *ua, const char *cmd)
1483 POOLMEM *val = get_pool_memory(PM_FNAME);
1489 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1492 while (*var == ' ') { /* skip spaces */
1495 Dmsg1(100, "Var=%s:\n", var);
1496 variable_expansion(ua->jcr, var, &val);
1497 bsendmsg(ua, "%s\n", val);
1498 free_pool_memory(val);
1502 static int estimate_cmd(UAContext *ua, const char *cmd)
1505 CLIENT *client = NULL;
1506 FILESET *fileset = NULL;
1509 char since[MAXSTRING];
1512 jcr->JobLevel = L_FULL;
1513 for (int i=1; i<ua->argc; i++) {
1514 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1515 strcasecmp(ua->argk[i], _("fd")) == 0) {
1517 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1521 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1523 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1527 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1529 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1533 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1537 if (strcasecmp(ua->argk[i], _("level")) == 0) {
1538 if (!get_level_from_name(ua->jcr, ua->argv[i])) {
1539 bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
1544 if (!job && !(client && fileset)) {
1545 if (!(job = select_job_resource(ua))) {
1550 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1552 bsendmsg(ua, _("No job specified.\n"));
1557 client = job->client;
1560 fileset = job->fileset;
1562 jcr->client = client;
1563 jcr->fileset = fileset;
1565 ua->catalog = client->catalog;
1572 jcr->JobType = JT_BACKUP;
1573 init_jcr_job_record(jcr);
1575 if (!get_or_create_client_record(jcr)) {
1578 if (!get_or_create_fileset_record(jcr, &fsr)) {
1582 get_level_since_time(ua->jcr, since, sizeof(since));
1584 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1585 job->client->hdr.name, job->client->address, job->client->FDport);
1586 if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
1587 bsendmsg(ua, _("Failed to connect to Client.\n"));
1591 if (!send_include_list(jcr)) {
1592 bsendmsg(ua, _("Error sending include list.\n"));
1596 if (!send_exclude_list(jcr)) {
1597 bsendmsg(ua, _("Error sending exclude list.\n"));
1601 if (!send_level_command(jcr)) {
1605 bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1606 while (bnet_recv(jcr->file_bsock) >= 0) {
1607 bsendmsg(ua, "%s", jcr->file_bsock->msg);
1611 if (jcr->file_bsock) {
1612 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1613 bnet_close(jcr->file_bsock);
1614 jcr->file_bsock = NULL;
1623 static int time_cmd(UAContext *ua, const char *cmd)
1626 time_t ttime = time(NULL);
1628 localtime_r(&ttime, &tm);
1629 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1630 bsendmsg(ua, "%s\n", sdt);
1635 * reload the conf file
1637 extern "C" void reload_config(int sig);
1639 static int reload_cmd(UAContext *ua, const char *cmd)
1646 * Delete Pool records (should purge Media with it).
1648 * delete pool=<pool-name>
1649 * delete volume pool=<pool-name> volume=<name>
1650 * delete job jobid=xxx
1652 static int delete_cmd(UAContext *ua, const char *cmd)
1654 static const char *keywords[] = {
1665 switch (find_arg_keyword(ua, keywords)) {
1675 while ((i=find_arg(ua, _("jobid"))) > 0) {
1677 *ua->argk[i] = 0; /* zap keyword already visited */
1685 "In general it is not a good idea to delete either a\n"
1686 "Pool or a Volume since they may contain data.\n\n"));
1688 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1699 bsendmsg(ua, _("Nothing done.\n"));
1707 * delete_job has been modified to parse JobID lists like the
1709 * delete job JobID=3,4,6,7-11,14
1711 * Thanks to Phil Stracchino for the above addition.
1714 static void delete_job(UAContext *ua)
1719 int i = find_arg_with_value(ua, _("jobid"));
1721 if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1722 s = bstrdup(ua->argv[i]);
1725 * We could use strtok() here. But we're not going to, because:
1726 * (a) strtok() is deprecated, having been replaced by strsep();
1727 * (b) strtok() is broken in significant ways.
1728 * we could use strsep() instead, but it's not universally available.
1729 * so we grow our own using strchr().
1731 sep = strchr(tok, ',');
1732 while (sep != NULL) {
1734 if (strchr(tok, '-')) {
1735 delete_job_id_range(ua, tok);
1737 JobId = str_to_int64(tok);
1738 do_job_delete(ua, JobId);
1741 sep = strchr(tok, ',');
1743 /* pick up the last token */
1744 if (strchr(tok, '-')) {
1745 delete_job_id_range(ua, tok);
1747 JobId = str_to_int64(tok);
1748 do_job_delete(ua, JobId);
1753 JobId = str_to_int64(ua->argv[i]);
1754 do_job_delete(ua, JobId);
1756 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1759 JobId = ua->pint32_val;
1760 do_job_delete(ua, JobId);
1765 * we call delete_job_id_range to parse range tokens and iterate over ranges
1767 static void delete_job_id_range(UAContext *ua, char *tok)
1772 tok2 = strchr(tok, '-');
1775 j1 = str_to_int64(tok);
1776 j2 = str_to_int64(tok2);
1777 for (j=j1; j<=j2; j++) {
1778 do_job_delete(ua, j);
1783 * do_job_delete now performs the actual delete operation atomically
1784 * we always return 1 because C++ is pissy about void functions
1787 static void do_job_delete(UAContext *ua, JobId_t JobId)
1789 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1791 Mmsg(query, "DELETE FROM Job WHERE JobId=%u", JobId);
1792 db_sql_query(ua->db, query, NULL, (void *)NULL);
1793 Mmsg(query, "DELETE FROM File WHERE JobId=%u", JobId);
1794 db_sql_query(ua->db, query, NULL, (void *)NULL);
1795 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1796 db_sql_query(ua->db, query, NULL, (void *)NULL);
1797 free_pool_memory(query);
1798 bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1802 * Delete media records from database -- dangerous
1804 static int delete_volume(UAContext *ua)
1808 if (!select_media_dbr(ua, &mr)) {
1811 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1812 "and all Jobs saved on that volume from the Catalog\n"),
1815 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1818 if (ua->pint32_val) {
1819 db_delete_media_record(ua->jcr, ua->db, &mr);
1825 * Delete a pool record from the database -- dangerous
1827 static int delete_pool(UAContext *ua)
1831 memset(&pr, 0, sizeof(pr));
1833 if (!get_pool_dbr(ua, &pr)) {
1836 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1839 if (ua->pint32_val) {
1840 db_delete_pool_record(ua->jcr, ua->db, &pr);
1846 static void do_mount_cmd(UAContext *ua, const char *command)
1851 char dev_name[MAX_NAME_LENGTH];
1856 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1858 store = get_storage_resource(ua, 1);
1863 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1864 store->media_type, store->dev_name);
1867 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
1868 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1871 sd = jcr->store_bsock;
1872 bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
1873 bash_spaces(dev_name);
1874 bnet_fsend(sd, "%s %s", command, dev_name);
1875 while (bnet_recv(sd) >= 0) {
1876 bsendmsg(ua, "%s", sd->msg);
1878 bnet_sig(sd, BNET_TERMINATE);
1880 jcr->store_bsock = NULL;
1884 * mount [storage | device] <name>
1886 static int mount_cmd(UAContext *ua, const char *cmd)
1888 do_mount_cmd(ua, "mount"); /* mount */
1894 * unmount [storage | device] <name>
1896 static int unmount_cmd(UAContext *ua, const char *cmd)
1898 do_mount_cmd(ua, "unmount"); /* unmount */
1904 * release [storage | device] <name>
1906 static int release_cmd(UAContext *ua, const char *cmd)
1908 do_mount_cmd(ua, "release"); /* release */
1915 * use catalog=<name>
1917 static int use_cmd(UAContext *ua, const char *cmd)
1919 CAT *oldcatalog, *catalog;
1922 close_db(ua); /* close any previously open db */
1923 oldcatalog = ua->catalog;
1925 if (!(catalog = get_catalog_resource(ua))) {
1926 ua->catalog = oldcatalog;
1928 ua->catalog = catalog;
1931 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1932 ua->catalog->hdr.name, ua->catalog->db_name);
1937 int quit_cmd(UAContext *ua, const char *cmd)
1944 * Wait until no job is running
1946 int wait_cmd(UAContext *ua, const char *cmd)
1949 bmicrosleep(0, 200000); /* let job actually start */
1950 for (bool running=true; running; ) {
1954 if (jcr->JobId != 0) {
1956 free_locked_jcr(jcr);
1959 free_locked_jcr(jcr);
1970 static int help_cmd(UAContext *ua, const char *cmd)
1974 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1975 for (i=0; i<comsize; i++) {
1976 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1978 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1982 int qhelp_cmd(UAContext *ua, const char *cmd)
1986 for (i=0; i<comsize; i++) {
1987 bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
1992 static int version_cmd(UAContext *ua, const char *cmd)
1994 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1999 /* A bit brain damaged in that if the user has not done
2000 * a "use catalog xxx" command, we simply find the first
2001 * catalog resource and open it.
2003 int open_db(UAContext *ua)
2010 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
2013 bsendmsg(ua, _("Could not find a Catalog resource\n"));
2016 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
2017 ua->catalog->hdr.name, ua->catalog->db_name);
2021 ua->jcr->catalog = ua->catalog;
2023 Dmsg0(150, "Open database\n");
2024 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
2025 ua->catalog->db_password, ua->catalog->db_address,
2026 ua->catalog->db_port, ua->catalog->db_socket,
2027 ua->catalog->mult_db_connections);
2028 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
2029 bsendmsg(ua, _("Could not open database \"%s\".\n"),
2030 ua->catalog->db_name);
2032 bsendmsg(ua, "%s", db_strerror(ua->db));
2037 ua->jcr->db = ua->db;
2038 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
2042 void close_db(UAContext *ua)
2045 db_close_database(ua->jcr, ua->db);