]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/make_ingres_tables.in
caeeaffddee883137ae5d5d29f44ed07e574e032
[bacula/bacula] / bacula / src / cats / make_ingres_tables.in
1 #!/bin/sh
2 #
3 # shell script to create Bacula PostgreSQL tables
4 #
5 bindir=@SQL_BINDIR@
6 PATH="$bindir:$PATH"
7 db_name=${db_name:-@db_name@}
8
9 sql $* ${db_name} <<END-OF-DATA
10
11 SET AUTOCOMMIT ON\g
12
13 CREATE SEQUENCE filename_seq;
14 CREATE TABLE filename
15 (
16     filenameid        integer     not null default filename_seq.nextval,
17     name              text        not null,
18     primary key (filenameid)
19 );
20
21 ALTER TABLE filename ALTER COLUMN name SET STATISTICS 1000;
22 CREATE UNIQUE INDEX filename_name_idx on filename (name);
23
24 CREATE SEQUENCE path_seq;
25 CREATE TABLE path
26 (
27     pathid            integer     not null default path_seq.nextval,
28     path              text        not null,
29     primary key (pathid)
30 );
31
32 ALTER TABLE path ALTER COLUMN path SET STATISTICS 1000;
33 CREATE UNIQUE INDEX path_name_idx on path (path);
34
35 CREATE SEQUENCE file_seq;
36 CREATE TABLE file
37 (
38     fileid            integer8    not null default file_seq.nextval,
39     fileindex         integer     not null  default 0,
40     jobid             integer     not null,
41     pathid            integer     not null,
42     filenameid        integer     not null,
43     markid            integer     not null  default 0,
44     lstat             text        not null,
45     md5               text        not null,
46     primary key (fileid)
47 );
48
49 CREATE INDEX file_jobid_idx on file (jobid);
50 CREATE INDEX file_fp_idx on file (filenameid, pathid);
51
52 --
53 -- Add this if you have a good number of job
54 -- that run at the same time
55 -- ALTER SEQUENCE file_fileid_seq CACHE 1000;
56
57 --
58 -- Possibly add one or more of the following indexes
59 --  if your Verifies are too slow.
60 --
61 -- CREATE INDEX file_pathid_idx on file(pathid);
62 -- CREATE INDEX file_filenameid_idx on file(filenameid);
63 -- CREATE INDEX file_jpfid_idx on file (jobid, pathid, filenameid);
64
65 CREATE SEQUENCE job_seq;
66 CREATE TABLE job
67 (
68     jobid             integer     not null default job_seq.nextval,
69     job               text        not null,
70     name              text        not null,
71     type              char(1)     not null,
72     level             char(1)     not null,
73     clientid          integer     default 0,
74     jobstatus         char(1)     not null,
75     schedtime         timestamp   without time zone,
76     starttime         timestamp   without time zone,
77     endtime           timestamp   without time zone,
78     realendtime       timestamp   without time zone,
79     jobtdate          bigint      default 0,
80     volsessionid      integer     default 0,
81     volsessiontime    integer     default 0,
82     jobfiles          integer     default 0,
83     jobbytes          bigint      default 0,
84     readbytes        bigint      default 0,
85     joberrors         integer     default 0,
86     jobmissingfiles   integer     default 0,
87     poolid            integer     default 0,
88     filesetid         integer     default 0,
89     purgedfiles       smallint    default 0,
90     hasbase           smallint    default 0,
91     priorjobid        integer     default 0,
92     primary key (jobid)
93 );
94
95 CREATE INDEX job_name_idx on job (name);
96
97 -- Create a table like Job for long term statistics 
98 CREATE TABLE JobHisto (LIKE Job);
99 CREATE INDEX jobhisto_idx ON jobhisto ( starttime );
100
101
102 CREATE SEQUENCE Location_seq;
103 CREATE TABLE Location (
104    LocationId         integer     not null default Location_seq.nextval,
105    Location           text        not null,
106    Cost               integer     default 0,
107    Enabled            smallint,
108    primary key (LocationId)
109 );
110
111
112 CREATE SEQUENCE fileset_seq;
113 CREATE TABLE fileset
114 (
115     filesetid         integer     not null default fileset_seq.nextval,
116     fileset           text        not null,
117     md5               text        not null,
118     createtime        timestamp without time zone not null,
119     primary key (filesetid)
120 );
121
122 CREATE INDEX fileset_name_idx on fileset (fileset);
123
124 CREATE SEQUENCE jobmedia_seq;
125 CREATE TABLE jobmedia
126 (
127     jobmediaid        integer     not null default jobmedia_seq.nestval,
128     jobid             integer     not null,
129     mediaid           integer     not null,
130     firstindex        integer     default 0,
131     lastindex         integer     default 0,
132     startfile         integer     default 0,
133     endfile           integer     default 0,
134     startblock        bigint      default 0,
135     endblock          bigint      default 0,
136     volindex          integer     default 0,
137     copy              integer     default 0,
138     primary key (jobmediaid)
139 );
140
141 CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
142
143 CREATE SEQUENCE media_seq;
144 CREATE TABLE media
145 (
146     mediaid           integer     not null default media_seq.nextval,
147     volumename        text        not null,
148     slot              integer     default 0,
149     poolid            integer     default 0,
150     mediatype         text        not null,
151     mediatypeid       integer     default 0,
152     labeltype         integer     default 0,
153     firstwritten      timestamp   without time zone,
154     lastwritten       timestamp   without time zone,
155     labeldate         timestamp   without time zone,
156     voljobs           integer     default 0,
157     volfiles          integer     default 0,
158     volblocks         integer     default 0,
159     volmounts         integer     default 0,
160     volbytes          bigint      default 0,
161     volparts          integer     default 0,
162     volerrors         integer     default 0,
163     volwrites         integer     default 0,
164     volcapacitybytes  bigint      default 0,
165     volstatus         text        not null
166         check (volstatus in ('Full','Archive','Append',
167               'Recycle','Purged','Read-Only','Disabled',
168               'Error','Busy','Used','Cleaning','Scratch')),
169     enabled           smallint    default 1,
170     recycle           smallint    default 0,
171     ActionOnPurge     smallint    default 0,
172     volretention      bigint      default 0,
173     voluseduration    bigint      default 0,
174     maxvoljobs        integer     default 0,
175     maxvolfiles       integer     default 0,
176     maxvolbytes       bigint      default 0,
177     inchanger         smallint    default 0,
178     StorageId         bigint      default 0,
179     DeviceId          integer     default 0,
180     mediaaddressing   smallint    default 0,
181     volreadtime       bigint      default 0,
182     volwritetime      bigint      default 0,
183     endfile           integer     default 0,
184     endblock          bigint      default 0,
185     LocationId        integer     default 0,
186     recyclecount      integer     default 0,
187     initialwrite      timestamp   without time zone,
188     scratchpoolid     integer     default 0,
189     recyclepoolid     integer     default 0,
190     comment           text,
191     primary key (mediaid)
192 );
193
194 create unique index media_volumename_id on media (volumename);
195
196  
197 CREATE SEQUENCE MediaType_seq;
198 CREATE TABLE MediaType (
199    MediaTypeId INTEGER DEFAULT MediaType_seq.NEXTVAL,
200    MediaType TEXT NOT NULL,
201    ReadOnly INTEGER DEFAULT 0,
202    PRIMARY KEY(MediaTypeId)
203    );
204
205 CREATE SEQUENCE Storage_seq;
206 CREATE TABLE Storage (
207    StorageId INTEGER DEFAULT Storage_seq.NEXTVAL,
208    Name TEXT NOT NULL,
209    AutoChanger INTEGER DEFAULT 0,
210    PRIMARY KEY(StorageId)
211    );
212
213 CREATE SEQUENCE Device_seq;
214 CREATE TABLE Device (
215    DeviceId INTEGER DEFAULT Device_seq.NEXTVAL,
216    Name TEXT NOT NULL,
217    MediaTypeId INTEGER NOT NULL,
218    StorageId INTEGER NOT NULL,
219    DevMounts INTEGER NOT NULL DEFAULT 0,
220    DevReadBytes BIGINT NOT NULL DEFAULT 0,
221    DevWriteBytes BIGINT NOT NULL DEFAULT 0,
222    DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
223    DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
224    DevReadTime BIGINT NOT NULL DEFAULT 0,
225    DevWriteTime BIGINT NOT NULL DEFAULT 0,
226    DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
227    DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
228    CleaningDate timestamp without time zone,
229    CleaningPeriod BIGINT NOT NULL DEFAULT 0,
230    PRIMARY KEY(DeviceId)
231    );
232
233
234 CREATE SEQUENCE pool_seq;
235 CREATE TABLE pool
236 (
237     poolid            integer     not null default pool_seq.nextval,
238     name              text        not null,
239     numvols           integer     default 0,
240     maxvols           integer     default 0,
241     useonce           smallint    default 0,
242     usecatalog        smallint    default 0,
243     acceptanyvolume   smallint    default 0,
244     volretention      bigint      default 0,
245     voluseduration    bigint      default 0,
246     maxvoljobs        integer     default 0,
247     maxvolfiles       integer     default 0,
248     maxvolbytes       bigint      default 0,
249     autoprune         smallint    default 0,
250     recycle           smallint    default 0,
251     ActionOnPurge     smallint    default 0,
252     pooltype          text                          
253       check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
254     labeltype         integer     default 0,
255     labelformat       text        not null,
256     enabled           smallint    default 1,
257     scratchpoolid     integer     default 0,
258     recyclepoolid     integer     default 0,
259     NextPoolId        integer     default 0,
260     MigrationHighBytes BIGINT     DEFAULT 0,
261     MigrationLowBytes  BIGINT     DEFAULT 0,
262     MigrationTime      BIGINT     DEFAULT 0,
263     primary key (poolid)
264 );
265
266 CREATE INDEX pool_name_idx on pool (name);
267
268 CREATE SEQUENCE client_seq;
269 CREATE TABLE client
270 (
271     clientid          integer     not null default client_seq.nextval,
272     name              text        not null,
273     uname             text        not null,
274     autoprune         smallint    default 0,
275     fileretention     bigint      default 0,
276     jobretention      bigint      default 0,
277     primary key (clientid)
278 );
279
280 create unique index client_name_idx on client (name);
281
282 CREATE SEQUENCE Log_seq;
283 CREATE TABLE Log
284 (
285     LogId             integer     not null default Log_seq.nextval,
286     JobId             integer     not null,
287     Time              timestamp   without time zone,
288     LogText           text        not null,
289     primary key (LogId)
290 );
291 create index log_name_idx on Log (JobId);
292
293 CREATE SEQUENCE LocationLog_seq;
294 CREATE TABLE LocationLog (
295    LocLogId INTEGER NOT NULL DEFAULT LocationLog_seq.NEXTVAL,
296    Date timestamp   without time zone,
297    Comment TEXT NOT NULL,
298    MediaId INTEGER DEFAULT 0,
299    LocationId INTEGER DEFAULT 0,
300    newvolstatus text not null
301         check (newvolstatus in ('Full','Archive','Append',
302               'Recycle','Purged','Read-Only','Disabled',
303               'Error','Busy','Used','Cleaning','Scratch')),
304    newenabled smallint,
305    PRIMARY KEY(LocLogId)
306 );
307
308
309
310 CREATE TABLE counters
311 (
312     counter           text        not null,
313     minvalue          integer     default 0,
314     maxvalue          integer     default 0,
315     currentvalue      integer     default 0,
316     wrapcounter       text        not null,
317     primary key (counter)
318 );
319
320
321
322 CREATE SEQUENCE basefiles_seq;
323 CREATE TABLE basefiles
324 (
325     baseid            integer               not null default basefiles_seq.nextval,
326     jobid             integer               not null,
327     fileid            bigint                not null,
328     fileindex         integer                       ,
329     basejobid         integer                       ,
330     primary key (baseid)
331 );
332
333 CREATE TABLE unsavedfiles
334 (
335     UnsavedId         integer               not null,
336     jobid             integer               not null,
337     pathid            integer               not null,
338     filenameid        integer               not null,
339     primary key (UnsavedId)
340 );
341
342 CREATE TABLE CDImages 
343 (
344    MediaId integer not null,
345    LastBurn timestamp without time zone not null,
346    primary key (MediaId)
347 );
348
349
350 CREATE TABLE version
351 (
352     versionid         integer               not null
353 );
354
355 CREATE TABLE Status (
356    JobStatus CHAR(1) NOT NULL,
357    JobStatusLong TEXT, 
358    PRIMARY KEY (JobStatus)
359    );
360
361 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
362    ('C', 'Created, not yet running');
363 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
364    ('R', 'Running');
365 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
366    ('B', 'Blocked');
367 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
368    ('T', 'Completed successfully');
369 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
370    ('E', 'Terminated with errors');
371 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
372    ('e', 'Non-fatal error');
373 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
374    ('f', 'Fatal error');
375 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
376    ('D', 'Verify found differences');
377 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
378    ('A', 'Canceled by user');
379 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
380    ('F', 'Waiting for Client');
381 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
382    ('S', 'Waiting for Storage daemon');
383 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
384    ('m', 'Waiting for new media');
385 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
386    ('M', 'Waiting for media mount');
387 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
388    ('s', 'Waiting for storage resource');
389 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
390    ('j', 'Waiting for job resource');
391 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
392    ('c', 'Waiting for client resource');
393 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
394    ('d', 'Waiting on maximum jobs');
395 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
396    ('t', 'Waiting on start time');
397 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
398    ('p', 'Waiting on higher priority jobs');
399 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
400    ('a', 'SD despooling attributes');
401 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
402    ('i', 'Doing batch insert file records');
403
404 INSERT INTO Version (VersionId) VALUES (12);
405
406 -- Make sure we have appropriate permissions
407 \g
408
409 END-OF-DATA
410 pstat=$?
411 if test $pstat = 0; 
412 then
413    echo "Creation of Bacula Ingres tables succeeded."
414 else
415    echo "Creation of Bacula Ingres tables failed."
416 fi
417 exit $pstat