
2. 字符串,这个是需要通过pg_create_restore_point函数来创建的一个还原点,需要超级用户调用这个函数。
3. XID也很好理解,就是恢复到指定事务的结束位置。
既然我们已经知道了数据库可以恢复到指定的这几个位置,我们怎么来结合呢?
例如我们在做一笔比较重要的 *** 作前,可以创建一个还原点(但是需要超级用户),不适合阿里云RDS。
postgres=# select pg_create_restore_point('digoal')
pg_create_restore_point
-------------------------
1D6/FB17EC08
(1 row)
阿里云为了防止一些用户的误 *** 作,只开放了普通用户给用户使用,所以有一些东西都无法 *** 作,例如创建检查点,切换XLOG,创建还原点都无法 *** 作。
postgres=>checkpoint
ERROR: must be superuser to do CHECKPOINT
postgres=>select pg_switch_xlog()
ERROR: must be superuser to switch transaction log files
postgres=>select pg_create_restore_point('ab')
ERROR: must be superuser to create a restore point
时间其实是一个比较模糊的概念,所以也不建议使用,除非是我们没有其他信息,才使用时间。
XID是一个很不错的信息,我们在阿里云就用它了。
首先要创建一个记录还原点XID的表。记录XID,时间,以及描述信息。(来代替pg_create_restore_point系统函数的功能)
postgres=>create table restore_point(id serial primary key, xid int8, crt_time timestamp default now(), point text)
CREATE TABLE
创建一个函数,代替pg_create_restore_point的功能,插入还原点。
postgres=>create or replace function create_restore_point(i_point text) returns void as $$
declare
begin
insert into restore_point(xid,point) values (txid_current(),i_point)
end
$$ language plpgsql strict
CREATE FUNCTION
插入一个还原点
postgres=>select create_restore_point('digoal')
create_restore_point
----------------------
(1 row)
查询这个表的信息:
postgres=>select * from restore_point
id | xid | crt_time | point
----+--------+----------------------------+--------
1 | 561426 | 2015-06-19 09:18:57.525475 | digoal
(1 row)
postgres=>select * from restore_point where point='digoal'
id | xid | crt_time | point
----+--------+----------------------------+--------
1 | 561426 | 2015-06-19 09:18:57.525475 | digoal
(1 row)
接下来要模拟一下还原:
postgres=>create table test(id int,info text)
CREATE TABLE
postgres=>insert into test select generate_series(1,1000),md5(random()::text)
INSERT 0 1000
记录当前哈希值。用于恢复后的比对。
postgres=>select sum(hashtext(t.*::text)) from test t
sum
--------------
-69739904784
(1 row)
接下来我要做一笔删除 *** 作,在删除前,我先创建一条还原点信息。
postgres=>select create_restore_point('before delete test')
create_restore_point
----------------------
(1 row)
postgres=>delete from test
DELETE 1000
postgres=>select * from restore_point where point='before delete test'
id | xid | crt_time | point
----+--------+----------------------------+--------------------
2 | 561574 | 2015-06-19 09:45:28.030295 | before delete test
(1 row)
我只需要恢复到561574 即可。接下来就是模拟恢复了。
但是这个文件可能还没有归档,而pg_switch_xlog()函数又不能用,我们只能主动产生一些XLOG,让RDS触发归档。
postgres=>select pg_xlogfile_name(pg_current_xlog_location())
pg_xlogfile_name
--------------------------
000000010000000200000041
(1 row)
postgres=>insert into test select generate_series(1,100000)
INSERT 0 100000
postgres=>insert into test select generate_series(1,100000)
INSERT 0 100000
postgres=>select pg_xlogfile_name(pg_current_xlog_location())
pg_xlogfile_name
--------------------------
000000010000000200000042
(1 row)
已经切换。接下来我们需要下载阿里云RDS的备份和归档到本地。
并且在本地需要安装一个postgresql, 并且与阿里云RDS的编译配置参数一致(例如数据块的大小),最好使用的模块也一致,但是这里没有用到其他模块,所以无所谓。
给阿里云RDS一个建议,最好提供用户一个软件打包,方便用户恢复,降低恢复门槛。
编译项可以使用pg_config命令查看,但是RDS我们没有办法这么查看。通过pg_settings来看吧。
postgres=>select name,setting,unit from pg_settings where category='Preset Options'
name | setting | unit
-----------------------+---------+------
block_size| 8192|
data_checksums| on |
integer_datetimes | on |
max_function_args | 100 |
max_identifier_length | 63 |
max_index_keys| 32 |
segment_size | 131072 | 8kB
server_version| 9.4.1 |
server_version_num| 90401 |
wal_block_size| 8192|
wal_segment_size | 2048| 8kB
(11 rows)
postgres=>select version()
version
--------------------------------------------------------------------------------------------------------------
PostgreSQL 9.4.1 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3), 64-bit
(1 row)
本地编译安装PostgreSQL 9.4.1,编译参数与RDS一致。阿里云RDS这几个参数都是默认的。
--with-blocksize=BLOCKSIZE
set table block size in kB [8]
--with-segsize=SEGSIZE set table segment size in GB [1]
--with-wal-blocksize=BLOCKSIZE
set WAL block size in kB [8]
--with-wal-segsize=SEGSIZE
set WAL segment size in MB [16]
你用的是RDS还是ECS啊,如果是ECS上的数据库可以用 mysqldump备份出来,或者是phpmyadmin备份出来也很简单,mysqldump的导出方式是 mysqldump -u 用户名 -p 数据库名 >导出的文件名 ,导出一个表就是 mysqldump -u 用户名 -p 数据库名 表名>打开腾讯手机管家-更多-微云网盘进入微云界面后我们会发现微云界面有三种不同的功能选项,分别是网盘相册和传输三种功能。 进入到微云网盘中用Q登陆,我们能上传我们的资料等,下次可以在别处用Q登陆微云根据不同需求进行不同的选择和使用,能很好的保护我们的资料
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)