天天看点

数据库恢复方案

中国广东省深圳市龙华新区民治街道溪山美地

518131

+86 13113668890

+86 755 29812080

$id$

$date$

摘要

这里所谈的内容是对备份数据的恢复,不是对损坏数据表的恢复,或者说灾难恢复。

<a></a>

我的系列文档

<a target="_top" href="http://netkiller.github.io/architect/index.html">netkiller architect 手札</a>

<a target="_top" href="http://netkiller.github.io/developer/index.html">netkiller developer 手札</a>

<a target="_top" href="http://netkiller.github.io/php/index.html">netkiller php 手札</a>

<a target="_top" href="http://netkiller.github.io/python/index.html">netkiller python 手札</a>

<a target="_top" href="http://netkiller.github.io/testing/index.html">netkiller testing 手札</a>

<a target="_top" href="http://netkiller.github.io/cryptography/index.html">netkiller cryptography 手札</a>

<a target="_top" href="http://netkiller.github.io/linux/index.html">netkiller linux 手札</a>

<a target="_top" href="http://netkiller.github.io/centos/index.html">netkiller centos 手札</a>

<a target="_top" href="http://netkiller.github.io/freebsd/index.html">netkiller freebsd 手札</a>

<a target="_top" href="http://netkiller.github.io/security/index.html">netkiller security 手札</a>

<a target="_top" href="http://netkiller.github.io/version/index.html">netkiller version 手札</a>

<a target="_top" href="http://netkiller.github.io/www/index.html">netkiller web 手札</a>

<a target="_top" href="http://netkiller.github.io/monitoring/index.html">netkiller monitoring 手札</a>

<a target="_top" href="http://netkiller.github.io/storage/index.html">netkiller storage 手札</a>

<a target="_top" href="http://netkiller.github.io/mail/index.html">netkiller mail 手札</a>

<a target="_top" href="http://netkiller.github.io/shell/index.html">netkiller shell 手札</a>

<a target="_top" href="http://netkiller.github.io/network/index.html">netkiller network 手札</a>

<a target="_top" href="http://netkiller.github.io/database/index.html">netkiller database 手札</a>

<a target="_top" href="http://netkiller.github.io/postgresql/index.html">netkiller postgresql 手札</a>

<a target="_top" href="http://netkiller.github.io/mysql/index.html">netkiller mysql 手札</a>

<a target="_top" href="http://netkiller.github.io/nosql/index.html">netkiller nosql 手札</a>

<a target="_top" href="http://netkiller.github.io/ldap/index.html">netkiller ldap 手札</a>

<a target="_top" href="http://netkiller.github.io/cisco/index.html">netkiller cisco ios 手札</a>

<a target="_top" href="http://netkiller.github.io/h3c/index.html">netkiller h3c 手札</a>

<a target="_top" href="http://netkiller.github.io/multimedia/index.html">netkiller multimedia 手札</a>

<a target="_top" href="http://netkiller.github.io/docbook/index.html">netkiller docbook 手札</a>

<a target="_top" href="http://netkiller.github.io/oss/index.html">netkiller 开源软件 手札</a>

目录

我们来假设一个场景。

公司决策你是不是因为数据持续增加,有些数据已经不会再查询,会删除旧的历史数据。

有时公司突然说要恢复历史数据,有可能全补回复,有可能部分恢复。

你将怎么做?

首先看看备份方式,你是不是采用这种方法备份

我使用一串数字表述数据库数据递增情况,数据的增长变化

垂直轴表示备份时间轴

最常见的备份方法,完全备份

下面这种备份方式也比较常见,这种方式很有规律。

更复杂的情况,无规律可循

以此类推,删除原因有多种,如空间不足,改善查询性能。。。等等

最杂的情况,无规律可循,同时交叉数据可能会有更新

我用'o' 表示与上次备份中有差异的部分。

,最好恢复,第二种。

上面所提三种备份方式

第一种

最好恢复,100% 都能搞定.

第二种

恢复起来稍复杂,仍能搞得定.

第三种

比较复杂,因为本档案中存在重复记录,费点脑筋

第四种

最复杂,看似复杂,其实也不复杂,跟第三种差不多.

这种备份非常简单,菜鸟也搞搞定

文本格式回复

压缩格式恢复

或者先使用gunzip解压,再恢复数据

很多人喜欢用tar打包,我不见这样做,一个文件时无需使用tar打包的,画蛇添足

仅使用gzip压缩,可以方便使用zcat直接操作文件。

这种备份时连续的,只要依次按顺序恢复即可。

也可以跳跃恢复数据

反向恢复数据

总之怎么恢复都可以

这种恢复建议按照顺序进行,即可以顺时间轴恢复也可以逆时间轴,条件是表结构需要有主键(pk)

正时序恢复案例,

逆时序恢复数据

因为有主键,所以已存在的重复记录不会被重复插入。

必须是

不能是

这种恢复必须按照顺序进行,即可以顺时间轴恢复也可以逆时间轴,但处理上稍有不同.一旦操作错误数据就会损坏,同时也有很多条件。

顺时序恢复数据, 只需将 insert 替换为 replace 即可

新数据总会覆盖旧数据

但逆向就不同了,逆时序恢复数据与上面第三种相同, 恢复过程中旧数据在 insert 的时候不会覆盖现有的新数据。仅仅将失去的数据恢复到数据库中。

操作要十分谨慎,理解正向与逆向的不同,方能操作。

有时上面所讲的四种恢复方法不能满足你需求,我们模拟一个场景,假如你需要恢复一个时间段的数据,或者id字段去一个范围等等,上面所举例子均为一刀切。该怎么办呢?

不用担心方法总是有的

insert ... select

replace ... select

例 1. insert ... select

这里仅给一个简单实例,因为每个人的需求都不同,你只需灵活变通,发挥你的想象力。