【转载】使用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
时间: 2024-10-17 06:30:31

【转载】使用barman备份PostgreSQL的相关文章

Barman安装及备份PostgreSQL

barman特点 零数据丢失备份.保证用户在只有一台备份服务器的情况下达到零数据丢失. 与备份服务器合作.允许备份服务器在与主服务器的流式复制不可用时,从barman获取wal文件. 可靠的监控集成.用于监控和创建有关Barman配置状态的报告. 可并行的备份和恢复.以更快的速度执行拷贝,加快了整个备份和恢复过程. 备份目录.允许用户在同一界面中列出.保留.删除.存档和恢复多个完整备份. 支持多个服务器.允许您从同一位置管理多个服务器的备份和恢复. 存储备份数据.存储来自多个PostgreSQL

pg_dump实例详解(备份postgresql和greenplum数据库)

一.pg_dump的用法:数据库的导入导出是最常用的功能之一,每种数据库都提供有这方面的工具,例如Oracle的exp/imp,Informix的dbexp/dbimp,MySQL的mysqldump,而PostgreSQL提供的对应工具为pg_dump和pg_restore.pg_dump是用于备份PostgreSQL数据库的工具.它可以在数据库正在使用的时候进行完整一致的备份,并不阻塞其它用户对数据库的访问.转储格式可以是一个脚本或者归档文件.转储脚本的格式是纯文本,包含许多SQL命令,这些

Windows 2008R2 定时备份PostgreSQL 11.6

PostgreSQL 自动备份,并删除10天前的备份文件的windows脚本. 第一步,创建脚本,命名back.bat文件,可直接点击执行或者CMD执行此批处理命令. @ECHO OFF @setlocal enableextensions @cd /d "%~dp0" set PGPASSWORD=password SET PGPATH=D:\postgresql\binSET SVPATH=d:\zoobackupSET PRJDB=gzzoo SET DBUSR=postgres

shell脚本备份postgresql数据库个人笔记

[[email protected] tmp]# vi 2.sh #!/bin/bashBACKUP="/home/bak"#RSYCN="/usr/bin/rsync"#OPTIONS="-rlHpogDtS --delete"DATE=`date +%Y%m%d`LOG_FILE="/var/log/backup/backupdb_$DATE.log"SERVER="192.168.2.31" #1.p

【转载】RMAN备份保留策略设置相关命令

转自 http://blog.sina.com.cn/s/blog_7c5a82970101g4s7.html 策略有两种,一种是基于恢复窗口策略的,一种是基于冗余策略的. 基于恢复窗口策略: 定义的是保留的备份文件必须要能保证恢复到近N天的任意时间点. CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 14 DAYS; 让恢复窗口成为14天大小. 基于冗余策略: 定义的是每个文件至少要保留几份备份. CONFIGURE RETENTION POL

postgresql 增量备份

介绍: barman是postgresql备份还原的管理工具.官网: http://www.pgbarman.org/ 本文环境: 系统: centos6.6 PostgreSQL 9.3.9 barman-1.4.1-1.rhel6.noarch.rpm 主机如下: 192.168.33.30 pgserver 192.168.33.31 backup 其中pgserver与backup安装相同版本的pg postgresql的安装见:http://my.oschina.net/firxiao

postgresql自动备份

bak_dir="/home/backupfiles/db" #备份路径 pg_dir="/usr/local/pgsql/bin" #pgsql路径 date_dir=`date +%Y/%m/%d` dbname="数据库名字" PGPASSWORD="超级用户postgres的密码" export PGPASSWORD > /dev/null 2>&1 mkdir -p ${bak_dir}/${da

PostgreSQL数据库备份和恢复(脚本方式)

数据库备份: D:\Program Files\PostgreSQL\9.2\bin> pg_dump -h localhost -U postgres -C --column-inserts -f E:/dbname.sql dbname 数据库恢复: D:\Program Files\PostgreSQL\9.2\bin> psql -h localhost -U postgres -f E:/dbname.sql   注:D:\Program Files\PostgreSQL\9.2\b

恢复PostgreSQL数据库备份至GuassDB 200

gs_restore是GaussDB 200提供的与gs_dump配套的导入工具.通过该工具,可将gs_dump导出的文件导入至数据库.这里通过postgreSQL的pg_dump命令备份数据库,然后通过gs_restore将其恢复到GuassDB 200中. 1.备份PostgreSQL [[email protected] ~]$ pg_dump -Fc -C rhnschema >/var/satellite/bak/pg_rhnschema.dump 2.GuassDB创建对应数据库以及