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