针对于Postgresql备份方式有三种:
1.sql转储
2.热备
3.文件系统冷备份
之后对于这三种备份方式一一演示:
1.sql转储
这里我们用到的工具是pg_dump和pg_dumpall,它和Oracle中的expdp,MysqL的MysqLdump类似,它可以在数据库正在使用的时候进行完整一致的备份,并不阻塞其它用户对数据库的访问。
同时在使用此种备份方法时可以使用数据库层面的命令对数据进行压缩或分割,如:
1)压缩
备份:
pg_dumpdbname|gzip>filename.gz
恢复
gunzip-cfilename.gz|psqldbname
2)分割
备份:
pg_dumpdbname|split-b1m-filename
恢复
catfilename*|psqldbname
具体实验步骤如下:
pg_dump
首先创建数据库并创建测试表
[pg@edb1~]$createdbpg
[pg@edb1~]$psqlpg
psql(9.3.9)
Type"help"forhelp.
pg=#createtablebackup_test(xint);
CREATETABLE
pg=#insertintobackup_testvalues(1);
INSERT01
pg=#insertintobackup_testvalues(2);
pg=#select*frombackup_test;
x
---
1
2
(2rows)
pg=#\q
使用pg_dump工具进行sql转储:
[pg@edb1~]$pg_dumppg>/home/pg/pg.dmp
[pg@edb1~]$dropdbpg
[pg@edb1~]$psqlpg</home/pg/pg.dmp
....
验证数据是否恢复:
(2rows)
pg_dumpall
刚才使用pg_dump只能备份单库,使用pg_dumpall可以备份本地所有库
实验过程如下:
创建测试数据库及测试表
[pg@edb1~]$createdbpg1
[pg@edb1~]$createdbpg2
[pg@edb1~]$psqlpg1
pg1=#createtablebtest1(xint);
pg1=#insertintobtest1values(1);
pg1=#\q
[pg@edb1~]$psqlpg2
pg2=#createtablebtest2(xint);
pg2=#insertintobtest2values(1);
pg2=#\q
备份数据库:
[pg@edb1~]$pg_dumpall>/home/pg/pg_all.dmp
[pg@edb1~]$dropdbpg1
[pg@edb1~]$dropdbpg2
恢复数据库:
[pg@edb1~]$psql-f/home/pg/pg_all.dmppostgres
......
验证数据:
pg1=#select*frombtest1;
(1row)
pg2=#select*frombtest2;
pg2=#\q
2.在线热备
使用selectpg_start_backup和selectpg_stop_backup()对数据库进行热备
实验过程如下:
(1)开启归档
[pg@edb1~]$vi/home/pg/pgsql/data/postgresql.conf
配置以下三个参数:
wal_level=archive
archive_mode=on
archive_command='cp%p/home/pg/pgsql/backup/archived_log/%f'
(2)创建归档目录
[pg@edb1~]$mkdir-p/home/pg/pgsql/backup/archived_log
(3)重启数据库,使参数生效
[pg@edb1~]$pg_ctl-D/home/pg/pgsql/datastop
[pg@edb1~]$pg_ctl-D/home/pg/pgsql/datastart
(4)创建测试库
[pg@edb1~]$createdbarch
[pg@edb1~]$psqlarch
arch=#createtablebtest(xint);
arch=#insertintobtestvalues(1);
INSERT01
(5)创建备份
arch=#selectpg_start_backup('baseline');
pg_start_backup
-----------------
0/5000028
arch=#\q
(6)备份数据库目录
[pg@edb1~]$tar-zcvf/home/pg/pg_backup.tar.gz/home/pg/pgsql/data/
......
(7)停止备份
arch=#selectpg_stop_backup();
NOTICE:pg_stop_backupcomplete,allrequiredWALsegmentshavebeenarchived
pg_stop_backup
----------------
0/50000B8
(1row)
(8)再插入一些其他数据并切换WAL日志
arch=#insertintobtestvalues(2);
arch=#insertintobtestvalues(3);
arch=#insertintobtestvalues(4);
arch=#selectpg_switch_xlog();
pg_switch_xlog
0/6000208
(1row)
(9)停止数据库
LOG:receivedsmartshutdownrequest
LOG:autovacuumlaunchershuttingdown
LOG:shuttingdown
waitingforservertoshutdown....LOG:databasesystemisshutdown
done
serverstopped
(10)删除目录
[pg@edb1~]$rm-rf/home/pg/pgsql/data/
(11)使用tar包备份恢复目录
[pg@edb1~]$tar-zxvfpg_backup.tar.gz-C/
......
(12)重置pg_xlog目录
[pg@edb1~]$rm-r/home/pg/pgsql/data/pg_xlog/
[pg@edb1~]$mkdir/home/pg/pgsql/data/pg_xlog/
(13)创建目录存放之前的归档
[pg@edb1~]$mkdirarch
[pg@edb1~]$cp/home/pg/pgsql/backup/archived_log/*arch/
(14)编辑recovery.conf文件
[pg@edb1~]$vipgsql/data/recovery.conf
restore_command=‘cp/home/pg/arch/%f“%p”’
(15)启动数据库
[pg@edb1~]$pg_ctl-D/home/pg/pgsql/data/start
serverstarting
[pg@edb1~]$LOG:databasesystemwasinterrupted;lastknownupat2015-09-2118:27:41CST
LOG:startingarchiverecovery
LOG:restoredlogfile"000000010000000000000005"fromarchive
LOG:redostartsat0/5000090
LOG:consistentrecoverystatereachedat0/50000B8
LOG:restoredlogfile"000000010000000000000006"fromarchive
cp:cannotstat`/home/pg/arch/000000010000000000000007':Nosuchfileordirectory
LOG:redodoneat0/60001E8
LOG:lastcompletedtransactionwasatlogtime2015-09-2118:30:16.033281+08
cp:cannotstat`/home/pg/arch/00000002.history':Nosuchfileordirectory
LOG:selectednewtimelineID:2
cp:cannotstat`/home/pg/arch/00000001.history':Nosuchfileordirectory
LOG:archiverecoverycomplete
LOG:MultiXactmemberwraparoundprotectionsarenowenabled
LOG:databasesystemisreadytoacceptconnections
LOG:autovacuumlauncherstarted
可以看到数据库恢复到了最后一个归档所在的时间点
(16)验证数据
arch=#select*frombtest;
3
4
(4rows)
3.文件系统冷备份
方法就是对数据库相关文件直接进行备份,由于需要停库,对业务影响较大,所以一般不使用这种方法。
实验过程如下:
创建测试库
[pg@edb1~]$createdbtest1
[pg@edb1~]$psqltest1
test1=#createtabletest1(xint);
test1=#insertintotest1values(1);
test1=#\q
关库
[pg@edb1~]$pg_ctl-D/home/pg/pg
pg_all.dmppg.dmppg.logpgsql/
[pg@edb1~]$pg_ctl-D/home/pg/pgsql/data/stop
waitingforservertoshutdown....done
serverstopped
进行文件系统打包冷备
[pg@edb1~]$tar-zcvfpgdata.tar.gz/home/pg/pgsql/data
......
[pg@edb1~]$rm-rf/home/pg/pgsql/data/
使用tar包中内容进行替换
[pg@edb1~]$tar-zxvfpgdata.tar.gz-C/
启动数据库查看
[pg@edb1~]$LOG:databasesystemwasshutdownat2015-09-2118:02:27CST
LOG:autovacuumlauncherstarted
[pg@edb1~]$
test1=#select*fromtest1;
test1=#\q