]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/make_postgresql_tables.in
kes Make database name and user configurable
[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@
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 CREATE INDEX filename_name_idx on filename (name);
18
19 CREATE TABLE path
20 (
21     pathid            serial      not null,
22     path              text        not null,
23     primary key (pathid)
24 );
25
26 CREATE INDEX path_name_idx on path (path);
27
28 CREATE TABLE file
29 (
30     fileid            serial      not null,
31     fileindex         integer     not null  default 0,
32     jobid             integer     not null,
33     pathid            integer     not null,
34     filenameid        integer     not null,
35     markid            integer     not null  default 0,
36     lstat             text        not null,
37     md5               text        not null,
38     primary key (fileid)
39 );
40
41 CREATE INDEX file_jobid_idx on file (jobid);
42 CREATE INDEX file_fp_idx on file (filenameid, pathid);
43
44 --
45 -- Possibly add one or more of the following indexes
46 --  if your Verifies are too slow.
47 --
48 -- CREATE INDEX file_pathid_idx on file(pathid);
49 -- CREATE INDEX file_filenameid_idx on file(filenameid);
50 -- CREATE INDEX file_jpfid_idx on file (jobid, pathid, filenameid);
51
52 CREATE TABLE job
53 (
54     jobid             serial      not null,
55     job               text        not null,
56     name              text        not null,
57     type              char(1)     not null,
58     level             char(1)     not null,
59     clientid          integer     default 0,
60     jobstatus         char(1)     not null,
61     schedtime         timestamp   without time zone,
62     starttime         timestamp   without time zone,
63     endtime           timestamp   without time zone,
64     realendtime       timestamp   without time zone,
65     jobtdate          bigint      default 0,
66     volsessionid      integer     default 0,
67     volsessiontime    integer     default 0,
68     jobfiles          integer     default 0,
69     jobbytes          bigint      default 0,
70     joberrors         integer     default 0,
71     jobmissingfiles   integer     default 0,
72     poolid            integer     default 0,
73     filesetid         integer     default 0,
74     purgedfiles       smallint    default 0,
75     hasbase           smallint    default 0,
76     priorjobid        integer     default 0,
77     primary key (jobid)
78 );
79
80 CREATE INDEX job_name_idx on job (name);
81
82 CREATE TABLE Location (
83    LocationId         serial      not null,
84    Location           text        not null,
85    Cost               integer     default 0,
86    Enabled            smallint,
87    primary key (LocationId)
88 );
89
90
91 CREATE TABLE fileset
92 (
93     filesetid         serial      not null,
94     fileset           text        not null,
95     md5               text        not null,
96     createtime        timestamp without time zone not null,
97     primary key (filesetid)
98 );
99
100 CREATE INDEX fileset_name_idx on fileset (fileset);
101
102 CREATE TABLE jobmedia
103 (
104     jobmediaid        serial      not null,
105     jobid             integer     not null,
106     mediaid           integer     not null,
107     firstindex        integer     default 0,
108     lastindex         integer     default 0,
109     startfile         integer     default 0,
110     endfile           integer     default 0,
111     startblock        bigint      default 0,
112     endblock          bigint      default 0,
113     volindex          integer     default 0,
114     copy              integer     default 0,
115     primary key (jobmediaid)
116 );
117
118 CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
119
120 CREATE TABLE media
121 (
122     mediaid           serial      not null,
123     volumename        text        not null,
124     slot              integer     default 0,
125     poolid            integer     default 0,
126     mediatype         text        not null,
127     mediatypeid       integer     default 0,
128     labeltype         integer     default 0,
129     firstwritten      timestamp   without time zone,
130     lastwritten       timestamp   without time zone,
131     labeldate         timestamp   without time zone,
132     voljobs           integer     default 0,
133     volfiles          integer     default 0,
134     volblocks         integer     default 0,
135     volmounts         integer     default 0,
136     volbytes          bigint      default 0,
137     volparts          integer     default 0,
138     volerrors         integer     default 0,
139     volwrites         integer     default 0,
140     volcapacitybytes  bigint      default 0,
141     volstatus         text        not null
142         check (volstatus in ('Full','Archive','Append',
143               'Recycle','Purged','Read-Only','Disabled',
144               'Error','Busy','Used','Cleaning','Scratch')),
145     enabled           smallint    default 1,
146     recycle           smallint    default 0,
147     volretention      bigint      default 0,
148     voluseduration    bigint      default 0,
149     maxvoljobs        integer     default 0,
150     maxvolfiles       integer     default 0,
151     maxvolbytes       bigint      default 0,
152     inchanger         smallint    default 0,
153     StorageId         integer     default 0,
154     DeviceId          integer     default 0,
155     mediaaddressing   smallint    default 0,
156     volreadtime       bigint      default 0,
157     volwritetime      bigint      default 0,
158     endfile           integer     default 0,
159     endblock          bigint      default 0,
160     LocationId        integer     default 0,
161     recyclecount      integer     default 0,
162     initialwrite      timestamp   without time zone,
163     scratchpoolid     integer     default 0,
164     recyclepoolid     integer     default 0,
165     comment           text,
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     default 0,
211     maxvols           integer     default 0,
212     useonce           smallint    default 0,
213     usecatalog        smallint    default 0,
214     acceptanyvolume   smallint    default 0,
215     volretention      bigint      default 0,
216     voluseduration    bigint      default 0,
217     maxvoljobs        integer     default 0,
218     maxvolfiles       integer     default 0,
219     maxvolbytes       bigint      default 0,
220     autoprune         smallint    default 0,
221     recycle           smallint    default 0,
222     pooltype          text                          
223       check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
224     labeltype         integer     default 0,
225     labelformat       text        not null,
226     enabled           smallint    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      default 0,
245     jobretention      bigint      default 0,
246     primary key (clientid)
247 );
248
249 create unique index client_name_idx on client (name);
250
251 CREATE TABLE Log
252 (
253     LogId             serial      not null,
254     JobId             integer     not null,
255     Time              timestamp   without time zone,
256     LogText           text        not null,
257     primary key (LogId)
258 );
259 create index log_name_idx on Log (JobId);
260
261 CREATE TABLE LocationLog (
262    LocLogId SERIAL NOT NULL,
263    Date timestamp   without time zone,
264    Comment TEXT NOT NULL,
265    MediaId INTEGER DEFAULT 0,
266    LocationId INTEGER DEFAULT 0,
267    newvolstatus text not null
268         check (newvolstatus in ('Full','Archive','Append',
269               'Recycle','Purged','Read-Only','Disabled',
270               'Error','Busy','Used','Cleaning','Scratch')),
271    newenabled smallint,
272    PRIMARY KEY(LocLogId)
273 );
274
275
276
277 CREATE TABLE counters
278 (
279     counter           text        not null,
280     minvalue          integer     default 0,
281     maxvalue          integer     default 0,
282     currentvalue      integer     default 0,
283     wrapcounter       text        not null,
284     primary key (counter)
285 );
286
287
288
289 CREATE TABLE basefiles
290 (
291     baseid            serial                not null,
292     jobid             integer               not null,
293     fileid            integer               not null,
294     fileindex         integer                       ,
295     basejobid         integer                       ,
296     primary key (baseid)
297 );
298
299 CREATE TABLE unsavedfiles
300 (
301     UnsavedId         integer               not null,
302     jobid             integer               not null,
303     pathid            integer               not null,
304     filenameid        integer               not null,
305     primary key (UnsavedId)
306 );
307
308 CREATE TABLE CDImages 
309 (
310    MediaId integer not null,
311    LastBurn timestamp without time zone not null,
312    primary key (MediaId)
313 );
314
315
316 CREATE TABLE version
317 (
318     versionid         integer               not null
319 );
320
321 CREATE TABLE Status (
322    JobStatus CHAR(1) NOT NULL,
323    JobStatusLong TEXT, 
324    PRIMARY KEY (JobStatus)
325    );
326
327 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
328    ('C', 'Created, not yet running');
329 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
330    ('R', 'Running');
331 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
332    ('B', 'Blocked');
333 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
334    ('T', 'Completed successfully');
335 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
336    ('E', 'Terminated with errors');
337 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
338    ('e', 'Non-fatal error');
339 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
340    ('f', 'Fatal error');
341 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
342    ('D', 'Verify found differences');
343 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
344    ('A', 'Canceled by user');
345 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
346    ('F', 'Waiting for Client');
347 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
348    ('S', 'Waiting for Storage daemon');
349 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
350    ('m', 'Waiting for new media');
351 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
352    ('M', 'Waiting for media mount');
353 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
354    ('s', 'Waiting for storage resource');
355 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
356    ('j', 'Waiting for job resource');
357 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
358    ('c', 'Waiting for client resource');
359 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
360    ('d', 'Waiting on maximum jobs');
361 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
362    ('t', 'Waiting on start time');
363 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
364    ('p', 'Waiting on higher priority jobs');
365
366
367 INSERT INTO Version (VersionId) VALUES (10);
368
369 -- Make sure we have appropriate permissions
370
371
372 END-OF-DATA
373 pstat=$?
374 if test $pstat = 0; 
375 then
376    echo "Creation of Bacula PostgreSQL tables succeeded."
377 else
378    echo "Creation of Bacula PostgreSQL tables failed."
379 fi
380 exit $pstat