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 retentioncmd(UAContext *ua, const char *cmd);
56 extern int prunecmd(UAContext *ua, const char *cmd);
57 extern int purgecmd(UAContext *ua, const char *cmd);
58 extern int restore_cmd(UAContext *ua, const char *cmd);
59 extern int label_cmd(UAContext *ua, const char *cmd);
60 extern int relabel_cmd(UAContext *ua, const char *cmd);
61 extern int update_slots(UAContext *ua); /* ua_label.c */
63 /* Forward referenced functions */
64 static int add_cmd(UAContext *ua, const char *cmd);
65 static int create_cmd(UAContext *ua, const char *cmd);
66 static int cancel_cmd(UAContext *ua, const char *cmd);
67 static int setdebug_cmd(UAContext *ua, const char *cmd);
68 static int trace_cmd(UAContext *ua, const char *cmd);
69 static int var_cmd(UAContext *ua, const char *cmd);
70 static int estimate_cmd(UAContext *ua, const char *cmd);
71 static int help_cmd(UAContext *ua, const char *cmd);
72 static int delete_cmd(UAContext *ua, const char *cmd);
73 static int use_cmd(UAContext *ua, const char *cmd);
74 static int unmount_cmd(UAContext *ua, const char *cmd);
75 static int version_cmd(UAContext *ua, const char *cmd);
76 static int automount_cmd(UAContext *ua, const char *cmd);
77 static int time_cmd(UAContext *ua, const char *cmd);
78 static int reload_cmd(UAContext *ua, const char *cmd);
79 static int update_volume(UAContext *ua);
80 static int update_pool(UAContext *ua);
81 static int delete_volume(UAContext *ua);
82 static int delete_pool(UAContext *ua);
83 static void delete_job(UAContext *ua);
84 static int mount_cmd(UAContext *ua, const char *cmd);
85 static int release_cmd(UAContext *ua, const char *cmd);
86 static int update_cmd(UAContext *ua, const char *cmd);
87 static int wait_cmd(UAContext *ua, const char *cmd);
88 static int setip_cmd(UAContext *ua, const char *cmd);
89 static int python_cmd(UAContext *ua, const char *cmd);
90 static void do_job_delete(UAContext *ua, JobId_t JobId);
91 static void delete_job_id_range(UAContext *ua, char *tok);
93 int qhelp_cmd(UAContext *ua, const char *cmd);
94 int quit_cmd(UAContext *ua, const char *cmd);
97 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; };
98 static struct cmdstruct commands[] = {
99 { N_("add"), add_cmd, _("add media to a pool")},
100 { N_("autodisplay"), autodisplay_cmd, _("autodisplay [on/off] -- console messages")},
101 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
102 { N_("cancel"), cancel_cmd, _("cancel job=nnn -- cancel a job")},
103 { N_("create"), create_cmd, _("create DB Pool from resource")},
104 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
105 { N_("estimate"), estimate_cmd, _("performs FileSet estimate, listing gives full listing")},
106 { N_("exit"), quit_cmd, _("exit = quit")},
107 { N_("gui"), gui_cmd, _("gui [on/off] -- non-interactive gui mode")},
108 { N_("help"), help_cmd, _("print this command")},
109 { N_("list"), list_cmd, _("list [pools | jobs | jobtotals | media <pool> | files jobid=<nn>]; from catalog")},
110 { N_("label"), label_cmd, _("label a tape")},
111 { N_("llist"), llist_cmd, _("full or long list like list command")},
112 { N_("messages"), messagescmd, _("messages")},
113 { N_("mount"), mount_cmd, _("mount <storage-name>")},
114 { N_("prune"), prunecmd, _("prune expired records from catalog")},
115 { N_("purge"), purgecmd, _("purge records from catalog")},
116 { N_("python"), python_cmd, _("python control commands")},
117 { N_("quit"), quit_cmd, _("quit")},
118 { N_("query"), querycmd, _("query catalog")},
119 { N_("restore"), restore_cmd, _("restore files")},
120 { N_("relabel"), relabel_cmd, _("relabel a tape")},
121 { N_("release"), release_cmd, _("release <storage-name>")},
122 { N_("reload"), reload_cmd, _("reload conf file")},
123 { N_("run"), run_cmd, _("run <job-name>")},
124 { N_("status"), status_cmd, _("status [storage | client]=<name>")},
125 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
126 { N_("setip"), setip_cmd, _("sets new client address -- if authorized")},
127 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
128 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
129 { N_("time"), time_cmd, _("print current time")},
130 { N_("trace"), trace_cmd, _("turn on/off trace to file")},
131 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
132 { N_("umount"), unmount_cmd, _("umount <storage-name> for old-time Unix guys")},
133 { N_("update"), update_cmd, _("update Volume or Pool")},
134 { N_("use"), use_cmd, _("use catalog xxx")},
135 { N_("var"), var_cmd, _("does variable expansion")},
136 { N_("version"), version_cmd, _("print Director version")},
137 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
139 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
142 * Execute a command from the UA
144 int do_a_command(UAContext *ua, const char *cmd)
152 Dmsg1(200, "Command: %s\n", ua->UA_sock->msg);
157 len = strlen(ua->argk[0]);
158 for (i=0; i<comsize; i++) { /* search for command */
159 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
160 if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
163 stat = (*commands[i].func)(ua, cmd); /* go execute command */
169 bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
175 * This is a common routine used to stuff the Pool DB record defaults
176 * into the Media DB record just before creating a media (Volume)
179 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
181 mr->PoolId = pr->PoolId;
182 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
183 mr->Recycle = pr->Recycle;
184 mr->VolRetention = pr->VolRetention;
185 mr->VolUseDuration = pr->VolUseDuration;
186 mr->MaxVolJobs = pr->MaxVolJobs;
187 mr->MaxVolFiles = pr->MaxVolFiles;
188 mr->MaxVolBytes = pr->MaxVolBytes;
193 * Add Volumes to an existing Pool
195 static int add_cmd(UAContext *ua, const char *cmd)
199 int num, i, max, startnum;
201 char name[MAX_NAME_LENGTH];
203 int Slot = 0, InChanger = 0;
206 "You probably don't want to be using this command since it\n"
207 "creates database records without labeling the Volumes.\n"
208 "You probably want to use the \"label\" command.\n\n"));
214 memset(&pr, 0, sizeof(pr));
215 memset(&mr, 0, sizeof(mr));
217 if (!get_pool_dbr(ua, &pr)) {
221 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
222 pr.MaxVols, pr.PoolType);
224 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
225 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
227 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
230 pr.MaxVols = ua->pint32_val;
235 if ((store = get_storage_resource(ua, 0)) != NULL) {
236 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
237 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
241 if (pr.MaxVols == 0) {
244 max = pr.MaxVols - pr.NumVols;
248 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
249 if (!get_pint(ua, buf)) {
252 num = ua->pint32_val;
253 if (num < 0 || num > max) {
254 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
261 if (!get_cmd(ua, _("Enter Volume name: "))) {
265 if (!get_cmd(ua, _("Enter base volume name: "))) {
269 /* Don't allow | in Volume name because it is the volume separator character */
270 if (!is_volume_name_legal(ua, ua->cmd)) {
273 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
274 bsendmsg(ua, _("Volume name too long.\n"));
277 if (strlen(ua->cmd) == 0) {
278 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
282 bstrncpy(name, ua->cmd, sizeof(name));
284 strcat(name, "%04d");
287 if (!get_pint(ua, _("Enter the starting number: "))) {
290 startnum = ua->pint32_val;
292 bsendmsg(ua, _("Start number must be greater than zero.\n"));
302 if (store && store->autochanger) {
303 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
306 Slot = ua->pint32_val;
307 if (!get_yesno(ua, _("InChanger? yes/no: "))) {
310 InChanger = ua->pint32_val;
313 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
314 for (i=startnum; i < num+startnum; i++) {
315 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
317 mr.InChanger = InChanger;
318 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
319 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
320 bsendmsg(ua, "%s", db_strerror(ua->db));
324 first_id = mr.PoolId;
328 Dmsg0(200, "Update pool record.\n");
329 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
330 bsendmsg(ua, "%s", db_strerror(ua->db));
333 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
339 * Turn auto mount on/off
344 int automount_cmd(UAContext *ua, const char *cmd)
349 if (!get_cmd(ua, _("Turn on or off? "))) {
357 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
365 static int cancel_cmd(UAContext *ua, const char *cmd)
370 char JobName[MAX_NAME_LENGTH];
376 for (i=1; i<ua->argc; i++) {
377 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
382 JobId = str_to_int64(ua->argv[i]);
383 if (!(jcr=get_jcr_by_id(JobId))) {
384 bsendmsg(ua, _("JobId %d is not running.\n"), JobId);
388 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
392 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
393 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
399 /* If we still do not have a jcr,
400 * throw up a list and ask the user to select one.
403 /* Count Jobs running */
406 if (jcr->JobId == 0) { /* this is us */
407 free_locked_jcr(jcr);
410 free_locked_jcr(jcr);
416 bsendmsg(ua, _("No Jobs running.\n"));
419 start_prompt(ua, _("Select Job:\n"));
422 if (jcr->JobId == 0) { /* this is us */
423 free_locked_jcr(jcr);
426 add_prompt(ua, jcr->Job);
427 free_locked_jcr(jcr);
431 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
435 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
439 /* NOTE! This increments the ref_count */
440 jcr = get_jcr_by_full_name(JobName);
442 bsendmsg(ua, _("Job %s not found.\n"), JobName);
447 ret = cancel_job(ua, jcr);
454 * This is a common routine to create or update a
455 * Pool DB base record from a Pool Resource. We handle
456 * the setting of MaxVols and NumVols slightly differently
457 * depending on if we are creating the Pool or we are
458 * simply bringing it into agreement with the resource (updage).
460 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
462 strcpy(pr->PoolType, pool->pool_type);
463 if (op == POOL_OP_CREATE) {
464 pr->MaxVols = pool->max_volumes;
466 } else { /* update pool */
467 if (pr->MaxVols != pool->max_volumes) {
468 pr->MaxVols = pool->max_volumes;
470 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
471 pr->MaxVols = pr->NumVols;
474 pr->UseOnce = pool->use_volume_once;
475 pr->UseCatalog = pool->use_catalog;
476 pr->AcceptAnyVolume = pool->accept_any_volume;
477 pr->Recycle = pool->Recycle;
478 pr->VolRetention = pool->VolRetention;
479 pr->VolUseDuration = pool->VolUseDuration;
480 pr->MaxVolJobs = pool->MaxVolJobs;
481 pr->MaxVolFiles = pool->MaxVolFiles;
482 pr->MaxVolBytes = pool->MaxVolBytes;
483 pr->AutoPrune = pool->AutoPrune;
484 pr->Recycle = pool->Recycle;
485 if (pool->label_format) {
486 strcpy(pr->LabelFormat, pool->label_format);
488 strcpy(pr->LabelFormat, "*"); /* none */
494 * Create a pool record from a given Pool resource
495 * Also called from backup.c
496 * Returns: -1 on error
497 * 0 record already exists
501 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
505 memset(&pr, 0, sizeof(POOL_DBR));
507 strcpy(pr.Name, pool->hdr.name);
509 if (db_get_pool_record(jcr, db, &pr)) {
511 if (op == POOL_OP_UPDATE) { /* update request */
512 set_pooldbr_from_poolres(&pr, pool, op);
513 db_update_pool_record(jcr, db, &pr);
515 return 0; /* exists */
518 set_pooldbr_from_poolres(&pr, pool, op);
520 if (!db_create_pool_record(jcr, db, &pr)) {
521 return -1; /* error */
529 * Create a Pool Record in the database.
530 * It is always created from the Resource record.
532 static int create_cmd(UAContext *ua, const char *cmd)
540 pool = get_pool_resource(ua);
545 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
547 bsendmsg(ua, _("Error: Pool %s already exists.\n"
548 "Use update to change it.\n"), pool->hdr.name);
552 bsendmsg(ua, "%s", db_strerror(ua->db));
558 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
563 extern DIRRES *director;
566 * Python control command
567 * python restart (restarts interpreter)
569 static int python_cmd(UAContext *ua, const char *cmd)
571 if (strcasecmp(ua->argk[1], _("restart")) == 0) {
572 term_python_interpreter();
573 init_python_interpreter(director->hdr.name, director->scripts_directory ?
574 director->scripts_directory : ".");
575 bsendmsg(ua, _("Python interpreter restarted.\n"));
577 bsendmsg(ua, _("Nothing done.\n"));
584 * Set a new address in a Client resource. We do this only
585 * if the Console name is the same as the Client name
586 * and the Console can access the client.
588 static int setip_cmd(UAContext *ua, const char *cmd)
592 if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
593 bsendmsg(ua, _("Illegal command from this console.\n"));
597 client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
600 bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
603 if (client->address) {
604 free(client->address);
606 /* MA Bug 6 remove ifdef */
607 sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf));
608 client->address = bstrdup(buf);
609 bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
610 client->hdr.name, client->address);
618 * Update a Pool Record in the database.
619 * It is always updated from the Resource record.
621 * update pool=<pool-name>
622 * updates pool from Pool resource
623 * update media pool=<pool-name> volume=<volume-name>
624 * changes pool info for volume
625 * update slots [scan=...]
626 * updates autochanger slots
628 static int update_cmd(UAContext *ua, const char *cmd)
630 static const char *kw[] = {
632 N_("volume"), /* 1 */
641 switch (find_arg_keyword(ua, kw)) {
656 start_prompt(ua, _("Update choice:\n"));
657 add_prompt(ua, _("Volume parameters"));
658 add_prompt(ua, _("Pool from resource"));
659 add_prompt(ua, _("Slots from autochanger"));
660 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
676 static void update_volstatus(UAContext *ua, const char *val, MEDIA_DBR *mr)
678 POOLMEM *query = get_pool_memory(PM_MESSAGE);
692 for (i=0; kw[i]; i++) {
693 if (strcasecmp(val, kw[i]) == 0) {
699 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
701 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
702 Mmsg(query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
703 mr->VolStatus, mr->MediaId);
704 if (!db_sql_query(ua->db, query, NULL, NULL)) {
705 bsendmsg(ua, "%s", db_strerror(ua->db));
707 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
710 free_pool_memory(query);
713 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
717 if (!duration_to_utime(val, &mr->VolRetention)) {
718 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
721 query = get_pool_memory(PM_MESSAGE);
722 Mmsg(query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
723 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
724 if (!db_sql_query(ua->db, query, NULL, NULL)) {
725 bsendmsg(ua, "%s", db_strerror(ua->db));
727 bsendmsg(ua, _("New retention period is: %s\n"),
728 edit_utime(mr->VolRetention, ed1, sizeof(ed1)));
730 free_pool_memory(query);
733 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
738 if (!duration_to_utime(val, &mr->VolUseDuration)) {
739 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
742 query = get_pool_memory(PM_MESSAGE);
743 Mmsg(query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
744 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
745 if (!db_sql_query(ua->db, query, NULL, NULL)) {
746 bsendmsg(ua, "%s", db_strerror(ua->db));
748 bsendmsg(ua, _("New use duration is: %s\n"),
749 edit_utime(mr->VolUseDuration, ed1, sizeof(ed1)));
751 free_pool_memory(query);
754 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
756 POOLMEM *query = get_pool_memory(PM_MESSAGE);
757 Mmsg(query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
759 if (!db_sql_query(ua->db, query, NULL, NULL)) {
760 bsendmsg(ua, "%s", db_strerror(ua->db));
762 bsendmsg(ua, _("New max jobs is: %s\n"), val);
764 free_pool_memory(query);
767 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
769 POOLMEM *query = get_pool_memory(PM_MESSAGE);
770 Mmsg(query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
772 if (!db_sql_query(ua->db, query, NULL, NULL)) {
773 bsendmsg(ua, "%s", db_strerror(ua->db));
775 bsendmsg(ua, _("New max files is: %s\n"), val);
777 free_pool_memory(query);
780 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
786 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
787 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
790 query = get_pool_memory(PM_MESSAGE);
791 Mmsg(query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
792 edit_uint64(maxbytes, ed1), mr->MediaId);
793 if (!db_sql_query(ua->db, query, NULL, NULL)) {
794 bsendmsg(ua, "%s", db_strerror(ua->db));
796 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
798 free_pool_memory(query);
801 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
805 if (strcasecmp(val, _("yes")) == 0) {
807 } else if (strcasecmp(val, _("no")) == 0) {
810 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
813 query = get_pool_memory(PM_MESSAGE);
814 Mmsg(query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
815 recycle, mr->MediaId);
816 if (!db_sql_query(ua->db, query, NULL, NULL)) {
817 bsendmsg(ua, "%s", db_strerror(ua->db));
819 bsendmsg(ua, _("New Recycle flag is: %s\n"),
820 mr->Recycle==1?_("yes"):_("no"));
822 free_pool_memory(query);
825 /* Modify the Pool in which this Volume is located */
826 static void update_vol_pool(UAContext *ua, char *val, MEDIA_DBR *mr, POOL_DBR *opr)
831 memset(&pr, 0, sizeof(pr));
832 bstrncpy(pr.Name, val, sizeof(pr.Name));
833 if (!get_pool_dbr(ua, &pr)) {
836 mr->PoolId = pr.PoolId; /* set new PoolId */
839 query = get_pool_memory(PM_MESSAGE);
841 Mmsg(query, "UPDATE Media SET PoolId=%d WHERE MediaId=%u",
842 mr->PoolId, mr->MediaId);
843 if (!db_sql_query(ua->db, query, NULL, NULL)) {
844 bsendmsg(ua, "%s", db_strerror(ua->db));
846 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
848 if (!db_update_pool_record(ua->jcr, ua->db, opr)) {
849 bsendmsg(ua, "%s", db_strerror(ua->db));
852 if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
853 bsendmsg(ua, "%s", db_strerror(ua->db));
855 db_make_inchanger_unique(ua->jcr, ua->db, mr);
858 free_pool_memory(query);
862 * Refresh the Volume information from the Pool record
864 static void update_volfrompool(UAContext *ua, MEDIA_DBR *mr)
868 memset(&pr, 0, sizeof(pr));
869 pr.PoolId = mr->PoolId;
870 if (!db_get_pool_record(ua->jcr, ua->db, &pr) ||
871 !acl_access_ok(ua, Pool_ACL, pr.Name)) {
874 set_pool_dbr_defaults_in_media_dbr(mr, &pr);
875 if (!db_update_media_defaults(ua->jcr, ua->db, mr)) {
876 bsendmsg(ua, _("Error updating Volume record: ERR=%s"), db_strerror(ua->db));
878 bsendmsg(ua, _("Volume defaults updated from \"%s\" Pool record.\n"),
884 * Refresh the Volume information from the Pool record
887 static void update_all_vols_from_pool(UAContext *ua)
892 memset(&pr, 0, sizeof(pr));
893 memset(&mr, 0, sizeof(mr));
894 if (!get_pool_dbr(ua, &pr)) {
897 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
898 mr.PoolId = pr.PoolId;
899 if (!db_update_media_defaults(ua->jcr, ua->db, &mr)) {
900 bsendmsg(ua, _("Error updating Volume records: ERR=%s"), db_strerror(ua->db));
902 bsendmsg(ua, _("All Volume defaults updated from Pool record.\n"));
908 * Update a media record -- allows you to change the
909 * Volume status. E.g. if you want Bacula to stop
910 * writing on the volume, set it to anything other
913 static int update_volume(UAContext *ua)
921 N_("VolStatus"), /* 0 */
922 N_("VolRetention"), /* 1 */
923 N_("VolUse"), /* 2 */
924 N_("MaxVolJobs"), /* 3 */
925 N_("MaxVolFiles"), /* 4 */
926 N_("MaxVolBytes"), /* 5 */
927 N_("Recycle"), /* 6 */
929 N_("FromPool"), /* 8 */
930 N_("AllFromPool"), /* 9 */
933 for (int i=0; kw[i]; i++) {
936 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
937 if (i != 9 && !select_media_dbr(ua, &mr)) {
942 update_volstatus(ua, ua->argv[j], &mr);
945 update_volretention(ua, ua->argv[j], &mr);
948 update_voluseduration(ua, ua->argv[j], &mr);
951 update_volmaxjobs(ua, ua->argv[j], &mr);
954 update_volmaxfiles(ua, ua->argv[j], &mr);
957 update_volmaxbytes(ua, ua->argv[j], &mr);
960 update_volrecycle(ua, ua->argv[j], &mr);
963 memset(&pr, 0, sizeof(POOL_DBR));
964 pr.PoolId = mr.PoolId;
965 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
966 bsendmsg(ua, "%s", db_strerror(ua->db));
969 update_vol_pool(ua, ua->argv[j], &mr, &pr);
972 update_volfrompool(ua, &mr);
975 update_all_vols_from_pool(ua);
983 if (!select_media_dbr(ua, &mr)) {
986 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
987 start_prompt(ua, _("Parameters to modify:\n"));
988 add_prompt(ua, _("Volume Status"));
989 add_prompt(ua, _("Volume Retention Period"));
990 add_prompt(ua, _("Volume Use Duration"));
991 add_prompt(ua, _("Maximum Volume Jobs"));
992 add_prompt(ua, _("Maximum Volume Files"));
993 add_prompt(ua, _("Maximum Volume Bytes"));
994 add_prompt(ua, _("Recycle Flag"));
995 add_prompt(ua, _("Slot"));
996 add_prompt(ua, _("InChanger Flag"));
997 add_prompt(ua, _("Volume Files"));
998 add_prompt(ua, _("Pool"));
999 add_prompt(ua, _("Volume from Pool"));
1000 add_prompt(ua, _("All Volumes from Pool"));
1001 add_prompt(ua, _("Done"));
1002 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
1003 case 0: /* Volume Status */
1004 /* Modify Volume Status */
1005 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
1006 start_prompt(ua, _("Possible Values are:\n"));
1007 add_prompt(ua, "Append"); /* Better not translate these as */
1008 add_prompt(ua, "Archive"); /* They are known in the database code */
1009 add_prompt(ua, "Disabled");
1010 add_prompt(ua, "Full");
1011 add_prompt(ua, "Used");
1012 add_prompt(ua, "Cleaning");
1013 if (strcmp(mr.VolStatus, "Purged") == 0) {
1014 add_prompt(ua, "Recycle");
1016 add_prompt(ua, "Read-Only");
1017 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
1020 update_volstatus(ua, ua->cmd, &mr);
1022 case 1: /* Retention */
1023 bsendmsg(ua, _("Current retention period is: %s\n"),
1024 edit_utime(mr.VolRetention, ed1, sizeof(ed1)));
1025 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
1028 update_volretention(ua, ua->cmd, &mr);
1031 case 2: /* Use Duration */
1032 bsendmsg(ua, _("Current use duration is: %s\n"),
1033 edit_utime(mr.VolUseDuration, ed1, sizeof(ed1)));
1034 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
1037 update_voluseduration(ua, ua->cmd, &mr);
1040 case 3: /* Max Jobs */
1041 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
1042 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
1045 update_volmaxjobs(ua, ua->cmd, &mr);
1048 case 4: /* Max Files */
1049 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
1050 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
1053 update_volmaxfiles(ua, ua->cmd, &mr);
1056 case 5: /* Max Bytes */
1057 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
1058 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
1061 update_volmaxbytes(ua, ua->cmd, &mr);
1065 case 6: /* Recycle */
1066 bsendmsg(ua, _("Current recycle flag is: %s\n"),
1067 mr.Recycle==1?_("yes"):_("no"));
1068 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
1071 update_volrecycle(ua, ua->cmd, &mr);
1077 memset(&pr, 0, sizeof(POOL_DBR));
1078 pr.PoolId = mr.PoolId;
1079 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1080 bsendmsg(ua, "%s", db_strerror(ua->db));
1083 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
1084 if (!get_pint(ua, _("Enter new Slot: "))) {
1087 Slot = ua->pint32_val;
1088 if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
1089 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
1095 * Make sure to use db_update... rather than doing this directly,
1096 * so that any Slot is handled correctly.
1098 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1099 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1101 bsendmsg(ua, _("New Slot is: %d\n"), mr.Slot);
1105 case 8: /* InChanger */
1106 bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
1107 if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
1110 mr.InChanger = ua->pint32_val;
1112 * Make sure to use db_update... rather than doing this directly,
1113 * so that any Slot is handled correctly.
1115 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1116 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1118 bsendmsg(ua, _("New InChanger flag is: %d\n"), mr.InChanger);
1123 case 9: /* Volume Files */
1125 bsendmsg(ua, _("Warning changing Volume Files can result\n"
1126 "in loss of data on your Volume\n\n"));
1127 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1128 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1131 VolFiles = ua->pint32_val;
1132 if (VolFiles != (int)(mr.VolFiles + 1)) {
1133 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1134 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1138 query = get_pool_memory(PM_MESSAGE);
1139 Mmsg(query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1140 VolFiles, mr.MediaId);
1141 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1142 bsendmsg(ua, "%s", db_strerror(ua->db));
1144 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1146 free_pool_memory(query);
1149 case 10: /* Volume's Pool */
1150 memset(&pr, 0, sizeof(POOL_DBR));
1151 pr.PoolId = mr.PoolId;
1152 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1153 bsendmsg(ua, "%s", db_strerror(ua->db));
1156 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1157 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1160 update_vol_pool(ua, ua->cmd, &mr, &pr);
1164 update_volfrompool(ua, &mr);
1167 update_all_vols_from_pool(ua);
1169 default: /* Done or error */
1170 bsendmsg(ua, "Selection done.\n");
1178 * Update pool record -- pull info from current POOL resource
1180 static int update_pool(UAContext *ua)
1187 pool = get_pool_resource(ua);
1192 memset(&pr, 0, sizeof(pr));
1193 strcpy(pr.Name, pool->hdr.name);
1194 if (!get_pool_dbr(ua, &pr)) {
1198 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1200 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1202 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1203 id, db_strerror(ua->db));
1205 query = get_pool_memory(PM_MESSAGE);
1206 Mmsg(query, list_pool, pr.PoolId);
1207 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1208 free_pool_memory(query);
1209 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1214 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
1220 /* Try connecting for up to 15 seconds */
1221 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1222 store->hdr.name, store->address, store->SDport);
1223 if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
1224 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1227 Dmsg0(120, _("Connected to storage daemon\n"));
1228 sd = jcr->store_bsock;
1229 bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
1230 if (bnet_recv(sd) >= 0) {
1231 bsendmsg(ua, "%s", sd->msg);
1233 bnet_sig(sd, BNET_TERMINATE);
1235 jcr->store_bsock = NULL;
1239 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
1243 /* Connect to File daemon */
1245 ua->jcr->client = client;
1246 /* Try to connect for 15 seconds */
1247 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1248 client->hdr.name, client->address, client->FDport);
1249 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1250 bsendmsg(ua, _("Failed to connect to Client.\n"));
1253 Dmsg0(120, "Connected to file daemon\n");
1254 fd = ua->jcr->file_bsock;
1255 bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
1256 if (bnet_recv(fd) >= 0) {
1257 bsendmsg(ua, "%s", fd->msg);
1259 bnet_sig(fd, BNET_TERMINATE);
1261 ua->jcr->file_bsock = NULL;
1266 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
1268 STORE *store, **unique_store;
1269 CLIENT *client, **unique_client;
1273 debug_level = level;
1275 /* Count Storage items */
1278 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1280 unique_store = (STORE **) malloc(i * sizeof(STORE));
1281 /* Find Unique Storage address/port */
1282 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1284 unique_store[i++] = store;
1285 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1287 for (j=0; j<i; j++) {
1288 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1289 unique_store[j]->SDport == store->SDport) {
1295 unique_store[i++] = store;
1296 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1301 /* Call each unique Storage daemon */
1302 for (j=0; j<i; j++) {
1303 do_storage_setdebug(ua, unique_store[j], level, trace_flag);
1307 /* Count Client items */
1310 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1312 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1313 /* Find Unique Client address/port */
1314 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1316 unique_client[i++] = client;
1317 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1319 for (j=0; j<i; j++) {
1320 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1321 unique_client[j]->FDport == client->FDport) {
1327 unique_client[i++] = client;
1328 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1333 /* Call each unique File daemon */
1334 for (j=0; j<i; j++) {
1335 do_client_setdebug(ua, unique_client[j], level, trace_flag);
1337 free(unique_client);
1341 * setdebug level=nn all trace=1/0
1343 static int setdebug_cmd(UAContext *ua, const char *cmd)
1348 int trace_flag = -1;
1354 Dmsg1(120, "setdebug:%s:\n", cmd);
1357 i = find_arg_with_value(ua, _("level"));
1359 level = atoi(ua->argv[i]);
1362 if (!get_pint(ua, _("Enter new debug level: "))) {
1365 level = ua->pint32_val;
1368 /* Look for trace flag. -1 => not change */
1369 i = find_arg_with_value(ua, _("trace"));
1371 trace_flag = atoi(ua->argv[i]);
1372 if (trace_flag > 0) {
1377 /* General debug? */
1378 for (i=1; i<ua->argc; i++) {
1379 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1380 do_all_setdebug(ua, level, trace_flag);
1383 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1384 strcasecmp(ua->argk[i], _("director")) == 0) {
1385 debug_level = level;
1386 set_trace(trace_flag);
1389 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1390 strcasecmp(ua->argk[i], _("fd")) == 0) {
1393 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1395 do_client_setdebug(ua, client, level, trace_flag);
1399 client = select_client_resource(ua);
1401 do_client_setdebug(ua, client, level, trace_flag);
1406 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1407 strcasecmp(ua->argk[i], _("storage")) == 0 ||
1408 strcasecmp(ua->argk[i], _("sd")) == 0) {
1411 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1413 do_storage_setdebug(ua, store, level, trace_flag);
1417 store = get_storage_resource(ua, 0);
1419 do_storage_setdebug(ua, store, level, trace_flag);
1425 * We didn't find an appropriate keyword above, so
1428 start_prompt(ua, _("Available daemons are: \n"));
1429 add_prompt(ua, _("Director"));
1430 add_prompt(ua, _("Storage"));
1431 add_prompt(ua, _("Client"));
1432 add_prompt(ua, _("All"));
1433 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1434 case 0: /* Director */
1435 debug_level = level;
1436 set_trace(trace_flag);
1439 store = get_storage_resource(ua, 0);
1441 do_storage_setdebug(ua, store, level, trace_flag);
1445 client = select_client_resource(ua);
1447 do_client_setdebug(ua, client, level, trace_flag);
1451 do_all_setdebug(ua, level, trace_flag);
1460 * Turn debug tracing to file on/off
1462 static int trace_cmd(UAContext *ua, const char *cmd)
1466 if (ua->argc != 2) {
1467 if (!get_cmd(ua, _("Turn on or off? "))) {
1472 onoff = ua->argk[1];
1475 set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
1480 static int var_cmd(UAContext *ua, const char *cmd)
1482 POOLMEM *val = get_pool_memory(PM_FNAME);
1488 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1491 while (*var == ' ') { /* skip spaces */
1494 Dmsg1(100, "Var=%s:\n", var);
1495 variable_expansion(ua->jcr, var, &val);
1496 bsendmsg(ua, "%s\n", val);
1497 free_pool_memory(val);
1501 static int estimate_cmd(UAContext *ua, const char *cmd)
1504 CLIENT *client = NULL;
1505 FILESET *fileset = NULL;
1508 char since[MAXSTRING];
1511 jcr->JobLevel = L_FULL;
1512 for (int i=1; i<ua->argc; i++) {
1513 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1514 strcasecmp(ua->argk[i], _("fd")) == 0) {
1516 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1520 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1522 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1526 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1528 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1532 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1536 if (strcasecmp(ua->argk[i], _("level")) == 0) {
1537 if (!get_level_from_name(ua->jcr, ua->argv[i])) {
1538 bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
1543 if (!job && !(client && fileset)) {
1544 if (!(job = select_job_resource(ua))) {
1549 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1551 bsendmsg(ua, _("No job specified.\n"));
1556 client = job->client;
1559 fileset = job->fileset;
1561 jcr->client = client;
1562 jcr->fileset = fileset;
1564 ua->catalog = client->catalog;
1571 jcr->JobType = JT_BACKUP;
1572 init_jcr_job_record(jcr);
1574 if (!get_or_create_client_record(jcr)) {
1577 if (!get_or_create_fileset_record(jcr, &fsr)) {
1581 get_level_since_time(ua->jcr, since, sizeof(since));
1583 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1584 job->client->hdr.name, job->client->address, job->client->FDport);
1585 if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
1586 bsendmsg(ua, _("Failed to connect to Client.\n"));
1590 if (!send_include_list(jcr)) {
1591 bsendmsg(ua, _("Error sending include list.\n"));
1595 if (!send_exclude_list(jcr)) {
1596 bsendmsg(ua, _("Error sending exclude list.\n"));
1600 if (!send_level_command(jcr)) {
1604 bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1605 while (bnet_recv(jcr->file_bsock) >= 0) {
1606 bsendmsg(ua, "%s", jcr->file_bsock->msg);
1610 if (jcr->file_bsock) {
1611 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1612 bnet_close(jcr->file_bsock);
1613 jcr->file_bsock = NULL;
1622 static int time_cmd(UAContext *ua, const char *cmd)
1625 time_t ttime = time(NULL);
1627 localtime_r(&ttime, &tm);
1628 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1629 bsendmsg(ua, "%s\n", sdt);
1634 * reload the conf file
1636 extern "C" void reload_config(int sig);
1638 static int reload_cmd(UAContext *ua, const char *cmd)
1645 * Delete Pool records (should purge Media with it).
1647 * delete pool=<pool-name>
1648 * delete volume pool=<pool-name> volume=<name>
1649 * delete job jobid=xxx
1651 static int delete_cmd(UAContext *ua, const char *cmd)
1653 static const char *keywords[] = {
1664 switch (find_arg_keyword(ua, keywords)) {
1674 while ((i=find_arg(ua, _("jobid"))) > 0) {
1676 *ua->argk[i] = 0; /* zap keyword already visited */
1684 "In general it is not a good idea to delete either a\n"
1685 "Pool or a Volume since they may contain data.\n\n"));
1687 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1698 bsendmsg(ua, _("Nothing done.\n"));
1706 * delete_job has been modified to parse JobID lists like the
1708 * delete job JobID=3,4,6,7-11,14
1710 * Thanks to Phil Stracchino for the above addition.
1713 static void delete_job(UAContext *ua)
1718 int i = find_arg_with_value(ua, _("jobid"));
1720 if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1721 s = bstrdup(ua->argv[i]);
1724 * We could use strtok() here. But we're not going to, because:
1725 * (a) strtok() is deprecated, having been replaced by strsep();
1726 * (b) strtok() is broken in significant ways.
1727 * we could use strsep() instead, but it's not universally available.
1728 * so we grow our own using strchr().
1730 sep = strchr(tok, ',');
1731 while (sep != NULL) {
1733 if (strchr(tok, '-')) {
1734 delete_job_id_range(ua, tok);
1736 JobId = str_to_int64(tok);
1737 do_job_delete(ua, JobId);
1740 sep = strchr(tok, ',');
1742 /* pick up the last token */
1743 if (strchr(tok, '-')) {
1744 delete_job_id_range(ua, tok);
1746 JobId = str_to_int64(tok);
1747 do_job_delete(ua, JobId);
1752 JobId = str_to_int64(ua->argv[i]);
1753 do_job_delete(ua, JobId);
1755 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1758 JobId = ua->pint32_val;
1759 do_job_delete(ua, JobId);
1764 * we call delete_job_id_range to parse range tokens and iterate over ranges
1766 static void delete_job_id_range(UAContext *ua, char *tok)
1771 tok2 = strchr(tok, '-');
1774 j1 = str_to_int64(tok);
1775 j2 = str_to_int64(tok2);
1776 for (j=j1; j<=j2; j++) {
1777 do_job_delete(ua, j);
1782 * do_job_delete now performs the actual delete operation atomically
1783 * we always return 1 because C++ is pissy about void functions
1786 static void do_job_delete(UAContext *ua, JobId_t JobId)
1788 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1790 Mmsg(query, "DELETE FROM Job WHERE JobId=%u", JobId);
1791 db_sql_query(ua->db, query, NULL, (void *)NULL);
1792 Mmsg(query, "DELETE FROM File WHERE JobId=%u", JobId);
1793 db_sql_query(ua->db, query, NULL, (void *)NULL);
1794 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1795 db_sql_query(ua->db, query, NULL, (void *)NULL);
1796 free_pool_memory(query);
1797 bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1801 * Delete media records from database -- dangerous
1803 static int delete_volume(UAContext *ua)
1807 if (!select_media_dbr(ua, &mr)) {
1810 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1811 "and all Jobs saved on that volume from the Catalog\n"),
1814 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1817 if (ua->pint32_val) {
1818 db_delete_media_record(ua->jcr, ua->db, &mr);
1824 * Delete a pool record from the database -- dangerous
1826 static int delete_pool(UAContext *ua)
1830 memset(&pr, 0, sizeof(pr));
1832 if (!get_pool_dbr(ua, &pr)) {
1835 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1838 if (ua->pint32_val) {
1839 db_delete_pool_record(ua->jcr, ua->db, &pr);
1845 static void do_mount_cmd(UAContext *ua, const char *command)
1850 char dev_name[MAX_NAME_LENGTH];
1855 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1857 store = get_storage_resource(ua, 1);
1862 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1863 store->media_type, store->dev_name);
1866 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
1867 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1870 sd = jcr->store_bsock;
1871 bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
1872 bash_spaces(dev_name);
1873 bnet_fsend(sd, "%s %s", command, dev_name);
1874 while (bnet_recv(sd) >= 0) {
1875 bsendmsg(ua, "%s", sd->msg);
1877 bnet_sig(sd, BNET_TERMINATE);
1879 jcr->store_bsock = NULL;
1883 * mount [storage | device] <name>
1885 static int mount_cmd(UAContext *ua, const char *cmd)
1887 do_mount_cmd(ua, "mount"); /* mount */
1893 * unmount [storage | device] <name>
1895 static int unmount_cmd(UAContext *ua, const char *cmd)
1897 do_mount_cmd(ua, "unmount"); /* unmount */
1903 * release [storage | device] <name>
1905 static int release_cmd(UAContext *ua, const char *cmd)
1907 do_mount_cmd(ua, "release"); /* release */
1914 * use catalog=<name>
1916 static int use_cmd(UAContext *ua, const char *cmd)
1918 CAT *oldcatalog, *catalog;
1921 close_db(ua); /* close any previously open db */
1922 oldcatalog = ua->catalog;
1924 if (!(catalog = get_catalog_resource(ua))) {
1925 ua->catalog = oldcatalog;
1927 ua->catalog = catalog;
1930 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1931 ua->catalog->hdr.name, ua->catalog->db_name);
1936 int quit_cmd(UAContext *ua, const char *cmd)
1943 * Wait until no job is running
1945 int wait_cmd(UAContext *ua, const char *cmd)
1948 bmicrosleep(0, 200000); /* let job actually start */
1949 for (bool running=true; running; ) {
1953 if (jcr->JobId != 0) {
1955 free_locked_jcr(jcr);
1958 free_locked_jcr(jcr);
1969 static int help_cmd(UAContext *ua, const char *cmd)
1973 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1974 for (i=0; i<comsize; i++) {
1975 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1977 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1981 int qhelp_cmd(UAContext *ua, const char *cmd)
1985 for (i=0; i<comsize; i++) {
1986 bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
1991 static int version_cmd(UAContext *ua, const char *cmd)
1993 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1998 /* A bit brain damaged in that if the user has not done
1999 * a "use catalog xxx" command, we simply find the first
2000 * catalog resource and open it.
2002 int open_db(UAContext *ua)
2009 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
2012 bsendmsg(ua, _("Could not find a Catalog resource\n"));
2015 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
2016 ua->catalog->hdr.name, ua->catalog->db_name);
2020 ua->jcr->catalog = ua->catalog;
2022 Dmsg0(150, "Open database\n");
2023 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
2024 ua->catalog->db_password, ua->catalog->db_address,
2025 ua->catalog->db_port, ua->catalog->db_socket,
2026 ua->catalog->mult_db_connections);
2027 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
2028 bsendmsg(ua, _("Could not open database \"%s\".\n"),
2029 ua->catalog->db_name);
2031 bsendmsg(ua, "%s", db_strerror(ua->db));
2036 ua->jcr->db = ua->db;
2037 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
2041 void close_db(UAContext *ua)
2044 db_close_database(ua->jcr, ua->db);