postgresql 增量备份
介绍:
barman是postgresql备份还原的管理工具。
本文环境:
系统: centos6.6
PostgreSQL 9.3.9
barman-1.4.1-1.rhel6.noarch.rpm
主机如下:
| 1 | <span style="font-size: 12px;">192.168.33.30 pgserver<br>192.168.33.31 backup<br></span> | 
其中pgserver与backup安装相同版本的pg
postgresql的安装见:http://my.oschina.net/firxiao/blog/295027
安装与配置:
在pgserver上安装rsync
| 1 | yum installrsync-y | 
在backup上安装并配置barman:
配置epel源:
| 1 | curl firxiao.com/sh/chrepo.sh|bash | 
安装barman:
| 1 | yum install-y http://nchc.dl.sourceforge.net/project/pgbarman/1.4.1/barman-1.4.1-1.rhel6.noarch.rpm | 
配置barman:
| 1 2 | mv/etc/barman.conf /etc/barman.conf.bakvim /etc/barman.conf | 
添加如下内容:
| 1 2 3 4 5 6 7 8 9 10 | [barman]barman_home = /var/lib/barmanbarman_user = barmanlog_file = /var/log/barman/barman.logconfiguration_files_directory = /etc/barman.dcompression = gzipreuse_backup = linkbandwidth_limit = 4000basebackup_retry_times = 3basebackup_retry_sleep = 30 | 
创建配置文件目录
| 1 | mkdir/etc/barman.d | 
创建名字为pgserver的备份配置
| 1 |  vim /etc/barman.d/pgserver.conf | 
添加如下内容
| 1 2 3 4 5 6 | [pgserver]description =  "The pgserver PostgreSQL Database"ssh_command = sshpostgres@pgserverconninfo = host=pgserver user=postgresminimum_redundancy = 1retention_policy = RECOVERY WINDOW OF 4 WEEKS | 
各个参数的定义详见
| 1 | man5 barman | 
配置双机ssh信任登录
使用ssh-copy-id 需要用到pgserver上的postgre用户密码及backup上barman用户密码
使用passwd 给予即可
配置完毕后使用passwd -d 删除密码
在pgserver上配置
| 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 | [root@pgserver ~]# mkdir /var/lib/pgsql/.ssh[root@pgserver ~]# chown postgres /var/lib/pgsql/.ssh[root@pgserver ~]# su - postgres-bash-4.1$ ssh-keygen Generating public/privatersa key pair.Enter fileinwhichto save the key (/var/lib/pgsql/.ssh/id_rsa): Enter passphrase (empty forno passphrase): Enter same passphrase again: Your identification has been saved in/var/lib/pgsql/.ssh/id_rsa.Your public key has been saved in/var/lib/pgsql/.ssh/id_rsa.pub.The key fingerprint is:5b:bf:25:02:1c:36:e3:b9:98:42:5a:85:27:d4:03:92 postgres@pgserverThe key's randomart image is:+--[ RSA 2048]----+|  ..oo           ||  Eo .o          ||    o o.=        ||     + + =       ||    o   S .      ||   +   o = .     ||  . . o o . o .  ||     .     . +   ||            .    |+-----------------+-bash-4.1$ ssh-copy-idbarman@backupThe authenticity of host 'backup (192.168.33.31)'can't be established.RSA key fingerprint is f3:48:30:89:03:76:cb:04:19:7a:fe:8d:6c:90:e4:fe.Are you sure you want to continueconnecting (yes/no)? yesWarning: Permanently added 'backup,192.168.33.31'(RSA) to the list of known hosts.barman@backup's password: Now try logging into the machine, with "ssh 'barman@backup'", and check in:  .ssh/authorized_keysto makesure we haven't added extra keys that you weren't expecting.-bash-4.1$ sshbarman@backup | 
不输入密码即可登录backup表示配置成功
在backup上配置
| 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 | [root@backup ~]# su - barman-bash-4.1$ ssh-keygen Generating public/privatersa key pair.Enter fileinwhichto save the key (/var/lib/barman/.ssh/id_rsa): Enter passphrase (empty forno passphrase): Enter same passphrase again: Your identification has been saved in/var/lib/barman/.ssh/id_rsa.Your public key has been saved in/var/lib/barman/.ssh/id_rsa.pub.The key fingerprint is:59:f3:8a:23:56:b5:00:b2:40:e2:a9:fb:ea:95:1d:b8 barman@backupThe key's randomart image is:+--[ RSA 2048]----+|..o . .          ||.... o .         || o  .   . +      ||.   .    = +     ||.  . .  S . .    || .  + .. . .     ||.  E .o o .      || ..  . . .       ||oo.              |+-----------------+-bash-4.1$ ssh-copy-idpostgres@pgserverpostgres@pgserver's password: Now try logging into the machine, with "ssh 'postgres@pgserver'", and check in:  .ssh/authorized_keysto makesure we haven't added extra keys that you weren't expecting.-bash-4.1$ sshpostgres@pgserver | 
不输入密码即可登录pgserver表示配置成功
配置pgserver中的postgresql:
| 1 | [root@pgserver ~]# vim /var/lib/pgsql/9.3/data/postgresql.conf | 
添加如下内容
| 1 2 3 | wal_level = 'archive'# For PostgreSQL >= 9.0archive_mode = onarchive_command = 'rsync -a %p barman@backup:INCOMING_WALS_DIRECTORY/%f' | 
其中INCOMING_WALS_DIRECTORY 可以在backup服务器上使用
| 1 2 | #barman show-server pgserver|grep "incoming_wals_directory"incoming_wals_directory: /var/lib/barman/pgserver/incoming | 
查看 并更改为那个目录
所以替换后应该是:
| 1 2 3 | wal_level = 'archive'# For PostgreSQL >= 9.0archive_mode = onarchive_command = 'rsync -a %p barman@backup:/var/lib/barman/pgserver/incoming/%f' | 
接下来配置backup可以免密码登录
| 1 | vim /var/lib/pgsql/9.3/data/pg_hba.conf | 
添加
| 1 | host    all             postgres         192.168.33.31/32trust | 
注意其中的IP地址为backup的地址
验证配置:
在backup上检查是否配置成功
| 1 2 3 4 5 | #psql -c 'SELECT version()' -U postgres -h pgserver                                                    version                                                    --------------------------------------------------------------------------------------------------------------- PostgreSQL 9.3.9 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11), 64-bit(1 行记录) | 
验证barman配置
| 1 2 3 4 5 6 7 8 9 10 11 | [root@backup ~]# barman check pgserverServer pgserver:ssh: OKPostgreSQL: OKarchive_mode: OKarchive_command: OKdirectories: OKretention policy settings: OKbackup maximum age: OK (no last_backup_maximum_age provided)compression settings: OKminimum redundancy requirements: FAILED (have 0 backups, expected at least 1) | 
最后一行为检查最小保存备份数,因为还没有备份所以为0,其他的都为ok就可以了
基本使用:
以下操作均在backup上执行
开始备份:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@backup ~]# barman backup pgserverStarting backup forserver pgserver in/var/lib/barman/pgserver/base/20150914T151454Backup start at xlog location: 0/6000028(000000010000000000000006, 00000028)Copying files.Copy done.Backup size: 32.1 MiB. Actual size on disk: 141.8 KiB (-99.57% deduplication ratio).Asking PostgreSQL server to finalize the backup.Backup end at xlog location: 0/60000B8(000000010000000000000006, 000000B8)Backup completedProcessing xlog segments forpgserver000000010000000000000005000000010000000000000006000000010000000000000006.00000028.backup | 
列出备份:
| 1 2 3 4 5 | [root@backup ~]# barman list-backup pgserverpgserver 20150914T151454 - Mon Sep 14 15:14:55 2015 - Size: 32.1 MiB - WAL Size: 0 Bpgserver 20150914T143210 - Mon Sep 14 14:32:11 2015 - Size: 32.1 MiB - WAL Size: 32.1 KiBpgserver 20150914T142931 - Mon Sep 14 14:29:41 2015 - Size: 32.1 MiB - WAL Size: 1.9 MiBpgserver 20150914T142547 - FAILED | 
删除备份:
| 1 2 3 4 | [root@backup ~]# barman delete pgserver 20150914T142547Deleting backup 20150914T142547 forserver pgserverDelete associated WAL segments:Done | 
恢复备份:
数据库误删除 丢失的时候 就需要恢复了
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@backup ~]# su - barman-bash-4.1$ barman recover pgserver 20150914T142931 /tmp/pgserver_recoverStarting localrestore forserver pgserver using backup 20150914T142931 Destination directory: /tmp/pgserver_recoverCopying the base backup.Copying required wal segments.Generating archive status filesDisabling dangerous settings indestination directory.The archive_command was setto 'false'to prevent data losses.Your PostgreSQL server has been successfully prepared forrecovery!Please review network and archive related settings inthe PostgreSQLconfiguration filebefore starting the just recovered instance.Recovery completed successful. | 
这个操作会将数据库启动所需的文件复制到指定的目录然后使用命令启动即可
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | -bash-4.1$  /usr/pgsql-9.3/bin/pg_ctl-D /tmp/pg_recover/startserver starting-bash-4.1$ < 2015-09-14 15:52:06.384 CST >LOG:  redirecting log output to logging collector process< 2015-09-14 15:52:06.384 CST >HINT:  Future log output will appear indirectory "pg_log".-bash-4.1$ ps-ef|greppostgresbarman    4547     1  0 15:52 pts/000:00:00 /usr/pgsql-9.3/bin/postgres-D /tmp/pg_recoverbarman    4548  4547  0 15:52 ?        00:00:00 postgres: logger process                      barman    4550  4547  0 15:52 ?        00:00:00 postgres: checkpointer process                barman    4551  4547  0 15:52 ?        00:00:00 postgres: writer process                      barman    4552  4547  0 15:52 ?        00:00:00 postgres: wal writer process                  barman    4553  4547  0 15:52 ?        00:00:00 postgres: autovacuum launcher process         barman    4554  4547  0 15:52 ?        00:00:00 postgres: archiver process   failed on 000000010000000000000009barman    4555  4547  0 15:52 ?        00:00:00 postgres: stats collector process             barman    4560  4434  0 15:52 pts/000:00:00 greppostgres | 
从中找出你丢失的数据 然后恢复至pgserver中吧
到此已经完成了barman的基本配置及备份还原。
参考: http://docs.pgbarman.org/#installation
更多barman配置及使用详见官方文档: http://docs.pgbarman.org/
postgresql 增量备份的更多相关文章
- PostgreSQL 增量备份详解以及相关示例
		PostgreSQL 没有类似MySQL 的二进制日志, 但是有和MySQL 类似的REDO LOG,并且有MySQL 没有的REDO 归档功能.当然REDO 的归档已经MariaDB 和Percon ... 
- mysql数据库的增量备份和全备
		还有一种简单的方法 参考 https://blog.csdn.net/u010098331/article/details/50932064 (注意:5.6版本以上新加了gtid 功能,gtid开启之 ... 
- MySQLdump增量备份、完全备份与恢复
		在数据库表丢失或损坏的情况下,备份你的数据库是很重要的.如果发生系统崩溃,你肯定想能够将你的表尽可能丢失最少的数据恢复到崩溃发生时的状态.场景:每周日执行一次完全备份,每天下午1点执行MySQLdum ... 
- 阿里云 如何减少备份使用量? mysql数据库的完整备份、差异备份、增量备份
		RDS for MySQL备份.SQL审计容量相关问题_MYSQL使用_技术运维问题_云数据库 RDS 版-阿里云 https://help.aliyun.com/knowledge_detail/4 ... 
- MySQL5.7.18 备份、Mysqldump,mysqlpump,xtrabackup,innobackupex 全量,增量备份,数据导入导出
		粗略介绍冷备,热备,温暖,及Mysqldump,mysqlpump,xtrabackup,innobackupex 全量,增量备份 --备份的目的 灾难恢复:意外情况下(如服务器宕机.磁盘损坏等)对损 ... 
- mysql全量和增量备份详解(带脚本)
		在日常运维工作中,对mysql数据库的备份是万分重要的,以防在数据库表丢失或损坏情况出现,可以及时恢复数据. 下面对这种备份方案详细说明下:1.MySQLdump增量备份配置执行增量备份的前提条件是M ... 
- Mysql性能优化三(分表、增量备份、还原)
		接上篇Mysql性能优化二 对表进行水平划分 如果一个表的记录数太多了,比如上千万条,而且需要经常检索,那么我们就有必要化整为零了.如果我拆成100个表,那么每个表只有10万条记录.当然这需要数据在逻 ... 
- mysql备份脚本,每天执行一次全量备份,三次增量备份
		线上一个小业务的mysql备份 全量备份 #!/bin/bash #crete by hexm at -- #scripte name : full_backup.sh #descriptioni : ... 
- 通过innobackupex实现对MySQL的增量备份与还原
		备份 增量备份是基于完整备份的,所以我们需要先做一次完整备份: innobackupex --password=test /backup/ 备注:test是我的MySQL服务的root用户的密码,/b ... 
随机推荐
- Android笔记(四十六) Android中的数据存储——XML(二)PULL解析
			PULL 的工作原理: XML pull提供了开始元素和结束元素.当某个元素开始时,可以调用parser.nextText()从XML文档中提取所有字符数据.当解析到一个文档结束时,自动生成EndDo ... 
- Flask统计代码行数
			流程: 1.获取前端的文件 2.判断文件是否zip文件 3.解压压缩包并保存 4.遍历解压后的文件夹 5.判断文件是否py文件,将绝对路径添加到列表 6.循环列表,排除注释和空号,统计行数 from ... 
- git注册和基本命令
			注册账户以及创建仓库 要想使用github第一步当然是注册github账号了.之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称后Create,之 ... 
- [ike][ipsec] child sa rekey机制的细节分析
			子标题:ipsec rekey是否会导致丢包 author: classic_tong 前言 什么叫rekey. rekey是指ipsec的通信两端定期更换加密信道秘钥的机制. 为了安全性考虑,随着秘 ... 
- P1600 天天爱跑步[桶+LCA+树上差分]
			题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵 ... 
- Django之路——6 Django的模型层(一)
			ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ... 
- CF600E Lomsat gelral 和 CF741D Dokhtar-kosh paths
			Lomsat gelral 一棵以\(1\)为根的树有\(n\)个结点,每个结点都有一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号(若有数量一样的,则求编号和). \(n \le 10^ ... 
- try catch 场景
			面试官:什么情况下用到try-catch?程序员:代码执行预料不到的情况,我会使用try-catch.面试官:什么是预料不到的情况呢?程序员:比如我要计算a除以b,但是b是变量,如果b等于0程序就会出 ... 
- machine learning (6)---how to choose features, polynomial regression
			how to choose features, polynomial regression:通过定义更适合我们的feature,选择更好的模型,使我们的曲线与数据更好的拟合(而不仅仅是一条直线) 可以 ... 
- PL/sql配置相关
			可以安装oracle之后,打开PL/sql之后,自动找到oracle的路径以及数据库连接. 或者:安装oracle客户端,手动在PL/sql中配置oic以及oracle主目录的位置,并且配置好C: ... 
