PostgreSQL备份之pg_basebackup

一、准备

连接上master节点创建replication用户

# CREATE ROLE replicator with login replication password 123456;

修改白名单pg_hba.conf

# host    replication    all        xxxxx/32    md5

二、制作基础备份

# pg_basebackup --format=tar \ #使用打包方式备份
--xlog-method=fetch \    #获取wal文件方式,等备份结束后把备份期间所有的wal备份
--compress=1 \    #压缩等级
--checkpoint=fast \    #执行检查点的方式
--label=backup \    #标签
--pgdata=/data/backup/ #备份目录
--progress \    #打印过程
--verbose \    #打印详细信息
--host=‘172.17.5.45‘ \    #master节点ip
--port=5432 \    #master节点端口
--user=postgres    #连接master节点的用户
Password: 
transaction log start point: 2/34000028 on timeline 1
414272/414272 kB (100%), 3/3 tablespaces                                         
transaction log end point: 2/34000128
pg_basebackup: base backup completed

备份默认会占用一个max_wal_senders连接,需要设置大一点,还可以使用

  -r, --max-rate=RATE    maximum transfer rate to transfer data directory
                         (in kB/s, or use suffix "k" or "M")

限制传输速度

# ll backup/
total 123196
-rw-r--r--. 1 root root       129 Sep  8 01:21 25375.tar.gz
-rw-r--r--. 1 root root       128 Sep  8 01:21 25376.tar.gz
-rw-r--r--. 1 root root 126141406 Sep  8 01:22 base.tar.gz

base.tar.gz是数据目录的备份,其他两个是表空间的备份

三、恢复

1. 解压缩
# cd /data/backup/
# mkdir pgdata 25375 25376
# tar zxf base.tar.gz -C pgdata/
# ls pgdata/
backup_label  global   pg_dynshmem  pg_ident.conf  pg_logical    pg_notify    pg_serial     pg_stat      pg_subtrans  pg_twophase  pg_xlog               postgresql.conf
base          pg_clog  pg_hba.conf  pg_log         pg_multixact  pg_replslot  pg_snapshots  pg_stat_tmp  pg_tblspc    PG_VERSION   postgresql.auto.conf  recovery.conf
# tar zxf 25375.tar.gz -C 25375
# tar zxf 25376.tar.gz -C 25376 
# ls
25375  25375.tar.gz  25376  25376.tar.gz  base.tar.gz  pgdata
2. 修改表空间软连接
# ln -sf /data/backup/25375 pgdata/pg_tblspc/25375 
[[email protected] backup]# ln -sf /data/backup/25376 pgdata/pg_tblspc/25376                     
[[email protected] backup]# ll pgdata/pg_tblspc/
total 0
lrwxrwxrwx. 1 root root 18 Sep  8 01:35 25375 -> /data/backup/25375
lrwxrwxrwx. 1 root root 18 Sep  8 01:35 25376 -> /data/backup/25376
3. 修改数据目录权限
# chown -R postgres:postgres pgdata/ 25375 25376 
# chmod 0700 pgdata/
# ll
total 123208
drwxr-xr-x.  3 postgres postgres      4096 Sep  8 01:32 25375
-rw-r--r--.  1 root     root           129 Sep  8 01:21 25375.tar.gz
drwxr-xr-x.  3 postgres postgres      4096 Sep  8 01:33 25376
-rw-r--r--.  1 root     root           128 Sep  8 01:21 25376.tar.gz
-rw-r--r--.  1 root     root     126141406 Sep  8 01:22 base.tar.gz
drwx------. 19 postgres postgres      4096 Sep  8 01:31 pgdata
4. 启动
# su postgres
$ pg_ctl -D pgdata/ start
server starting
 [    2015-09-08 05:40:04.510 UTC 11107 55ee74b4.2b63 1 0]LOG:  redirecting log output to logging collector process
[    2015-09-08 05:40:04.511 UTC 11107 55ee74b4.2b63 2 0]HINT:  Future log output will appear in directory "pg_log".
$ psql 
psql (9.4.4)
Type "help" for help.

postgres=# \db+
                                  List of tablespaces
    Name    |  Owner   |      Location      | Access privileges | Options | Description 
------------+----------+--------------------+-------------------+---------+-------------
 pg_default | postgres |                    |                   |         | 
 pg_global  | postgres |                    |                   |         | 
 tblspc1    | postgres | /data/backup/25375 |                   |         | 
 tblspc2    | postgres | /data/backup/25376 |                   |         | 
(4 rows)

四、 制作从库

1. 获取备份文件
# pg_basebackup --format=plain \    #使用目录方式
--write-recovery-conf \    #生成简单recovery.conf
--xlog-method=stream \    #使用stream的方式备份wal文件,会再占用一个max_wal_sender,wal文件一生成就会备份
--checkpoint=fast --label=backup --pgdata=/data/test_pgdata_94 --progress --verbose --host=‘172.17.5.45‘ --port=5432 --user=postgres
Password: 
transaction log start point: 2/38000028 on timeline 1
pg_basebackup: starting background WAL receiver
397887/397887 kB (100%), 3/3 tablespaces                                         
transaction log end point: 2/380000F0
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
2. 修改目录权限
# chown -R postgres:postgres test_pgdata_94 tblspc1/ tblspc2/
# chmod 0700 test_pgdata_94
# ll test_pgdata_94
total 104
-rw-------. 1 postgres postgres  189 Sep  8 01:43 backup_label
drwx------. 6 postgres postgres 4096 Sep  8 01:43 base
drwx------. 2 postgres postgres 4096 Sep  8 01:43 global
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_clog
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_dynshmem
-rw-------. 1 postgres postgres 4581 Sep  8 01:43 pg_hba.conf
-rw-------. 1 postgres postgres 1636 Sep  8 01:43 pg_ident.conf
drwxr-xr-x. 2 postgres postgres 4096 Sep  8 01:43 pg_log
drwx------. 4 postgres postgres 4096 Sep  8 01:43 pg_logical
drwx------. 4 postgres postgres 4096 Sep  8 01:43 pg_multixact
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_notify
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_replslot
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_serial
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_snapshots
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_stat
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_stat_tmp
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_subtrans
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_tblspc
drwx------. 2 postgres postgres 4096 Sep  8 01:43 pg_twophase
-rw-------. 1 postgres postgres    4 Sep  8 01:43 PG_VERSION
drwx------. 3 postgres postgres 4096 Sep  8 01:43 pg_xlog
-rw-------. 1 postgres postgres   88 Sep  8 01:43 postgresql.auto.conf
-rw-------. 1 postgres postgres 4676 Sep  8 01:43 postgresql.conf
-rw-r--r--. 1 postgres postgres  131 Sep  8 01:43 recovery.conf
# ll tblspc1/
total 4
drwx------. 2 postgres postgres 4096 Sep  8 01:43 PG_9.4_201409291
# ll tblspc2
total 4
drwx------. 2 postgres postgres 4096 Sep  8 01:43 PG_9.4_201409291
3. 启动
# su postgres
$ pg_ctl -D test_pgdata_94 start
server starting
$ [    2015-09-08 05:49:55.383 UTC 12100 55ee7703.2f44 1 0]LOG:  redirecting log output to logging collector process
[    2015-09-08 05:49:55.383 UTC 12100 55ee7703.2f44 2 0]HINT:  Future log output will appear in directory "pg_log".

$ psql 
psql (9.4.4)
Type "help" for help.

postgres=# \db+
                                List of tablespaces
    Name    |  Owner   |   Location    | Access privileges | Options | Description 
------------+----------+---------------+-------------------+---------+-------------
 pg_default | postgres |               |                   |         | 
 pg_global  | postgres |               |                   |         | 
 tblspc1    | postgres | /data/tblspc1 |                   |         | 
 tblspc2    | postgres | /data/tblspc2 |                   |         | 
(4 rows)

参考:http://www.postgresql.org/docs/9.4/static/app-pgbasebackup.html

时间: 2024-08-06 12:02:23

PostgreSQL备份之pg_basebackup的相关文章

postgresql 备份

PostgreSQL备份与恢复示例 文档控制 l       文档变更记录 日期 版本号 作者 备注 2009-8-13 v1.0 陈浩 创建 TOC \o "1-3" \h \z \u PostgreSQL备份与恢复 文档控制 一.       简介 二.       SQL转储 (一)pg_dump (二)pg_dumpall; (三)备份压缩与分割 三.       文件系统级别备份(冷备份) 四.       在线热备份(归档) (一)备份 (二)恢复 一.      简介 P

postgresql备份恢复数据库和批量导入导出数据到文件方法

备份数据库:pg_dump -h localhost -U root demo02 > /home/arno/dumps/demo02.bak 恢复数据库:psql -h localhost -U root -d demo <  demo.bak 备份表:pg_dump -h localhost -U root demo02 -t books > /home/arno/dumps/books.bak 恢复表:psql -h localhost -U root -d demo -t boo

PostgreSQL备份之pg_rman

一. 安装 PostgreSQL版本是9.4.4 # git clone -b REL9_4_STABLE https://github.com/ossc-db/pg_rman.git Initialized empty Git repository in /opt/pg_rman/.git/ remote: Counting objects: 1939, done. remote: Total 1939 (delta 0), reused 0 (delta 0), pack-reused 19

PostgreSQL备份之手工备份(Low Level API)

一.备份 1. 需要保证archive_mode = on 和 archive_command是有效的 2. 在master节点上连接上数据库并执行 SELECT pg_start_backup('label', true); pg_start_backup第二个参数设置为true的好处是备份开始会执行一个检查点,设置为true能尽快的完成检查点,并且能减少备份期间产生的wal文件,但是会对查询有影响,默认设置的checkpoint_completion_target的一半的时间,半夜的备份建议

postgresql 备份 还原

windows  linux 通用 第一步:通过 cmd 进入到postgresql 安装目录的 bin 下: cd C:\PostgreSQL\pg95\bin 第二步:备份数据库 pg_dump -h localhost -U TPlusPosAdmin -d tegen -f "C:\testdatabase.bak" -h:数据库服务器地址: -p:数据库端口号: -U:U 大写,表示用户名: -d:数据库名称:-f:把备份文件放在哪里: 第三步:还原数据库(你要还原的数据库软

postgreSQL备份数据

1.pg_dump 备份单一数据库 pg_dump仅导出数据库结构:pg_dump -U TestRole1 -s -f TestDb1.sql TestDb1 备份某个database,备份结果以自定义压缩格式输出:pg_dump -h localhost -p 5432 -U someuser -F c -b -v -f mydb.backup mydb 备份某个database,备份结果以SQL文本方式输出,输出结果中需包括CREATE DATABASE语句:pg_dump -h loca

postgreSQL 备份多张表

-U表示用户 -h表示主机 -p表示端口号 -t表示表名 -f表示备份后的sql文件的名字 -d表示要恢复数据库名 一.打开cmd 进入postgresql安装路径下的bin文件夹,以我的为例: cd D:\Program Files\PostgreSQL\bin 二.输入备份命令,多个表每个表前面都要加 -t: 输入口令,即你的数据库登录密码,完成备份. pg_dump -U postgres -h localhost -p 5432 -t fsrd_user -t fsrd_tenant -

PostgreSQL备份之omniPITR

一.准备 1. 安装 omnipitr是用perl写的,直接下载下来就可以用了 # git clone https://github.com/omniti-labs/omnipitr.git /opt/omnipitr/ Initialized empty Git repository in /opt/omnipitr/.git/ remote: Counting objects: 2627, done. remote: Total 2627 (delta 0), reused 0 (delta

PostgreSQL中流复制pg_basebackup做了什么

解压PostgreSQL源代码包后可以到如下路径:postgresql-9.2.4\src\backend\replication下可以看到,basebackup.c,另外还可以看到walreceiver.c,walsender.c. basebackup做了什么一目了然: /*------------------------------------------------------------------------- * * basebackup.c * code for taking a