如何解决 pg_dump:由于使用 docker 的服务器版本不匹配而中止

时间:2021-06-24 02:28:20

标签: django postgresql docker

在我按照computerforgeeks.com/install-postgresql-on-debian-linux 上的说明操作后它起作用了,但我今天再次尝试运行python manage.py dbbackup,但它不起作用。

我不明白为什么当路径与来自 No such file or directory: '/root/usr/bin/pg_dump' 的路径匹配时会显示 find . -name pg_dump

shell 到 postgres 容器

root@6d515751e154:/# pg_dump --version
pg_dump (PostgreSQL) 13.3 (Debian 13.3-1.pgdg100+1)
root@6d515751e154:/# find . -name pg_dump
./usr/lib/postgresql/13/bin/pg_dump
./usr/bin/pg_dump

base.py

DATABASES = {
    "default": {
        "ENGINE": os.environ.get("SQL_ENGINE"),
        "NAME": os.environ.get("SQL_DATABASE"),
        "USER": os.environ.get("SQL_USER"),
        "PASSWORD": os.environ.get("SQL_PASSWORD"),
        "HOST": os.environ.get("SQL_HOST"),
        "PORT": os.environ.get("SQL_PORT"),
    }
}

DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage'
DBBACKUP_STORAGE_OPTIONS = {'location': os.path.join(BASE_DIR, '../backups')}
DBBACKUP_CONNECTORS = {
    "default": {
        "ENGINE": 'django.db.backends.postgresql_psycopg2',
        "NAME": os.environ.get("SQL_DATABASE"),
        "USER": os.environ.get("SQL_USER"),
        "PASSWORD": os.environ.get("SQL_PASSWORD"),
        "HOST": os.environ.get("SQL_HOST"),
        "PORT": os.environ.get("SQL_PORT"),
        # "DUMP_CMD": os.path.join( # Tried both of these
        #     os.environ['HOME'],
        #     "usr",
        #     "bin",
        #     "pg_dump"
        #     )
        "DUMP_CMD": os.path.join( # Tried both of these
            os.environ['HOME'],
            "usr",
            "lib",
            "postgresql",
            "13",
            "bin",
            "pg_dump"
            )
    }
}

回溯 1:

Backing Up Database: postgres
CommandConnectorError: Error running:  /root/usr/bin/pg_dump  --host=pgdb --port=5432 --username=postgres --no-password --clean postgres
[Errno 2] No such file or directory: '/root/usr/bin/pg_dump'
  File "/usr/local/lib/python3.8/site-packages/dbbackup/utils.py", line 118, in wrapper
    func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 61, in handle  
    self._save_new_backup(database)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 74, in _save_new_backup
    outputfile = self.connector.create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 78, in create_dump
    dump = self._create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 38, in _create_dump
    stdout, stderr = self.run_command(cmd, env=self.dump_env)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 21, in run_command
    return super(PgDumpConnector, self).run_command(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 156, in run_command
    raise exceptions.CommandConnectorError(

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 146, in run_command
    process = Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr, env=full_env)
  File "/usr/local/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/root/usr/bin/pg_dump'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute      
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv    
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/utils.py", line 118, in wrapper
    func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 61, in handle  
    self._save_new_backup(database)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 74, in _save_new_backup
    outputfile = self.connector.create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 78, in create_dump
    dump = self._create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 38, in _create_dump
    stdout, stderr = self.run_command(cmd, env=self.dump_env)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 21, in run_command
    return super(PgDumpConnector, self).run_command(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 156, in run_command
    raise exceptions.CommandConnectorError(
dbbackup.db.exceptions.CommandConnectorError: Error running:  /root/usr/bin/pg_dump  --host=pgdb --port=5432 --username=postgres --no-password --clean postgres
[Errno 2] No such file or directory: '/root/usr/bin/pg_dump'

回溯 2:

Backing Up Database: postgres
CommandConnectorError: Error running:  /root/usr/lib/postgresql/13/bin/pg_dump  --host=pgdb --port=5432 --username=postgres --no-password --clean postgres
[Errno 2] No such file or directory: '/root/usr/lib/postgresql/13/bin/pg_dump'
  File "/usr/local/lib/python3.8/site-packages/dbbackup/utils.py", line 118, in wrapper
    func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 61, in handle  
    self._save_new_backup(database)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 74, in _save_new_backup
    outputfile = self.connector.create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 78, in create_dump
    dump = self._create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 38, in _create_dump
    stdout, stderr = self.run_command(cmd, env=self.dump_env)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 21, in run_command
    return super(PgDumpConnector, self).run_command(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 156, in run_command
    raise exceptions.CommandConnectorError(

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 146, in run_command
    process = Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr, env=full_env)
  File "/usr/local/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/root/usr/lib/postgresql/13/bin/pg_dump'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute      
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv    
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/utils.py", line 118, in wrapper
    func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 61, in handle  
    self._save_new_backup(database)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 74, in _save_new_backup
    outputfile = self.connector.create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 78, in create_dump
    dump = self._create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 38, in _create_dump
    stdout, stderr = self.run_command(cmd, env=self.dump_env)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 21, in run_command
    return super(PgDumpConnector, self).run_command(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 156, in run_command
    raise exceptions.CommandConnectorError(
dbbackup.db.exceptions.CommandConnectorError: Error running:  /root/usr/lib/postgresql/13/bin/pg_dump  --host=pgdb --port=5432 --username=postgres --no-password --clean postgres
[Errno 2] No such file or directory: '/root/usr/lib/postgresql/13/bin/pg_dump'

以上更新

我在单独的 docker 容器上运行 django 服务器(图像 python:3.8.2-slim-buster)和 postgresql(图像 postgres:13)数据库。我正在尝试使用 django-dbbackup 备份数据并遇到此错误。

# python manage.py dbbackup
System check identified some issues:

WARNINGS:
account.EmailAddress: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
account.EmailConfirmation: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
socialaccount.SocialAccount: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
socialaccount.SocialApp: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
socialaccount.SocialToken: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the SocialAccountConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
Backing Up Database: postgres
CommandConnectorError: Error running:  pg_dump  --host=pgdb --port=5432 --username=postgres --no-password --clean postgres
pg_dump: server version: 13.3 (Debian 13.3-1.pgdg100+1); pg_dump version: 11.12 (Debian 11.12-0+deb10u1)
pg_dump: aborting because of server version mismatch

  File "/usr/local/lib/python3.8/site-packages/dbbackup/utils.py", line 118, in wrapper
    func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 61, in handle
    self._save_new_backup(database)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 74, in _save_new_backup
    outputfile = self.connector.create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 78, in create_dump
    dump = self._create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 38, in _create_dump
    stdout, stderr = self.run_command(cmd, env=self.dump_env)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 21, in run_command
    return super(PgDumpConnector, self).run_command(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 150, in run_command
    raise exceptions.CommandConnectorError(

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/utils.py", line 118, in wrapper
    func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 61, in handle
    self._save_new_backup(database)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/management/commands/dbbackup.py", line 74, in _save_new_backup
    outputfile = self.connector.create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 78, in create_dump
    dump = self._create_dump()
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 38, in _create_dump
    stdout, stderr = self.run_command(cmd, env=self.dump_env)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/postgresql.py", line 21, in run_command
    return super(PgDumpConnector, self).run_command(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/dbbackup/db/base.py", line 150, in run_command
    raise exceptions.CommandConnectorError(
dbbackup.db.exceptions.CommandConnectorError: Error running:  pg_dump  --host=pgdb --port=5432 --username=postgres --no-password --clean postgres
pg_dump: server version: 13.3 (Debian 13.3-1.pgdg100+1); pg_dump version: 11.12 (Debian 11.12-0+deb10u1)
pg_dump: aborting because of server version mismatch

在寻找答案后,我尝试更新 postgresql-client,但它说我已经有了最新版本。

root@528fdf5ac614:/code/mysite# apt-get install postgresql-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
postgresql-client is already the newest version (11+200+deb10u4).
0 upgraded, 0 newly installed, 0 to remove and 18 not upgraded.

所以我不知道如何继续。我应该使用 postgres:11.12 docker image 降级数据库容器吗?这意味着数据会丢失吗?

另外,为什么错误说 dbbackup.db.exceptions.CommandConnectorError: Error running: pg_dump --host=pgdb --port=5432 --username=postgres --no-password --clean postgres ? 我已经在 base.py 中设置了密码,如下所示:

base.py 设置

DATABASES = {
    "default": {
        "ENGINE": os.environ.get("SQL_ENGINE"),
        "NAME": os.environ.get("SQL_DATABASE"),
        "USER": os.environ.get("SQL_USER"),
        "PASSWORD": os.environ.get("SQL_PASSWORD"),
        "HOST": os.environ.get("SQL_HOST"),
        "PORT": os.environ.get("SQL_PORT"),
    }
}

DBBACKUP_CONNECTORS = {
    "default": {
        "USER": os.environ.get("SQL_USER"),
        "PASSWORD": os.environ.get("SQL_PASSWORD"),
        "HOST": os.environ.get("SQL_HOST"),
    }
}

1 个答案:

答案 0 :(得分:0)

你的错误不言自明

pg_dump: server version: 13.3 (Debian 13.3-1.pgdg100+1); pg_dump version: 11.12 (Debian 11.12-0+deb10u1)

您应该在 /postgres/bin/ 下找到用于此安装的 psql 和 pg_dump。这是您要使用的 pg_dump。您可以使用

验证 pg_dump 的版本
pg_dump --version

django-dbbackup 允许您指定用于创建备份的连接器。特别是,它允许您指定转储命令 (DUMP_CMD)。要指定连接器,请将以下块添加到 settings.py:

import os  # if not yet imported
DBBACKUP_CONNECTORS = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'xxx',
        'USER': 'xxx',
        'PASSWORD': 'xxx',
        'HOST': 'xxx',
        'PORT': 'xxx',
        'DUMP_CMD': os.path.join(
            os.environ["HOME"],
            'xxx',
            'bin',
            'pg_dump'
            )
        }
    }

根据您的安装和用户进行所需的更改。