简体   繁体   English

PostgreSQL-意外删除数据文件后恢复功能代码

[英]PostgreSQL - Recovery of Functions' code following accidental deletion of data files

So, I am (well... I was) running PostgreSQL within a container (Ubuntu 14.04LTS with all the recent updates, back-end storage is "dir" because of convince). 因此,我(嗯……我在)在一个容器中运行PostgreSQL(Ubuntu 14.04LTS具有所有最近的更新,由于说服,后端存储为“ dir”)。

To cut the long story short, the container folder got deleted. 长话短说,容器文件夹被删除了。 Following the use of extundelete and ext4magic, I have managed to extract some of the database physical files (it appears as if most of the files are there... but not 100% sure if and what is missing). 使用extundelete和ext4magic之后,我设法提取了一些数据库物理文件(看起来好像大多数文件都在其中……但不是100%确定是否丢失了什么)。

I have two copies of the database files. 我有数据库文件的两个副本。 One from 9.5.3 (which appears to be more complete) and one from 9.6 (I upgraded the container very recently to 9.6, however it appears to be missing datafiles). 一个来自9.5.3(似乎更完整),另一个来自9.6(我最近将容器升级到9.6,但是它似乎缺少数据文件)。

All I am after is to attempt and extract the SQL code the relates to the user defined functions. 我要做的只是尝试提取与用户定义函数有关的SQL代码。 Is anyone aware of an approach that I could try? 有谁知道我可以尝试的方法?

PS: Last backup is a bit dated (due to bad practices really) so it would be last resort if the task of extracting the needed information is "reasonable" and "successful". PS:上次备份有些过时(实际上是由于不良做法),因此,如果提取所需信息的任务“合理”和“成功”,则将是最后的选择。

Regards, G 问候,G

Update - 20/4/2017 I was hoping for a "quick fix" by somehow extracting the function body text off the recovered data files... however, nothing's free in this life :) 更新-20/4/2017我希望通过某种方式从恢复的数据文件中提取功能主体文本来进行“快速修复”,但是,这一生没有什么免费的了:)

Starting from the old-ish backup along with the recovered logs, we managed to cover a lot of ground into bringing the DB back to life. 从旧的备份以及恢复的日志开始,我们设法覆盖了很多基础,使数据库恢复了生命。

Lessons learned : 获得的经验
1. Do implement a good backup/restore strategy 1. 不要实施好备份/恢复策略
2. Do not store backups on the same physical machine 2. 不要将备份存储在同一台物理计算机上
3. Hardware failure can be disruptive ... Human error can be disastrous! 3.硬件故障可能具有破坏性 。人为错误可能造成灾难性后果!

If you can reconstruct enough of a data directory to start postgres in single user mode you might be able to dump pg_proc. 如果您可以重构足够的数据目录以在单用户模式下启动postgres,则可以转储pg_proc。 But this seems unlikely. 但这似乎不太可能。

Otherwise, if you're really lucky you'll be able to find the relation for pg_proc and its corresponding pg_toast relation . 否则,如果您真的很幸运,您将能够找到pg_proc的关系及其对应的pg_toast关系 The latter will often contain compressed text, so searches for parts of variables you know appear in function bodies may not help you out. 后者通常包含压缩文本,因此搜索您知道出现在函数主体中的变量部分可能对您没有帮助。

Anything stored inline in pg_proc will be short functions, significantly less than 8k long. 内联存储在pg_proc任何东西都是短函数,明显少于8k长。 Everything else will be in the toast relation. 其他所有内容都将在敬酒关系中。

To decode that you have to unpack the pages to get the toast hunks, then reassemble them and uncompress them (if compressed). 要解码,您必须解压缩页面以获得烤面包块,然后重新组装它们并解压缩它们(如果已压缩)。

If I had to do this, I would probably create a table with the exact same schema as pg_proc in a new postgres instance of the same version. 如果必须这样做,我可能会在相同版本的新postgres实例中创建一个与pg_proc完全相同的架构的pg_proc I would then find the relfilenode(s) for pg_catalog.pg_proc and its toast table using the relfilenode map file (if it survived) or by pattern matching and guesswork. 然后,我将使用relfilenode映射文件(如果幸存下来)或通过模式匹配和猜测来找到pg_catalog.pg_proc的relfilenode及其吐司表。 I would replace the empty relation files for the new table I created with the recovered ones, restart postgres, and if I was right, I'd be able to select from the tables. 我将用恢复的表替换我创建的新表的空关系文件,重新启动postgres,如果我是对的,则可以从表中进行select

Not easy. 不容易。

I suggest reading up on postgres's storage format as you'll need to understand it. 我建议您阅读postgres的存储格式,因为您需要了解它。

You may consider https://www.postgresql.org/support/professional_support/ . 您可以考虑https://www.postgresql.org/support/professional_support/ (Disclaimer, I work for one of the listed companies). (免责声明,我为其中一家上市公司工作)。

PS: Last backup is a bit dated (due to bad practices really) so it would be last resort if the task of extracting the needed information is "reasonable" and "successful". PS:上次备份有些过时(实际上是由于不良做法),因此,如果提取所需信息的任务“合理”和“成功”,则将是最后的选择。

Backups are your first resort here. 备份是您的首要选择。

If the 9.5 files are complete and undamaged (or enough so to dump the schema) then simply copying them in place, checking permissions and starting the server will get you going. 如果9.5文件完整且未损坏(或足以转储架构),则只需将它们复制到位,检查权限并启动服务器即可开始工作。 Don't trust the data though, you'll need to check it all. 但是,不要信任数据,您需要检查所有数据。

Although it is possible to partially recover given damaged files, it's a long complicated process and the fact that you are asking on Stack Overflow probably means it's not for you. 虽然可以部分恢复给予损坏的文件,这是一个漫长复杂的过程,你要求对堆栈溢出的事实可能意味着它不适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM