使用zfs存放postgresql数据文件

zfs开源已经13年了,最近开始使用。zfs现在进入freebsd基本配置,使用方便,几条命令起来了,先准备好磁盘ad1

echo 'zfs_enable="YES"' >> /etc/rc.conf
/etc/rc.d/zfs start 
echo 'daily_status_zfs_enable="YES"'>> /etc/periodic.conf 
zpool create z01  ad1

zfs管理简洁得很!把数据库复制到新建的z01上面,启动数据库。每天做一个快照。查看结果:

df -h | egrep 'usr$|z01$' ; zfs list -t snapshot ; zpool status z01 ; top -no size

/dev/ada0s1f                                     434G    247G    152G    62%    /usr
z01                                              433G     54G    379G    12%    /z01
NAME                          USED  AVAIL  REFER  MOUNTPOINT
z01/pgsql/data@t20180507      206M      -  9.65G  -
z01/pgsql/data@201805080820  71.0M      -  9.56G  -
z01/pgsql/data@20180509      70.3M      -  9.56G  -
  pool: z01
 state: ONLINE
  scan: none requested
config:

        NAME        STATE     READ WRITE CKSUM
        z01         ONLINE       0     0     0
          ada1      ONLINE       0     0     0

errors: No known data errors
last pid: 41091;  load averages:  1.55,0.77,0.66  up 0+07:23:44    15:29:10
137 processes: 1 running,132 sleeping,4 zombie

Mem: 231M Active,468M Inact,1867M Wired,415M Buf,5364M Free
ARC: 964M Total,486M MFU,464M MRU,414K Anon,1995K Header,12M Other
     925M Compressed,966M Uncompressed,1.04:1 Ratio
Swap: 4096M Total,4096M Free


  PID USERNAME     THR PRI NICE   SIZE    RES STATE   C   TIME    WCPU COMMAND
 1260 pgsql          1   4    0  1172M   103M select  4   0:32   0.00% postgres
 1219 pgsql          1   4    0  1170M   102M select  6   0:33   0.00% postgres
 1258 pgsql          1   4    0  1170M   102M select  3   0:24   0.00% postgres
 1261 pgsql          1   4    0  1168M    98M select  5   0:11   0.00% postgres
 1259 pgsql          1   4    0  1168M    98M select  7   0:11   0.00% postgres
 1227 pgsql          1   4    0  1168M 91852K select  2   0:24   0.00% postgres
 1245 pgsql          1   4    0  1168M 91500K select  7   0:26   0.00% postgres
 1262 pgsql          1   4    0  1168M 75920K select  6   0:07   0.00% postgres
 1266 pgsql          1   4    0  1166M 74656K select  2   0:06   0.00% postgres
 1236 pgsql          1   4    0  1120M   125M select  4  21:45   0.00% postgres

平时没什么负载的时候zfs使用了900M字节作为管理和cache。

基于时间点的恢复其实是很耗时间的,如果你的基础备份每7天一次, 那么读取XLOG并执行到指定的时间点,再取出当时的数据,可能要半小时;如果你每天备份一次数据文件,而又没有使用zfs,那么备份的空间又占用很多。用zfs每天备份数据文件夹占用很少空间,从上面快照列表可以看到。

我们做一次基于时间点的恢复,克隆20180509凌晨的数据文件快照,数据恢复到2018-05-09 10:00这个时间点:

zfs clone z01/pgsql/data@20180509 z01/pgsql/datatest
cd /z01/pgsql/

cd datatest

cp ../postgresql.conf.recov_time ./postgresql.conf
cp ../recovery.conf.recov_time ./recovery.conf
cat ./recovery.conf
>restore_command='cp >/z01/pgsql/arch/%f %p || cp >/usr/local/pgsql/arch/%f %p'
>recovery_target_timeline='1'
>recovery_target_time = '2018-05-09 10:00:00+08'


rm -rf postmaster.pid
rm -rf pg_wal
mkdir pg_wal
cp -R /usr/local/pg_wal/* pg_wal/

chown -R pgsql:pgsql ../datatest
chmod -R og-rwx ../datatest

su pgsql -c "pg_ctl -D ../datatest start"

克隆数据文件夹是秒回的,执行XLOG跨越的时间段不会超过一天,既快又节省空间。后台向前滚动的事务日志:

cd /z01/pgsql/datatest/log
tail -f `ls -t | head -n 1`
2018-05-09 02:24:29 CST root@postgres [local] LOG:  duration: 29474.253 ms  statement: select pg_start_backup('')
2018-05-09 15:34:17 CST @  LOG:  database system was interrupted; last known up at 2018-05-09 02:24:29 CST
2018-05-09 15:34:17 CST @  LOG:  starting point-in-time recovery to 2018-05-09 10:00:00+08
2018-05-09 15:34:17 CST @  LOG:  restored log file "000000010000001900000011" from archive
2018-05-09 15:34:18 CST @  LOG:  redo starts at 19/11000028
2018-05-09 15:34:18 CST @  LOG:  consistent recovery state reached at 19/1105D218
2018-05-09 15:34:18 CST @  LOG:  database system is ready to accept read only connections
2018-05-09 15:34:18 CST @  LOG:  restored log file "000000010000001900000012" from archive
2018-05-09 15:34:19 CST @  LOG:  restored log file "000000010000001900000013" from archive
2018-05-09 15:34:20 CST @  LOG:  restored log file "000000010000001900000014" from archive
2018-05-09 15:34:20 CST @  LOG:  restored log file "000000010000001900000015" from archive
2018-05-09 15:34:21 CST @  LOG:  restored log file "000000010000001900000016" from archive
2018-05-09 15:34:22 CST @  LOG:  restored log file "000000010000001900000017" from archive
2018-05-09 15:34:22 CST @  LOG:  restored log file "000000010000001900000018" from archive
2018-05-09 15:34:23 CST @  LOG:  restored log file "000000010000001900000019" from archive
2018-05-09 15:34:24 CST @  LOG:  recovery stopping before commit of transaction 51331481,time 2018-05-09 10:00:00.043179+08

实验结束,我们删除测试文件夹。(* ̄︶ ̄)

su pgsql -c "pg_ctl -D ../datatest -mfast stop"
zfs destroy z01/pgsql/datatest

相关文章

文章浏览阅读601次。Oracle的数据导入导出是一项基本的技能,...
文章浏览阅读553次。开头还是介绍一下群,如果感兴趣polardb...
文章浏览阅读3.5k次,点赞3次,收藏7次。折腾了两个小时多才...
文章浏览阅读2.7k次。JSON 代表 JavaScript Object Notation...
文章浏览阅读2.9k次,点赞2次,收藏6次。navicat 连接postgr...
文章浏览阅读1.4k次。postgre进阶sql,包含分组排序、JSON解...