3 * Bacula Director -- User Agent Commands
5 * Kern Sibbald, September MM
11 Copyright (C) 2000-2005 Kern Sibbald
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 /* Imported functions */
44 extern int status_cmd(UAContext *ua, const char *cmd);
45 extern int list_cmd(UAContext *ua, const char *cmd);
46 extern int llist_cmd(UAContext *ua, const char *cmd);
47 extern int show_cmd(UAContext *ua, const char *cmd);
48 extern int messagescmd(UAContext *ua, const char *cmd);
49 extern int autodisplay_cmd(UAContext *ua, const char *cmd);
50 extern int gui_cmd(UAContext *ua, const char *cmd);
51 extern int sqlquerycmd(UAContext *ua, const char *cmd);
52 extern int querycmd(UAContext *ua, const char *cmd);
53 extern int retentioncmd(UAContext *ua, const char *cmd);
54 extern int prunecmd(UAContext *ua, const char *cmd);
55 extern int purgecmd(UAContext *ua, const char *cmd);
56 extern int restore_cmd(UAContext *ua, const char *cmd);
57 extern int label_cmd(UAContext *ua, const char *cmd);
58 extern int relabel_cmd(UAContext *ua, const char *cmd);
59 extern int update_cmd(UAContext *ua, const char *cmd);
61 /* Forward referenced functions */
62 static int add_cmd(UAContext *ua, const char *cmd);
63 static int create_cmd(UAContext *ua, const char *cmd);
64 static int cancel_cmd(UAContext *ua, const char *cmd);
65 static int setdebug_cmd(UAContext *ua, const char *cmd);
66 static int trace_cmd(UAContext *ua, const char *cmd);
67 static int var_cmd(UAContext *ua, const char *cmd);
68 static int estimate_cmd(UAContext *ua, const char *cmd);
69 static int help_cmd(UAContext *ua, const char *cmd);
70 static int delete_cmd(UAContext *ua, const char *cmd);
71 static int use_cmd(UAContext *ua, const char *cmd);
72 static int unmount_cmd(UAContext *ua, const char *cmd);
73 static int version_cmd(UAContext *ua, const char *cmd);
74 static int automount_cmd(UAContext *ua, const char *cmd);
75 static int time_cmd(UAContext *ua, const char *cmd);
76 static int reload_cmd(UAContext *ua, const char *cmd);
77 static int delete_volume(UAContext *ua);
78 static int delete_pool(UAContext *ua);
79 static void delete_job(UAContext *ua);
80 static int mount_cmd(UAContext *ua, const char *cmd);
81 static int release_cmd(UAContext *ua, const char *cmd);
82 static int wait_cmd(UAContext *ua, const char *cmd);
83 static int setip_cmd(UAContext *ua, const char *cmd);
84 static int python_cmd(UAContext *ua, const char *cmd);
85 static void do_job_delete(UAContext *ua, JobId_t JobId);
86 static void delete_job_id_range(UAContext *ua, char *tok);
88 int qhelp_cmd(UAContext *ua, const char *cmd);
89 int quit_cmd(UAContext *ua, const char *cmd);
92 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; };
93 static struct cmdstruct commands[] = {
94 { N_("add"), add_cmd, _("add media to a pool")},
95 { N_("autodisplay"), autodisplay_cmd, _("autodisplay [on|off] -- console messages")},
96 { N_("automount"), automount_cmd, _("automount [on|off] -- after label")},
97 { N_("cancel"), cancel_cmd, _("cancel [<jobid=nnn> | <job=name>] -- cancel a job")},
98 { N_("create"), create_cmd, _("create DB Pool from resource")},
99 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
100 { N_("estimate"), estimate_cmd, _("performs FileSet estimate, listing gives full listing")},
101 { N_("exit"), quit_cmd, _("exit = quit")},
102 { N_("gui"), gui_cmd, _("gui [on|off] -- non-interactive gui mode")},
103 { N_("help"), help_cmd, _("print this command")},
104 { N_("list"), list_cmd, _("list [pools | jobs | jobtotals | media <pool=pool-name> | files <jobid=nn>]; from catalog")},
105 { N_("label"), label_cmd, _("label a tape")},
106 { N_("llist"), llist_cmd, _("full or long list like list command")},
107 { N_("messages"), messagescmd, _("messages")},
108 { N_("mount"), mount_cmd, _("mount <storage-name>")},
109 { N_("prune"), prunecmd, _("prune expired records from catalog")},
110 { N_("purge"), purgecmd, _("purge records from catalog")},
111 { N_("python"), python_cmd, _("python control commands")},
112 { N_("quit"), quit_cmd, _("quit")},
113 { N_("query"), querycmd, _("query catalog")},
114 { N_("restore"), restore_cmd, _("restore files")},
115 { N_("relabel"), relabel_cmd, _("relabel a tape")},
116 { N_("release"), release_cmd, _("release <storage-name>")},
117 { N_("reload"), reload_cmd, _("reload conf file")},
118 { N_("run"), run_cmd, _("run <job-name>")},
119 { N_("status"), status_cmd, _("status [storage | client]=<name>")},
120 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
121 { N_("setip"), setip_cmd, _("sets new client address -- if authorized")},
122 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
123 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
124 { N_("time"), time_cmd, _("print current time")},
125 { N_("trace"), trace_cmd, _("turn on/off trace to file")},
126 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
127 { N_("umount"), unmount_cmd, _("umount <storage-name> for old-time Unix guys")},
128 { N_("update"), update_cmd, _("update Volume, Pool or slots")},
129 { N_("use"), use_cmd, _("use catalog xxx")},
130 { N_("var"), var_cmd, _("does variable expansion")},
131 { N_("version"), version_cmd, _("print Director version")},
132 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
134 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
137 * Execute a command from the UA
139 int do_a_command(UAContext *ua, const char *cmd)
147 Dmsg1(900, "Command: %s\n", ua->UA_sock->msg);
152 len = strlen(ua->argk[0]);
153 for (i=0; i<comsize; i++) { /* search for command */
154 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
155 if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
158 stat = (*commands[i].func)(ua, cmd); /* go execute command */
164 bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
170 * This is a common routine used to stuff the Pool DB record defaults
171 * into the Media DB record just before creating a media (Volume)
174 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
176 mr->PoolId = pr->PoolId;
177 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
178 mr->Recycle = pr->Recycle;
179 mr->VolRetention = pr->VolRetention;
180 mr->VolUseDuration = pr->VolUseDuration;
181 mr->MaxVolJobs = pr->MaxVolJobs;
182 mr->MaxVolFiles = pr->MaxVolFiles;
183 mr->MaxVolBytes = pr->MaxVolBytes;
184 mr->LabelType = pr->LabelType;
189 * Add Volumes to an existing Pool
191 static int add_cmd(UAContext *ua, const char *cmd)
195 int num, i, max, startnum;
197 char name[MAX_NAME_LENGTH];
199 int Slot = 0, InChanger = 0;
202 "You probably don't want to be using this command since it\n"
203 "creates database records without labeling the Volumes.\n"
204 "You probably want to use the \"label\" command.\n\n"));
210 memset(&pr, 0, sizeof(pr));
211 memset(&mr, 0, sizeof(mr));
213 if (!get_pool_dbr(ua, &pr)) {
217 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
218 pr.MaxVols, pr.PoolType);
220 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
221 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
223 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
226 pr.MaxVols = ua->pint32_val;
231 if ((store = get_storage_resource(ua, 0)) != NULL) {
232 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
233 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
237 if (pr.MaxVols == 0) {
240 max = pr.MaxVols - pr.NumVols;
244 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
245 if (!get_pint(ua, buf)) {
248 num = ua->pint32_val;
249 if (num < 0 || num > max) {
250 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
257 if (!get_cmd(ua, _("Enter Volume name: "))) {
261 if (!get_cmd(ua, _("Enter base volume name: "))) {
265 /* Don't allow | in Volume name because it is the volume separator character */
266 if (!is_volume_name_legal(ua, ua->cmd)) {
269 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
270 bsendmsg(ua, _("Volume name too long.\n"));
273 if (strlen(ua->cmd) == 0) {
274 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
278 bstrncpy(name, ua->cmd, sizeof(name));
280 bstrncat(name, "%04d", sizeof(name));
283 if (!get_pint(ua, _("Enter the starting number: "))) {
286 startnum = ua->pint32_val;
288 bsendmsg(ua, _("Start number must be greater than zero.\n"));
298 if (store && store->autochanger) {
299 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
302 Slot = ua->pint32_val;
303 if (!get_yesno(ua, _("InChanger? yes/no: "))) {
306 InChanger = ua->pint32_val;
309 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
310 for (i=startnum; i < num+startnum; i++) {
311 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
313 mr.InChanger = InChanger;
314 mr.StorageId = store->StorageId;
315 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
316 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
317 bsendmsg(ua, "%s", db_strerror(ua->db));
321 first_id = mr.PoolId;
325 Dmsg0(200, "Update pool record.\n");
326 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
327 bsendmsg(ua, "%s", db_strerror(ua->db));
330 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
336 * Turn auto mount on/off
341 int automount_cmd(UAContext *ua, const char *cmd)
346 if (!get_cmd(ua, _("Turn on or off? "))) {
354 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
362 static int cancel_cmd(UAContext *ua, const char *cmd)
367 char JobName[MAX_NAME_LENGTH];
373 for (i=1; i<ua->argc; i++) {
374 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
379 JobId = str_to_int64(ua->argv[i]);
380 if (!(jcr=get_jcr_by_id(JobId))) {
381 bsendmsg(ua, _("JobId %d is not running.\n"), JobId);
385 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
389 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
390 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
396 /* If we still do not have a jcr,
397 * throw up a list and ask the user to select one.
401 /* Count Jobs running */
404 if (jcr->JobId == 0) { /* this is us */
405 free_locked_jcr(jcr);
408 free_locked_jcr(jcr);
414 bsendmsg(ua, _("No Jobs running.\n"));
417 start_prompt(ua, _("Select Job:\n"));
420 if (jcr->JobId == 0) { /* this is us */
421 free_locked_jcr(jcr);
424 bsnprintf(buf, sizeof(buf), "JobId=%d Job=%s", jcr->JobId, jcr->Job);
426 free_locked_jcr(jcr);
430 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), buf, sizeof(buf)) < 0) {
434 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
438 /* NOTE! This increments the ref_count */
439 sscanf(buf, "JobId=%d Job=%127s", &njobs, JobName);
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 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
462 bstrncpy(pr->PoolType, pool->pool_type, sizeof(pr->PoolType));
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->LabelType = pool->LabelType;
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 bstrncpy(pr->LabelFormat, pool->label_format, sizeof(pr->LabelFormat));
489 bstrncpy(pr->LabelFormat, "*", sizeof(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 bstrncpy(pr.Name, pool->hdr.name, sizeof(pr.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,
575 director->scripts_directory, "DirStartUp");
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);
618 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
623 set_storage(jcr, store);
624 /* Try connecting for up to 15 seconds */
625 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
626 store->hdr.name, store->address, store->SDport);
627 if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
628 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
631 Dmsg0(120, _("Connected to storage daemon\n"));
632 sd = jcr->store_bsock;
633 bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
634 if (bnet_recv(sd) >= 0) {
635 bsendmsg(ua, "%s", sd->msg);
637 bnet_sig(sd, BNET_TERMINATE);
639 jcr->store_bsock = NULL;
643 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
647 /* Connect to File daemon */
649 ua->jcr->client = client;
650 /* Try to connect for 15 seconds */
651 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
652 client->hdr.name, client->address, client->FDport);
653 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
654 bsendmsg(ua, _("Failed to connect to Client.\n"));
657 Dmsg0(120, "Connected to file daemon\n");
658 fd = ua->jcr->file_bsock;
659 bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
660 if (bnet_recv(fd) >= 0) {
661 bsendmsg(ua, "%s", fd->msg);
663 bnet_sig(fd, BNET_TERMINATE);
665 ua->jcr->file_bsock = NULL;
670 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
672 STORE *store, **unique_store;
673 CLIENT *client, **unique_client;
679 /* Count Storage items */
683 foreach_res(store, R_STORAGE) {
686 unique_store = (STORE **) malloc(i * sizeof(STORE));
687 /* Find Unique Storage address/port */
688 store = (STORE *)GetNextRes(R_STORAGE, NULL);
690 unique_store[i++] = store;
691 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
693 for (j=0; j<i; j++) {
694 if (strcmp(unique_store[j]->address, store->address) == 0 &&
695 unique_store[j]->SDport == store->SDport) {
701 unique_store[i++] = store;
702 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
707 /* Call each unique Storage daemon */
708 for (j=0; j<i; j++) {
709 do_storage_setdebug(ua, unique_store[j], level, trace_flag);
713 /* Count Client items */
717 foreach_res(client, R_CLIENT) {
720 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
721 /* Find Unique Client address/port */
722 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
724 unique_client[i++] = client;
725 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
727 for (j=0; j<i; j++) {
728 if (strcmp(unique_client[j]->address, client->address) == 0 &&
729 unique_client[j]->FDport == client->FDport) {
735 unique_client[i++] = client;
736 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
741 /* Call each unique File daemon */
742 for (j=0; j<i; j++) {
743 do_client_setdebug(ua, unique_client[j], level, trace_flag);
749 * setdebug level=nn all trace=1/0
751 static int setdebug_cmd(UAContext *ua, const char *cmd)
762 Dmsg1(120, "setdebug:%s:\n", cmd);
765 i = find_arg_with_value(ua, _("level"));
767 level = atoi(ua->argv[i]);
770 if (!get_pint(ua, _("Enter new debug level: "))) {
773 level = ua->pint32_val;
776 /* Look for trace flag. -1 => not change */
777 i = find_arg_with_value(ua, _("trace"));
779 trace_flag = atoi(ua->argv[i]);
780 if (trace_flag > 0) {
786 for (i=1; i<ua->argc; i++) {
787 if (strcasecmp(ua->argk[i], _("all")) == 0) {
788 do_all_setdebug(ua, level, trace_flag);
791 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
792 strcasecmp(ua->argk[i], _("director")) == 0) {
794 set_trace(trace_flag);
797 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
798 strcasecmp(ua->argk[i], _("fd")) == 0) {
801 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
803 do_client_setdebug(ua, client, level, trace_flag);
807 client = select_client_resource(ua);
809 do_client_setdebug(ua, client, level, trace_flag);
814 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
815 strcasecmp(ua->argk[i], _("storage")) == 0 ||
816 strcasecmp(ua->argk[i], _("sd")) == 0) {
819 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
821 do_storage_setdebug(ua, store, level, trace_flag);
825 store = get_storage_resource(ua, 0);
827 do_storage_setdebug(ua, store, level, trace_flag);
833 * We didn't find an appropriate keyword above, so
836 start_prompt(ua, _("Available daemons are: \n"));
837 add_prompt(ua, _("Director"));
838 add_prompt(ua, _("Storage"));
839 add_prompt(ua, _("Client"));
840 add_prompt(ua, _("All"));
841 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
842 case 0: /* Director */
844 set_trace(trace_flag);
847 store = get_storage_resource(ua, 0);
849 do_storage_setdebug(ua, store, level, trace_flag);
853 client = select_client_resource(ua);
855 do_client_setdebug(ua, client, level, trace_flag);
859 do_all_setdebug(ua, level, trace_flag);
868 * Turn debug tracing to file on/off
870 static int trace_cmd(UAContext *ua, const char *cmd)
875 if (!get_cmd(ua, _("Turn on or off? "))) {
883 set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
888 static int var_cmd(UAContext *ua, const char *cmd)
890 POOLMEM *val = get_pool_memory(PM_FNAME);
896 for (var=ua->cmd; *var != ' '; ) { /* skip command */
899 while (*var == ' ') { /* skip spaces */
902 Dmsg1(100, "Var=%s:\n", var);
903 variable_expansion(ua->jcr, var, &val);
904 bsendmsg(ua, "%s\n", val);
905 free_pool_memory(val);
909 static int estimate_cmd(UAContext *ua, const char *cmd)
912 CLIENT *client = NULL;
913 FILESET *fileset = NULL;
916 char since[MAXSTRING];
919 jcr->JobLevel = L_FULL;
920 for (int i=1; i<ua->argc; i++) {
921 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
922 strcasecmp(ua->argk[i], _("fd")) == 0) {
924 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
928 if (strcasecmp(ua->argk[i], _("job")) == 0) {
930 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
934 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
936 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
940 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
944 if (strcasecmp(ua->argk[i], _("level")) == 0) {
945 if (!get_level_from_name(ua->jcr, ua->argv[i])) {
946 bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
951 if (!job && !(client && fileset)) {
952 if (!(job = select_job_resource(ua))) {
957 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
959 bsendmsg(ua, _("No job specified.\n"));
964 client = job->client;
967 fileset = job->fileset;
969 jcr->client = client;
970 jcr->fileset = fileset;
972 ua->catalog = client->catalog;
979 jcr->JobType = JT_BACKUP;
980 init_jcr_job_record(jcr);
982 if (!get_or_create_client_record(jcr)) {
985 if (!get_or_create_fileset_record(jcr, &fsr)) {
989 get_level_since_time(ua->jcr, since, sizeof(since));
991 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
992 job->client->hdr.name, job->client->address, job->client->FDport);
993 if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
994 bsendmsg(ua, _("Failed to connect to Client.\n"));
998 if (!send_include_list(jcr)) {
999 bsendmsg(ua, _("Error sending include list.\n"));
1003 if (!send_exclude_list(jcr)) {
1004 bsendmsg(ua, _("Error sending exclude list.\n"));
1008 if (!send_level_command(jcr)) {
1012 bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1013 while (bnet_recv(jcr->file_bsock) >= 0) {
1014 bsendmsg(ua, "%s", jcr->file_bsock->msg);
1018 if (jcr->file_bsock) {
1019 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1020 bnet_close(jcr->file_bsock);
1021 jcr->file_bsock = NULL;
1030 static int time_cmd(UAContext *ua, const char *cmd)
1033 time_t ttime = time(NULL);
1035 localtime_r(&ttime, &tm);
1036 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1037 bsendmsg(ua, "%s\n", sdt);
1042 * reload the conf file
1044 extern "C" void reload_config(int sig);
1046 static int reload_cmd(UAContext *ua, const char *cmd)
1053 * Delete Pool records (should purge Media with it).
1055 * delete pool=<pool-name>
1056 * delete volume pool=<pool-name> volume=<name>
1059 static int delete_cmd(UAContext *ua, const char *cmd)
1061 static const char *keywords[] = {
1071 switch (find_arg_keyword(ua, keywords)) {
1080 while ((i=find_arg(ua, _("jobid"))) > 0) {
1082 *ua->argk[i] = 0; /* zap keyword already visited */
1090 "In general it is not a good idea to delete either a\n"
1091 "Pool or a Volume since they may contain data.\n\n"));
1093 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1104 bsendmsg(ua, _("Nothing done.\n"));
1112 * delete_job has been modified to parse JobID lists like the
1114 * delete job JobID=3,4,6,7-11,14
1116 * Thanks to Phil Stracchino for the above addition.
1119 static void delete_job(UAContext *ua)
1124 int i = find_arg_with_value(ua, _("jobid"));
1126 if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1127 s = bstrdup(ua->argv[i]);
1130 * We could use strtok() here. But we're not going to, because:
1131 * (a) strtok() is deprecated, having been replaced by strsep();
1132 * (b) strtok() is broken in significant ways.
1133 * we could use strsep() instead, but it's not universally available.
1134 * so we grow our own using strchr().
1136 sep = strchr(tok, ',');
1137 while (sep != NULL) {
1139 if (strchr(tok, '-')) {
1140 delete_job_id_range(ua, tok);
1142 JobId = str_to_int64(tok);
1143 do_job_delete(ua, JobId);
1146 sep = strchr(tok, ',');
1148 /* pick up the last token */
1149 if (strchr(tok, '-')) {
1150 delete_job_id_range(ua, tok);
1152 JobId = str_to_int64(tok);
1153 do_job_delete(ua, JobId);
1158 JobId = str_to_int64(ua->argv[i]);
1159 do_job_delete(ua, JobId);
1161 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1164 JobId = ua->int64_val;
1165 do_job_delete(ua, JobId);
1170 * we call delete_job_id_range to parse range tokens and iterate over ranges
1172 static void delete_job_id_range(UAContext *ua, char *tok)
1177 tok2 = strchr(tok, '-');
1180 j1 = str_to_int64(tok);
1181 j2 = str_to_int64(tok2);
1182 for (j=j1; j<=j2; j++) {
1183 do_job_delete(ua, j);
1188 * do_job_delete now performs the actual delete operation atomically
1189 * we always return 1 because C++ is pissy about void functions
1192 static void do_job_delete(UAContext *ua, JobId_t JobId)
1194 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1197 Mmsg(query, "DELETE FROM Job WHERE JobId=%s", edit_int64(JobId, ed1));
1198 db_sql_query(ua->db, query, NULL, (void *)NULL);
1199 Mmsg(query, "DELETE FROM File WHERE JobId=%s", edit_int64(JobId, ed1));
1200 db_sql_query(ua->db, query, NULL, (void *)NULL);
1201 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%s", edit_int64(JobId, ed1));
1202 db_sql_query(ua->db, query, NULL, (void *)NULL);
1203 free_pool_memory(query);
1204 bsendmsg(ua, _("Job %s and associated records deleted from the catalog.\n"), edit_int64(JobId, ed1));
1208 * Delete media records from database -- dangerous
1210 static int delete_volume(UAContext *ua)
1214 if (!select_media_dbr(ua, &mr)) {
1217 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1218 "and all Jobs saved on that volume from the Catalog\n"),
1221 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1224 if (ua->pint32_val) {
1225 db_delete_media_record(ua->jcr, ua->db, &mr);
1231 * Delete a pool record from the database -- dangerous
1233 static int delete_pool(UAContext *ua)
1237 memset(&pr, 0, sizeof(pr));
1239 if (!get_pool_dbr(ua, &pr)) {
1242 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1245 if (ua->pint32_val) {
1246 db_delete_pool_record(ua->jcr, ua->db, &pr);
1252 static void do_mount_cmd(UAContext *ua, const char *command)
1257 char dev_name[MAX_NAME_LENGTH];
1262 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1264 store = get_storage_resource(ua, 1);
1269 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1270 store->media_type, store->dev_name());
1272 set_storage(jcr, store);
1273 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
1274 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1277 sd = jcr->store_bsock;
1278 bstrncpy(dev_name, store->dev_name(), sizeof(dev_name));
1279 bash_spaces(dev_name);
1280 bnet_fsend(sd, "%s %s", command, dev_name);
1281 while (bnet_recv(sd) >= 0) {
1282 bsendmsg(ua, "%s", sd->msg);
1284 bnet_sig(sd, BNET_TERMINATE);
1286 jcr->store_bsock = NULL;
1290 * mount [storage | device] <name>
1292 static int mount_cmd(UAContext *ua, const char *cmd)
1294 do_mount_cmd(ua, "mount"); /* mount */
1300 * unmount [storage | device] <name>
1302 static int unmount_cmd(UAContext *ua, const char *cmd)
1304 do_mount_cmd(ua, "unmount"); /* unmount */
1310 * release [storage | device] <name>
1312 static int release_cmd(UAContext *ua, const char *cmd)
1314 do_mount_cmd(ua, "release"); /* release */
1321 * use catalog=<name>
1323 static int use_cmd(UAContext *ua, const char *cmd)
1325 CAT *oldcatalog, *catalog;
1328 close_db(ua); /* close any previously open db */
1329 oldcatalog = ua->catalog;
1331 if (!(catalog = get_catalog_resource(ua))) {
1332 ua->catalog = oldcatalog;
1334 ua->catalog = catalog;
1337 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1338 ua->catalog->hdr.name, ua->catalog->db_name);
1343 int quit_cmd(UAContext *ua, const char *cmd)
1350 * Wait until no job is running
1352 int wait_cmd(UAContext *ua, const char *cmd)
1355 bmicrosleep(0, 200000); /* let job actually start */
1356 for (bool running=true; running; ) {
1360 if (jcr->JobId != 0) {
1362 free_locked_jcr(jcr);
1365 free_locked_jcr(jcr);
1376 static int help_cmd(UAContext *ua, const char *cmd)
1380 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1381 for (i=0; i<comsize; i++) {
1382 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1384 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1388 int qhelp_cmd(UAContext *ua, const char *cmd)
1392 for (i=0; i<comsize; i++) {
1393 bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
1398 static int version_cmd(UAContext *ua, const char *cmd)
1400 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1405 /* A bit brain damaged in that if the user has not done
1406 * a "use catalog xxx" command, we simply find the first
1407 * catalog resource and open it.
1409 int open_db(UAContext *ua)
1416 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1419 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1422 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1423 ua->catalog->hdr.name, ua->catalog->db_name);
1427 ua->jcr->catalog = ua->catalog;
1429 Dmsg0(150, "Open database\n");
1430 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1431 ua->catalog->db_password, ua->catalog->db_address,
1432 ua->catalog->db_port, ua->catalog->db_socket,
1433 ua->catalog->mult_db_connections);
1434 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
1435 bsendmsg(ua, _("Could not open database \"%s\".\n"),
1436 ua->catalog->db_name);
1438 bsendmsg(ua, "%s", db_strerror(ua->db));
1443 ua->jcr->db = ua->db;
1444 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1448 void close_db(UAContext *ua)
1451 db_close_database(ua->jcr, ua->db);