/
migrate.c
1486 lines (1333 loc) · 50.1 KB
/
migrate.c
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-2012 Planets Communications B.V.
Copyright (C) 2013-2013 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 MMVIII)
*
* Kern Sibbald, September MMIV
*
* 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 "bareos.h"
#include "dird.h"
#ifndef HAVE_REGEX_H
#include "lib/bregex.h"
#else
#include <regex.h>
#endif
static const int dbglevel = 10;
static int getJob_to_migrate(JCR *jcr);
struct idpkt;
static bool regex_find_jobids(JCR *jcr, idpkt *ids, const char *query1,
const char *query2, const char *type);
static bool find_mediaid_then_jobids(JCR *jcr, idpkt *ids, const char *query1,
const char *type);
static bool find_jobids_from_mediaid_list(JCR *jcr, idpkt *ids, const char *type);
static bool find_jobids_of_pool_uncopied_jobs(JCR *jcr, idpkt *ids);
static void start_migration_job(JCR *jcr);
static int get_next_dbid_from_list(char **p, DBId_t *DBId);
static bool set_migration_next_pool(JCR *jcr, POOLRES **pool);
/*
* Called here before the job is run to do the job
* specific setup. Note, one of the important things to
* complete in this init code is to make the definitive
* choice of input and output storage devices. This is
* because immediately after the init, the job is queued
* in the jobq.c code, and it checks that all the resources
* (storage resources in particular) are available, so these
* must all be properly defined.
*
* previous_jr refers to the job DB record of the Job that is
* going to be migrated.
* prev_job refers to the job resource of the Job that is
* going to be migrated.
* jcr is the jcr for the current "migration" job. It is a
* control job that is put in the DB as a migration job, which
* means that this job migrated a previous job to a new job.
* No Volume or File data is associated with this control
* job.
* mig_jcr refers to the newly migrated job that is run by
* the current jcr. It is a backup job that moves (migrates) the
* data written for the previous_jr into the new pool. This
* job (mig_jcr) becomes the new backup job that replaces
* the original backup job. Note, this jcr is not really run. It
* is simply attached to the current jcr. It will show up in
* the Director's status output, but not in the SD or FD, both of
* which deal only with the current migration job (i.e. jcr).
*/
bool do_migration_init(JCR *jcr)
{
POOLRES *pool = NULL;
JOBRES *job, *prev_job;
JCR *mig_jcr; /* newly migrated job */
int count;
apply_pool_overrides(jcr);
if (!allow_duplicate_job(jcr)) {
return false;
}
jcr->jr.PoolId = get_or_create_pool_record(jcr, jcr->res.pool->name());
if (jcr->jr.PoolId == 0) {
Dmsg1(dbglevel, "JobId=%d no PoolId\n", (int)jcr->JobId);
Jmsg(jcr, M_FATAL, 0, _("Could not get or create a Pool record.\n"));
return false;
}
/*
* Note, at this point, pool is the pool for this job. We
* transfer it to rpool (read pool), and a bit later,
* pool will be changed to point to the write pool,
* which comes from pool->NextPool.
*/
jcr->res.rpool = jcr->res.pool; /* save read pool */
pm_strcpy(jcr->res.rpool_source, jcr->res.pool_source);
Dmsg2(dbglevel, "Read pool=%s (From %s)\n", jcr->res.rpool->name(), jcr->res.rpool_source);
if (!get_or_create_fileset_record(jcr)) {
Dmsg1(dbglevel, "JobId=%d no FileSet\n", (int)jcr->JobId);
Jmsg(jcr, M_FATAL, 0, _("Could not get or create the FileSet record.\n"));
return false;
}
/* If we find a job or jobs to migrate it is previous_jr.JobId */
count = getJob_to_migrate(jcr);
if (count < 0) {
return false;
}
if (count == 0) {
set_migration_next_pool(jcr, &pool);
return true; /* no work */
}
Dmsg1(dbglevel, "Back from getJob_to_migrate JobId=%d\n", (int)jcr->JobId);
if (jcr->previous_jr.JobId == 0) {
Dmsg1(dbglevel, "JobId=%d no previous JobId\n", (int)jcr->JobId);
Jmsg(jcr, M_INFO, 0, _("No previous Job found to %s.\n"), jcr->get_ActionName());
set_migration_next_pool(jcr, &pool);
return true; /* no work */
}
if (create_restore_bootstrap_file(jcr) < 0) {
Jmsg(jcr, M_FATAL, 0, _("Create bootstrap file failed.\n"));
return false;
}
if (jcr->previous_jr.JobId == 0 || jcr->ExpectedFiles == 0) {
jcr->setJobStatus(JS_Terminated);
Dmsg1(dbglevel, "JobId=%d expected files == 0\n", (int)jcr->JobId);
if (jcr->previous_jr.JobId == 0) {
Jmsg(jcr, M_INFO, 0, _("No previous Job found to %s.\n"), jcr->get_ActionName());
} else {
Jmsg(jcr, M_INFO, 0, _("Previous Job has no data to %s.\n"), jcr->get_ActionName());
}
set_migration_next_pool(jcr, &pool);
return true; /* no work */
}
Dmsg5(dbglevel, "JobId=%d: Current: Name=%s JobId=%d Type=%c Level=%c\n",
(int)jcr->JobId,
jcr->jr.Name, (int)jcr->jr.JobId,
jcr->jr.JobType, jcr->jr.JobLevel);
LockRes();
job = (JOBRES *)GetResWithName(R_JOB, jcr->jr.Name);
prev_job = (JOBRES *)GetResWithName(R_JOB, jcr->previous_jr.Name);
UnlockRes();
if (!job) {
Jmsg(jcr, M_FATAL, 0, _("Job resource not found for \"%s\".\n"), jcr->jr.Name);
return false;
}
if (!prev_job) {
Jmsg(jcr, M_FATAL, 0, _("Previous Job resource not found for \"%s\".\n"),
jcr->previous_jr.Name);
return false;
}
jcr->spool_data = job->spool_data; /* turn on spooling if requested in job */
/* Create a migration jcr */
mig_jcr = jcr->mig_jcr = new_jcr(sizeof(JCR), dird_free_jcr);
memcpy(&mig_jcr->previous_jr, &jcr->previous_jr, sizeof(mig_jcr->previous_jr));
/*
* Turn the mig_jcr into a "real" job that takes on the aspects of
* the previous backup job "prev_job".
*/
set_jcr_defaults(mig_jcr, prev_job);
if (!setup_job(mig_jcr)) {
Jmsg(jcr, M_FATAL, 0, _("setup job failed.\n"));
return false;
}
/* Now reset the job record from the previous job */
memcpy(&mig_jcr->jr, &jcr->previous_jr, sizeof(mig_jcr->jr));
/* Update the jr to reflect the new values of PoolId and JobId. */
mig_jcr->jr.PoolId = jcr->jr.PoolId;
mig_jcr->jr.JobId = mig_jcr->JobId;
/* Don't let WatchDog checks Max*Time value on this Job */
mig_jcr->no_maxtime = true;
/*
* Don't check for duplicates on migration and copy jobs
*/
mig_jcr->res.job->IgnoreDuplicateJobChecking = true;
Dmsg4(dbglevel, "mig_jcr: Name=%s JobId=%d Type=%c Level=%c\n",
mig_jcr->jr.Name, (int)mig_jcr->jr.JobId,
mig_jcr->jr.JobType, mig_jcr->jr.JobLevel);
if (set_migration_next_pool(jcr, &pool)) {
/* If pool storage specified, use it as source */
copy_rstorage(mig_jcr, pool->storage, _("Pool resource"));
copy_rstorage(jcr, pool->storage, _("Pool resource"));
mig_jcr->res.pool = jcr->res.pool;
mig_jcr->res.next_pool = jcr->res.next_pool;
mig_jcr->jr.PoolId = jcr->jr.PoolId;
}
return true;
}
/*
* set_migration_next_pool() called by do_migration_init()
* at differents stages.
* The idea here is to make a common subroutine for the
* NextPool's search code and to permit do_migration_init()
* to return with NextPool set in jcr struct.
*/
static bool set_migration_next_pool(JCR *jcr, POOLRES **retpool)
{
POOL_DBR pr;
char ed1[100];
POOLRES *pool;
const char *storage_source;
/*
* Get the PoolId used with the original job. Then
* find the pool name from the database record.
*/
memset(&pr, 0, sizeof(pr));
pr.PoolId = jcr->jr.PoolId;
if (!db_get_pool_record(jcr, jcr->db, &pr)) {
Jmsg(jcr, M_FATAL, 0, _("Pool for JobId %s not in database. ERR=%s\n"),
edit_int64(pr.PoolId, ed1), db_strerror(jcr->db));
return false;
}
/*
* Get the pool resource corresponding to the original job
*/
pool = (POOLRES *)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->res.run_next_pool_override) {
pm_strcpy(jcr->res.npool_source, _("Run NextPool override"));
pm_strcpy(jcr->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->res.job->next_pool) {
jcr->res.next_pool = jcr->res.job->next_pool;
pm_strcpy(jcr->res.npool_source, _("Job's NextPool resource"));
pm_strcpy(jcr->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->res.next_pool = pool->NextPool;
pm_strcpy(jcr->res.npool_source, _("Job Pool's NextPool resource"));
pm_strcpy(jcr->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->res.next_pool) {
jcr->jr.PoolId = get_or_create_pool_record(jcr, jcr->res.next_pool->name());
if (jcr->jr.PoolId == 0) {
return false;
}
}
if (!set_migration_wstorage(jcr, pool, jcr->res.next_pool, storage_source)) {
return false;
}
jcr->res.pool = jcr->res.next_pool;
Dmsg2(dbglevel, "Write pool=%s read rpool=%s\n", jcr->res.pool->name(), jcr->res.rpool->name());
return true;
}
/*
* Do a Migration of a previous job
*
* Returns: false on failure
* true on success
*/
bool do_migration(JCR *jcr)
{
char ed1[100];
BSOCK *sd;
JCR *mig_jcr = jcr->mig_jcr; /* newly migrated job */
/*
* If mig_jcr is NULL, there is nothing to do for this job,
* so set a normal status, cleanup and return OK.
*/
if (!mig_jcr) {
jcr->setJobStatus(JS_Terminated);
migration_cleanup(jcr, jcr->JobStatus);
return true;
}
if (!db_get_job_record(jcr, jcr->db, &jcr->previous_jr)) {
Jmsg(jcr, M_FATAL, 0, _("Could not get job record for JobId %s to %s. ERR=%s"),
edit_int64(jcr->previous_jr.JobId, ed1),
jcr->get_ActionName(),
db_strerror(jcr->db));
jcr->setJobStatus(JS_Terminated);
migration_cleanup(jcr, jcr->JobStatus);
return true;
}
/* Make sure this job was not already migrated */
if (jcr->previous_jr.JobType != JT_BACKUP &&
jcr->previous_jr.JobType != JT_JOB_COPY) {
Jmsg(jcr, M_INFO, 0, _("JobId %s already %s probably by another Job. %s stopped.\n"),
edit_int64(jcr->previous_jr.JobId, ed1),
jcr->get_ActionName(true),
jcr->get_OperationName());
jcr->setJobStatus(JS_Terminated);
migration_cleanup(jcr, jcr->JobStatus);
return true;
}
/* Print Job Start message */
Jmsg(jcr, M_INFO, 0, _("Start %s JobId %s, Job=%s\n"),
jcr->get_OperationName(), edit_uint64(jcr->JobId, ed1), jcr->Job);
/*
* Open a message channel connection with the Storage
* daemon. This is to let him know that our client
* will be contacting him for a backup session.
*
*/
Dmsg0(110, "Open connection with storage daemon\n");
jcr->setJobStatus(JS_WaitSD);
mig_jcr->setJobStatus(JS_WaitSD);
/*
* Start conversation with Storage daemon
*/
if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
return false;
}
sd = jcr->store_bsock;
/*
* Now start a job with the Storage daemon
*/
Dmsg2(dbglevel, "Read store=%s, write store=%s\n",
((STORERES *)jcr->rstorage->first())->name(),
((STORERES *)jcr->wstorage->first())->name());
if (!start_storage_daemon_job(jcr, jcr->rstorage, jcr->wstorage, /*send_bsr*/true)) {
return false;
}
Dmsg0(150, "Storage daemon connection OK\n");
/*
* We re-update the job start record so that the start
* time is set after the run before job. This avoids
* that any files created by the run before job will
* be saved twice. They will be backed up in the current
* job, but not in the next one unless they are changed.
* Without this, they will be backed up in this job and
* in the next job run because in that case, their date
* is after the start of this run.
*/
jcr->start_time = time(NULL);
jcr->jr.StartTime = jcr->start_time;
jcr->jr.JobTDate = jcr->start_time;
jcr->setJobStatus(JS_Running);
/* Update job start record for this migration control job */
if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
return false;
}
/* Declare the job started to start the MaxRunTime check */
jcr->setJobStarted();
mig_jcr->start_time = time(NULL);
mig_jcr->jr.StartTime = mig_jcr->start_time;
mig_jcr->jr.JobTDate = mig_jcr->start_time;
mig_jcr->setJobStatus(JS_Running);
/* Update job start record for the real migration backup job */
if (!db_update_job_start_record(mig_jcr, mig_jcr->db, &mig_jcr->jr)) {
Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(mig_jcr->db));
return false;
}
Dmsg4(dbglevel, "mig_jcr: Name=%s JobId=%d Type=%c Level=%c\n",
mig_jcr->jr.Name, (int)mig_jcr->jr.JobId,
mig_jcr->jr.JobType, mig_jcr->jr.JobLevel);
/*
* Start the job prior to starting the message thread below
* to avoid two threads from using the BSOCK structure at
* the same time.
*/
if (!sd->fsend("run")) {
return false;
}
/*
* Now start a Storage daemon message thread
*/
if (!start_storage_daemon_message_thread(jcr)) {
return false;
}
jcr->setJobStatus(JS_Running);
mig_jcr->setJobStatus(JS_Running);
/* Pickup Job termination data */
/* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/JobErrors */
wait_for_storage_daemon_termination(jcr);
jcr->setJobStatus(jcr->SDJobStatus);
db_write_batch_file_records(jcr); /* used by bulk batch file insert */
if (jcr->JobStatus != JS_Terminated) {
return false;
}
migration_cleanup(jcr, jcr->JobStatus);
return true;
}
struct idpkt {
POOLMEM *list;
uint32_t count;
};
/* Add an item to the list if it is unique */
static void add_unique_id(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 {
pm_strcat(ids->list, ",");
}
pm_strcat(ids->list, item);
ids->count++;
// Dmsg3(0, "add_uniq count=%d Ids=%p %s\n", ids->count, ids->list, ids->list);
return;
}
/*
* Callback handler make list of DB Ids
*/
static int unique_dbid_handler(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 */
}
add_unique_id(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 item_compare(void *item1, void *item2)
{
uitem *i1 = (uitem *)item1;
uitem *i2 = (uitem *)item2;
return strcmp(i1->item, i2->item);
}
static int unique_name_handler(void *ctx, int num_fields, char **row)
{
dlist *list = (dlist *)ctx;
uitem *new_item = (uitem *)malloc(sizeof(uitem));
uitem *item;
memset(new_item, 0, sizeof(uitem));
new_item->item = bstrdup(row[0]);
Dmsg1(dbglevel, "Unique_name_hdlr Item=%s\n", row[0]);
item = (uitem *)list->binary_insert((void *)new_item, item_compare);
if (item != new_item) { /* already in list */
free(new_item->item);
free((char *)new_item);
return 0;
}
return 0;
}
/* 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";
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";
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";
/*
* const char *sql_ujobid =
* "SELECT DISTINCT Job.Job from Client,Pool,Media,Job,JobMedia "
* " WHERE Media.PoolId=Pool.PoolId AND Pool.Name='%s' AND"
* " JobMedia.JobId=Job.JobId AND Job.PoolId=Media.PoolId";
*/
/*
*
* This is the central piece of code that finds a job or jobs
* actually JobIds to migrate. It first looks to see if one
* has been "manually" specified in jcr->MigrateJobId, and if
* so, it returns that JobId to be run. Otherwise, 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, except the last
* one starting a new job with MigrationJobId set to that JobId, and
* finally, it returns the last JobId to the caller.
*
* Returns: -1 on error
* 0 if no jobs to migrate
* 1 if OK and jcr->previous_jr filled in
*/
static int getJob_to_migrate(JCR *jcr)
{
char ed1[30], ed2[30];
POOL_MEM query(PM_MESSAGE);
JobId_t JobId;
DBId_t DBId = 0;
int status;
char *p;
idpkt ids, mid, jids;
db_int64_ctx ctx;
int64_t pool_bytes;
time_t ttime;
struct tm tm;
char dt[MAX_TIME_LENGTH];
int count = 0;
int limit = 99; /* limit + 1 is max jobs to start */
ids.list = get_pool_memory(PM_MESSAGE);
ids.list[0] = 0;
ids.count = 0;
mid.list = get_pool_memory(PM_MESSAGE);
mid.list[0] = 0;
mid.count = 0;
jids.list = get_pool_memory(PM_MESSAGE);
jids.list[0] = 0;
jids.count = 0;
/*
* If MigrateJobId is set, then we migrate only that Job,
* otherwise, we go through the full selection of jobs to
* migrate.
*/
if (jcr->MigrateJobId != 0) {
Dmsg1(dbglevel, "At Job start previous jobid=%u\n", jcr->MigrateJobId);
JobId = jcr->MigrateJobId;
} else {
switch (jcr->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->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->res.job->selection_pattern);
if (!db_sql_query(jcr->db, jcr->res.job->selection_pattern,
unique_dbid_handler, (void *)&ids)) {
Jmsg(jcr, M_FATAL, 0,
_("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
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:
ctx.count = 0;
/* Find count of bytes in pool */
Mmsg(query, sql_pool_bytes, jcr->res.rpool->name());
if (!db_sql_query(jcr->db, query.c_str(), db_int64_handler, (void *)&ctx)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
goto bail_out;
}
if (ctx.count == 0) {
Jmsg(jcr, M_INFO, 0, _("No Volumes found to %s.\n"), jcr->get_ActionName());
goto ok_out;
}
pool_bytes = ctx.value;
Dmsg2(dbglevel, "highbytes=%lld pool=%lld\n", jcr->res.rpool->MigrationHighBytes,
pool_bytes);
if (pool_bytes < (int64_t)jcr->res.rpool->MigrationHighBytes) {
Jmsg(jcr, M_INFO, 0, _("No Volumes found to %s.\n"), jcr->get_ActionName());
goto ok_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->res.rpool->name());
Dmsg1(dbglevel, "query=%s\n", query.c_str());
if (!db_sql_query(jcr->db, query.c_str(), unique_dbid_handler, (void *)&ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
goto bail_out;
}
if (ids.count == 0) {
Jmsg(jcr, M_INFO, 0, _("No Volumes found to %s.\n"), jcr->get_ActionName());
goto ok_out;
}
Dmsg2(dbglevel, "Pool Occupancy ids=%d MediaIds=%s\n", ids.count, ids.list);
if (!find_jobids_from_mediaid_list(jcr, &ids, "Volume")) {
goto bail_out;
}
/* ids == list of jobs */
p = ids.list;
for (int i = 0; i < (int)ids.count; i++) {
status = get_next_dbid_from_list(&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) {
pm_strcat(jids.list, ",");
}
pm_strcat(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 (!db_sql_query(jcr->db, query.c_str(), db_int64_handler, (void *)&ctx)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
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->res.rpool->MigrationLowBytes, ed1),
edit_int64_with_commas(pool_bytes, ed2));
if (pool_bytes <= (int64_t)jcr->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;
pm_strcpy(ids.list, jids.list);
Dmsg2(dbglevel, "Pool Occupancy ids=%d JobIds=%s\n", ids.count, ids.list);
break;
case MT_POOL_TIME:
ttime = time(NULL) - (time_t)jcr->res.rpool->MigrationTime;
(void)localtime_r(&ttime, &tm);
strftime(dt, sizeof(dt), "%Y-%m-%d %H:%M:%S", &tm);
ids.count = 0;
Mmsg(query, sql_pool_time, jcr->res.rpool->name(), dt);
Dmsg1(dbglevel, "query=%s\n", query.c_str());
if (!db_sql_query(jcr->db, query.c_str(), unique_dbid_handler, (void *)&ids)) {
Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
goto bail_out;
}
if (ids.count == 0) {
Jmsg(jcr, M_INFO, 0, _("No Volumes found to %s.\n"), jcr->get_ActionName());
goto ok_out;
}
Dmsg2(dbglevel, "PoolTime ids=%d JobIds=%s\n", ids.count, ids.list);
break;
case MT_POOL_UNCOPIED_JOBS:
if (!find_jobids_of_pool_uncopied_jobs(jcr, &ids)) {
goto bail_out;
}
break;
default:
Jmsg(jcr, M_FATAL, 0, _("Unknown %s Selection Type.\n"), jcr->get_OperationName());
goto bail_out;
}
/*
* Loop over all jobids except the last one, sending
* them to start_migration_job(), which will start a job
* for each of them. For the last JobId, we handle it below.
*/
p = ids.list;
if (ids.count == 0) {
Jmsg(jcr, M_INFO, 0, _("No JobIds found to %s.\n"), jcr->get_ActionName());
goto ok_out;
}
Jmsg(jcr, M_INFO, 0, _("The following %u JobId%s chosen to be %s: %s\n"),
ids.count, (ids.count < 2) ? _(" was") : _("s were"),
jcr->get_ActionName(true), ids.list);
Dmsg2(dbglevel, "Before loop count=%d ids=%s\n", ids.count, ids.list);
/*
* Note: to not over load the system, limit the number
* of new jobs started to 100 (see limit above)
*/
for (int i = 1; i < (int)ids.count; i++) {
JobId = 0;
status = get_next_jobid_from_list(&p, &JobId);
Dmsg3(dbglevel, "getJobid_no=%d status=%d JobId=%u\n", i, status, JobId);
if (status < 0) {
Jmsg(jcr, M_FATAL, 0, _("Invalid JobId found.\n"));
goto bail_out;
} else if (status == 0) {
Jmsg(jcr, M_INFO, 0, _("No JobIds found to %s.\n"), jcr->get_ActionName());
goto ok_out;
}
jcr->MigrateJobId = JobId;
/* Don't start any more when limit reaches zero */
limit--;
if (limit > 0) {
start_migration_job(jcr);
Dmsg0(dbglevel, "Back from start_migration_job\n");
}
}
/* Now get the last JobId and handle it in the current job */
JobId = 0;
status = get_next_jobid_from_list(&p, &JobId);
Dmsg2(dbglevel, "Last get_next_jobid status=%d JobId=%u\n", status, (int)JobId);
if (status < 0) {
Jmsg(jcr, M_FATAL, 0, _("Invalid JobId found.\n"));
goto bail_out;
} else if (status == 0) {
Jmsg(jcr, M_INFO, 0, _("No JobIds found to %s.\n"), jcr->get_ActionName());
goto ok_out;
}
}
jcr->previous_jr.JobId = JobId;
Dmsg1(dbglevel, "Previous jobid=%d\n", (int)jcr->previous_jr.JobId);
if (!db_get_job_record(jcr, jcr->db, &jcr->previous_jr)) {
Jmsg(jcr, M_FATAL, 0, _("Could not get job record for JobId %s to %s. ERR=%s"),
edit_int64(jcr->previous_jr.JobId, ed1),
jcr->get_ActionName(), db_strerror(jcr->db));
goto bail_out;
}
Jmsg(jcr, M_INFO, 0, _("%s using JobId=%s Job=%s\n"),
jcr->get_OperationName(),
edit_int64(jcr->previous_jr.JobId, ed1), jcr->previous_jr.Job);
Dmsg4(dbglevel, "%s JobId=%d using JobId=%s Job=%s\n",
jcr->get_OperationName(),
jcr->JobId,
edit_int64(jcr->previous_jr.JobId, ed1), jcr->previous_jr.Job);
count = 1;
ok_out:
goto out;
bail_out:
count = -1;
out:
free_pool_memory(ids.list);
free_pool_memory(mid.list);
free_pool_memory(jids.list);
return count;
}
static void start_migration_job(JCR *jcr)
{
char ed1[50];
JobId_t jobid;
UAContext *ua;
POOL_MEM cmd(PM_MESSAGE);
ua = new_ua_context(jcr);
ua->batch = true;
Mmsg(ua->cmd, "run job=\"%s\" jobid=%s ignoreduplicatecheck=yes",
jcr->res.job->name(), edit_uint64(jcr->MigrateJobId, ed1));
/*
* Make sure we have something to compare against.
*/
if (jcr->res.pool) {
/*
* See if there was actually a pool override.
*/
if (jcr->res.pool != jcr->res.job->pool) {
Mmsg(cmd, " pool=\"%s\"", jcr->res.pool->name());
pm_strcat(ua->cmd, cmd.c_str());
}
/*
* See if there was actually a next pool override.
*/
if (jcr->res.next_pool && jcr->res.next_pool != jcr->res.pool->NextPool) {
Mmsg(cmd, " nextpool=\"%s\"", jcr->res.next_pool->name());
pm_strcat(ua->cmd, cmd.c_str());
}
}
Dmsg2(dbglevel, "=============== %s cmd=%s\n", jcr->get_OperationName(), ua->cmd);
parse_ua_args(ua); /* parse command */
jobid = run_cmd(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);
}
free_ua_context(ua);
}
static bool find_mediaid_then_jobids(JCR *jcr, idpkt *ids,
const char *query1,
const char *type)
{
bool ok = false;
POOL_MEM query(PM_MESSAGE);
ids->count = 0;
/* Basic query for MediaId */
Mmsg(query, query1, jcr->res.rpool->name());