3 * Bacula Director -- User Agent Database Purge Command
5 * Purges Files from specific JobIds
7 * Purges Jobs from Volumes
9 * Kern Sibbald, February MMII
13 Copyright (C) 2002 Kern Sibbald and John Walker
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
36 /* Forward referenced functions */
37 int purge_files_from_client(UAContext *ua, CLIENT *client);
38 int purge_jobs_from_client(UAContext *ua, CLIENT *client);
39 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr );
40 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
41 void purge_files_from_job(UAContext *ua, JOB_DBR *jr);
44 #define MAX_DEL_LIST_LEN 1000000
47 static char *select_jobsfiles_from_client =
48 "SELECT JobId FROM Job "
52 static char *select_jobs_from_client =
53 "SELECT JobId, PurgedFiles FROM Job "
57 /* In memory list of JobIds */
58 struct s_file_del_ctx {
60 int num_ids; /* ids stored */
61 int max_ids; /* size of array */
62 int num_del; /* number deleted */
63 int tot_ids; /* total to process */
66 struct s_job_del_ctx {
67 JobId_t *JobId; /* array of JobIds */
68 char *PurgedFiles; /* Array of PurgedFile flags */
69 int num_ids; /* ids stored */
70 int max_ids; /* size of array */
71 int num_del; /* number deleted */
72 int tot_ids; /* total to process */
81 * Called here to count entries to be deleted
83 static int file_count_handler(void *ctx, int num_fields, char **row)
85 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
91 static int job_count_handler(void *ctx, int num_fields, char **row)
93 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
100 * Called here to make in memory list of JobIds to be
101 * deleted and the associated PurgedFiles flag.
102 * The in memory list will then be transversed
103 * to issue the SQL DELETE commands. Note, the list
104 * is allowed to get to MAX_DEL_LIST_LEN to limit the
105 * maximum malloc'ed memory.
107 static int job_delete_handler(void *ctx, int num_fields, char **row)
109 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
111 if (del->num_ids == MAX_DEL_LIST_LEN) {
114 if (del->num_ids == del->max_ids) {
115 del->max_ids = (del->max_ids * 3) / 2;
116 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) * del->max_ids);
117 del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
119 del->JobId[del->num_ids] = (JobId_t)strtod(row[0], NULL);
120 del->PurgedFiles[del->num_ids++] = (char)atoi(row[0]);
124 static int file_delete_handler(void *ctx, int num_fields, char **row)
126 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
128 if (del->num_ids == MAX_DEL_LIST_LEN) {
131 if (del->num_ids == del->max_ids) {
132 del->max_ids = (del->max_ids * 3) / 2;
133 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
136 del->JobId[del->num_ids++] = (JobId_t)strtod(row[0], NULL);
140 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr ) {} /* ***FIXME*** implement */
141 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr) {} /* ***FIXME*** implement */
146 * Purge records from database
148 * Purge Files from [Job|JobId|Client|Volume]
149 * Purge Jobs from [Client|Volume]
151 * N.B. Not all above is implemented yet.
153 int purgecmd(UAContext *ua, char *cmd)
159 static char *keywords[] = {
164 static char *files_keywords[] = {
171 static char *jobs_keywords[] = {
177 "This command is DANGEROUUS!\n"
178 "It purges (deletes) all Files from a Job,\n"
179 "JobId, Client or Volume; or it purges (deletes)\n"
180 "all Jobs from a Client or Volume. Normally you\n"
181 "should use the PRUNE command instead.\n"));
186 switch (find_arg_keyword(ua, keywords)) {
189 switch(find_arg_keyword(ua, files_keywords)) {
192 if (get_job_dbr(ua, &jr)) {
193 purge_files_from_job(ua, &jr);
197 client = select_client_resource(ua);
198 purge_files_from_client(ua, client);
201 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
202 purge_files_from_volume(ua, &pr, &mr);
208 switch(find_arg_keyword(ua, jobs_keywords)) {
210 client = select_client_resource(ua);
211 purge_jobs_from_client(ua, client);
214 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
215 purge_jobs_from_volume(ua, &pr, &mr);
222 switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
224 client = select_client_resource(ua);
228 purge_files_from_client(ua, client);
231 client = select_client_resource(ua);
235 purge_jobs_from_client(ua, client);
242 * Prune File records from the database. For any Job which
243 * is older than the retention period, we unconditionally delete
244 * all File records for that Job. This is simple enough that no
245 * temporary tables are needed. We simply make an in memory list of
246 * the JobIds meeting the prune conditions, then delete all File records
247 * pointing to each of those JobIds.
249 int purge_files_from_client(UAContext *ua, CLIENT *client)
251 struct s_file_del_ctx del;
252 char *query = (char *)get_pool_memory(PM_MESSAGE);
256 memset(&cr, 0, sizeof(cr));
257 strcpy(cr.Name, client->hdr.name);
258 if (!db_create_client_record(ua->db, &cr)) {
268 Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
270 Dmsg1(050, "select sql=%s\n", query);
272 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
273 bsendmsg(ua, "%s", db_strerror(ua->db));
274 Dmsg0(050, "Count failed\n");
278 if (del.tot_ids == 0) {
279 bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
280 client->hdr.name, client->catalog->hdr.name);
284 if (del.tot_ids < MAX_DEL_LIST_LEN) {
285 del.max_ids = del.tot_ids + 1;
287 del.max_ids = MAX_DEL_LIST_LEN;
291 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
293 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
295 for (i=0; i < del.num_ids; i++) {
296 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
297 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
298 db_sql_query(ua->db, query, NULL, (void *)NULL);
300 * Now mark Job as having files purged. This is necessary to
301 * avoid having too many Jobs to process in future prunings. If
302 * we don't do this, the number of JobId's in our in memory list
303 * will grow very large.
305 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
306 db_sql_query(ua->db, query, NULL, (void *)NULL);
307 Dmsg1(050, "Del sql=%s\n", query);
309 bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
310 client->hdr.name, client->catalog->hdr.name);
316 free_pool_memory(query);
323 * Purging Jobs is a bit more complicated than purging Files
324 * because we delete Job records only if there is a more current
325 * backup of the FileSet. Otherwise, we keep the Job record.
326 * In other words, we never delete the only Job record that
327 * contains a current backup of a FileSet. This prevents the
328 * Volume from being recycled and destroying a current backup.
330 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
332 struct s_job_del_ctx del;
333 char *query = (char *)get_pool_memory(PM_MESSAGE);
337 memset(&cr, 0, sizeof(cr));
338 strcpy(cr.Name, client->hdr.name);
339 if (!db_create_client_record(ua->db, &cr)) {
349 Mmsg(&query, select_jobs_from_client, cr.ClientId);
351 Dmsg1(050, "select sql=%s\n", query);
353 if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
354 bsendmsg(ua, "%s", db_strerror(ua->db));
355 Dmsg0(050, "Count failed\n");
358 if (del.tot_ids == 0) {
359 bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
360 client->hdr.name, client->catalog->hdr.name);
364 if (del.tot_ids < MAX_DEL_LIST_LEN) {
365 del.max_ids = del.tot_ids + 1;
367 del.max_ids = MAX_DEL_LIST_LEN;
374 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
375 del.PurgedFiles = (char *)malloc(del.max_ids);
377 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
380 * OK, now we have the list of JobId's to be purged, first check
381 * if the Files have been purged, if not, purge (delete) them.
382 * Then delete the Job entry, and finally and JobMedia records.
384 for (i=0; i < del.num_ids; i++) {
385 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
386 if (!del.PurgedFiles[i]) {
387 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
388 db_sql_query(ua->db, query, NULL, (void *)NULL);
389 Dmsg1(050, "Del sql=%s\n", query);
392 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
393 db_sql_query(ua->db, query, NULL, (void *)NULL);
394 Dmsg1(050, "Del sql=%s\n", query);
396 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
397 db_sql_query(ua->db, query, NULL, (void *)NULL);
398 Dmsg1(050, "Del sql=%s\n", query);
400 bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
401 client->hdr.name, client->catalog->hdr.name);
407 if (del.PurgedFiles) {
408 free(del.PurgedFiles);
410 free_pool_memory(query);
414 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
416 char *query = (char *)get_pool_memory(PM_MESSAGE);
418 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
419 db_sql_query(ua->db, query, NULL, (void *)NULL);
421 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
422 db_sql_query(ua->db, query, NULL, (void *)NULL);
424 free_pool_memory(query);