简体   繁体   English

备份 AWS RDS Postgres 实例中的特定表

[英]Backup specific tables in AWS RDS Postgres Instance

I have two databases on Amazon RDS, both Postgres.我在 Amazon RDS 上有两个数据库,都是 Postgres。 Database 1 and 2数据库 1 和 2

I need to restore an instance from a snapshot of Database 1 for my Staging environment.我需要从数据库 1 的快照为我的暂存环境恢复一个实例。 (Database 2 is my current Staging DB). (数据库 2 是我当前的暂存数据库)。

However, I want the data from a few of the tables in Database 2 to overwrite the tables in the newly restored snapshot.但是,我希望数据库 2 中一些表中的数据覆盖新恢复的快照中的表。 What is the best way to do this?做这个的最好方式是什么?

When restoring RDS from a Snapshot, a new database instance is created. 从快照还原RDS时,将创建一个新的数据库实例。 If you only wish to copy a portion of the snapshot: 如果只希望复制快照的一部分,请执行以下操作:

  • Restore the snapshot to a new (temporary) database 快照还原到新的(临时)数据库
  • Connect to the new database and dump the desired tables using pg_dump 连接到新数据库并使用pg_dump 转储所需的表
  • Connect to your staging server and restore the tables using pg_restore (most probably deleting any matching existing tables first) 连接到登台服务器并使用pg_restore 还原表(最有可能首先删除任何匹配的现有表)
  • Delete the temporary database 删除临时数据库

pg_dump actually outputs SQL commands that are then used to recreate tables and restore data. pg_dump实际上输出SQL命令,然后这些命令用于重新创建表和恢复数据。 Look at the content of a dump to understand how the restore process actually works. 查看转储的内容,以了解还原过程实际上是如何工作的。

I hope this still works for someone else.我希望这对其他人仍然有效。

With my team we faced a similar issue.在我的团队中,我们遇到了类似的问题。 We also had 2 Postgres databases and we also just needed to backup some tables from db1 to db2.我们还有 2 个 Postgres 数据库,我们也只需要将一些表从 db1 备份到 db2。

What we did is to use a lambda function using Python (from AWS lambda ofc) that connected to both databases and validates if db1.table1 has the same data as db2.table1 , if not, then the lambda function should write the missing data from db1.table1 into db2.table1 .我们所做的是使用使用Python lambda函数(从AWS拉姆达OFC),其连接到这两个数据库,并验证是否db1.table1具有相同的数据db2.table1 ,如果没有,那么lambda函数应该写从丢失数据db1.table1db2.table1 The approach of using lambda was because we wanted to automate the process due to the main db (let's say db1 ) is constantly being updated.使用 lambda 的方法是因为我们想要自动化该过程,因为主数据库(假设db1 )不断更新。 In addition, it allowed us to only backup our desired tables (let's say 3 tables out of 10), instead of backing up the whole database.此外,它允许我们只备份我们想要的表(假设 10 个表中有 3 个),而不是备份整个数据库。

Note: Maybe you want to do these writes using temporary tables to avoid issues with any constraints you have in your tables.注意:也许您想使用临时表进行这些写入,以避免表中的任何约束出现问题。

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

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