简体   繁体   English

Django + PostgreSQL 在 EC2 实例上备份和恢复

[英]Django + PostgreSQL backup and restore on an EC2 instance

I have a simple monolithic architecture:我有一个简单的单体架构:

A Django project hosted on an EC2 instance, talks to a PostgreSQL DB running on the same instance.托管在 EC2 实例上的 Django 项目与运行在同一实例上的 PostgreSQL 数据库通信。 I chose this architecture considering the traffic and cost.考虑到流量和成本,我选择了这种架构。 So, don't bash me on this one.所以,不要在这个上给我 bash 我。 :) :)

For disaster recovery, I regularly dump my DB (a full dump pg_dump -U postgres fishercoder_db > fishercoder_dump.sql ).为了灾难恢复,我定期转储我的数据库(完整转储pg_dump -U postgres fishercoder_db > fishercoder_dump.sql )。

At restoring, I cannot get Django and the restoring DB to talk nicely with each other:在恢复时,我无法让 Django 和恢复数据库彼此很好地交谈:

  1. If I launch Django and run ./manage.py migrate first, and then restore the DB from the dump, it fails because Django has already created a bunch of internal tables after running ./manage.py migrate which have exactly the same name of in my dump;如果我启动 Django 并首先运行./manage.py migrate ,然后从转储中恢复数据库,它会失败,因为 Django 在运行./manage.py migrate后已经创建了一堆内部表,它们的名称完全相同在我的垃圾场;

  2. If I restore the DB from the dump first, then my Django app cannot stand up because of insufficientprivilege to run ./manage.py migrate , details asked here .如果我先从转储中恢复数据库,那么我的 Django 应用程序由于运行./manage.py migrate的权限不足而无法正常运行,详情请点击此处

My question is:我的问题是:

  1. Is my DR strategy reasonable?我的灾难恢复策略合理吗? Any other more optimal ways?还有其他更优化的方法吗?

  2. How can I get this approach to work: restore my site on a new EC2 instance with DB restored from a.sql dump.如何使这种方法起作用:在新的 EC2 实例上恢复我的站点,并使用从 a.sql 转储中恢复的数据库。

I wrote my own set of utils for exactly this, as I could never fully remember the command line options necessary: https://github.com/FlipperPA/django-pg-copy我为此编写了自己的一组实用程序,因为我永远无法完全记住必要的命令行选项: https://github.com/FlipperPA/django-pg-copy

Have you tried pg_restore with these options?您是否尝试过使用这些选项的pg_restore

pg_restore -c --if-exists -h localhost -U [your_django_user] -d [your_django_db]  fishercoder_dump.sql

You may also want to consider some additional parameters for your dump:您可能还需要为转储考虑一些附加参数:

pg_dump -Fc -c -x ...

-Fc : custom format -c : (or --clean ): dumps a clean version, with DROPs of objects. -Fc :自定义格式-c :(或--clean ):转储干净的版本,带有 DROPs 对象。 -x : skip dumping access privileges -x :跳过转储访问权限

Good luck!祝你好运!

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

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