3 # shell script to create Bacula PostgreSQL tables
6 db_name=${db_name:-@db_name@}
8 $bindir/psql -f - -d ${db_name} $* <<END-OF-DATA
12 filenameid serial not null,
14 primary key (filenameid)
17 ALTER TABLE filename ALTER COLUMN name SET STATISTICS 1000;
18 CREATE UNIQUE INDEX filename_name_idx on filename (name);
22 pathid serial not null,
27 ALTER TABLE path ALTER COLUMN path SET STATISTICS 1000;
28 CREATE UNIQUE INDEX path_name_idx on path (path);
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,
43 CREATE INDEX file_jpfid_idx on file (jobid, pathid, filenameid);
45 -- If you need performances, you can remove this index
46 -- the database engine is able to use the composite index
47 -- to find all records with a given JobId
48 CREATE INDEX file_jobid_idx on file(jobid);
51 -- Add this if you have a good number of job
52 -- that run at the same time
53 -- ALTER SEQUENCE file_fileid_seq CACHE 1000;
56 -- Possibly add one or more of the following indexes
57 -- if your Verifies are too slow.
59 -- CREATE INDEX file_pathid_idx on file(pathid);
60 -- CREATE INDEX file_filenameid_idx on file(filenameid);
64 jobid serial not null,
67 type char(1) not null,
68 level char(1) not null,
69 clientid integer default 0,
70 jobstatus char(1) not null,
71 schedtime timestamp without time zone,
72 starttime timestamp without time zone,
73 endtime timestamp without time zone,
74 realendtime timestamp without time zone,
75 jobtdate bigint default 0,
76 volsessionid integer default 0,
77 volsessiontime integer default 0,
78 jobfiles integer default 0,
79 jobbytes bigint default 0,
80 readbytes bigint default 0,
81 joberrors integer default 0,
82 jobmissingfiles integer default 0,
83 poolid integer default 0,
84 filesetid integer default 0,
85 purgedfiles smallint default 0,
86 hasbase smallint default 0,
87 priorjobid integer default 0,
91 CREATE INDEX job_name_idx on job (name);
93 -- Create a table like Job for long term statistics
94 CREATE TABLE JobHisto (LIKE Job);
95 CREATE INDEX jobhisto_idx ON jobhisto ( starttime );
98 CREATE TABLE Location (
99 LocationId serial not null,
100 Location text not null,
101 Cost integer default 0,
103 primary key (LocationId)
109 filesetid serial not null,
110 fileset text not null,
112 createtime timestamp without time zone not null,
113 primary key (filesetid)
116 CREATE INDEX fileset_name_idx on fileset (fileset);
118 CREATE TABLE jobmedia
120 jobmediaid serial not null,
121 jobid integer not null,
122 mediaid integer not null,
123 firstindex integer default 0,
124 lastindex integer default 0,
125 startfile integer default 0,
126 endfile integer default 0,
127 startblock bigint default 0,
128 endblock bigint default 0,
129 volindex integer default 0,
130 copy integer default 0,
131 primary key (jobmediaid)
134 CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
138 mediaid serial not null,
139 volumename text not null,
140 slot integer default 0,
141 poolid integer default 0,
142 mediatype text not null,
143 mediatypeid integer default 0,
144 labeltype integer default 0,
145 firstwritten timestamp without time zone,
146 lastwritten timestamp without time zone,
147 labeldate timestamp without time zone,
148 voljobs integer default 0,
149 volfiles integer default 0,
150 volblocks integer default 0,
151 volmounts integer default 0,
152 volbytes bigint default 0,
153 volparts integer default 0,
154 volerrors integer default 0,
155 volwrites integer default 0,
156 volcapacitybytes bigint default 0,
157 volstatus text not null
158 check (volstatus in ('Full','Archive','Append',
159 'Recycle','Purged','Read-Only','Disabled',
160 'Error','Busy','Used','Cleaning','Scratch')),
161 enabled smallint default 1,
162 recycle smallint default 0,
163 ActionOnPurge smallint default 0,
164 volretention bigint default 0,
165 voluseduration bigint default 0,
166 maxvoljobs integer default 0,
167 maxvolfiles integer default 0,
168 maxvolbytes bigint default 0,
169 inchanger smallint default 0,
170 StorageId integer default 0,
171 DeviceId integer default 0,
172 mediaaddressing smallint default 0,
173 volreadtime bigint default 0,
174 volwritetime bigint default 0,
175 endfile integer default 0,
176 endblock bigint default 0,
177 LocationId integer default 0,
178 recyclecount integer default 0,
179 initialwrite timestamp without time zone,
180 scratchpoolid integer default 0,
181 recyclepoolid integer default 0,
183 primary key (mediaid)
186 create unique index media_volumename_id on media (volumename);
189 CREATE TABLE MediaType (
191 MediaType TEXT NOT NULL,
192 ReadOnly INTEGER DEFAULT 0,
193 PRIMARY KEY(MediaTypeId)
196 CREATE TABLE Storage (
199 AutoChanger INTEGER DEFAULT 0,
200 PRIMARY KEY(StorageId)
203 CREATE TABLE Device (
206 MediaTypeId INTEGER NOT NULL,
207 StorageId INTEGER NOT NULL,
208 DevMounts INTEGER NOT NULL DEFAULT 0,
209 DevReadBytes BIGINT NOT NULL DEFAULT 0,
210 DevWriteBytes BIGINT NOT NULL DEFAULT 0,
211 DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
212 DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
213 DevReadTime BIGINT NOT NULL DEFAULT 0,
214 DevWriteTime BIGINT NOT NULL DEFAULT 0,
215 DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
216 DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
217 CleaningDate timestamp without time zone,
218 CleaningPeriod BIGINT NOT NULL DEFAULT 0,
219 PRIMARY KEY(DeviceId)
225 poolid serial not null,
227 numvols integer default 0,
228 maxvols integer default 0,
229 useonce smallint default 0,
230 usecatalog smallint default 0,
231 acceptanyvolume smallint default 0,
232 volretention bigint default 0,
233 voluseduration bigint default 0,
234 maxvoljobs integer default 0,
235 maxvolfiles integer default 0,
236 maxvolbytes bigint default 0,
237 autoprune smallint default 0,
238 recycle smallint default 0,
239 ActionOnPurge smallint default 0,
241 check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
242 labeltype integer default 0,
243 labelformat text not null,
244 enabled smallint default 1,
245 scratchpoolid integer default 0,
246 recyclepoolid integer default 0,
247 NextPoolId integer default 0,
248 MigrationHighBytes BIGINT DEFAULT 0,
249 MigrationLowBytes BIGINT DEFAULT 0,
250 MigrationTime BIGINT DEFAULT 0,
254 CREATE INDEX pool_name_idx on pool (name);
258 clientid serial not null,
261 autoprune smallint default 0,
262 fileretention bigint default 0,
263 jobretention bigint default 0,
264 primary key (clientid)
267 create unique index client_name_idx on client (name);
271 LogId serial not null,
272 JobId integer not null,
273 Time timestamp without time zone,
274 LogText text not null,
277 create index log_name_idx on Log (JobId);
279 CREATE TABLE LocationLog (
280 LocLogId SERIAL NOT NULL,
281 Date timestamp without time zone,
282 Comment TEXT NOT NULL,
283 MediaId INTEGER DEFAULT 0,
284 LocationId INTEGER DEFAULT 0,
285 newvolstatus text not null
286 check (newvolstatus in ('Full','Archive','Append',
287 'Recycle','Purged','Read-Only','Disabled',
288 'Error','Busy','Used','Cleaning','Scratch')),
290 PRIMARY KEY(LocLogId)
295 CREATE TABLE counters
297 counter text not null,
298 minvalue integer default 0,
299 maxvalue integer default 0,
300 currentvalue integer default 0,
301 wrapcounter text not null,
302 primary key (counter)
307 CREATE TABLE basefiles
309 baseid serial not null,
310 jobid integer not null,
311 fileid bigint not null,
317 CREATE TABLE unsavedfiles
319 UnsavedId integer not null,
320 jobid integer not null,
321 pathid integer not null,
322 filenameid integer not null,
323 primary key (UnsavedId)
326 CREATE TABLE CDImages
328 MediaId integer not null,
329 LastBurn timestamp without time zone not null,
330 primary key (MediaId)
336 versionid integer not null
339 CREATE TABLE Status (
340 JobStatus CHAR(1) NOT NULL,
342 PRIMARY KEY (JobStatus)
345 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
346 ('C', 'Created, not yet running');
347 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
349 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
351 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
352 ('T', 'Completed successfully');
353 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
354 ('E', 'Terminated with errors');
355 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
356 ('e', 'Non-fatal error');
357 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
358 ('f', 'Fatal error');
359 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
360 ('D', 'Verify found differences');
361 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
362 ('A', 'Canceled by user');
363 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
364 ('F', 'Waiting for Client');
365 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
366 ('S', 'Waiting for Storage daemon');
367 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
368 ('m', 'Waiting for new media');
369 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
370 ('M', 'Waiting for media mount');
371 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
372 ('s', 'Waiting for storage resource');
373 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
374 ('j', 'Waiting for job resource');
375 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
376 ('c', 'Waiting for client resource');
377 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
378 ('d', 'Waiting on maximum jobs');
379 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
380 ('t', 'Waiting on start time');
381 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
382 ('p', 'Waiting on higher priority jobs');
383 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
384 ('a', 'SD despooling attributes');
385 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
386 ('i', 'Doing batch insert file records');
388 INSERT INTO Version (VersionId) VALUES (11);
390 -- Make sure we have appropriate permissions
397 echo "Creation of Bacula PostgreSQL tables succeeded."
399 echo "Creation of Bacula PostgreSQL tables failed."