转载使用barman备份PostgreSQL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转载使用barman备份PostgreSQL相关的知识,希望对你有一定的参考价值。

什么是barman

Barman (备份和恢复管理器) 是 PostgreSQL 数据库服务器中非常方便的备份和恢复工具,允许远程备份多个服务器,允许从一个备份集中一个命令就恢复数据库。同时还可以对多个备份集进行方便的管理.

环境说明

环境有两台机器:

  • 192.168.1.21 主数据库所在主机
  • 192.168.1.22 为barman备份机

安装barman

到barman的官方网站http://www.pgbarman.org/下载barman安装包,把安装包拷贝到192.168.1.22机器上,解压:

tar xvf barman-1.2.1.tar.gz

然后进入目录,进行安装:

cd barman-1.2.1
python setup.py build
sudo python setup.py install

安装barman需要的附加的包:

sudo aptitude install python-argh
sudo aptitude install python-psycopg2
sudo aptitude install python-dateutil
sudo pip install argcomplete

检测argh的版本

[email protected]:~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import argh
>>> argh.__version__
‘0.17.2‘

barman要求argh >= 0.21.2,版本太低,升级版本:

[email protected]:~$ sudo pip install --upgrade argh
Downloading/unpacking argh from https://pypi.python.org/packages/source/a/argh/argh-0.23.2.tar.gz#md5=0d026a85bdfb5dd895138b274a3fc665
 Downloading argh-0.23.2.tar.gz
 Running setup.py egg_info for package argh

Installing collected packages: argh
 Found existing installation: argh 0.17.2
   Uninstalling argh:
     Successfully uninstalled argh
 Running setup.py install for argh

Successfully installed argh
Cleaning up...

[email protected]:~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import argh
>>> argh.__version__
‘0.23.2‘

配置barman

配置要备份的主库

到192.168.1.21配置主库的postgresql.conf文件,打开主库的归档模式,同时修改archive_command的配置,配置内容如下:

wal_level = archive
archive_mode = on
archive_command = ‘rsync -a %p [email protected]:/home/osdba/barman/main/incoming/%f‘

注意到上面的归档命令是把WAL日志拷贝到192.168.1.22机器的/home/osdba/barman/main/incoming/目录下,所以要先在备份机上建这个目录:

mkdir -p /home/osdba/barman/main/incoming 

当然你也可以使用其它的目录. 配置完后,需要重启启动主库.

打通备份机与数据库机器之间的ssh通道

barman需要打通两台机器之间的ssh通道,假设我们的用户是osdba,方法如下: 先进入192.168.1.21机器上:

[email protected]:~/$ cd .ssh
[email protected]:~/.ssh$ cat id_rsa.pub >> authorized_keys
scp authorized_keys 192.168.1.22:`pwd`

再进入192.168.1.22机器上:

[email protected]:~/$ cd .ssh
[email protected]:~/.ssh$ cat id_rsa.pub >> authorized_keys
scp authorized_keys 192.168.1.21:`pwd`/.

实际上上面的操作就是生成一个authroized_keys文件,其中存有192.168.1.21和192.168.1.22两台机器上id_rsa.pub文件的内容.

把下面的内容加到这两台机器的/etc/hosts文件中:

192.168.1.21 pg
192.168.1.22 backup

测试是否能互相ssh: 在192.168.1.21上:

[email protected]:~/.ssh$ ssh [email protected]
Welcome to Ubuntu 13.04 (GNU/Linux 3.8.0-25-generic x86_64)
Documentation: https://help.ubuntu.com/
Last login: Sat Jun 22 16:02:07 2013 from pg 
[email protected]:~$ exit
logout Connection to backup closed.
[email protected]:~/.ssh$ ssh [email protected]
Welcome to Ubuntu 13.04 (GNU/Linux 3.8.0-25-generic x86_64)
Documentation: https://help.ubuntu.com/
45 packages can be updated. 
13 updates are security updates.
Last login: Sat Jun 22 16:01:53 2013 from pg
[email protected]:~$ exit
logout Connection to pg closed.

在192.168.1.22上:

[email protected]:~$ ssh [email protected]
Welcome to Ubuntu 13.04 (GNU/Linux 3.8.0-25-generic x86_64)
Documentation: https://help.ubuntu.com/
45 packages can be updated. 13 updates are security updates.
Last login: Sat Jun 22 16:02:21 2013 from pg
[email protected]:~$ exit
logout Connection to pg closed. 
[email protected]:~$ ssh [email protected]
Welcome to Ubuntu 13.04 (GNU/Linux 3.8.0-25-generic x86_64)
Documentation: https://help.ubuntu.com/
Last login: Sat Jun 22 16:02:12 2013 from pg
[email protected]:~$ exit
logout Connection to backup closed.

设置barman的配置文件

在192.168.1.22从模板中拷贝一个barman的配置文件,在此模板的基础上进行修改:

[email protected]:~/src/barman-1.2.1/doc$ cp barman.conf ~/.barman.conf 
[email protected]:~/src/barman-1.2.1/doc$ vi ~/.barman.conf

把.barman.conf文件中内容修改为:

 [barman] ; 
 Main directory barman_home = /home/osdba/barman
; System user barman_user = osdba
; Log location
log_file = /home/osdba/barman/log
[main] ; 
; Human readable description 
description = "Main PostgreSQL Database"

; SSH options 
; ssh_command = ssh [email protected] 
ssh_command = ssh [email protected]

; PostgreSQL connection string
conninfo = host=pg user=osdba dbname=master

; Minimum number of required backups (redundancy)
minimum_redundancy = 1

配置完后,barman需要在192.168.1.22机器上连接到192.168.1.21上的数据库,在配置文件中配置的连接串为: conninfo = host=pg user=osdba dbname=master, 一般我们还有密码,为了不输入密码,我们把密码配置到~/.pgpass文件中:

[email protected]:~/src/barman-1.2.1/doc$ cat ~/.pgpass
pg:5432:master:osdba:XXXXXX 

上面内容”XXXXXX”是数据库用户osdba的密码,当然你也可以用其它的超用用户连接PostgreSQL,这里使用的是”osdba”用户.

备份数据库

查看要备份的数据库

使用barman show-server main查看要备份的数据库的情况: [email protected]:~/barman$ barman show-server main

Server main:
active: true
description: Main PostgreSQL Database
ssh_command: ssh [email protected]
conninfo: host=pg user=osdba dbname=master
backup_directory: /home/osdba/barman/main
basebackups_directory: /home/osdba/barman/main/base
wals_directory: /home/osdba/barman/main/wals
incoming_wals_directory: /home/osdba/barman/main/incoming
lock_file: /home/osdba/barman/main/main.lock
compression: None
custom_compression_filter: None
custom_decompression_filter: None
retention_policy_mode: auto
retention_policy: None
wal_retention_policy: main
pre_backup_script: None
post_backup_script: None
minimum_redundancy: 1
bandwidth_limit: None
tablespace_bandwidth_limit: None
current_xlog: 000000010000000000000005
last_shipped_wal: None
archive_command: rsync -a %p [email protected]:/home/osdba/barman/main/incoming/%f
server_txt_version: 9.2.3
data_directory: /home/osdba/pgdata
archive_mode: on
config_file: /home/osdba/pgdata/postgresql.conf
hba_file: /home/osdba/pgdata/pg_hba.conf
ident_file: /home/osdba/pgdata/pg_ident.conf

执行备份

Starting backup for server main in /home/osdba/barman/main/base/20130622T120619
Backup start at xlog location: 0/B000020 (00000001000000000000000B, 00000020)
Copying files.
Copy done.
Asking PostgreSQL server to finalize the backup.
Backup end at xlog location: 0/B0000A8 (00000001000000000000000B, 000000A8)
Backup completed

查看备份结果

[email protected]:~$ barman list-backup main
main 20130622T120619 - Sat Jun 22 12:06:25 2013 - Size: 25.0 MiB - WAL Size: 0 B 

恢复数据库

执行恢复

做恢复测试,把数据库恢复到/home/osdba/pgd2目录下:

[email protected]:~$ barman recover main 20130622T120619 /home/osdba/pgd2
Processing xlog segments for main
000000010000000000000005
000000010000000000000006
000000010000000000000006.00000020.backup
000000010000000000000007
000000010000000000000008
000000010000000000000008.00000020.backup
000000010000000000000009
00000001000000000000000A
00000001000000000000000A.00000020.backup
00000001000000000000000B
00000001000000000000000B.00000020.backup
Starting local restore for server main using backup 20130622T120619
Destination directory: /home/osdba/pgd2
Copying the base backup.
Copying required wal segments.
The archive_command was set to ‘false‘ to prevent data losses.

Your PostgreSQL server has been successfully prepared for recovery!

Please review network and archive related settings in the PostgreSQL
configuration file before starting the just recovered instance.

启动恢复的数据库

[email protected]:~$ pg_ctl -D /home/osdba/pgd2 start server starting
[email protected]:~$

检查恢复的数据库:

[email protected]:~$ psql postgres
psql (9.2.3)
Type "help" for help.

postgres=# \l
                             List of databases
  Name    | Owner | Encoding |   Collate   |    Ctype    | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
master    | slony | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
postgres  | osdba | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | osdba | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/osdba         +
          |       |          |             |             | osdba=CTc/osdba
template1 | osdba | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/osdba         +
          |       |          |             |             | osdba=CTc/osdba
(4 rows)

停止恢复的数据库

[email protected]:~$ pg_ctl -D /home/osdba/pgd2 stop
waiting for server to shut down....... done
server stopped

以上是关于转载使用barman备份PostgreSQL的主要内容,如果未能解决你的问题,请参考以下文章

SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase(代码片

postgresql备份与恢复数据库

PostgreSQL:甚至读取访问更改数据文件磁盘导致使用 pgbackrest 进行大型增量备份

postgresql使用pg_basebackup备份与恢复

postgresql数据库备份策略

postgresql数据库备份策略