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