-
Notifications
You must be signed in to change notification settings - Fork 257
/
ua_prune.cc
958 lines (819 loc) · 26.9 KB
/
ua_prune.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2002-2009 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Kern Sibbald, February MMII
*/
/**
* @file
* User Agent Database prune Command
*
* Applies retention periods
*/
#include "include/bareos.h"
#include "dird.h"
#include "dird/dird_globals.h"
#include "dird/ua_input.h"
#include "cats/sql.h"
#include "dird/ua_db.h"
#include "dird/ua_select.h"
#include "dird/ua_prune.h"
#include "dird/ua_purge.h"
#include "lib/edit.h"
#include "lib/parse_conf.h"
namespace directordaemon {
/* Forward referenced functions */
static bool PruneDirectory(UaContext* ua, ClientResource* client);
static bool PruneStats(UaContext* ua, utime_t retention);
static bool GrowDelList(del_ctx* del);
/**
* Called here to count entries to be deleted
*/
int DelCountHandler(void* ctx, int num_fields, char** row)
{
s_count_ctx* cnt = static_cast<s_count_ctx*>(ctx);
if (row[0]) {
cnt->count = str_to_int64(row[0]);
} else {
cnt->count = 0;
}
return 0;
}
/**
* Called here to make in memory list of JobIds to be
* deleted and the associated PurgedFiles flag.
* The in memory list will then be traversed
* to issue the SQL DELETE commands. Note, the list
* is allowed to get to MAX_DEL_LIST_LEN to limit the
* maximum malloc'ed memory.
*/
int JobDeleteHandler(void* ctx, int num_fields, char** row)
{
del_ctx* del = static_cast<del_ctx*>(ctx);
if (!GrowDelList(del)) { return 1; }
del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]);
Dmsg2(60, "JobDeleteHandler row=%d val=%d\n", del->num_ids,
del->JobId[del->num_ids]);
del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[1]);
return 0;
}
int FileDeleteHandler(void* ctx, int num_fields, char** row)
{
del_ctx* del = static_cast<del_ctx*>(ctx);
if (!GrowDelList(del)) { return 1; }
del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
// Dmsg2(150, "row=%d val=%d\n", del->num_ids-1, del->JobId[del->num_ids-1]);
return 0;
}
/**
* Prune records from database
*/
bool PruneCmd(UaContext* ua, const char* cmd)
{
ClientResource* client;
PoolResource* pool;
PoolDbRecord pr;
MediaDbRecord mr;
utime_t retention;
int kw;
const char* permission_denied_message =
_("Permission denied: need full %s permission.\n");
static const char* keywords[] = {NT_("Files"), NT_("Jobs"),
NT_("Volume"), NT_("Stats"),
NT_("Directory"), NULL};
/*
* All prune commands might target jobs that reside on different storages.
* Instead of checking all of them,
* we require full permission on jobs and storages.
* Client and Pool permissions are checked at the individual subcommands.
*/
if (ua->AclHasRestrictions(Job_ACL)) {
ua->ErrorMsg(permission_denied_message, "job");
return false;
}
if (ua->AclHasRestrictions(Storage_ACL)) {
ua->ErrorMsg(permission_denied_message, "storage");
return false;
}
if (!OpenClientDb(ua, true)) { return false; }
/*
* First search args
*/
kw = FindArgKeyword(ua, keywords);
if (kw < 0 || kw > 4) {
/*
* No args, so ask user
*/
kw = DoKeywordPrompt(ua, _("Choose item to prune"), keywords);
}
switch (kw) {
case 0: /* prune files */
if (!(client = get_client_resource(ua))) { return false; }
if ((FindArgWithValue(ua, NT_("pool")) >= 0) ||
ua->AclHasRestrictions(Pool_ACL)) {
pool = get_pool_resource(ua);
} else {
pool = NULL;
}
/*
* Pool File Retention takes precedence over client File Retention
*/
if (pool && pool->FileRetention > 0) {
if (!ConfirmRetention(ua, &pool->FileRetention, "File")) {
return false;
}
} else if (!ConfirmRetention(ua, &client->FileRetention, "File")) {
return false;
}
PruneFiles(ua, client, pool);
return true;
case 1: { /* prune jobs */
int jobtype = -1;
if (!(client = get_client_resource(ua))) { return false; }
if ((FindArgWithValue(ua, NT_("pool")) >= 0) ||
ua->AclHasRestrictions(Pool_ACL)) {
pool = get_pool_resource(ua);
} else {
pool = NULL;
}
/*
* Ask what jobtype to prune.
*/
if (!GetUserJobTypeSelection(ua, &jobtype)) { return false; }
/*
* Verify that result jobtype is valid (this should always be the case).
*/
if (jobtype < 0) { return false; }
/*
* Pool Job Retention takes precedence over client Job Retention
*/
if (pool && pool->JobRetention > 0) {
if (!ConfirmRetention(ua, &pool->JobRetention, "Job")) { return false; }
} else if (!ConfirmRetention(ua, &client->JobRetention, "Job")) {
return false;
}
return PruneJobs(ua, client, pool, jobtype);
}
case 2: /* prune volume */
if (ua->AclHasRestrictions(Client_ACL)) {
ua->ErrorMsg(permission_denied_message, "client");
return false;
}
if (!SelectPoolAndMediaDbr(ua, &pr, &mr)) { return false; }
if (mr.Enabled == VOL_ARCHIVED) {
ua->ErrorMsg(_("Cannot prune Volume \"%s\" because it is archived.\n"),
mr.VolumeName);
return false;
}
if (!ConfirmRetention(ua, &mr.VolRetention, "Volume")) { return false; }
return PruneVolume(ua, &mr);
case 3: /* prune stats */
if (!me->stats_retention) { return false; }
retention = me->stats_retention;
if (ua->AclHasRestrictions(Client_ACL)) {
ua->ErrorMsg(permission_denied_message, "client");
return false;
}
if (ua->AclHasRestrictions(Pool_ACL)) {
ua->ErrorMsg(permission_denied_message, "pool");
return false;
}
if (!ConfirmRetention(ua, &retention, "Statistics")) { return false; }
return PruneStats(ua, retention);
case 4: /* prune directory */
if (ua->AclHasRestrictions(Pool_ACL)) {
ua->ErrorMsg(permission_denied_message, "pool");
return false;
}
if ((FindArgWithValue(ua, NT_("client")) >= 0) ||
ua->AclHasRestrictions(Client_ACL)) {
if (!(client = get_client_resource(ua))) { return false; }
} else {
client = NULL;
}
return PruneDirectory(ua, client);
default:
break;
}
return true;
}
/**
* Prune Directory meta data records from the database.
*/
static bool PruneDirectory(UaContext* ua, ClientResource* client)
{
int i, len;
ClientDbRecord cr;
char* prune_topdir = NULL;
PoolMem query(PM_MESSAGE), temp(PM_MESSAGE);
bool recursive = false;
bool retval = false;
/*
* See if a client was selected.
*/
if (!client) {
if (!GetYesno(ua, _("No client restriction given really remove "
"directory for all clients (yes/no): ")) ||
!ua->pint32_val) {
if (!(client = get_client_resource(ua))) { return false; }
}
}
/*
* See if we need to recursively remove all directories under a certain path.
*/
recursive = FindArg(ua, NT_("recursive")) >= 0;
/*
* Get the directory to prune.
*/
i = FindArgWithValue(ua, NT_("directory"));
if (i >= 0) {
PmStrcpy(temp, ua->argv[i]);
} else {
if (recursive) {
if (!GetCmd(ua, _("Please enter the full path prefix to remove: "),
false)) {
return false;
}
} else {
if (!GetCmd(ua, _("Please enter the full path to remove: "), false)) {
return false;
}
}
PmStrcpy(temp, ua->cmd);
}
/*
* See if the directory ends in a / and escape it for usage in a database
* query.
*/
len = strlen(temp.c_str());
if (*(temp.c_str() + len - 1) != '/') {
PmStrcat(temp, "/");
len++;
}
prune_topdir = (char*)malloc(len * 2 + 1);
ua->db->EscapeString(ua->jcr, prune_topdir, temp.c_str(), len);
/*
* Remove all files in particular directory.
*/
if (recursive) {
Mmsg(query,
"DELETE FROM file WHERE pathid IN ("
"SELECT pathid FROM path "
"WHERE path LIKE '%s%%'"
")",
prune_topdir);
} else {
Mmsg(query,
"DELETE FROM file WHERE pathid IN ("
"SELECT pathid FROM path "
"WHERE path LIKE '%s'"
")",
prune_topdir);
}
if (client) {
char ed1[50];
cr = ClientDbRecord{};
bstrncpy(cr.Name, client->resource_name_, sizeof(cr.Name));
if (!ua->db->CreateClientRecord(ua->jcr, &cr)) { goto bail_out; }
Mmsg(temp,
" AND JobId IN ("
"SELECT JobId FROM Job "
"WHERE ClientId=%s"
")",
edit_int64(cr.ClientId, ed1));
PmStrcat(query, temp.c_str());
}
DbLock(ua->db);
ua->db->SqlQuery(query.c_str());
DbUnlock(ua->db);
/*
* If we removed the entries from the file table without limiting it to a
* certain client we created orphaned path entries as no one is referencing
* them anymore.
*/
if (!client) {
if (!GetYesno(ua, _("Cleanup orphaned path records (yes/no):")) ||
!ua->pint32_val) {
retval = true;
goto bail_out;
}
if (recursive) {
Mmsg(query,
"DELETE FROM path "
"WHERE path LIKE '%s%%'",
prune_topdir);
} else {
Mmsg(query,
"DELETE FROM path "
"WHERE path LIKE '%s'",
prune_topdir);
}
DbLock(ua->db);
ua->db->SqlQuery(query.c_str());
DbUnlock(ua->db);
}
retval = true;
bail_out:
if (prune_topdir) { free(prune_topdir); }
return retval;
}
/**
* Prune Job stat records from the database.
*/
static bool PruneStats(UaContext* ua, utime_t retention)
{
char ed1[50];
char dt[MAX_TIME_LENGTH];
PoolMem query(PM_MESSAGE);
utime_t now = (utime_t)time(NULL);
DbLock(ua->db);
Mmsg(query, "DELETE FROM JobHisto WHERE JobTDate < %s",
edit_int64(now - retention, ed1));
ua->db->SqlQuery(query.c_str());
DbUnlock(ua->db);
ua->InfoMsg(_("Pruned Jobs from JobHisto in catalog.\n"));
bstrutime(dt, sizeof(dt), now - retention);
DbLock(ua->db);
Mmsg(query, "DELETE FROM DeviceStats WHERE SampleTime < '%s'", dt);
ua->db->SqlQuery(query.c_str());
DbUnlock(ua->db);
ua->InfoMsg(_("Pruned Statistics from DeviceStats in catalog.\n"));
DbLock(ua->db);
Mmsg(query, "DELETE FROM JobStats WHERE SampleTime < '%s'", dt);
ua->db->SqlQuery(query.c_str());
DbUnlock(ua->db);
ua->InfoMsg(_("Pruned Statistics from JobStats in catalog.\n"));
return true;
}
/**
* Use pool and client specified by user to select jobs to prune
* returns add_from string to add in FROM clause
* add_where string to add in WHERE clause
*/
static bool prune_set_filter(UaContext* ua,
ClientResource* client,
PoolResource* pool,
utime_t period,
PoolMem* add_from,
PoolMem* add_where)
{
utime_t now;
char ed1[50], ed2[MAX_ESCAPE_NAME_LENGTH];
PoolMem tmp(PM_MESSAGE);
now = (utime_t)time(NULL);
edit_int64(now - period, ed1);
Dmsg3(150, "now=%lld period=%lld JobTDate=%s\n", now, period, ed1);
Mmsg(tmp, " AND JobTDate < %s ", ed1);
PmStrcat(*add_where, tmp.c_str());
DbLock(ua->db);
if (client) {
ua->db->EscapeString(ua->jcr, ed2, client->resource_name_,
strlen(client->resource_name_));
Mmsg(tmp, " AND Client.Name = '%s' ", ed2);
PmStrcat(*add_where, tmp.c_str());
PmStrcat(*add_from, " JOIN Client USING (ClientId) ");
}
if (pool) {
ua->db->EscapeString(ua->jcr, ed2, pool->resource_name_,
strlen(pool->resource_name_));
Mmsg(tmp, " AND Pool.Name = '%s' ", ed2);
PmStrcat(*add_where, tmp.c_str());
/* Use ON() instead of USING for some old SQLite */
PmStrcat(*add_from, " JOIN Pool USING(PoolId) ");
}
Dmsg2(150, "f=%s w=%s\n", add_from->c_str(), add_where->c_str());
DbUnlock(ua->db);
return true;
}
/**
* Prune File records from the database. For any Job which
* is older than the retention period, we unconditionally delete
* all File records for that Job. This is simple enough that no
* temporary tables are needed. We simply make an in memory list of
* the JobIds meeting the prune conditions, then delete all File records
* pointing to each of those JobIds.
*
* This routine assumes you want the pruning to be done. All checking
* must be done before calling this routine.
*
* Note: client or pool can possibly be NULL (not both).
*/
bool PruneFiles(UaContext* ua, ClientResource* client, PoolResource* pool)
{
del_ctx del;
struct s_count_ctx cnt;
PoolMem query(PM_MESSAGE);
PoolMem sql_where(PM_MESSAGE);
PoolMem sql_from(PM_MESSAGE);
utime_t period;
char ed1[50];
if (pool && pool->FileRetention > 0) {
period = pool->FileRetention;
} else if (client) {
period = client->FileRetention;
} else { /* should specify at least pool or client */
return false;
}
DbLock(ua->db);
/* Specify JobTDate and Pool.Name= and/or Client.Name= in the query */
if (!prune_set_filter(ua, client, pool, period, &sql_from, &sql_where)) {
goto bail_out;
}
// edit_utime(now-period, ed1, sizeof(ed1));
// Jmsg(ua->jcr, M_INFO, 0, _("Begin pruning Jobs older than %s secs.\n"),
// ed1);
Jmsg(ua->jcr, M_INFO, 0, _("Begin pruning Files.\n"));
/* Select Jobs -- for counting */
Mmsg(query, "SELECT COUNT(1) FROM Job %s WHERE PurgedFiles=0 %s",
sql_from.c_str(), sql_where.c_str());
Dmsg1(050, "select sql=%s\n", query.c_str());
cnt.count = 0;
if (!ua->db->SqlQuery(query.c_str(), DelCountHandler, (void*)&cnt)) {
ua->ErrorMsg("%s", ua->db->strerror());
Dmsg0(050, "Count failed\n");
goto bail_out;
}
if (cnt.count == 0) {
if (ua->verbose) { ua->WarningMsg(_("No Files found to prune.\n")); }
goto bail_out;
}
if (cnt.count < MAX_DEL_LIST_LEN) {
del.max_ids = cnt.count + 1;
} else {
del.max_ids = MAX_DEL_LIST_LEN;
}
del.tot_ids = 0;
del.JobId = (JobId_t*)malloc(sizeof(JobId_t) * del.max_ids);
/* Now process same set but making a delete list */
Mmsg(query, "SELECT JobId FROM Job %s WHERE PurgedFiles=0 %s",
sql_from.c_str(), sql_where.c_str());
Dmsg1(050, "select sql=%s\n", query.c_str());
ua->db->SqlQuery(query.c_str(), FileDeleteHandler, (void*)&del);
PurgeFilesFromJobList(ua, del);
edit_uint64_with_commas(del.num_del, ed1);
ua->InfoMsg(_("Pruned Files from %s Jobs for client %s from catalog.\n"), ed1,
client->resource_name_);
bail_out:
DbUnlock(ua->db);
if (del.JobId) { free(del.JobId); }
return 1;
}
static void DropTempTables(UaContext* ua)
{
ua->db->SqlQuery(BareosDb::SQL_QUERY::drop_deltabs);
}
static bool CreateTempTables(UaContext* ua)
{
/* Create temp tables and indicies */
if (!ua->db->SqlQuery(BareosDb::SQL_QUERY::create_deltabs)) {
ua->ErrorMsg("%s", ua->db->strerror());
Dmsg0(050, "create DelTables table failed\n");
return false;
}
if (!ua->db->SqlQuery(BareosDb::SQL_QUERY::create_delindex)) {
ua->ErrorMsg("%s", ua->db->strerror());
Dmsg0(050, "create DelInx1 index failed\n");
return false;
}
return true;
}
static bool GrowDelList(del_ctx* del)
{
if (del->num_ids == MAX_DEL_LIST_LEN) { return false; }
if (del->num_ids == del->max_ids) {
del->max_ids = (del->max_ids * 3) / 2;
del->JobId = (JobId_t*)realloc(del->JobId, sizeof(JobId_t) * del->max_ids);
del->PurgedFiles = (char*)realloc(del->PurgedFiles, del->max_ids);
}
return true;
}
struct accurate_check_ctx {
DBId_t ClientId; /* Id of client */
DBId_t FileSetId; /* Id of FileSet */
};
/**
* row: Job.Name, FileSet, Client.Name, FileSetId, ClientId, Type
*/
static int JobSelectHandler(void* ctx, int num_fields, char** row)
{
alist* lst = (alist*)ctx;
struct accurate_check_ctx* res;
ASSERT(num_fields == 6);
/*
* If this job doesn't exist anymore in the configuration, delete it.
*/
if (my_config->GetResWithName(R_JOB, row[0], false) == NULL) { return 0; }
/*
* If this fileset doesn't exist anymore in the configuration, delete it.
*/
if (my_config->GetResWithName(R_FILESET, row[1], false) == NULL) { return 0; }
/*
* If this client doesn't exist anymore in the configuration, delete it.
*/
if (my_config->GetResWithName(R_CLIENT, row[2], false) == NULL) { return 0; }
/*
* Don't compute accurate things for Verify jobs
*/
if (*row[5] == 'V') { return 0; }
res = (struct accurate_check_ctx*)malloc(sizeof(struct accurate_check_ctx));
res->FileSetId = str_to_int64(row[3]);
res->ClientId = str_to_int64(row[4]);
lst->append(res);
// Dmsg2(150, "row=%d val=%d\n", del->num_ids-1, del->JobId[del->num_ids-1]);
return 0;
}
/**
* Pruning Jobs is a bit more complicated than purging Files
* because we delete Job records only if there is a more current
* backup of the FileSet. Otherwise, we keep the Job record.
* In other words, we never delete the only Job record that
* contains a current backup of a FileSet. This prevents the
* Volume from being recycled and destroying a current backup.
*
* For Verify Jobs, we do not delete the last InitCatalog.
*
* For Restore Jobs there are no restrictions.
*/
static bool PruneBackupJobs(UaContext* ua,
ClientResource* client,
PoolResource* pool)
{
PoolMem query(PM_MESSAGE);
PoolMem sql_where(PM_MESSAGE);
PoolMem sql_from(PM_MESSAGE);
utime_t period;
char ed1[50];
alist* jobids_check = NULL;
struct accurate_check_ctx* elt = nullptr;
db_list_ctx jobids, tempids;
JobDbRecord jr;
del_ctx del;
if (pool && pool->JobRetention > 0) {
period = pool->JobRetention;
} else if (client) {
period = client->JobRetention;
} else { /* should specify at least pool or client */
return false;
}
DbLock(ua->db);
if (!prune_set_filter(ua, client, pool, period, &sql_from, &sql_where)) {
goto bail_out;
}
/* Drop any previous temporary tables still there */
DropTempTables(ua);
/* Create temp tables and indicies */
if (!CreateTempTables(ua)) { goto bail_out; }
edit_utime(period, ed1, sizeof(ed1));
Jmsg(ua->jcr, M_INFO, 0, _("Begin pruning Jobs older than %s.\n"), ed1);
del.max_ids = 100;
del.JobId = (JobId_t*)malloc(sizeof(JobId_t) * del.max_ids);
del.PurgedFiles = (char*)malloc(del.max_ids);
/*
* Select all files that are older than the JobRetention period
* and add them into the "DeletionCandidates" table.
*/
Mmsg(query,
"INSERT INTO DelCandidates "
"SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus "
"FROM Job %s " /* JOIN Pool/Client */
"WHERE Type IN ('B', 'C', 'M', 'V', 'D', 'R', 'c', 'm', 'g') "
" %s ", /* Pool/Client + JobTDate */
sql_from.c_str(), sql_where.c_str());
Dmsg1(050, "select sql=%s\n", query.c_str());
if (!ua->db->SqlQuery(query.c_str())) {
if (ua->verbose) { ua->ErrorMsg("%s", ua->db->strerror()); }
goto bail_out;
}
/*
* Now, for the selection, we discard some of them in order to be always
* able to restore files. (ie, last full, last diff, last incrs)
* Note: The DISTINCT could be more useful if we don't get FileSetId
*/
jobids_check = new alist(10, owned_by_alist);
Mmsg(query,
"SELECT DISTINCT Job.Name, FileSet, Client.Name, Job.FileSetId, "
"Job.ClientId, Job.Type "
"FROM DelCandidates "
"JOIN Job USING (JobId) "
"JOIN Client USING (ClientId) "
"JOIN FileSet ON (Job.FileSetId = FileSet.FileSetId) "
"WHERE Job.Type IN ('B') " /* Look only Backup jobs */
"AND Job.JobStatus IN ('T', 'W') " /* Look only useful jobs */
);
/*
* The JobSelectHandler will skip jobs or filesets that are no longer
* in the configuration file. Interesting ClientId/FileSetId will be
* added to jobids_check.
*/
if (!ua->db->SqlQuery(query.c_str(), JobSelectHandler, jobids_check)) {
ua->ErrorMsg("%s", ua->db->strerror());
}
/*
* For this selection, we exclude current jobs used for restore or
* accurate. This will prevent to prune the last full backup used for
* current backup & restore
*/
/*
* To find useful jobs, we do like an incremental
*/
jr.JobLevel = L_INCREMENTAL;
foreach_alist (elt, jobids_check) {
jr.ClientId = elt->ClientId; /* should be always the same */
jr.FileSetId = elt->FileSetId;
ua->db->AccurateGetJobids(ua->jcr, &jr, &tempids);
jobids.add(tempids);
}
/*
* Discard latest Verify level=InitCatalog job
* TODO: can have multiple fileset
*/
Mmsg(query,
"SELECT JobId, JobTDate "
"FROM Job %s " /* JOIN Client/Pool */
"WHERE Type='V' AND Level='V' "
" %s " /* Pool, JobTDate, Client */
"ORDER BY JobTDate DESC LIMIT 1",
sql_from.c_str(), sql_where.c_str());
if (!ua->db->SqlQuery(query.c_str(), DbListHandler, &jobids)) {
ua->ErrorMsg("%s", ua->db->strerror());
}
/* If we found jobs to exclude from the DelCandidates list, we should
* also remove BaseJobs that can be linked with them
*/
if (jobids.count > 0) {
Dmsg1(60, "jobids to exclude before basejobs = %s\n", jobids.list);
/* We also need to exclude all basejobs used */
ua->db->GetUsedBaseJobids(ua->jcr, jobids.list, &jobids);
/* Removing useful jobs from the DelCandidates list */
Mmsg(query,
"DELETE FROM DelCandidates "
"WHERE JobId IN (%s) " /* JobId used in accurate */
"AND JobFiles!=0", /* Discard when JobFiles=0 */
jobids.list);
if (!ua->db->SqlQuery(query.c_str())) {
ua->ErrorMsg("%s", ua->db->strerror());
goto bail_out; /* Don't continue if the list isn't clean */
}
Dmsg1(60, "jobids to exclude = %s\n", jobids.list);
}
/* We use DISTINCT because we can have two times the same job */
Mmsg(query,
"SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
"FROM DelCandidates");
if (!ua->db->SqlQuery(query.c_str(), JobDeleteHandler, (void*)&del)) {
ua->ErrorMsg("%s", ua->db->strerror());
}
PurgeJobListFromCatalog(ua, del);
if (del.num_del > 0) {
ua->InfoMsg(_("Pruned %d %s for client %s from catalog.\n"), del.num_del,
del.num_del == 1 ? _("Job") : _("Jobs"),
client->resource_name_);
} else if (ua->verbose) {
ua->InfoMsg(_("No Jobs found to prune.\n"));
}
bail_out:
DropTempTables(ua);
DbUnlock(ua->db);
if (del.JobId) { free(del.JobId); }
if (del.PurgedFiles) { free(del.PurgedFiles); }
if (jobids_check) { delete jobids_check; }
return 1;
}
/**
* Dispatch to the right prune jobs function.
*/
bool PruneJobs(UaContext* ua,
ClientResource* client,
PoolResource* pool,
int JobType)
{
switch (JobType) {
case JT_BACKUP:
return PruneBackupJobs(ua, client, pool);
default:
return true;
}
}
/**
* Prune a given Volume
*/
bool PruneVolume(UaContext* ua, MediaDbRecord* mr)
{
PoolMem query(PM_MESSAGE);
del_ctx del;
bool VolumeIsNowEmtpy = false;
if (mr->Enabled == VOL_ARCHIVED) {
return false; /* Cannot prune archived volumes */
}
del.max_ids = 10000;
del.JobId = (JobId_t*)malloc(sizeof(JobId_t) * del.max_ids);
DbLock(ua->db);
/* Prune only Volumes with status "Full", or "Used" */
if (bstrcmp(mr->VolStatus, "Full") || bstrcmp(mr->VolStatus, "Used")) {
Dmsg2(050, "get prune list MediaId=%d Volume %s\n", (int)mr->MediaId,
mr->VolumeName);
int NumJobsToBePruned = GetPruneListForVolume(ua, mr, &del);
Jmsg(ua->jcr, M_INFO, 0,
_("Pruning volume %s: %d Jobs have expired and can be pruned.\n"),
mr->VolumeName, NumJobsToBePruned);
Dmsg1(050, "Num pruned = %d\n", NumJobsToBePruned);
if (NumJobsToBePruned != 0) { PurgeJobListFromCatalog(ua, del); }
VolumeIsNowEmtpy = IsVolumePurged(ua, mr);
if (!VolumeIsNowEmtpy) {
Jmsg(ua->jcr, M_INFO, 0,
_("Volume \"%s\" still contains jobs after pruning.\n"),
mr->VolumeName);
} else {
Jmsg(ua->jcr, M_INFO, 0,
_("Volume \"%s\" contains no jobs after pruning.\n"),
mr->VolumeName);
}
}
DbUnlock(ua->db);
if (del.JobId) { free(del.JobId); }
return VolumeIsNowEmtpy;
}
/**
* Get prune list for a volume
*/
int GetPruneListForVolume(UaContext* ua, MediaDbRecord* mr, del_ctx* del)
{
PoolMem query(PM_MESSAGE);
utime_t now;
char ed1[50], ed2[50];
if (mr->Enabled == VOL_ARCHIVED) {
return 0; /* cannot prune Archived volumes */
}
/*
* Now add to the list of JobIds for Jobs written to this Volume
*/
utime_t VolRetention = mr->VolRetention;
now = (utime_t)time(NULL);
ua->db->FillQuery(query, BareosDb::SQL_QUERY::sel_JobMedia,
edit_int64(mr->MediaId, ed1),
edit_int64(now - VolRetention, ed2));
Dmsg3(250, "Now=%d VolRetention=%d now-VolRetention=%s\n", (int)now,
(int)VolRetention, ed2);
Dmsg1(050, "Query=%s\n", query.c_str());
if (!ua->db->SqlQuery(query.c_str(), FileDeleteHandler, (void*)del)) {
if (ua->verbose) { ua->ErrorMsg("%s", ua->db->strerror()); }
Dmsg0(050, "Count failed\n");
return 0;
}
int NumJobsToBePruned = ExcludeRunningJobsFromList(del);
if (NumJobsToBePruned > 0) {
Jmsg(ua->jcr, M_INFO, 0,
_("Volume \"%s\" has Volume Retention of %d sec. and has %d jobs that "
"will be pruned\n"),
mr->VolumeName, VolRetention, NumJobsToBePruned);
}
return NumJobsToBePruned;
}
/**
* We have a list of jobs to prune or purge. If any of them is
* currently running, we set its JobId to zero which effectively
* excludes it.
*
* Returns the number of jobs that can be pruned or purged.
*/
int ExcludeRunningJobsFromList(del_ctx* prune_list)
{
int count = 0;
JobControlRecord* jcr;
bool skip;
int i;
/* Do not prune any job currently running */
for (i = 0; i < prune_list->num_ids; i++) {
skip = false;
foreach_jcr (jcr) {
if (jcr->JobId == prune_list->JobId[i]) {
Dmsg2(050, "skip running job JobId[%d]=%d\n", i,
(int)prune_list->JobId[i]);
prune_list->JobId[i] = 0;
skip = true;
break;
}
}
endeach_jcr(jcr);
if (skip) { continue; /* don't increment count */ }
Dmsg2(050, "accept JobId[%d]=%d\n", i, (int)prune_list->JobId[i]);
count++;
}
return count;
}
} /* namespace directordaemon */