一 环境信息 
主节点
备节点
二 FreeBSD 安装 pg 备节点 
环境变量 ~/.prpfile
1 2 3 4 5 6 7 8 9 10 11 12  
export  PGPORT =1921  export  PGUSER =postgres  export  PGDATA =/database/pg93/pg_root  export  LANG =en_US.utf8  export  PGHOME =/opt/pgsql9.3.5  export  PGPOOL_HOME =/opt/pgpool  export  LD_LIBRARY_PATH =$PGHOME /lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib # 这里的设置可能要改下  export  DATE =`date +"%Y%m%d%H%M" `  export  PATH =/opt/pgbouncer/bin:$PGHOME/bin:$PGPOOL_HOME/bin:$PATH:.  export  MANPATH =$PGHOME /share/man:$MANPATH  alias rm ='rm -i'    alias ll ='ls -lh'   
 
 
编译并安装
1 2 3  
root@francs :/   root@francs :/   root@francs :/    
 
 
创建数据目录并赋权
1 2 3 4 5 6 7 8 9  
root@francs :/      root@francs :~    NAME USED AVAIL REFER MOUNTPOINT   zp_db 174 M 29.1 G 31 K /zp_db   zp_db/database 31 K 29.1 G 31 K /database/   zp_db/opt 174 M 29.1 G 174 M /opt      root@francs :/    
 
 
编辑密码文件
1 2  
[francs@francs  ~]$ cat ~/.pgpass   192.168 .2 .200 :1921 :replication: repuser: rep123us345er 
 
 
备注:同时修改主节点的 pg_hba.conf。  
pg_basebackup 创建流复制备节点
1 2 3 4 5 6  
[francs@francs ~]$ pg_basebackup -D /database/pg93/pg_root -Fp  -Xs  -v -P -h 192.168 .2 .200   -p 1921   -U repuser    recovery.conf 配置   recovery_target_timeline =  'latest'   standby_mode = on   primary_conninfo =  'host=192.168 .2 .200  port=1921  user=repuser'  
 
 
pg_ctl 启动备节点
1 2 3 4 5 6 7 8  
[francs@francs /database/pg93/pg_root]$ pg_ctl start   server starting   [francs@francs /database/pg93/pg_root]$ FATAL: XX000: could not  create semaphores:  No  space left on device   DETAIL: Failed system  call was semget(1921021,  17,  03600).   HINT: This error  does *not * mean that you have run  out of disk space. It occurs when either the system  limit for  the maximum number of semaphore sets (SEMMNI),  or  the system  wide maximum number of semaphores (SEMMNS), would be exceeded. You need to  raise the respective kernel parameter. Alternatively, reduce PostgreSQL s consumption of semaphores by reducing its max_connections parameter.    The  PostgreSQL documentation contains more information about configuring your system  for   PostgreSQL.   LOCATION: InternalIpcSemaphoreCreate, pg_sema.c:125  
 
 
备注:启动报错,内核设置 kernel 的共享内存不够,设置以下即可。  
配置/boot/loader.conf
1 2 3  
kern.ipc .semmni =512    kern.ipc .semmns =1024    kern.ipc .semmnu =256   
 
 
配置/etc/sysctl.conf
1 2 3  
sysctl kern.ipc .shmall =32768    sysctl kern.ipc .shmmax =134217728    sysctl kern.ipc .semmap =256   
 
 
备注:这些设置满足实验目的,reboot 生效。  
三 生成快照 
create_snap.sh 脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  
root@francs:/usr/local /bin  #!/usr/local/bin/bash      while   true     do   current_time=`date "+%Y%m%d_%H%M%S" `   zfs snapshot zp_db/database@"$current_time "       if   [ $?  -ne 0  ];  then   echo  `date "+%Y-%m-%d %H:%M:%S" `  "zp_db/database snapshot generates failed!"    break    fi      echo  `date "+%Y-%m-%d %H:%M:%S" `  "zp_db/database snapshot generates successfuly!"   >>  /tmp/snapshot.log  sleep 60   done  
 
 
备注: 创建一个生成快照的脚本,每分钟创建一个快照。  
开启脚本
1  
nohup /usr/ local/bin/ create_snap.sh 2 >&1   &  
 
 
打开 .zfs 隐藏目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  
root@francs :/usr/local/bin   . .. pg93      root@francs :/usr/local/bin      root@francs :/usr/local/bin   . .. .zfs pg93      root@francs :~    total 140    drwxr-xr-x 3  francs francs 3   10  9   21 : 11   20141010_114052 /   drwxr-xr-x 3  francs francs 3   10  9   21 : 11   20141010_134732 /   drwxr-xr-x 3  francs francs 3   10  9   21 : 11   20141010_135359 /   drwxr-xr-x 3  francs francs 3   10  9   21 : 11   20141010_140045 /   drwxr-xr-x 3  francs francs 3   10  9   21 : 11   20141010_140413 /   drwxr-xr-x 3  francs francs 3   10  9   21 : 11   20141010_140513 / ....  
 
 
备注:查看快照有两种方式,一种是通过” zfs list -t snapshot” 命令, 另一种是查看 .zfs 隐藏目录下,可以打开数据集的 snapdir 属性使 .zfs 目录可见。  
四 恢复测试 
主节点
1 2 3 4 5 6 7  
[pg93@db1 script]$ cat pitr_insert.sh   #!/bin/bash      for ((i=1;i>0;i=1))  do   psql -d postgres -U postgres -c "insert into test_pitr (name) values('test_pitr');"    sleep 10;   done  
 
 
备注:主节点跑一个数据插入脚本,每 10 秒向主节点插入一条数据。通过这个程序,测试 ZFS 是否能做基于时间点的数据恢复。  
表结构如下
1 2 3 4 5 6  
Table   "public.test_pitr"    Column | Type  | Modifiers    -------------+--------------------------------+--------------------------------------------------------   id | integer  |  not   null  default nextval('test_pitr_id_seq'::regclass)   name | character  varying(64 ) |     create_time  | timestamp (0 ) without time  zone |  default  clock_timestamp() 
 
 
zfs clone
1 2 3 4 5 6 7 8  
root@francs :/usr/local/bin      root@francs :/usr/local/bin   NAME USED AVAIL REFER MOUNTPOINT   zp_db 1.14 G 28.1 G 32 K /zp_db   zp_db/database 990 M 28.1 G 947 M /database/   zp_db/opt 174 M 28.1 G 174 M /opt   zp_db/pitr141412 1 K 28.1 G 947 M /zp_db/pitr141412  
 
 
备注: 我们将数据恢复到 2014-10-10 14-14-12 时刻,新的数据目录为 /zp_db/pitr141412,可以看到这个目录空间使用为 1K ,几乎没占用额外空间。  
修改 recovery.conf, postgresql.conf
启动恢复目录的 pg
1  
[francs@francs  ~]$ pg_ctl start -D /zp_db/ pitr141412/pg93/ pg_root  
 
 
五 数据验证 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  
[francs@francs /zp_db/pitr141412/pg93/pg_root/pg_log]$ psql -p 1922   psql (9.3.5)   Type  "help"   for  help.      postgres =# select * from  test_pitr order by id desc limit 3;  id |name | create_time    ------+-----------+---------------------   7909  | test_pitr |  2014-10-10  14:14:08   7908  | test_pitr |  2014-10-10  14:13:58   7907  | test_pitr |  2014-10-10  14:13:47   (3 rows)      postgres =# select count(*) from  test_pitr;  count    -------    935   (1 row)  
 
 
备注:数据成功恢复到 2014-10-10 14-14-12 时刻。  
六 总结 
   利用 ZFS 的 snapshot 和 clone 功能能够轻松满足 PostgreSQL 数据库的 PITR 需求,而且具有实施简单,低成本,恢复时间短的优点。  
七 参考 
                                                        原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/tech/bigdata/239589.html