将django数据库从本地计算机还原到服务器的防弹方法是什么?

问题描述 投票:0回答:2

我没有在这个问题上找到大量的指导 - 当我找到指导时,没有太多可以解释我遇到的一些问题。我正在自学Python和Django,这很好,但部署到服务器是......地狱。如果有人在那里有一个用于备份Django DB并恢复到服务器的防弹工作流程,那将是很棒的。参考虚拟环境,迁移,--fake和-initial。

为了我自己 - 以及任何阅读的人 - 我将完全按照我所做的去做,看看我是否能看出错误在哪里 - 因为我正处于砖墙阶段。

The Problem

“它可以在我的机器上运行”几乎总结了我的位置。它工作正常。在我的机器上。

我已经设法将数据库(Postgresql)恢复到我的服务器 - Python Anywhere。我的Django服务器实例正在与数据库通信,并且正在提供一些数据。但是,有一些迁移问题仍然存在,并最终导致某些表不存在的致命错误 - 如下所示:

Column podcast_show.type does not exist
LINE 1: SELECT "podcast_show"."id", "podcast_show"."type", "podcast_...

运行makemigrations / migrate会让我遇到类似这样的错误

django.db.utils.IntegrityError: null value in column "id" violates not-null constraint
DETAIL:  Failing row contains (null, curate, 0005_item_tags, 2019-04-29 12:38:32.000168+00).

The Environment

我在Windows上,在Atom上进行本地开发。我有一个安装了Django 2.1的虚拟环境。我正在使用GIT进行版本控制(我只是理解),并且我有一些pypy安装了django应用程序(在virtualENV中),现在考虑它,我已经做了直接更改。

这是我认为我已经破解它的第一个“啊哈”时刻。我在myLocalVirtualEnv(包括模型)中对pypy包所做的更改未包含在我推送到服务器的GIT仓库中。因此,当我从服务器上的requirements.txt安装了所有依赖项时,我已经安装了这些应用程序,而不是修改后的版本。

*问题1)避免这种情况的正确工作流程是什么? *一旦我开始修改pypy包,这是一个依赖项,我应该将它从virtualenv中移出,并进入我的标准django文件夹结构,并将其作为我自己的应用程序进行处理?

The Backup

这似乎很容易。我在我的本地机器上使用了pgAdmin4 - 右键单击​​,备份,选择输出,然后关闭它。我现在正在做这件事 - 来自我在当地的工作项目。

完成。我现在有一个大型数据库(3GB未压缩 - 这是巨大的?)数据库文件'appBU'。我现在使用SFTP(filezilla)将此文件发送到我的服务器,将其放在与我的顶级应用程序级别相同的目录中。家庭/用户/应用程序/应用程序

现在在postgresql控制台中,我创建了一个新的数据库。

CREATE DATABASE app;

然后我为数据库创建一个用户。我并不总是这样做,如果我放弃了数据库并尝试再次备份,我只是使用预先存在的用户。在这种情况下,我运行命令DROP DATABASE app;然后是CREATE DATABASE app;所以我准备好了一个新的数据库来恢复。无论如何,用户:

CREATE USER username WITH PASSWORD 'password';

ALTER ROLE username WITH PASSWORD client_encoding TO 'utf8';
ALTER ROLE username SET default_transaction_isolation TO 'read committed';
ALTER ROLE username SET timezone TO 'UTC';

GRANT ALL PRIVILEGES ON DATABASE app TO username;

现在我将数据库恢复到我刚创建的新创建的空数据库 - 这是来自bash控制台(而不是psql控制台 - 需要一段时间来锻炼!)

pg_restore -h the-host-address-for-my-postgresql-database -p 11111 -U super -W  -C app < appBU

这就是问题开始的地方,因为pg_restore一直不高兴,因为它旋转了,这些是我得到的错误:


pg_restore: [archiver (db)] Error from TOC entry 3347; 0 0 SEQUENCE SET django_comment_flags_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.django_comment_flags_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_comment_flags_id_seq...
                                 ^
    Command was: SELECT pg_catalog.setval('public.django_comment_flags_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3348; 0 0 SEQUENCE SET django_comments_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.django_comments_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_comments_id_seq', 17...
                                 ^
    Command was: SELECT pg_catalog.setval('public.django_comments_id_seq', 17, true);
pg_restore: [archiver (db)] Error from TOC entry 3349; 0 0 SEQUENCE SET django_content_type_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.django_content_type_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_content_type_id_seq'...
                                 ^
    Command was: SELECT pg_catalog.setval('public.django_content_type_id_seq', 22, true);
pg_restore: [archiver (db)] Error from TOC entry 3350; 0 0 SEQUENCE SET django_migrations_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.django_migrations_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_migrations_id_seq', ...
                                 ^
    Command was: SELECT pg_catalog.setval('public.django_migrations_id_seq', 108, true);
pg_restore: [archiver (db)] Error from TOC entry 3351; 0 0 SEQUENCE SET django_site_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.django_site_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_site_id_seq', 1, tru...
                                 ^
pg_restore: [archiver (db)] Error from TOC entry 3352; 0 0 SEQUENCE SET podcast_category_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_category_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_category_id_seq', 1...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_category_id_seq', 1098, true);



pg_restore: [archiver (db)] Error from TOC entry 3353; 0 0 SEQUENCE SET podcast_enclosure_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_enclosure_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_enclosure_id_seq', ...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_enclosure_id_seq', 1, false);



pg_restore: [archiver (db)] Error from TOC entry 3354; 0 0 SEQUENCE SET podcast_episode_guests_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_episode_guests_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_episode_guests_id_s...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_episode_guests_id_seq', 1, false);



pg_restore: [archiver (db)] Error from TOC entry 3355; 0 0 SEQUENCE SET podcast_episode_hosts_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_episode_hosts_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_episode_hosts_id_se...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_episode_hosts_id_seq', 1, false);



pg_restore: [archiver (db)] Error from TOC entry 3356; 0 0 SEQUENCE SET podcast_episode_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_episode_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_episode_id_seq', 19...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_episode_id_seq', 1995295, true);


pg_restore: [archiver (db)] Error from TOC entry 3357; 0 0 SEQUENCE SET podcast_show_categories_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_show_categories_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_show_categories_id_...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_show_categories_id_seq', 3873, true);
pg_restore: [archiver (db)] Error from TOC entry 3358; 0 0 SEQUENCE SET podcast_show_hosts_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_show_hosts_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_show_hosts_id_seq',...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_show_hosts_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3359; 0 0 SEQUENCE SET podcast_show_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_show_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_show_id_seq', 27121...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_show_id_seq', 27121, true);
pg_restore: [archiver (db)] Error from TOC entry 3360; 0 0 SEQUENCE SET podcast_show_itunes_categories_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_show_itunes_categories_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_show_itunes_categor...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_show_itunes_categories_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3361; 0 0 SEQUENCE SET podcast_speaker_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.podcast_speaker_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_speaker_id_seq', 1,...
                                 ^
    Command was: SELECT pg_catalog.setval('public.podcast_speaker_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3362; 0 0 SEQUENCE SET taggit_tag_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.taggit_tag_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.taggit_tag_id_seq', 49920, ...
                                 ^
    Command was: SELECT pg_catalog.setval('public.taggit_tag_id_seq', 49920, true);


pg_restore: [archiver (db)] Error from TOC entry 2912; 2604 34102 DEFAULT taggit_taggeditem id postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.taggit_taggeditem_id_seq" does not exist
    Command was: ALTER TABLE ONLY public.taggit_taggeditem ALTER COLUMN id SET DEFAULT nextval('public.taggit_taggeditem_id_seq'::regclass);

但是 - 如果我现在进入我的域,我可以看到数据存在并被Django访问/提供。持续存在的错误会导致某些页面查询上述某些数据。如果我尝试makemigrations / migrate,我得到这个:

django.db.utils.IntegrityError: null value in column "id" violates not-null constraint
DETAIL:  Failing row contains (null, curate, 0006_auto_20190429_1108, 2019-04-29 15:50:36.927028+00).

为了记录,它所指的'podcast'应用程序是我的virtualenv修改过的应用程序。但是,在这种情况下,我将它从virtualenv中移出并将其重新定位到我的项目中的git repo - 用它传输所有代码。

有谁知道发生了什么?

我应该补充一下,代码库通过git更新到pythonanywhere。我在Atom上进行更改,然后提交它们。然后将它们推送到github,然后使用bash命令将它们下拉到服务器。

python django postgresql backup restore
2个回答
0
投票

pypy(python编译器)和PyPI(python包索引)之间有很大的区别。我假设你的意思是后者。对于您需要修改的第三方软件包,您应该分叉它们,进行更改并将它们推送到您的分支;最后将链接放在您的requirements.txt中的fork中。这是一个维护问题,所以如果你可以通过子类化等获得相同的效果,那么这通常是一个更好的策略。

最重要的是,如果你没有运行相同的代码,其他任何东西肯定都不会稳定。

3GB(未压缩)对于数据库来说不是很大。在某些文件系统上,超过4GB的文件大小可能会出现问题,因此将来可能会压缩/拆分备份。

我对postgresql了解不多,但总的来说,在进行跨平台备份和恢复时,需要非常谨慎地选择格式和工具。在恢复执行没有错误之前,绝对不能继续前进。

看起来您可能有数据/约束错误(“空值...违反非空约束”)。您应该在备份之前在数据库中修复这些错误。

看起来你对postgresql自动增量序列有一些问题,虽然我不知道如何在postgresql中修复它。

我会认真地建议长期看看pg_dump / pg_restore,而不是只有一半工作的gui解决方案,参见https://www.postgresql.org/docs/9.1/backup-dump.htmlRestore a postgres backup file using the command line?

如果你想要一个完整的gui解决方案,那就是Navicat(https://www.navicat.com/en/products/navicat-premium - 我是一个满意的用户),至少在我使用的数据库上,这使得这是一个全选,复制和粘贴操作。 JetBrains的DataGrip看起来也很有能力(https://www.jetbrains.com/datagrip/),虽然我只玩过它。

ps:在本地计算机上,您应该执行makemigrations并在备份之前进行迁移。当您在prod服务器上重复这些操作时,应该没有任何操作(因为您已经“复制”了当前版本的数据库状态)。


0
投票

感谢大家的帮助。我一丝不苟地完成了我的项目,对迁移进行了全面的“重置”,因此我的本地机器上完全没有问题。

最后,我发现PythonAnywhere正在运行PSQL 9.4并且我正在运行PSQL 11.2。我下载了PSQL 9.4并尝试在本地备份所有,每次都会收到150个错误。 PythonAnywhere没有计划在一段时间内升级他们的PSQL版本,我在开发过程中生成的数据非常有价值,所以我想保留它。

因此,我有两个选择。要么我可以尝试使用MYSQL工作台迁移到MYSQL,希望保持我的数据,或者我可以删除我完全生成的数据并坚持使用PSQL 9.4并重新开始。

© www.soinside.com 2019 - 2024. All rights reserved.