文章目录
  1. 1. 安装barman
    1. 1.1. 系统需求
  2. 2. 尝试同一台机子的备份
  3. 3. 配置postgresql
    1. 3.1. 配置ssh-key认证
  4. 4. 执行首次备份
  5. 5. 增量备份
  6. 6. 恢复数据库

通常我在vps备份pgsql都是直接使用pg_dump,但这个工具不支持增量备份,每次都传完整的数据回本地比较花时间。昨天发现了barman这个工具,看介绍好像是可以实现比较方便的备份pgsql,还可以在远程直接备份到本地,增量方面应该是利用了wal归档日志做的。下载过来做个备份实验试试,官方网站是:http://www.pgbarman.org/。

安装barman

首先呢,这个程序是用python写的,所以直接用pip安装吧,依赖部分自己处理一下,psycopg2这个pgsql的驱动需要用到postgresql的dev库。

系统需求

  • Linux/Unix
  • Python 2.6 or 2.7
  • Python modules:
  • argcomplete
  • argh >= 0.21.2
  • psycopg2
  • python-dateutil < 2.0 (since version 2.0 requires python3)
  • distribute (optional)
  • PostgreSQL >= 8.4
  • rsync >= 3.0.4
1
pip install barman

note

根据官方的描述,barman需要使用pgsql9.0以上版本。

尝试同一台机子的备份

按照官方的说明,这玩意是支持远程备份的,但我看了他们的配置似乎要在服务器上通过配置pgsql来直接把每次生成的wal复制到备份机,这个对于我这种没有固定备份机的比较麻烦。所以就先试一下直接在vps进行增量备份,然后再想办法解决其它的事情。

note

准备配置的时候才发现我弄错了,其实应该从官网下载源码的,源码包里面的doc目录下有个barman.conf配置文件需要用到。

把barman.conf放到/etc/barman.conf或着~/.barman.conf都可以。

配置文件的内容如下:

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
; Barman, Backup and Recovery Manager for PostgreSQL
; http://www.pgbarman.org/ - http://www.2ndQuadrant.com/
;
; Main configuration file

[barman]
; Main directory
barman_home = /var/lib/barman

; System user
barman_user = root

; Log location
log_file = /var/log/barman/barman.log

; Default compression level: possible values are None (default), bzip2, gzip or custom
compression = bzip2

; Pre/post backup hook scripts
;pre_backup_script = env | grep ^BARMAN
;post_backup_script = env | grep ^BARMAN

; Directory of configuration files. Place your sections in separate files with .conf extension
; For example place the 'main' server section in /etc/barman.d/main.conf
;configuration_files_directory = /etc/barman.d

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

; Global retention policy (REDUNDANCY or RECOVERY WINDOW) - default empty
;retention_policy =

; Global bandwidth limit in KBPS - default 0 (meaning no limit)
;bandwidth_limit = 4000

; 'main' PostgreSQL Server configuration
[main]
; Human readable description
description = "Main PostgreSQL Database"

incoming_wals_directory = /root/bak/incoming/

; SSH options
ssh_command = ssh root@localhost

; PostgreSQL connection string
conninfo = host=localhost user=postgres dbname=pitayacd

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

; Examples of retention policies

; Retention policy (disabled)
; retention_policy =
; Retention policy (based on redundancy)
; retention_policy = REDUNDANCY 2
; Retention policy (based on recovery window)
; retention_policy = RECOVERY WINDOW OF 4 WEEKS

我这里图省事直接用root用户了。

配置postgresql

需要配置postgresql开启wal归档,打开postgresql.conf文件在里面配置下面的信息。

1
2
3
wal_level = archive
archive_mode = on
archive_command = 'rsync -a %p root@localhost:/root/bak/incoming/%f'

配置ssh-key认证

重启postgresql服务之前还需要对postgresql的运行用户进行ssh认证的配置,配置成无密码认证就行。然后在postgresql的运行用户中执行一下下面的命令,如果能正常连接进去就行。

1
ssh root@localhost

然后重启一下postgresql,这样就启用了归档模式。这个时候看一下/root/bak/incoming文件夹下面是不是有很多文件了,如果没有文件复制过来就需要检查一下是不是哪个步骤弄错了。

执行首次备份

执行备份之前先用barman show-server main来查看一下信息,确定备份信息是否正确。然后就可以执行下面的命令进行备份了。

1
barman backup main

note

我这边刚开始的时候忘了配置postgresql的ssh-key,所以一直不成功,后来配置上去就没问题了。

增量备份

增量备份只需要执行 barman cron 就可以了,它会自动的把wals备份过来。

1
select pg_switch_xlog();

使用这条语句可以强制切换wal归档文件,这样就能输出最近的wal档。

恢复数据库

使用下面的命令恢复

1
barman recover main 20130905T111017 ./test

tip

20130905T111017是备份id,可以用 barman list-backup main 查看

恢复到指定时间的方法

1
barman recover main 20130905T111017 ./test --target-time "2013-09-05 11:15:00.000"
文章目录
  1. 1. 安装barman
    1. 1.1. 系统需求
  2. 2. 尝试同一台机子的备份
  3. 3. 配置postgresql
    1. 3.1. 配置ssh-key认证
  4. 4. 执行首次备份
  5. 5. 增量备份
  6. 6. 恢复数据库