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