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