]> git.sur5r.net Git - bacula/docs/blob - docs/manual/postgresql.tex
updates
[bacula/docs] / docs / manual / postgresql.tex
1 %%
2 %%
3
4 \chapter{Installing and Configuring PostgreSQL}
5 \label{PostgreSqlChapter}
6 \index[general]{PostgreSQL!Installing and Configuring }
7 \index[general]{Installing and Configuring PostgreSQL }
8 \index[general]{Upgrading}
9
10 Warning!!! If you are considering using PostreSQL, you should be aware
11 of their philosophy of upgrades, which could be 
12 destabilizing for a production shop.  Basically at every major version
13 upgrade, you are required to dump your database in an ASCII format, 
14 do the upgrade, and then reload your database (or databases). This is
15 because they frequently update the "data format" from version to   
16 version, and they supply no tools to automatically do the conversion.
17 If you forget to do the ASCII dump, your database may become totally
18 useless because none of the new tools can access it due to the format
19 change, and the PostgreSQL server will not be able to start.
20
21 \section{Installing PostgreSQL}
22 \index[general]{PostgreSQL!Installing }
23
24 If you use the {\bf ./configure \verb:--:with-postgresql=PostgreSQL-Directory}
25 statement for configuring {\bf Bacula}, you will need PostgreSQL version 7.3
26 or later installed. NOTE! PostgreSQL versions earlier than 7.3 do not work
27 with Bacula. If PostgreSQL is installed in the standard system location, you
28 need only enter {\bf \verb:--:with-postgresql} since the configure program will
29 search all the standard locations. If you install PostgreSQL in your home
30 directory or some other non-standard directory, you will need to provide the
31 full path with the {\bf \verb:--:with-postgresql} option. 
32
33 Installing and configuring PostgreSQL is not difficult but can be confusing
34 the first time. If you prefer, you may want to use a package provided by your
35 chosen operating system. Binary packages are available on most PostgreSQL
36 mirrors. 
37
38 If you prefer to install from source, we recommend following the instructions
39 found in the 
40 \elink{PostgreSQL documentation}{http://www.postgresql.org/docs/}. 
41
42 If you are using FreeBSD, 
43 \elink{this FreeBSD Diary article}{http://www.freebsddiary.org/postgresql.php}
44 will be useful. Even if you are not using FreeBSD, the article will contain
45 useful configuration and setup information. 
46
47 After installing PostgreSQL, you should return to completing the installation
48 of {\bf Bacula}. Later, after Bacula is installed, come back to this chapter
49 to complete the installation. Please note, the installation files used in the
50 second phase of the PostgreSQL installation are created during the Bacula
51 Installation. You must still come back to complete the second phase of the 
52 PostgreSQL installation even if you installed binaries (e.g. rpm, deb,
53 ...).
54
55
56 \label{PostgreSQL_configure}
57 \section{Configuring PostgreSQL}
58 \index[general]{PostgreSQL!Configuring PostgreSQL -- }
59
60 At this point, you should have built and installed PostgreSQL, or already have
61 a running PostgreSQL, and you should have configured, built and installed {\bf
62 Bacula}. If not, please complete these items before proceeding. 
63
64 Please note that the {\bf ./configure} used to build {\bf Bacula} will need to
65 include {\bf \verb:--:with-postgresql=PostgreSQL-directory}, where {\bf
66 PostgreSQL-directory} is the directory name that you specified on the
67 ./configure command for configuring PostgreSQL (if you didn't specify a
68 directory or PostgreSQL is installed in a default location, you do not need to
69 specify the directory). This is needed so that Bacula can find the necessary
70 include headers and library files for interfacing to PostgreSQL. 
71
72 {\bf Bacula} will install scripts for manipulating the database (create,
73 delete, make tables etc) into the main installation directory. These files
74 will be of the form *\_bacula\_* (e.g. create\_bacula\_database). These files
75 are also available in the \lt{}bacula-src\gt{}/src/cats directory after
76 running ./configure. If you inspect create\_bacula\_database, you will see
77 that it calls create\_postgresql\_database. The *\_bacula\_* files are
78 provided for convenience. It doesn't matter what database you have chosen;
79 create\_bacula\_database will always create your database. 
80
81 Now you will create the Bacula PostgreSQL database and the tables that Bacula
82 uses. These instructions assume that you already have PostgreSQL running. You
83 will need to perform these steps as a user that is able to create new
84 databases. This can be the PostgreSQL user (on most systems, this is the pgsql
85 user). 
86
87 \begin{enumerate}
88 \item cd \lt{}install-directory\gt{}
89
90    This directory contains the Bacula catalog  interface routines.  
91
92 \item ./create\_bacula\_database
93
94    This script creates the PostgreSQL {\bf bacula} database.  
95    Before running this command, you should carefully think about
96    what encoding sequence you want for the text fields (paths, files, ...).
97    Ideally, the encoding should be set to UTF8. However, many Unix systems
98    have filenames that are not encoded in UTF8, either because you have
99    not set UTF8 as your default character set or because you have imported
100    files from elsewhere (e.g. MacOS X).  For this reason, Bacula uses
101    SQL\_ASCII as the default encoding.  If you want to change this,
102    please modify the script before running it.
103
104    If running the script fails, it is probably because the database is
105    owned by a user other than yourself.  On many systems, the database
106    owner is {\bf pgsql} and on others such as Red Hat and Fedora it is {\bf
107    postgres}.  You can find out which it is by examining your /etc/passwd
108    file.  To create a new user under either your name or with say the name
109    {\bf bacula}, you can do the following:
110
111 \begin{verbatim}
112    su
113    (enter root password)
114    su pgsql (or postgres)
115    createuser kern (or perhaps bacula)
116    Shall the new user be allowed to create databases? (y/n) y
117    Shall the new user be allowed to create more new users? (y/n) (choose
118          what you want)
119    exit
120 \end{verbatim}
121     
122     At this point, you should be able to execute the
123     ./create\_bacula\_database command.
124
125 \item ./make\_bacula\_tables
126
127    This script creates the PostgreSQL tables used by {\bf Bacula}.  
128 \item ./grant\_bacula\_privileges
129
130    This script creates the database user {\bf bacula}  with restricted access
131 rights. You may  want to modify it to suit your situation. Please note that 
132 this database is not password protected.  
133
134 \end{enumerate}
135
136 Each of the three scripts (create\_bacula\_database, make\_bacula\_tables, and
137 grant\_bacula\_privileges) allows the addition of a command line argument.
138 This can be useful for specifying the user name. For example, you might need
139 to add {\bf -h hostname} to the command line to specify a remote database
140 server. 
141
142 To take a closer look at the access privileges that you have setup with the
143 above, you can do: 
144
145 \footnotesize
146 \begin{verbatim}
147 PostgreSQL-directory/bin/psql --command \\dp bacula
148 \end{verbatim}
149 \normalsize
150
151 Also, I had an authorization problem with the password. In the end,
152 I had to modify my {\bf pg\_hba.conf} file (in /var/lib/pgsql/data on my machine)
153 from:
154
155 \footnotesize
156 \begin{verbatim}
157   local   all    all        ident  sameuser
158 to
159   local   all    all        trust  sameuser
160 \end{verbatim}
161 \normalsize
162
163 This solved the problem for me, but it is not always a good thing
164 to do from a security standpoint.  However, it allowed me to run
165 my regression scripts without having a password.
166
167 A more secure way to perform database authentication is with md5
168 password hashes.  Begin by editing the {\bf pg\_hba.conf} file, and
169 just prior the the existing ``local'' and ``host'' lines, add the line:
170
171 \footnotesize
172 \begin{verbatim}
173   local bacula bacula md5
174 \end{verbatim}
175 \normalsize
176
177 and restart the Postgres database server (frequently, this can be done
178 using "/etc/init.d/postgresql restart") to put this new authentication
179 rule into effect.
180
181 Next, become the Postgres administrator, postgres, either by logging
182 on as the postgres user, or by using su to become root and then using
183 su - postgres to become postgres.  Add a password to the bacula
184 database for the bacula user using:
185
186 \footnotesize
187 \begin{verbatim}
188   \$ psql bacula
189   bacula=# alter user bacula with password 'secret';
190   ALTER USER
191   bacula=# \\q
192 \end{verbatim}
193 \normalsize
194
195 Next, you'll have to add this password to two locations in the
196 bacula-dir.conf file: once to the Catalog resource and once to the
197 RunBeforeJob entry in the BackupCatalog Job resource.  With the
198 password in place, these two lines should look something like:
199
200 \footnotesize
201 \begin{verbatim}
202   dbname = bacula; user = bacula; password = "secret"
203     ... and ...
204   RunBeforeJob = "/etc/make_catalog_backup bacula bacula secret"
205 \end{verbatim}
206 \normalsize
207
208 Naturally, you should choose your own significantly more random
209 password, and ensure that the bacula-dir.conf file containing this
210 password is readable only by the root.
211
212 Even with the files containing the database password properly
213 restricted, there is still a security problem with this approach: on
214 some platforms, the environment variable that is used to supply the
215 password to Postgres is available to all users of the
216 local system.  To eliminate this problem, the Postgres team have
217 deprecated the use of the environment variable password-passing
218 mechanism and recommend the use of a .pgpass file instead.  To use
219 this mechanism, create a file named .pgpass containing the single
220 line:
221
222 \footnotesize
223 \begin{verbatim}
224   localhost:5432:bacula:bacula:secret
225 \end{verbatim}
226 \normalsize
227
228 This file should be copied into the home directory of all accounts
229 that will need to gain access to the database: typically, root,
230 bacula, and any users who will make use of any of the console
231 programs.  The files must then have the owner and group set to match
232 the user (so root:root for the copy in ~root, and so on), and the mode
233 set to 600, limiting access to the owner of the file.
234
235 \section{Re-initializing the Catalog Database}
236 \index[general]{Database!Re-initializing the Catalog }
237 \index[general]{Re-initializing the Catalog Database }
238
239 After you have done some initial testing with {\bf Bacula}, you will probably
240 want to re-initialize the catalog database and throw away all the test Jobs
241 that you ran. To do so, you can do the following: 
242
243 \footnotesize
244 \begin{verbatim}
245   cd <install-directory>
246   ./drop_bacula_tables
247   ./make_bacula_tables
248   ./grant_bacula_privileges
249 \end{verbatim}
250 \normalsize
251
252 Please note that all information in the database will be lost and you will be
253 starting from scratch. If you have written on any Volumes, you must write an
254 end of file mark on the volume so that Bacula can reuse it. Do so with: 
255
256 \footnotesize
257 \begin{verbatim}
258    (stop Bacula or unmount the drive)
259    mt -f /dev/nst0 rewind
260    mt -f /dev/nst0 weof
261 \end{verbatim}
262 \normalsize
263
264 Where you should replace {\bf /dev/nst0} with the appropriate tape drive
265 device name for your machine. 
266
267 \section{Installing PostgreSQL from RPMs}
268 \index[general]{PostgreSQL!Installing from RPMs}
269 \index[general]{Installing PostgreSQL from RPMs}
270 If you are installing PostgreSQL from RPMs, you will need to install
271 both the PostgreSQL binaries and the client libraries.  The client
272 libraries are usually found in a devel package, so you must
273 install:
274
275 \footnotesize
276 \begin{verbatim}
277   postgresql
278   postgresql-devel
279 \end{verbatim}
280 \normalsize
281
282 This will be the same with most other package managers too.  After
283 installing from rpms, you will still need to run the scripts that set up
284 the database and create the tables as described above.
285
286
287 \section{Converting from MySQL to PostgreSQL}
288 \index[general]{PostgreSQL!Converting from MySQL to }
289 \index[general]{Converting from MySQL to PostgreSQL }
290
291 The conversion procedure presented here was worked out by Norm Dressler
292 \lt{}ndressler at dinmar dot com\gt{} 
293
294 This process was tested using the following software versions: 
295
296 \begin{itemize}
297 \item Linux Mandrake 10/Kernel 2.4.22-10 SMP 
298 \item Mysql Ver 12.21 Distrib 4.0.15, for mandrake-linux-gnu (i586) 
299 \item PostgreSQL 7.3.4 
300 \item Bacula 1.34.5 
301    \end{itemize}
302
303 WARNING: Always as a precaution, take a complete backup of your databases
304 before proceeding with this process! 
305
306 \begin{enumerate}
307 \item Shutdown bacula (cd /etc/bacula;./bacula stop)  
308 \item Run the following command to dump your Mysql database:  
309
310    \footnotesize
311 \begin{verbatim}
312        mysqldump -f -t -n >bacula-backup.dmp
313     
314 \end{verbatim}
315 \normalsize
316
317 \item Make a backup of your /etc/bacula directory (but leave the  original in
318    place).  
319 \item Go to your Bacula source directory and rebuild it to include  PostgreSQL
320    support rather then Mysql support. Check the  config.log file for your
321    original configure command and replace  enable-mysql with enable-postgresql.  
322 \item Recompile Bacula with a make and if everything compiles  completely,
323    perform a make install.  
324 \item Shutdown Mysql. 
325 \item Start PostgreSQL on your system.  
326 \item Create a bacula user in Postgres with the createuser command.  Depending on
327    your Postgres install, you may have to SU to the  user who has privileges to
328    create a user.  
329 \item Verify your pg\_hba.conf file contains sufficient permissions to  allow
330    bacula to access the server. Mine has the following since  it's on a secure
331    network:  
332
333 \footnotesize
334 \begin{verbatim}
335 local all all trust
336                 
337 host all all 127.0.0.1 255.255.255.255 trust
338                 
339 NOTE: you should restart your postgres server if you
340       made changes
341       
342 \end{verbatim}
343 \normalsize
344
345 \item Change into the /etc/bacula directory and prepare the database  and
346    tables with the following commands:  
347
348 \footnotesize
349 \begin{verbatim}
350 ./create_postgresql_database
351                                 
352 ./make_postgresql_tables
353                                 
354 ./grant_postgresql_privileges
355        
356 \end{verbatim}
357 \normalsize
358
359 \item Verify you have access to the database:  
360
361    \footnotesize
362 \begin{verbatim}
363   
364 psql -Ubacula bacula
365       
366 \end{verbatim}
367 \normalsize
368
369 You should not get any errors.  
370 \item Load your database from the Mysql database dump with:  
371
372    \footnotesize
373 \begin{verbatim}
374 psql -Ubacula bacula <bacula-backup.dmp>
375       
376 \end{verbatim}
377 \normalsize
378
379 \item Resequence your tables with the following commands:  
380
381    \footnotesize
382 \begin{verbatim}
383 psql -Ubacula bacula
384                 
385 SELECT SETVAL('basefiles_baseid_seq', (SELECT
386 MAX(baseid) FROM basefiles));
387 SELECT SETVAL('client_clientid_seq', (SELECT
388 MAX(clientid) FROM client));
389 SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid)
390 FROM file));
391 SELECT SETVAL('filename_filenameid_seq', (SELECT
392 MAX(filenameid) FROM filename));
393                 
394 SELECT SETVAL('fileset_filesetid_seq', (SELECT
395 MAX(filesetid) FROM fileset));
396                 
397 SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));
398 SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT
399 MAX(jobmediaid) FROM jobmedia));
400 SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));
401 SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
402                 
403 SELECT SETVAL('pool_poolid_seq', (SELECT MAX(poolid) FROM pool));
404        
405 \end{verbatim}
406 \normalsize
407
408 \item At this point, start up Bacula, verify your volume library and  perform
409    a test backup to make sure everything is working  properly. 
410 \end{enumerate}
411
412 \section{Upgrading PostgreSQL}
413 \index[general]{Upgrading PostgreSQL }
414 \index[general]{Upgrading!PostgreSQL }
415 \index[general]{Upgrading}
416 If you upgrade PostgreSQL, you must reconfigure, rebuild, and re-install 
417 Bacula otherwise you are likely to get bizarre failures.  If you
418 to modify the bacula.spec file to account for the new PostgreSQL version.
419 You can do so by rebuilding from the source rpm. To do so, you may need
420 install from rpms and you upgrade PostgreSQL, you must also rebuild Bacula.
421
422
423 \section{Credits}
424 \index[general]{Credits }
425 Many thanks to Dan Langille for writing the PostgreSQL driver. This will
426 surely become the most popular database that Bacula supports.