/
migrate.cc
2103 lines (1848 loc) · 65.3 KB
/
migrate.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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2004-2012 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.
*/
/*
* BAREOS Director -- migrate.c -- responsible for doing migration and copy
* jobs. Also handles Copy jobs (March 2008) Kern Sibbald, September 2004 SD-SD
* Migration by Marco van Wieringen, November 2012
*/
/**
* @file
* responsible for doing migration and copy jobs.
*
* Also handles Copy jobs
*
* Basic tasks done here:
* Open DB and create records for this job.
* Open Message Channel with Storage daemon to tell him a job will be
* starting. Open connection with Storage daemon and pass him commands to do the
* backup. When the Storage daemon finishes the job, update the DB.
*/
#include "include/bareos.h"
#include "dird.h"
#include "dird/dird_globals.h"
#include "dird/backup.h"
#include "dird/jcr_private.h"
#include "dird/job.h"
#include "dird/migration.h"
#include "dird/msgchan.h"
#include "dird/sd_cmds.h"
#include "dird/storage.h"
#include "dird/ua_input.h"
#include "dird/ua_server.h"
#include "dird/ua_purge.h"
#include "dird/ua_run.h"
#include "include/auth_protocol_types.h"
#include "include/migration_selection_types.h"
#include "lib/edit.h"
#include "lib/parse_conf.h"
#include "lib/util.h"
#include "cats/sql.h"
#ifndef HAVE_REGEX_H
#include "lib/bregex.h"
#else
#include <regex.h>
#endif
namespace directordaemon {
/* Commands sent to other storage daemon */
static char replicatecmd[] =
"replicate JobId=%d Job=%s address=%s port=%d ssl=%d Authorization=%s\n";
/**
* Get Job names in Pool
*/
static const char* sql_job =
"SELECT DISTINCT Job.Name from Job,Pool"
" WHERE Pool.Name='%s' AND Job.PoolId=Pool.PoolId";
/**
* Get JobIds from regex'ed Job names
*/
static const char* sql_jobids_from_job =
"SELECT DISTINCT Job.JobId,Job.StartTime FROM Job,Pool"
" WHERE Job.Name='%s' AND Pool.Name='%s' AND Job.PoolId=Pool.PoolId"
" ORDER by Job.StartTime";
/**
* Get Client names in Pool
*/
static const char* sql_client =
"SELECT DISTINCT Client.Name from Client,Pool,Job"
" WHERE Pool.Name='%s' AND Job.ClientId=Client.ClientId AND"
" Job.PoolId=Pool.PoolId";
/**
* Get JobIds from regex'ed Client names
*/
static const char* sql_jobids_from_client =
"SELECT DISTINCT Job.JobId,Job.StartTime FROM Job,Pool,Client"
" WHERE Client.Name='%s' AND Pool.Name='%s' AND Job.PoolId=Pool.PoolId"
" AND Job.ClientId=Client.ClientId AND Job.Type IN ('B','C')"
" AND Job.JobStatus IN ('T','W')"
" ORDER by Job.StartTime";
/**
* Get Volume names in Pool
*/
static const char* sql_vol =
"SELECT DISTINCT VolumeName FROM Media,Pool WHERE"
" VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
" Media.PoolId=Pool.PoolId AND Pool.Name='%s'";
/**
* Get JobIds from regex'ed Volume names
*/
static const char* sql_jobids_from_vol =
"SELECT DISTINCT Job.JobId,Job.StartTime FROM Media,JobMedia,Job"
" WHERE Media.VolumeName='%s' AND Media.MediaId=JobMedia.MediaId"
" AND JobMedia.JobId=Job.JobId AND Job.Type IN ('B','C')"
" AND Job.JobStatus IN ('T','W') AND Media.Enabled=1"
" ORDER by Job.StartTime";
/**
* Get JobIds from the smallest volume
*/
static const char* sql_smallest_vol =
"SELECT Media.MediaId FROM Media,Pool,JobMedia WHERE"
" Media.MediaId in (SELECT DISTINCT MediaId from JobMedia) AND"
" Media.VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
" Media.PoolId=Pool.PoolId AND Pool.Name='%s'"
" ORDER BY VolBytes ASC LIMIT 1";
/**
* Get JobIds from the oldest volume
*/
static const char* sql_oldest_vol =
"SELECT Media.MediaId FROM Media,Pool,JobMedia WHERE"
" Media.MediaId in (SELECT DISTINCT MediaId from JobMedia) AND"
" Media.VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
" Media.PoolId=Pool.PoolId AND Pool.Name='%s'"
" ORDER BY LastWritten ASC LIMIT 1";
/**
* Get JobIds when we have selected MediaId
*/
static const char* sql_jobids_from_mediaid =
"SELECT DISTINCT Job.JobId,Job.StartTime FROM JobMedia,Job"
" WHERE JobMedia.JobId=Job.JobId AND JobMedia.MediaId IN (%s)"
" AND Job.Type IN ('B','C') AND Job.JobStatus IN ('T','W')"
" ORDER by Job.StartTime";
/**
* Get the number of bytes in the pool
*/
static const char* sql_pool_bytes =
"SELECT SUM(JobBytes) FROM Job WHERE JobId IN"
" (SELECT DISTINCT Job.JobId from Pool,Job,Media,JobMedia WHERE"
" Pool.Name='%s' AND Media.PoolId=Pool.PoolId AND"
" VolStatus in ('Full','Used','Error','Append') AND Media.Enabled=1 AND"
" Job.Type IN ('B','C') AND Job.JobStatus IN ('T','W') AND"
" JobMedia.JobId=Job.JobId AND Job.PoolId=Media.PoolId)";
/**
* Get the number of bytes in the Jobs
*/
static const char* sql_job_bytes =
"SELECT SUM(JobBytes) FROM Job WHERE JobId IN (%s)";
/**
* Get Media Ids in Pool
*/
static const char* sql_mediaids =
"SELECT MediaId FROM Media,Pool WHERE"
" VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
" Media.PoolId=Pool.PoolId AND Pool.Name='%s' ORDER BY LastWritten ASC";
/**
* Get JobIds in Pool longer than specified time
*/
static const char* sql_pool_time =
"SELECT DISTINCT Job.JobId FROM Pool,Job,Media,JobMedia WHERE"
" Pool.Name='%s' AND Media.PoolId=Pool.PoolId AND"
" VolStatus IN ('Full','Used','Error') AND Media.Enabled=1 AND"
" Job.Type IN ('B','C') AND Job.JobStatus IN ('T','W') AND"
" JobMedia.JobId=Job.JobId AND Job.PoolId=Media.PoolId"
" AND Job.RealEndTime<='%s'";
/**
* Get JobIds from successfully completed backup jobs which have not been copied
* before
*/
static const char* sql_jobids_of_pool_uncopied_jobs =
"SELECT DISTINCT Job.JobId,Job.StartTime FROM Job,Pool"
" WHERE Pool.Name = '%s' AND Pool.PoolId = Job.PoolId"
" AND Job.Type = 'B' AND Job.JobStatus IN ('T','W')"
" AND Job.jobBytes > 0"
" AND Job.JobId NOT IN"
" (SELECT PriorJobId FROM Job WHERE"
" Type IN ('B','C') AND Job.JobStatus IN ('T','W')"
" AND PriorJobId != 0)"
" ORDER by Job.StartTime";
/**
* Migrate NDMP Job MetaData.
*/
static const char* sql_migrate_ndmp_metadata =
"UPDATE File SET JobId=%s "
"WHERE JobId=%s "
"AND Name NOT IN ("
"SELECT Name "
"FROM File "
"WHERE JobId=%s)";
/**
* Copy NDMP Job MetaData.
*/
static const char* sql_copy_ndmp_metadata =
"INSERT INTO File (FileIndex, JobId, PathId, Name, DeltaSeq, MarkId, "
"LStat, MD5) "
"SELECT FileIndex, %s, PathId, Name, DeltaSeq, MarkId, LStat, MD5 "
"FROM File "
"WHERE JobId=%s "
"AND Name NOT IN ("
"SELECT Name "
"FROM File "
"WHERE JobId=%s)";
static const int dbglevel = 10;
struct idpkt {
POOLMEM* list;
uint32_t count;
};
/**
* See if two storage definitions point to the same Storage Daemon.
*
* We compare:
* - address
* - SDport
* - password
*/
static inline bool IsSameStorageDaemon(StorageResource* read_storage,
StorageResource* write_storage)
{
return read_storage->SDport == write_storage->SDport &&
Bstrcasecmp(read_storage->address, write_storage->address) &&
Bstrcasecmp(read_storage->password_.value,
write_storage->password_.value);
}
bool SetMigrationWstorage(JobControlRecord* jcr,
PoolResource* pool,
PoolResource* next_pool,
const char* where)
{
if (!next_pool) {
Jmsg(jcr, M_FATAL, 0,
_("No Next Pool specification found in Pool \"%s\".\n"),
pool->resource_name_);
return false;
}
if (!next_pool->storage || next_pool->storage->size() == 0) {
Jmsg(jcr, M_FATAL, 0,
_("No Storage specification found in Next Pool \"%s\".\n"),
next_pool->resource_name_);
return false;
}
CopyWstorage(jcr, next_pool->storage, where);
return true;
}
/**
* SetMigrationNextPool() called by DoMigrationInit()
* at different stages.
*
* The idea here is to make a common subroutine for the
* NextPool's search code and to permit DoMigrationInit()
* to return with NextPool set in jcr struct.
*/
static inline bool SetMigrationNextPool(JobControlRecord* jcr,
PoolResource** retpool)
{
PoolDbRecord pr;
char ed1[100];
PoolResource* pool;
const char* storage_source;
/*
* Get the PoolId used with the original job. Then
* find the pool name from the database record.
*/
pr.PoolId = jcr->impl->jr.PoolId;
if (!jcr->db->GetPoolRecord(jcr, &pr)) {
Jmsg(jcr, M_FATAL, 0, _("Pool for JobId %s not in database. ERR=%s\n"),
edit_int64(pr.PoolId, ed1), jcr->db->strerror());
return false;
}
/*
* Get the pool resource corresponding to the original job
*/
pool = (PoolResource*)my_config->GetResWithName(R_POOL, pr.Name);
*retpool = pool;
if (!pool) {
Jmsg(jcr, M_FATAL, 0, _("Pool resource \"%s\" not found.\n"), pr.Name);
return false;
}
/*
* See if there is a next pool override.
*/
if (jcr->impl->res.run_next_pool_override) {
PmStrcpy(jcr->impl->res.npool_source, _("Run NextPool override"));
PmStrcpy(jcr->impl->res.pool_source, _("Run NextPool override"));
storage_source = _("Storage from Run NextPool override");
} else {
/*
* See if there is a next pool override in the Job definition.
*/
if (jcr->impl->res.job->next_pool) {
jcr->impl->res.next_pool = jcr->impl->res.job->next_pool;
PmStrcpy(jcr->impl->res.npool_source, _("Job's NextPool resource"));
PmStrcpy(jcr->impl->res.pool_source, _("Job's NextPool resource"));
storage_source = _("Storage from Job's NextPool resource");
} else {
/*
* Fall back to the pool's NextPool definition.
*/
jcr->impl->res.next_pool = pool->NextPool;
PmStrcpy(jcr->impl->res.npool_source, _("Job Pool's NextPool resource"));
PmStrcpy(jcr->impl->res.pool_source, _("Job Pool's NextPool resource"));
storage_source = _("Storage from Pool's NextPool resource");
}
}
/*
* If the original backup pool has a NextPool, make sure a
* record exists in the database. Note, in this case, we
* will be migrating from pool to pool->NextPool.
*/
if (jcr->impl->res.next_pool) {
jcr->impl->jr.PoolId =
GetOrCreatePoolRecord(jcr, jcr->impl->res.next_pool->resource_name_);
if (jcr->impl->jr.PoolId == 0) { return false; }
}
if (!SetMigrationWstorage(jcr, pool, jcr->impl->res.next_pool,
storage_source)) {
return false;
}
jcr->impl->res.pool = jcr->impl->res.next_pool;
Dmsg2(dbglevel, "Write pool=%s read rpool=%s\n",
jcr->impl->res.pool->resource_name_,
jcr->impl->res.rpool->resource_name_);
return true;
}
/**
* Sanity check that we are not using the same storage for reading and writing.
*/
static inline bool SameStorage(JobControlRecord* jcr)
{
StorageResource *read_store, *write_store;
read_store = (StorageResource*)jcr->impl->res.read_storage_list->first();
write_store = (StorageResource*)jcr->impl->res.write_storage_list->first();
if (!read_store->autochanger && !write_store->autochanger &&
bstrcmp(read_store->resource_name_, write_store->resource_name_)) {
return true;
}
return false;
}
static inline void StartNewMigrationJob(JobControlRecord* jcr)
{
char ed1[50];
JobId_t jobid;
UaContext* ua;
PoolMem cmd(PM_MESSAGE);
ua = new_ua_context(jcr);
ua->batch = true;
Mmsg(ua->cmd, "run job=\"%s\" jobid=%s ignoreduplicatecheck=yes",
jcr->impl->res.job->resource_name_,
edit_uint64(jcr->impl->MigrateJobId, ed1));
/*
* Make sure we have something to compare against.
*/
if (jcr->impl->res.pool) {
/*
* See if there was actually a pool override.
*/
if (jcr->impl->res.pool != jcr->impl->res.job->pool) {
Mmsg(cmd, " pool=\"%s\"", jcr->impl->res.pool->resource_name_);
PmStrcat(ua->cmd, cmd.c_str());
}
/*
* See if there was actually a next pool override.
*/
if (jcr->impl->res.next_pool &&
jcr->impl->res.next_pool != jcr->impl->res.pool->NextPool) {
Mmsg(cmd, " nextpool=\"%s\"", jcr->impl->res.next_pool->resource_name_);
PmStrcat(ua->cmd, cmd.c_str());
}
}
Dmsg2(dbglevel, "=============== %s cmd=%s\n", jcr->get_OperationName(),
ua->cmd);
ParseUaArgs(ua); /* parse command */
jobid = DoRunCmd(ua, ua->cmd);
if (jobid == 0) {
Jmsg(jcr, M_ERROR, 0, _("Could not start migration job.\n"));
} else {
Jmsg(jcr, M_INFO, 0, _("%s JobId %d started.\n"), jcr->get_OperationName(),
(int)jobid);
}
FreeUaContext(ua);
}
/**
* Return next DBId from comma separated list
*
* Returns:
* 1 if next DBId returned
* 0 if no more DBIds are in list
* -1 there is an error
*/
static inline int GetNextDbidFromList(char** p, DBId_t* DBId)
{
int i;
const int maxlen = 30;
char id[maxlen + 1];
char* q = *p;
id[0] = 0;
for (i = 0; i < maxlen; i++) {
if (*q == 0) {
break;
} else if (*q == ',') {
q++;
break;
}
id[i] = *q++;
id[i + 1] = 0;
}
if (id[0] == 0) {
return 0;
} else if (!Is_a_number(id)) {
return -1; /* error */
}
*p = q;
*DBId = str_to_int64(id);
return 1;
}
/**
* Add an item to the list if it is unique
*/
static void AddUniqueId(idpkt* ids, char* item)
{
const int maxlen = 30;
char id[maxlen + 1];
char* q = ids->list;
/*
* Walk through current list to see if each item is the same as item
*/
while (*q) {
id[0] = 0;
for (int i = 0; i < maxlen; i++) {
if (*q == 0) {
break;
} else if (*q == ',') {
q++;
break;
}
id[i] = *q++;
id[i + 1] = 0;
}
if (bstrcmp(item, id)) { return; }
}
/*
* Did not find item, so add it to list
*/
if (ids->count == 0) {
ids->list[0] = 0;
} else {
PmStrcat(ids->list, ",");
}
PmStrcat(ids->list, item);
ids->count++;
return;
}
/**
* Callback handler make list of DB Ids
*/
static int UniqueDbidHandler(void* ctx, int num_fields, char** row)
{
idpkt* ids = (idpkt*)ctx;
/*
* Sanity check
*/
if (!row || !row[0]) {
Dmsg0(dbglevel, "dbid_hdlr error empty row\n");
return 1; /* stop calling us */
}
AddUniqueId(ids, row[0]);
Dmsg3(dbglevel, "dbid_hdlr count=%d Ids=%p %s\n", ids->count, ids->list,
ids->list);
return 0;
}
struct uitem {
dlink link;
char* item;
};
static int ItemCompare(void* item1, void* item2)
{
uitem* i1 = (uitem*)item1;
uitem* i2 = (uitem*)item2;
return strcmp(i1->item, i2->item);
}
static int UniqueNameHandler(void* ctx, int num_fields, char** row)
{
dlist* list = (dlist*)ctx;
uitem* new_item = (uitem*)malloc(sizeof(uitem));
new (new_item) uitem();
uitem* item;
new_item->item = strdup(row[0]);
Dmsg1(dbglevel, "Unique_name_hdlr Item=%s\n", row[0]);
item = (uitem*)list->binary_insert((void*)new_item, ItemCompare);
if (item != new_item) { /* already in list */
free(new_item->item);
free((char*)new_item);
return 0;
}
return 0;
}
/**
* This routine returns:
* false if an error occurred
* true otherwise
* ids.count number of jobids found (may be zero)
*/
static bool FindJobidsFromMediaidList(JobControlRecord* jcr,
idpkt* ids,
const char* type)
{
bool ok = false;
PoolMem query(PM_MESSAGE);
Mmsg(query, sql_jobids_from_mediaid, ids->list);
ids->count = 0;
if (!jcr->db->SqlQuery(query.c_str(), UniqueDbidHandler, (void*)ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), jcr->db->strerror());
goto bail_out;
}
if (ids->count == 0) {
Jmsg(jcr, M_INFO, 0, _("No %ss found to %s.\n"), type,
jcr->get_ActionName());
}
ok = true;
bail_out:
return ok;
}
static bool find_mediaid_then_jobids(JobControlRecord* jcr,
idpkt* ids,
const char* query1,
const char* type)
{
bool ok = false;
PoolMem query(PM_MESSAGE);
ids->count = 0;
/*
* Basic query for MediaId
*/
Mmsg(query, query1, jcr->impl->res.rpool->resource_name_);
if (!jcr->db->SqlQuery(query.c_str(), UniqueDbidHandler, (void*)ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), jcr->db->strerror());
goto bail_out;
}
if (ids->count == 0) {
Jmsg(jcr, M_INFO, 0, _("No %s found to %s.\n"), type,
jcr->get_ActionName());
ok = true; /* Not an error */
goto bail_out;
} else if (ids->count != 1) {
Jmsg(jcr, M_FATAL, 0, _("SQL error. Expected 1 MediaId got %d\n"),
ids->count);
goto bail_out;
}
Dmsg2(dbglevel, "%s MediaIds=%s\n", type, ids->list);
ok = FindJobidsFromMediaidList(jcr, ids, type);
bail_out:
return ok;
}
/**
* This routine returns:
* false if an error occurred
* true otherwise
* ids.count number of jobids found (may be zero)
*/
static inline bool FindJobidsOfPoolUncopiedJobs(JobControlRecord* jcr,
idpkt* ids)
{
bool ok = false;
PoolMem query(PM_MESSAGE);
/*
* Only a copy job is allowed
*/
if (!jcr->is_JobType(JT_COPY)) {
Jmsg(jcr, M_FATAL, 0,
_("Selection Type 'pooluncopiedjobs' only applies to Copy Jobs"));
goto bail_out;
}
Dmsg1(dbglevel, "copy selection pattern=%s\n",
jcr->impl->res.rpool->resource_name_);
Mmsg(query, sql_jobids_of_pool_uncopied_jobs,
jcr->impl->res.rpool->resource_name_);
Dmsg1(dbglevel, "get uncopied jobs query=%s\n", query.c_str());
if (!jcr->db->SqlQuery(query.c_str(), UniqueDbidHandler, (void*)ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL to get uncopied jobs failed. ERR=%s\n"),
jcr->db->strerror());
goto bail_out;
}
ok = true;
bail_out:
return ok;
}
static bool regex_find_jobids(JobControlRecord* jcr,
idpkt* ids,
const char* query1,
const char* query2,
const char* type)
{
dlist* item_chain;
uitem* item = NULL;
uitem* last_item = NULL;
regex_t preg;
char prbuf[500];
int rc;
bool ok = false;
PoolMem query(PM_MESSAGE);
item_chain = new dlist(item, &item->link);
if (!jcr->impl->res.job->selection_pattern) {
Jmsg(jcr, M_FATAL, 0, _("No %s %s selection pattern specified.\n"),
jcr->get_OperationName(), type);
goto bail_out;
}
Dmsg1(dbglevel, "regex-sel-pattern=%s\n",
jcr->impl->res.job->selection_pattern);
/*
* Basic query for names
*/
Mmsg(query, query1, jcr->impl->res.rpool->resource_name_);
Dmsg1(dbglevel, "get name query1=%s\n", query.c_str());
if (!jcr->db->SqlQuery(query.c_str(), UniqueNameHandler, (void*)item_chain)) {
Jmsg(jcr, M_FATAL, 0, _("SQL to get %s failed. ERR=%s\n"), type,
jcr->db->strerror());
goto bail_out;
}
Dmsg1(dbglevel, "query1 returned %d names\n", item_chain->size());
if (item_chain->size() == 0) {
Jmsg(jcr, M_INFO, 0, _("Query of Pool \"%s\" returned no Jobs to %s.\n"),
jcr->impl->res.rpool->resource_name_, jcr->get_ActionName());
ok = true;
goto bail_out; /* skip regex match */
} else {
/*
* Compile regex expression
*/
rc = regcomp(&preg, jcr->impl->res.job->selection_pattern, REG_EXTENDED);
if (rc != 0) {
regerror(rc, &preg, prbuf, sizeof(prbuf));
Jmsg(jcr, M_FATAL, 0,
_("Could not compile regex pattern \"%s\" ERR=%s\n"),
jcr->impl->res.job->selection_pattern, prbuf);
goto bail_out;
}
/*
* Now apply the regex to the names and remove any item not matched
*/
foreach_dlist (item, item_chain) {
if (last_item) {
Dmsg1(dbglevel, "Remove item %s\n", last_item->item);
free(last_item->item);
item_chain->remove(last_item);
}
Dmsg1(dbglevel, "get name Item=%s\n", item->item);
rc = regexec(&preg, item->item, 0, NULL, 0);
if (rc == 0) {
last_item = NULL; /* keep this one */
} else {
last_item = item;
}
}
if (last_item) {
free(last_item->item);
Dmsg1(dbglevel, "Remove item %s\n", last_item->item);
item_chain->remove(last_item);
}
regfree(&preg);
}
if (item_chain->size() == 0) {
Jmsg(jcr, M_INFO, 0, _("Regex pattern matched no Jobs to %s.\n"),
jcr->get_ActionName());
ok = true;
goto bail_out; /* skip regex match */
}
/*
* At this point, we have a list of items in item_chain
* that have been matched by the regex, so now we need
* to look up their jobids.
*/
ids->count = 0;
foreach_dlist (item, item_chain) {
Dmsg2(dbglevel, "Got %s: %s\n", type, item->item);
Mmsg(query, query2, item->item, jcr->impl->res.rpool->resource_name_);
Dmsg1(dbglevel, "get id from name query2=%s\n", query.c_str());
if (!jcr->db->SqlQuery(query.c_str(), UniqueDbidHandler, (void*)ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), jcr->db->strerror());
goto bail_out;
}
}
if (ids->count == 0) {
Jmsg(jcr, M_INFO, 0, _("No %ss found to %s.\n"), type,
jcr->get_ActionName());
}
ok = true;
bail_out:
Dmsg2(dbglevel, "Count=%d Jobids=%s\n", ids->count, ids->list);
foreach_dlist (item, item_chain) {
free(item->item);
}
delete item_chain;
return ok;
}
/**
* This is the central piece of code that finds jobs actually JobIds to migrate.
* It examines the Selection Type to see what kind of migration we are doing
* (Volume, Job, Client, ...) and applies any Selection Pattern if appropriate
* to obtain a list of JobIds.
*
* Finally, it will loop over all the JobIds found, starting a new job with
* MigrationJobId set to that JobId.
*
* Returns: false - On error
* true - If OK
*/
static inline bool getJobs_to_migrate(JobControlRecord* jcr)
{
char* p;
int status;
int limit = -1;
bool apply_limit = false;
bool retval = false;
JobId_t JobId;
db_int64_ctx ctx;
idpkt ids, mid, jids;
char ed1[30], ed2[30];
PoolMem query(PM_MESSAGE);
ids.list = GetPoolMemory(PM_MESSAGE);
ids.list[0] = 0;
ids.count = 0;
mid.list = NULL;
jids.list = NULL;
switch (jcr->impl->res.job->selection_type) {
case MT_JOB:
if (!regex_find_jobids(jcr, &ids, sql_job, sql_jobids_from_job, "Job")) {
goto bail_out;
}
break;
case MT_CLIENT:
if (!regex_find_jobids(jcr, &ids, sql_client, sql_jobids_from_client,
"Client")) {
goto bail_out;
}
break;
case MT_VOLUME:
if (!regex_find_jobids(jcr, &ids, sql_vol, sql_jobids_from_vol,
"Volume")) {
goto bail_out;
}
break;
case MT_SQLQUERY:
if (!jcr->impl->res.job->selection_pattern) {
Jmsg(jcr, M_FATAL, 0, _("No %s SQL selection pattern specified.\n"),
jcr->get_OperationName());
goto bail_out;
}
Dmsg1(dbglevel, "SQL=%s\n", jcr->impl->res.job->selection_pattern);
if (!jcr->db->SqlQuery(jcr->impl->res.job->selection_pattern,
UniqueDbidHandler, (void*)&ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), jcr->db->strerror());
goto bail_out;
}
break;
case MT_SMALLEST_VOL:
if (!find_mediaid_then_jobids(jcr, &ids, sql_smallest_vol,
"Smallest Volume")) {
goto bail_out;
}
break;
case MT_OLDEST_VOL:
if (!find_mediaid_then_jobids(jcr, &ids, sql_oldest_vol,
"Oldest Volume")) {
goto bail_out;
}
break;
case MT_POOL_OCCUPANCY: {
int64_t pool_bytes;
DBId_t DBId = 0;
mid.list = GetPoolMemory(PM_MESSAGE);
mid.list[0] = 0;
mid.count = 0;
jids.list = GetPoolMemory(PM_MESSAGE);
jids.list[0] = 0;
jids.count = 0;
ctx.count = 0;
/*
* Find count of bytes in pool
*/
Mmsg(query, sql_pool_bytes, jcr->impl->res.rpool->resource_name_);
if (!jcr->db->SqlQuery(query.c_str(), db_int64_handler, (void*)&ctx)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), jcr->db->strerror());
goto bail_out;
}
if (ctx.count == 0) {
Jmsg(jcr, M_INFO, 0, _("No Volumes found to %s.\n"),
jcr->get_ActionName());
retval = true;
goto bail_out;
}
pool_bytes = ctx.value;
Dmsg2(dbglevel, "highbytes=%lld pool=%lld\n",
jcr->impl->res.rpool->MigrationHighBytes, pool_bytes);
if (pool_bytes < (int64_t)jcr->impl->res.rpool->MigrationHighBytes) {
Jmsg(jcr, M_INFO, 0, _("No Volumes found to %s.\n"),
jcr->get_ActionName());
retval = true;
goto bail_out;
}
Dmsg0(dbglevel, "We should do Occupation migration.\n");
ids.count = 0;
/*
* Find a list of MediaIds that could be migrated
*/
Mmsg(query, sql_mediaids, jcr->impl->res.rpool->resource_name_);
Dmsg1(dbglevel, "query=%s\n", query.c_str());
if (!jcr->db->SqlQuery(query.c_str(), UniqueDbidHandler, (void*)&ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), jcr->db->strerror());
goto bail_out;
}
if (ids.count == 0) {
Jmsg(jcr, M_INFO, 0, _("No Volumes found to %s.\n"),
jcr->get_ActionName());
retval = true;
goto bail_out;
}
Dmsg2(dbglevel, "Pool Occupancy ids=%d MediaIds=%s\n", ids.count,
ids.list);
if (!FindJobidsFromMediaidList(jcr, &ids, "Volume")) { goto bail_out; }
/*
* ids == list of jobs
*/
p = ids.list;
for (int i = 0; i < (int)ids.count; i++) {
status = GetNextDbidFromList(&p, &DBId);
Dmsg2(dbglevel, "get_next_dbid status=%d JobId=%u\n", status,
(uint32_t)DBId);
if (status < 0) {
Jmsg(jcr, M_FATAL, 0, _("Invalid JobId found.\n"));
goto bail_out;
} else if (status == 0) {
break;
}
mid.count = 1;
Mmsg(mid.list, "%s", edit_int64(DBId, ed1));
if (jids.count > 0) { PmStrcat(jids.list, ","); }
PmStrcat(jids.list, mid.list);
jids.count += mid.count;
/*
* Find count of bytes from Jobs
*/
Mmsg(query, sql_job_bytes, mid.list);
Dmsg1(dbglevel, "Jobbytes query: %s\n", query.c_str());
if (!jcr->db->SqlQuery(query.c_str(), db_int64_handler, (void*)&ctx)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), jcr->db->strerror());
goto bail_out;
}
pool_bytes -= ctx.value;
Dmsg2(dbglevel, "Total %s Job bytes=%s\n", jcr->get_ActionName(),
edit_int64_with_commas(ctx.value, ed1));
Dmsg2(dbglevel, "lowbytes=%s poolafter=%s\n",
edit_int64_with_commas(jcr->impl->res.rpool->MigrationLowBytes,
ed1),
edit_int64_with_commas(pool_bytes, ed2));
if (pool_bytes <= (int64_t)jcr->impl->res.rpool->MigrationLowBytes) {
Dmsg0(dbglevel, "We should be done.\n");
break;
}
}
/*
* Transfer jids to ids, where the jobs list is expected
*/
ids.count = jids.count;
PmStrcpy(ids.list, jids.list);
Dmsg2(dbglevel, "Pool Occupancy ids=%d JobIds=%s\n", ids.count, ids.list);
break;
}
case MT_POOL_TIME: {
time_t ttime;
char dt[MAX_TIME_LENGTH];
ttime = time(NULL) - (time_t)jcr->impl->res.rpool->MigrationTime;
bstrutime(dt, sizeof(dt), ttime);
ids.count = 0;
Mmsg(query, sql_pool_time, jcr->impl->res.rpool->resource_name_, dt);
Dmsg1(dbglevel, "query=%s\n", query.c_str());
if (!jcr->db->SqlQuery(query.c_str(), UniqueDbidHandler, (void*)&ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), jcr->db->strerror());
goto bail_out;
}