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