Welcome to mirror list, hosted at ThFree Co, Russian Federation.

postgresql.tex « main « en « manuals - github.com/bareos/bareos-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1ab6e0cd3070c332ff20c65e4afc29a2476bfa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
%%
%%

\chapter{Installing and Configuring PostgreSQL}
\label{PostgreSqlChapter}
\index[general]{PostgreSQL!Installing and Configuring}
\index[general]{Installing and Configuring PostgreSQL}
\index[general]{Upgrading}

If you are considering using PostreSQL, you should be aware
of their philosophy of upgrades, which could be
destabilizing for a production shop.  Basically at every major version
upgrade, you are required to dump your database in an ASCII format,
do the upgrade, and then reload your database (or databases). This is
because they frequently update the "data format" from version to
version, and they supply no tools to automatically do the conversion.
If you forget to do the ASCII dump, your database may become totally
useless because none of the new tools can access it due to the format
change, and the PostgreSQL server will not be able to start.

If you are building PostgreSQL from source, please be sure to add
the {\bf \verb:--:enable-thread-safety} option when doing the ./configure
for PostgreSQL.

\section{Installing PostgreSQL}
\index[general]{PostgreSQL!Installing}

If you use the {\bf ./configure \verb:--:with-postgresql=PostgreSQL-Directory}
statement for configuring {\bf Bareos}, you will need PostgreSQL version 7.4
or later installed. NOTE! PostgreSQL versions earlier than 7.4 do not work
with Bareos. If PostgreSQL is installed in the standard system location, you
need only enter {\bf \verb:--:with-postgresql} since the configure program will
search all the standard locations. If you install PostgreSQL in your home
directory or some other non-standard directory, you will need to provide the
full path with the {\bf \verb:--:with-postgresql} option.

Installing and configuring PostgreSQL is not difficult but can be confusing
the first time. If you prefer, you may want to use a package provided by your
chosen operating system. Binary packages are available on most PostgreSQL
mirrors.

If you prefer to install from source, we recommend following the instructions
found in the
\elink{PostgreSQL documentation}{http://www.postgresql.org/docs/}.

If you are using FreeBSD,
\elink{this FreeBSD Diary article}{http://www.freebsddiary.org/postgresql.php}
will be useful. Even if you are not using FreeBSD, the article will contain
useful configuration and setup information.

If you configure the Batch Insert code in Bareos (attribute inserts are
10 times faster), you {\bf must} be using a PostgreSQL that was built with
the {\bf \verb:--:enable-thread-safety} option, otherwise you will get
data corruption. Most major Linux distros have thread safety turned on, but
it is better to check.  One way is to see if the PostgreSQL library that
Bareos will be linked against references pthreads.  This can be done
with a command such as:

\footnotesize
\begin{verbatim}
  nm /usr/lib/libpq.a | grep pthread_mutex_lock
\end{verbatim}
\normalsize

The above command should print a line that looks like:

\footnotesize
\begin{verbatim}
         U pthread_mutex_lock
\end{verbatim}
\normalsize

if does, then everything is OK. If it prints nothing, do not enable batch
inserts when building Bareos.

After installing PostgreSQL, you should return to completing the installation
of {\bf Bareos}. Later, after Bareos is installed, come back to this chapter
to complete the installation. Please note, the installation files used in the
second phase of the PostgreSQL installation are created during the Bareos
Installation. You must still come back to complete the second phase of the
PostgreSQL installation even if you installed binaries (e.g. rpm, deb,
...).


\section{Configuring PostgreSQL}
\index[general]{PostgreSQL!Configuring PostgreSQL -- }

At this point, you should have built and installed PostgreSQL, or already have
a running PostgreSQL, and you should have configured, built and installed {\bf
Bareos}. If not, please complete these items before proceeding.

Please note that the {\bf ./configure} used to build {\bf Bareos} will need to
include {\bf \verb:--:with-postgresql=PostgreSQL-directory}, where {\bf
PostgreSQL-directory} is the directory name that you specified on the
./configure command for configuring PostgreSQL (if you didn't specify a
directory or PostgreSQL is installed in a default location, you do not need to
specify the directory). This is needed so that Bareos can find the necessary
include headers and library files for interfacing to PostgreSQL.

An important thing to note here is that {\bf Bareos} makes two connections
to the PostgreSQL server for each backup job that is currently running.  If
you are intending to run a large number of concurrent jobs, check the value
of {\bf max\_connections} in your PostgreSQL configuration file to ensure
that it is larger than the setting {\bf Maximum Concurrent Jobs}
in your director configuration.  {\bf Setting this too low will result in
some backup jobs failing to run correctly!}

{\bf Bareos} will install scripts for manipulating the database (create,
delete, make tables etc) into the main installation directory. These files
will be of the form *\_bareos\_* (e.g. create\_bareos\_database). These files
are also available in the {\textless}bareos-src{\textgreater}/src/cats directory after
running ./configure. If you inspect create\_bareos\_database, you will see
that it calls create\_postgresql\_database. The *\_bareos\_* files are
provided for convenience. It doesn't matter what database you have chosen;
create\_bareos\_database will always create your database.

Now you will create the Bareos PostgreSQL database and the tables that Bareos
uses. These instructions assume that you already have PostgreSQL running. You
will need to perform these steps as a user that is able to create new
databases. This can be the PostgreSQL user (on most systems, this is the pgsql
user).

\begin{enumerate}
\item cd {\textless}install-directory{\textgreater}

   This directory contains the Bareos catalog interface routines.

\item Create the database owner ({\bf bareos})
   On many systems, the PostreSQL master
   owner is {\bf pgsql} and on others such as Red Hat and Fedora it is {\bf
   postgres}.  You can find out which it is by examining your /etc/passwd
   file.  To create a new user under either your name or with say the name
   {\bf bareos}, you can do the following:

\begin{verbatim}
   su
   (enter root password)
   su pgsql (or postgres)
   createuser bareos
   Shall the new user be allowed to create databases? (y/n) y
   Shall the new user be allowed to create more new users? (y/n) (choose
         what you want)
   exit
\end{verbatim}
   Normally the {\bf bareos} user must be able to create new databases,
   if you use the script in the next item,
   or you will have to create one for it, but it does not need to
   create new users.

\item ./create\_bareos\_database

   This script creates the PostgreSQL {\bf bareos} database.
   Before running this command, you should carefully think about
   what encoding sequence you want for the text fields (paths, files, ...).
   We strongly recommend that you use the default value of SQL\_ASCII
   that is in the create\_bareos\_database script.  Please be warned
   that if you change this value, your backups may fail.  After running
   the script, you can check with the command:

\begin{verbatim}
   psql -l
\end{verbatim}

   and the column marked {\bf Encoding} should be {\bf SQL\_ASCII} for
   all your Bareos databases (normally {\bf bareos}).

\item ./make\_bareos\_tables

   This script creates the PostgreSQL tables used by {\bf Bareos}.
\item ./grant\_bareos\_privileges

   This script creates the database user {\bf bareos}  with restricted access
rights. You may  want to modify it to suit your situation. Please note that
this database is not password protected.

\end{enumerate}

Each of the three scripts (create\_bareos\_database, make\_bareos\_tables, and
grant\_bareos\_privileges) allows the addition of a command line argument.
This can be useful for specifying the user name. For example, you might need
to add {\bf -h hostname} to the command line to specify a remote database
server.

To take a closer look at the access privileges that you have setup with the
above, you can do:

\footnotesize
\begin{verbatim}
PostgreSQL-directory/bin/psql --command \\dp bareos
\end{verbatim}
\normalsize

Also, I had an authorization problem with the password. In the end,
I had to modify my {\bf pg\_hba.conf} file (in /var/lib/pgsql/data on my machine
in /var/lib/postgresql/8.x on others, and in /etc/postgres/8.x/main on
still others -- what a mess!) from:

\footnotesize
\begin{verbatim}
  local   all    all        ident  sameuser
to
  local   all    all        trust
\end{verbatim}
\normalsize

This solved the problem for me, but it is not always a good thing
to do from a security standpoint.  However, it allowed me to run
my regression scripts without having a password.

A more secure way to perform database authentication is with md5
password hashes.  Begin by editing the {\bf pg\_hba.conf} file, and
above the existing ``local'' and ``host'' lines, add the line:

\footnotesize
\begin{verbatim}
  local bareos bareos md5
\end{verbatim}
\normalsize

then restart the Postgres database server (frequently, this can be done
using "/etc/init.d/postgresql restart" or "service postgresql restart") to
put this new authentication rule into effect.

Next, become the Postgres administrator, postgres, either by logging
on as the postgres user, or by using su to become root and then using
{\bf su - postgres} or {\bf su - pgsql} to become postgres.
Add a password to the {\bf bareos} database for the {\bf bareos} user using:

\footnotesize
\begin{verbatim}
  \$ psql bareos
  bareos=# alter user bareos with password 'secret';
  ALTER USER
  bareos=# \\q
\end{verbatim}
\normalsize

You'll have to add this password to two locations in the
bareos-dir.conf file: once to the Catalog resource and once to the
RunBeforeJob entry in the BackupCatalog Job resource.  With the
password in place, these two lines should look something like:

\footnotesize
\begin{verbatim}
  dbname = bareos; user = bareos; password = "secret"
    ... and ...
  # WARNING!!! Passing the password via the command line is insecure.
  # see comments in make_catalog_backup for details.
  RunBeforeJob = "/etc/make_catalog_backup bareos bareos secret"
\end{verbatim}
\normalsize

Naturally, you should choose your own significantly more random
password, and ensure that the bareos-dir.conf file containing this
password is readable only by the root.

Even with the files containing the database password properly
restricted, there is still a security problem with this approach: on
some platforms, the environment variable that is used to supply the
password to Postgres is available to all users of the
local system.  To eliminate this problem, the Postgres team have
deprecated the use of the environment variable password-passing
mechanism and recommend the use of a .pgpass file instead.  To use
this mechanism, create a file named .pgpass containing the single
line:

\footnotesize
\begin{verbatim}
  localhost:5432:bareos:bareos:secret
\end{verbatim}
\normalsize

This file should be copied into the home directory of all accounts
that will need to gain access to the database: typically, root,
bareos, and any users who will make use of any of the console
programs.  The files must then have the owner and group set to match
the user (so root:root for the copy in ~root, and so on), and the mode
set to 600, limiting access to the owner of the file.

\section{Re-initializing the Catalog Database}
\index[general]{Database!Re-initializing the Catalog}
\index[general]{Re-initializing the Catalog Database}

After you have done some initial testing with {\bf Bareos}, you will probably
want to re-initialize the catalog database and throw away all the test Jobs
that you ran. To do so, you can do the following:

\footnotesize
\begin{verbatim}
  cd <install-directory>
  ./drop_bareos_tables
  ./make_bareos_tables
  ./grant_bareos_privileges
\end{verbatim}
\normalsize

Please note that all information in the database will be lost and you will be
starting from scratch. If you have written on any Volumes, you must write an
end of file mark on the volume so that Bareos can reuse it. Do so with:

\footnotesize
\begin{verbatim}
   (stop Bareos or unmount the drive)
   mt -f /dev/nst0 rewind
   mt -f /dev/nst0 weof
\end{verbatim}
\normalsize

Where you should replace {\bf /dev/nst0} with the appropriate tape drive
device name for your machine.

\section{Installing PostgreSQL from RPMs}
\index[general]{PostgreSQL!Installing from RPMs}
\index[general]{Installing PostgreSQL from RPMs}
If you are installing PostgreSQL from RPMs, you will need to install
both the PostgreSQL binaries and the client libraries.  The client
libraries are usually found in a {\bf devel} or {\bf dev} package, so you must
install the following for rpms:

\footnotesize
\begin{verbatim}
  postgresql
  postgresql-devel
  postgresql-server
  postgresql-libs
\end{verbatim}
\normalsize


and the following for debs:

\footnotesize
\begin{verbatim}
  postgresql
  postgresql-common
  postgresql-client
  postgresql-client-common
  libpq5
  libpq-dev
\end{verbatim}
\normalsize


These will be similar with most other package managers too.  After
installing from rpms, you will still need to run the scripts that set up
the database and create the tables as described above.


\section{Converting from MySQL to PostgreSQL}
\index[general]{PostgreSQL!Converting from MySQL to}
\index[general]{Converting from MySQL to PostgreSQL}

The conversion procedure presented here was worked out by Norm Dressler
{\textless}ndressler at dinmar dot com{\textgreater}

This process was tested using the following software versions:

\begin{itemize}
\item Linux Ubuntu Lucid
\item Mysql Ver 5.0.83
\item PostgreSQL 8.4.4
\item Bareos 5.0
   \end{itemize}

WARNING: Always as a precaution, take a complete backup of your databases
before proceeding with this process!

\begin{enumerate}
\item Shutdown bareos (cd /etc/bareos;./bareos stop)
\item Run the following command to dump your Mysql database:

   \footnotesize
\begin{verbatim}
mysqldump -t -n -c --compatible=postgresql --skip-quote-names --skip-opt \
          --disable-keys --lock-tables -u bareos -ppassword bareos \
  | grep -v "INSERT INTO Status" \
  | sed -e 's/0000-00-00 00:00:00/1970-01-01 00:00:00/g' \
  | sed -e 's/\\0//' > bareos-backup.sql
\end{verbatim}
\normalsize

\item Make a backup of your /etc/bareos directory (but leave the  original in
   place).
\item Go to your Bareos source directory and rebuild it to include  PostgreSQL
   support rather then Mysql support. Check the  config.log file for your
   original configure command and replace  enable-mysql with enable-postgresql.
\item Recompile Bareos with a make and if everything compiles  completely,
   perform a make install.
\item Shutdown Mysql.
\item Start PostgreSQL on your system.
\item Create a bareos user in Postgres with the createuser command.  Depending on
   your Postgres install, you may have to SU to the  user who has privileges to
   create a user, you can also have to change permissions on catalog scripts
   to fit your situation.
\item Verify your pg\_hba.conf file contains sufficient permissions to  allow
   bareos to access the server. Mine has the following since  it's on a secure
   network:

\footnotesize
\begin{verbatim}
local all all trust

host all all 127.0.0.1 255.255.255.255 trust

NOTE: you should reload (or restart) your postgres server if you  made changes

\end{verbatim}
\normalsize

\item Change into the /etc/bareos directory and prepare the database  and
   tables with the following commands:

\footnotesize
\begin{verbatim}
./create_postgresql_database

./make_postgresql_tables

./grant_postgresql_privileges

\end{verbatim}
\normalsize

\item Verify you have access to the database:

   \footnotesize
\begin{verbatim}

psql -Ubareos bareos

\end{verbatim}
\normalsize

You should not get any errors.
\item Load your database from the Mysql database dump with:

   \footnotesize
\begin{verbatim}
psql -Ubareos bareos <bareos-backup.dmp>

\end{verbatim}
\normalsize

\item Resequence your tables with the following commands:

   \footnotesize
\begin{verbatim}
psql -Ubareos bareos

SELECT SETVAL('basefiles_baseid_seq', (SELECT MAX(baseid) FROM basefiles));
SELECT SETVAL('client_clientid_seq', (SELECT MAX(clientid) FROM client));
SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid) FROM file));
SELECT SETVAL('filename_filenameid_seq', (SELECT MAX(filenameid) FROM filename));
SELECT SETVAL('fileset_filesetid_seq', (SELECT MAX(filesetid) FROM fileset));
SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));
SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT MAX(jobmediaid) FROM jobmedia));
SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));
SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
SELECT SETVAL('basefiles_baseid_seq', (SELECT MAX(baseid) FROM basefiles));
SELECT SETVAL('client_clientid_seq', (SELECT MAX(clientid) FROM client));
SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid) FROM file));
SELECT SETVAL('filename_filenameid_seq', (SELECT MAX(filenameid) FROM filename));
SELECT SETVAL('fileset_filesetid_seq', (SELECT MAX(filesetid) FROM fileset));
SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));
SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT MAX(jobmediaid) FROM jobmedia));
SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));
SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
SELECT SETVAL('pool_poolid_seq', (SELECT MAX(poolid) FROM pool));

SELECT SETVAL('device_deviceid_seq', (SELECT MAX(deviceid) FROM device));
SELECT SETVAL('location_locationid_seq', (SELECT MAX(locationid) FROM location));
SELECT SETVAL('locationlog_loclogid_seq', (SELECT MAX(loclogid) FROM locationlog));
SELECT SETVAL('log_logid_seq', (SELECT MAX(logid) FROM log));
SELECT SETVAL('mediatype_mediatypeid_seq', (SELECT MAX(mediatypeid) FROM mediatype));
SELECT SETVAL('storage_storageid_seq', (SELECT MAX(storageid) FROM storage));
\end{verbatim}
\normalsize

\item At this point, start up Bareos, verify your volume library and  perform
   a test backup to make sure everything is working  properly.
\end{enumerate}

\section{Upgrading PostgreSQL}
\index[general]{Upgrading PostgreSQL}
\index[general]{Upgrading!PostgreSQL}
\index[general]{Upgrading}
If you upgrade PostgreSQL, you must reconfigure, rebuild, and re-install
Bareos otherwise you are likely to get bizarre failures.  If you
to modify the bareos.spec file to account for the new PostgreSQL version.
You can do so by rebuilding from the source rpm. To do so, you may need
install from rpms and you upgrade PostgreSQL, you must also rebuild Bareos.

\section{Tuning PostgreSQL}
\index[general]{Tuning}

If you despool attributes for many jobs at the same time, you can tune the
sequence object for the \texttt{FileId} field.
\begin{verbatim}
psql -Ubareos bareos

ALTER SEQUENCE file_fileid_seq CACHE 1000;
\end{verbatim}

\section{Credits}
\index[general]{Credits}
Many thanks to Dan Langille for writing the PostgreSQL driver. This will
surely become the most popular database that Bareos supports.