]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/make_postgresql_tables.in
Backport from BEE
[bacula/bacula] / bacula / src / cats / make_postgresql_tables.in
1 #!/bin/sh
2 #
3 # shell script to create Bacula PostgreSQL tables
4 #
5 # Important note: 
6 #   You won't get any support for performance issue if you changed the default
7 #   schema.
8 #
9 #
10 #  Bacula® - The Network Backup Solution
11 #
12 #  Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
13 #
14 #  The main author of Bacula is Kern Sibbald, with contributions from many
15 #  others, a complete list can be found in the file AUTHORS.
16 #
17 #  You may use this file and others of this release according to the
18 #  license defined in the LICENSE file, which includes the Affero General
19 #  Public License, v3.0 ("AGPLv3") and some additional permissions and
20 #  terms pursuant to its AGPLv3 Section 7.
21 #
22 #  Bacula® is a registered trademark of Kern Sibbald.
23 #
24 bindir=@POSTGRESQL_BINDIR@
25 PATH="$bindir:$PATH"
26 db_name=${db_name:-@db_name@}
27
28 psql -f - -d ${db_name} $* <<END-OF-DATA
29
30 CREATE TABLE Filename
31 (
32     FilenameId        serial      not null,
33     Name              text        not null,
34     primary key (FilenameId)
35 );
36
37 ALTER TABLE Filename ALTER COLUMN Name SET STATISTICS 1000;
38 CREATE UNIQUE INDEX filename_name_idx on Filename (Name);
39
40 CREATE TABLE Path
41 (
42     PathId            serial      not null,
43     Path              text        not null,
44     primary key (PathId)
45 );
46
47 ALTER TABLE Path ALTER COLUMN Path SET STATISTICS 1000;
48 CREATE UNIQUE INDEX path_name_idx on Path (Path);
49
50 -- We strongly recommend to avoid the temptation to add new indexes.
51 -- In general, these will cause very significant performance
52 -- problems in other areas.  A better approch is to carefully check
53 -- that all your memory configuation parameters are
54 -- suitable for the size of your installation.  If you backup
55 -- millions of files, you need to adapt the database memory
56 -- configuration parameters concerning sorting, joining and global
57 -- memory.  By default, sort and join parameters are very small
58 -- (sometimes 8Kb), and having sufficient memory specified by those
59 -- parameters is extremely important to run fast.  
60
61 -- In File table
62 -- FileIndex can be 0 for FT_DELETED files
63 -- FileNameId can link to Filename.Name='' for directories
64 CREATE TABLE File
65 (
66     FileId            bigserial   not null,
67     FileIndex         integer     not null  default 0,
68     JobId             integer     not null,
69     PathId            integer     not null,
70     FilenameId        integer     not null,
71     DeltaSeq          smallint    not null  default 0,
72     MarkId            integer     not null  default 0,
73     LStat             text        not null,
74     Md5               text        not null,
75     primary key (FileId)
76 );
77
78 CREATE INDEX file_jpfid_idx on File (JobId, PathId, FilenameId);
79 CREATE INDEX file_jobid_idx on File (JobId);
80
81 --
82 -- Add this if you have a good number of job
83 -- that run at the same time
84 -- ALTER SEQUENCE file_fileid_seq CACHE 1000;
85
86 --
87 -- Possibly add one or more of the following indexes
88 --  if your Verifies are too slow, but they can slow down
89 --  backups.
90 --
91 -- CREATE INDEX file_pathid_idx on file(pathid);
92 -- CREATE INDEX file_filenameid_idx on file(filenameid);
93
94 CREATE TABLE RestoreObject (
95    RestoreObjectId SERIAL NOT NULL,
96    ObjectName TEXT NOT NULL,
97    RestoreObject BYTEA NOT NULL,
98    PluginName TEXT NOT NULL,
99    ObjectLength INTEGER DEFAULT 0,
100    ObjectFullLength INTEGER DEFAULT 0,
101    ObjectIndex INTEGER DEFAULT 0,
102    ObjectType INTEGER DEFAULT 0,
103    FileIndex INTEGER DEFAULT 0,
104    JobId INTEGER,
105    ObjectCompression INTEGER DEFAULT 0,
106    PRIMARY KEY(RestoreObjectId)
107    );
108 CREATE INDEX restore_jobid_idx on RestoreObject(JobId);
109
110
111 CREATE TABLE Job
112 (
113     JobId             serial      not null,
114     Job               text        not null,
115     Name              text        not null,
116     Type              char(1)     not null,
117     Level             char(1)     not null,
118     ClientId          integer     default 0,
119     JobStatus         char(1)     not null,
120     SchedTime         timestamp   without time zone,
121     StartTime         timestamp   without time zone,
122     EndTime           timestamp   without time zone,
123     RealEndTime       timestamp   without time zone,
124     JobTDate          bigint      default 0,
125     VolSessionId      integer     default 0,
126     volSessionTime    integer     default 0,
127     JobFiles          integer     default 0,
128     JobBytes          bigint      default 0,
129     ReadBytes         bigint      default 0,
130     JobErrors         integer     default 0,
131     JobMissingFiles   integer     default 0,
132     PoolId            integer     default 0,
133     FilesetId         integer     default 0,
134     PriorJobid        integer     default 0,
135     PurgedFiles       smallint    default 0,
136     HasBase           smallint    default 0,
137     HasCache          smallint    default 0,
138     Reviewed          smallint    default 0,
139     Comment           text,
140     primary key (jobid)
141 );
142
143 CREATE INDEX job_name_idx on job (name);
144
145 -- Create a table like Job for long term statistics 
146 CREATE TABLE JobHisto (LIKE Job);
147 CREATE INDEX jobhisto_idx ON JobHisto ( StartTime );
148
149
150 CREATE TABLE Location (
151    LocationId         serial      not null,
152    Location           text        not null,
153    Cost               integer     default 0,
154    Enabled            smallint,
155    primary key (LocationId)
156 );
157
158
159 CREATE TABLE fileset
160 (
161     filesetid         serial      not null,
162     fileset           text        not null,
163     md5               text        not null,
164     createtime        timestamp without time zone not null,
165     primary key (filesetid)
166 );
167
168 CREATE INDEX fileset_name_idx on fileset (fileset);
169
170 CREATE TABLE jobmedia
171 (
172     jobmediaid        serial      not null,
173     jobid             integer     not null,
174     mediaid           integer     not null,
175     firstindex        integer     default 0,
176     lastindex         integer     default 0,
177     startfile         integer     default 0,
178     endfile           integer     default 0,
179     startblock        bigint      default 0,
180     endblock          bigint      default 0,
181     volindex          integer     default 0,
182     primary key (jobmediaid)
183 );
184
185 CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
186
187 CREATE TABLE media
188 (
189     mediaid           serial      not null,
190     volumename        text        not null,
191     slot              integer     default 0,
192     poolid            integer     default 0,
193     mediatype         text        not null,
194     mediatypeid       integer     default 0,
195     labeltype         integer     default 0,
196     firstwritten      timestamp   without time zone,
197     lastwritten       timestamp   without time zone,
198     labeldate         timestamp   without time zone,
199     voljobs           integer     default 0,
200     volfiles          integer     default 0,
201     volblocks         integer     default 0,
202     volmounts         integer     default 0,
203     volbytes          bigint      default 0,
204     volparts          integer     default 0,
205     volerrors         integer     default 0,
206     volwrites         integer     default 0,
207     volcapacitybytes  bigint      default 0,
208     volstatus         text        not null
209         check (volstatus in ('Full','Archive','Append',
210               'Recycle','Purged','Read-Only','Disabled',
211               'Error','Busy','Used','Cleaning','Scratch')),
212     enabled           smallint    default 1,
213     recycle           smallint    default 0,
214     ActionOnPurge     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     inchanger         smallint    default 0,
221     StorageId         integer     default 0,
222     DeviceId          integer     default 0,
223     mediaaddressing   smallint    default 0,
224     volreadtime       bigint      default 0,
225     volwritetime      bigint      default 0,
226     endfile           integer     default 0,
227     endblock          bigint      default 0,
228     LocationId        integer     default 0,
229     recyclecount      integer     default 0,
230     initialwrite      timestamp   without time zone,
231     scratchpoolid     integer     default 0,
232     recyclepoolid     integer     default 0,
233     comment           text,
234     primary key (mediaid)
235 );
236
237 CREATE UNIQUE INDEX media_volumename_id ON Media (VolumeName);
238 CREATE INDEX media_poolid_idx ON Media (PoolId);
239
240  
241 CREATE TABLE MediaType (
242    MediaTypeId SERIAL,
243    MediaType TEXT NOT NULL,
244    ReadOnly INTEGER DEFAULT 0,
245    PRIMARY KEY(MediaTypeId)
246    );
247
248 CREATE TABLE Storage (
249    StorageId SERIAL,
250    Name TEXT NOT NULL,
251    AutoChanger INTEGER DEFAULT 0,
252    PRIMARY KEY(StorageId)
253    );
254
255 CREATE TABLE Device (
256    DeviceId SERIAL,
257    Name TEXT NOT NULL,
258    MediaTypeId INTEGER NOT NULL,
259    StorageId INTEGER NOT NULL,
260    DevMounts INTEGER NOT NULL DEFAULT 0,
261    DevReadBytes BIGINT NOT NULL DEFAULT 0,
262    DevWriteBytes BIGINT NOT NULL DEFAULT 0,
263    DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
264    DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
265    DevReadTime BIGINT NOT NULL DEFAULT 0,
266    DevWriteTime BIGINT NOT NULL DEFAULT 0,
267    DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
268    DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
269    CleaningDate timestamp without time zone,
270    CleaningPeriod BIGINT NOT NULL DEFAULT 0,
271    PRIMARY KEY(DeviceId)
272    );
273
274
275 CREATE TABLE pool
276 (
277     poolid            serial      not null,
278     name              text        not null,
279     numvols           integer     default 0,
280     maxvols           integer     default 0,
281     useonce           smallint    default 0,
282     usecatalog        smallint    default 0,
283     acceptanyvolume   smallint    default 0,
284     volretention      bigint      default 0,
285     voluseduration    bigint      default 0,
286     maxvoljobs        integer     default 0,
287     maxvolfiles       integer     default 0,
288     maxvolbytes       bigint      default 0,
289     autoprune         smallint    default 0,
290     recycle           smallint    default 0,
291     ActionOnPurge     smallint    default 0,
292     pooltype          text                          
293       check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
294     labeltype         integer     default 0,
295     labelformat       text        not null,
296     enabled           smallint    default 1,
297     scratchpoolid     integer     default 0,
298     recyclepoolid     integer     default 0,
299     NextPoolId        integer     default 0,
300     MigrationHighBytes BIGINT     DEFAULT 0,
301     MigrationLowBytes  BIGINT     DEFAULT 0,
302     MigrationTime      BIGINT     DEFAULT 0,
303     primary key (poolid)
304 );
305
306 CREATE INDEX pool_name_idx on pool (name);
307
308 CREATE TABLE client
309 (
310     clientid          serial      not null,
311     name              text        not null,
312     uname             text        not null,
313     autoprune         smallint    default 0,
314     fileretention     bigint      default 0,
315     jobretention      bigint      default 0,
316     primary key (clientid)
317 );
318
319 create unique index client_name_idx on client (name);
320
321 CREATE TABLE Log
322 (
323     LogId             serial      not null,
324     JobId             integer     not null,
325     Time              timestamp   without time zone,
326     LogText           text        not null,
327     primary key (LogId)
328 );
329 create index log_name_idx on Log (JobId);
330
331 CREATE TABLE LocationLog (
332    LocLogId SERIAL NOT NULL,
333    Date timestamp   without time zone,
334    Comment TEXT NOT NULL,
335    MediaId INTEGER DEFAULT 0,
336    LocationId INTEGER DEFAULT 0,
337    newvolstatus text not null
338         check (newvolstatus in ('Full','Archive','Append',
339               'Recycle','Purged','Read-Only','Disabled',
340               'Error','Busy','Used','Cleaning','Scratch')),
341    newenabled smallint,
342    PRIMARY KEY(LocLogId)
343 );
344
345
346
347 CREATE TABLE counters
348 (
349     counter           text        not null,
350     minvalue          integer     default 0,
351     maxvalue          integer     default 0,
352     currentvalue      integer     default 0,
353     wrapcounter       text        not null,
354     primary key (counter)
355 );
356
357
358
359 CREATE TABLE basefiles
360 (
361     baseid            serial                not null,
362     jobid             integer               not null,
363     fileid            bigint                not null,
364     fileindex         integer                       ,
365     basejobid         integer                       ,
366     primary key (baseid)
367 );
368
369 CREATE INDEX basefiles_jobid_idx ON BaseFiles ( JobId );
370
371 CREATE TABLE unsavedfiles
372 (
373     UnsavedId         integer               not null,
374     jobid             integer               not null,
375     pathid            integer               not null,
376     filenameid        integer               not null,
377     primary key (UnsavedId)
378 );
379
380 CREATE TABLE CDImages 
381 (
382    MediaId integer not null,
383    LastBurn timestamp without time zone not null,
384    primary key (MediaId)
385 );
386
387
388 CREATE TABLE PathHierarchy
389 (
390      PathId integer NOT NULL,
391      PPathId integer NOT NULL,
392      CONSTRAINT pathhierarchy_pkey PRIMARY KEY (PathId)
393 );
394
395 CREATE INDEX pathhierarchy_ppathid 
396           ON PathHierarchy (PPathId);
397
398 CREATE TABLE PathVisibility
399 (
400       PathId integer NOT NULL,
401       JobId integer NOT NULL,
402       Size int8 DEFAULT 0,
403       Files int4 DEFAULT 0,
404       CONSTRAINT pathvisibility_pkey PRIMARY KEY (JobId, PathId)
405 );
406 CREATE INDEX pathvisibility_jobid
407              ON PathVisibility (JobId);
408
409 CREATE TABLE version
410 (
411     versionid         integer               not null
412 );
413
414 CREATE TABLE Status (
415    JobStatus CHAR(1) NOT NULL,
416    JobStatusLong TEXT,
417    Severity int,
418    PRIMARY KEY (JobStatus)
419    );
420
421 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
422    ('C', 'Created, not yet running',15);
423 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
424    ('R', 'Running',15);
425 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
426    ('B', 'Blocked',15);
427 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
428    ('T', 'Completed successfully', 10);
429 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
430    ('E', 'Terminated with errors', 25);
431 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
432    ('e', 'Non-fatal error',20);
433 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
434    ('f', 'Fatal error',100);
435 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
436    ('D', 'Verify found differences',15);
437 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
438    ('A', 'Canceled by user',90);
439 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
440    ('F', 'Waiting for Client',15);
441 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
442    ('S', 'Waiting for Storage daemon',15);
443 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
444    ('m', 'Waiting for new media');
445 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
446    ('M', 'Waiting for media mount',15);
447 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
448    ('s', 'Waiting for storage resource',15);
449 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
450    ('j', 'Waiting for job resource',15);
451 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
452    ('c', 'Waiting for client resource',15);
453 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
454    ('d', 'Waiting on maximum jobs',15);
455 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
456    ('t', 'Waiting on start time',15);
457 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
458    ('p', 'Waiting on higher priority jobs',15);
459 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
460    ('a', 'SD despooling attributes',15);
461 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
462    ('i', 'Doing batch insert file records',15);
463
464 INSERT INTO Version (VersionId) VALUES (@BDB_VERSION@);
465
466 -- Make sure we have appropriate permissions
467
468
469 END-OF-DATA
470 pstat=$?
471 if test $pstat = 0; 
472 then
473    echo "Creation of Bacula PostgreSQL tables succeeded."
474 else
475    echo "Creation of Bacula PostgreSQL tables failed."
476 fi
477 exit $pstat