]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/make_postgresql_tables.in
5832b234d69ad902761f0c80166123d4ad9168cf
[bacula/bacula] / bacula / src / cats / make_postgresql_tables.in
1 #!/bin/sh
2 #
3 # shell script to create Bacula PostgreSQL tables
4 #
5 bindir=@SQL_BINDIR@
6
7 if $bindir/psql -f - -d bacula $* <<END-OF-DATA
8
9 CREATE TABLE filename
10 (
11     filenameid        serial      not null,
12     name              text        not null,
13     primary key (filenameid)
14 );
15
16 CREATE INDEX filename_name_idx on filename (name);
17
18 CREATE TABLE path
19 (
20     pathid            serial      not null,
21     path              text        not null,
22     primary key (pathid)
23 );
24
25 CREATE INDEX path_name_idx on path (path);
26
27 CREATE TABLE file
28 (
29     fileid            serial      not null,
30     fileindex         integer     not null  default 0,
31     jobid             integer     not null,
32     pathid            integer     not null,
33     filenameid        integer     not null,
34     markid            integer     not null  default 0,
35     lstat             text        not null,
36     md5               text        not null,
37     primary key (fileid)
38 );
39
40 CREATE INDEX file_jobid_idx on file (jobid);
41 CREATE INDEX file_fp_idx on file (filenameid, pathid);
42
43 --
44 -- Possibly add one or more of the following indexes
45 --  if your Verifies are too slow.
46 --
47 -- CREATE INDEX file_pathid_idx on file(pathid);
48 -- CREATE INDEX file_filenameid_idx on file(filenameid);
49 -- CREATE INDEX file_jpfid_idx on file (jobid, pathid, filenameid);
50
51 CREATE TABLE job
52 (
53     jobid             serial      not null,
54     job               text        not null,
55     name              text        not null,
56     type              char(1)     not null,
57     level             char(1)     not null,
58     clientid          integer,
59     jobstatus         char(1)     not null,
60     schedtime         timestamp   without time zone not null,
61     starttime         timestamp   without time zone,
62     endtime           timestamp   without time zone,
63     jobtdate          bigint      not null,
64     volsessionid      integer     not null default 0,
65     volsessiontime    integer     not null default 0,
66     jobfiles          integer     not null default 0,
67     jobbytes          bigint      not null default 0,
68     joberrors         integer     not null default 0,
69     jobmissingfiles   integer     not null default 0,
70     poolid            integer,
71     filesetid         integer,
72     purgedfiles       smallint    not null default 0,
73     hasbase           smallint    not null default 0,
74     primary key (jobid)
75 );
76
77 CREATE INDEX job_name_idx on job (name);
78
79 CREATE TABLE MAC (
80     JobId             serial      not null,
81     OriginalJobId     serial      not null,
82     JobType           char(1)     not null,
83     JobLevel          char(1)     not null,
84     schedtime         timestamp   without time zone not null,
85     starttime         timestamp   without time zone,
86     endtime           timestamp   without time zone,
87     jobtdate          bigint      not null,
88     primary key (jobid)
89 );
90
91
92 CREATE TABLE fileset
93 (
94     filesetid         serial      not null,
95     fileset           text        not null,
96     md5               text        not null,
97     createtime        timestamp without time zone not null,
98     primary key (filesetid)
99 );
100
101 CREATE INDEX fileset_name_idx on fileset (fileset);
102
103 CREATE TABLE jobmedia
104 (
105     jobmediaid        serial      not null,
106     jobid             integer     not null,
107     mediaid           integer     not null,
108     firstindex        integer     not null default 0,
109     lastindex         integer     not null default 0,
110     startfile         integer     not null default 0,
111     endfile           integer     not null default 0,
112     startblock        bigint      not null default 0,
113     endblock          bigint      not null default 0,
114     volindex          integer     not null default 0,
115     copy              integer     not null default 0,
116     stripe            integer     not null default 0,
117     primary key (jobmediaid)
118 );
119
120 CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
121
122 CREATE TABLE media
123 (
124     mediaid           serial      not null,
125     volumename        text        not null,
126     slot              integer     not null default 0,
127     poolid            integer     not null,
128     mediatype         text        not null,
129     mediatypeid       integer     not null,
130     labeltype         integer     not null default 0,
131     firstwritten      timestamp   without time zone,
132     lastwritten       timestamp   without time zone,
133     labeldate         timestamp   without time zone,
134     voljobs           integer     not null default 0,
135     volfiles          integer     not null default 0,
136     volblocks         integer     not null default 0,
137     volmounts         integer     not null default 0,
138     volbytes          bigint      not null default 0,
139     volparts          integer     not null default 0,
140     volerrors         integer     not null default 0,
141     volwrites         integer     not null default 0,
142     volcapacitybytes  bigint      not null default 0,
143     volstatus         text        not null
144         check (volstatus in ('Full','Archive','Append',
145               'Recycle','Purged','Read-Only','Disabled',
146               'Error','Busy','Used','Cleaning','Scratch')),
147     recycle           smallint    not null default 0,
148     volretention      bigint      not null default 0,
149     voluseduration    bigint      not null default 0,
150     maxvoljobs        integer     not null default 0,
151     maxvolfiles       integer     not null default 0,
152     maxvolbytes       bigint      not null default 0,
153     inchanger         smallint    not null default 0,
154     StorageId         integer              default 0,
155     DeviceId          integer              default 0,
156     mediaaddressing   smallint    not null default 0,
157     volreadtime       bigint      not null default 0,
158     volwritetime      bigint      not null default 0,
159     endfile           integer     not null default 0,
160     endblock          bigint      not null default 0,
161     LocationId        integer              default 0,
162     recyclcount       integer     not null default 0,
163     initialwrite      timestamp   without time zone,
164     scratchpoolid     integer default 0,
165     recyclepoolid     integer default 0,
166     primary key (mediaid)
167 );
168
169 create unique index media_volumename_id on media (volumename);
170
171  
172 CREATE TABLE MediaType (
173    MediaTypeId SERIAL,
174    MediaType TEXT NOT NULL,
175    ReadOnly INTEGER DEFAULT 0,
176    PRIMARY KEY(MediaTypeId)
177    );
178
179 CREATE TABLE Storage (
180    StorageId SERIAL,
181    Name TEXT NOT NULL,
182    AutoChanger INTEGER DEFAULT 0,
183    PRIMARY KEY(StorageId)
184    );
185
186 CREATE TABLE Device (
187    DeviceId SERIAL,
188    Name TEXT NOT NULL,
189    MediaTypeId INTEGER NOT NULL,
190    StorageId INTEGER NOT NULL,
191    DevMounts INTEGER NOT NULL DEFAULT 0,
192    DevReadBytes BIGINT NOT NULL DEFAULT 0,
193    DevWriteBytes BIGINT NOT NULL DEFAULT 0,
194    DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
195    DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
196    DevReadTime BIGINT NOT NULL DEFAULT 0,
197    DevWriteTime BIGINT NOT NULL DEFAULT 0,
198    DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
199    DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
200    CleaningDate TIMESTAMP WITHOUT TIME ZONE,
201    CleaningPeriod BIGINT NOT NULL DEFAULT 0,
202    PRIMARY KEY(DeviceId)
203    );
204
205
206 CREATE TABLE pool
207 (
208     poolid            serial      not null,
209     name              text        not null,
210     numvols           integer     not null default 0,
211     maxvols           integer     not null default 0,
212     useonce           smallint    not null default 0,
213     usecatalog        smallint    not null default 0,
214     acceptanyvolume   smallint    not null default 0,
215     volretention      bigint      not null default 0,
216     voluseduration    bigint      not null default 0,
217     maxvoljobs        integer     not null default 0,
218     maxvolfiles       integer     not null default 0,
219     maxvolbytes       bigint      not null default 0,
220     autoprune         smallint    not null default 0,
221     recycle           smallint    not null default 0,
222     pooltype          text                          
223       check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
224     labeltype         integer     not null default 0,
225     labelformat       text        not null,
226     enabled           smallint    not null default 1,
227     scratchpoolid     integer default 0,
228     recyclepoolid     integer default 0,
229     NextPoolId        integer default 0,
230     MigrationHighBytes BIGINT DEFAULT 0,
231     MigrationLowBytes  BIGINT DEFAULT 0,
232     MigrationTime      BIGINT DEFAULT 0,
233     primary key (poolid)
234 );
235
236 CREATE INDEX pool_name_idx on pool (name);
237
238 CREATE TABLE client
239 (
240     clientid          serial      not null,
241     name              text        not null,
242     uname             text        not null,
243     autoprune         smallint    default 0,
244     fileretention     bigint      not null,
245     jobretention      bigint      not null,
246     primary key (clientid)
247 );
248
249 create unique index client_name_idx on client (name);
250
251
252 CREATE TABLE counters
253 (
254     counter           text        not null,
255     minvalue          integer,
256     maxvalue          integer,
257     currentvalue      integer,
258     wrapcounter       text        not null,
259     primary key (counter)
260 );
261
262
263
264 CREATE TABLE basefiles
265 (
266     baseid            serial                not null,
267     jobid             integer               not null,
268     fileid            integer               not null,
269     fileindex         integer                       ,
270     basejobid         integer                       ,
271     primary key (baseid)
272 );
273
274 CREATE TABLE unsavedfiles
275 (
276     UnsavedId         integer               not null,
277     jobid             integer               not null,
278     pathid            integer               not null,
279     filenameid        integer               not null,
280     primary key (UnsavedId)
281 );
282
283 CREATE TABLE CDImages 
284 (
285    MediaId integer not null,
286    LastBurn timestamp without time zone not null,
287    primary key (MediaId)
288 );
289
290
291 CREATE TABLE version
292 (
293     versionid         integer               not null
294 );
295
296 CREATE TABLE Status (
297    JobStatus CHAR(1) NOT NULL,
298    JobStatusLong TEXT, 
299    PRIMARY KEY (JobStatus)
300    );
301
302 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
303    ('C', 'Created, not yet running');
304 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
305    ('R', 'Running');
306 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
307    ('B', 'Blocked');
308 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
309    ('T', 'Completed successfully');
310 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
311    ('E', 'Terminated with errors');
312 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
313    ('e', 'Non-fatal error');
314 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
315    ('f', 'Fatal error');
316 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
317    ('D', 'Verify found differences');
318 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
319    ('A', 'Canceled by user');
320 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
321    ('F', 'Waiting for Client');
322 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
323    ('S', 'Waiting for Storage daemon');
324 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
325    ('m', 'Waiting for new media');
326 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
327    ('M', 'Waiting for media mount');
328 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
329    ('s', 'Waiting for storage resource');
330 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
331    ('j', 'Waiting for job resource');
332 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
333    ('c', 'Waiting for client resource');
334 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
335    ('d', 'Waiting on maximum jobs');
336 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
337    ('t', 'Waiting on start time');
338 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
339    ('p', 'Waiting on higher priority jobs');
340
341
342 INSERT INTO Version (VersionId) VALUES (9);
343
344 -- Make sure we have appropriate permissions
345
346
347 END-OF-DATA
348 then
349    echo "Creation of Bacula PostgreSQL tables succeeded."
350 else
351    echo "Creation of Bacula PostgreSQL tables failed."
352 fi
353 exit 0