使用 PostgreSQL 存储
建议在生产环境中部署 PostgreSQL。它支持并发访问,可以启用多个管理实例以实现高可用性。
配置
组合设置(config.yaml)
要使用 PostgreSQL,请更新您的config.yaml:
shell
server:
store:
engine: "postgres"
dsn: "host=<PG_HOST> user=<PG_USER> password=<PG_PASSWORD> dbname=<PG_DB_NAME> port=<PG_PORT>"
encryptionKey: "your_encryption_key"WARNING
encryptionKey很重要,它用于加密和解密您的数据,如果未配置每次启动会生成一个随机密钥,这会导致之前的数据无法解密,控制台将会报错。
您还可以将 DSN 作为NB_环境变量传递给netbird-server容器docker-compose.yml:
shell
environment:
- NB_STORE_ENGINE_POSTGRES_DSN=host=<PG_HOST> user=<PG_USER> password=<PG_PASSWORD> dbname=<PG_DB_NAME> port=<PG_PORT>重启服务器并确认:
shell
docker compose restart netbird-server
docker compose logs netbird-server你应该看看:
shell
INFO management/server/store.go:109: using Postgres store engine将 SQLite 存储迁移到 Postgres 存储
此迁移过程允许用户在保持数据完整性的同时,无缝地在不同存储选项之间进行过渡。
- 备份您的数据存储(
store.db默认)datadir/var/lib/netbird/对于组合配置:
shell
mkdir backup
docker compose cp -a netbird-server:/var/lib/netbird/. backup/- 将 SQLite 数据导入 Postgres 迁移 SQLite 数据时,我们使用pgloadersudo apt-get install pgloader工具。您可以在 Debian 或macOS 系统上运行命令来安装它
brew install pgloader。
shell
pgloader --type sqlite backup/store.db "postgresql://<PG_USER>:<PG_PASSWORD>@<PG_HOST>:<PG_PORT>/<PG_DB_NAME>"- 解决迁移过程中可能出现的任何兼容性问题。 为了解决这个问题,您可以在 PostgreSQL 数据库中执行以下 SQL 命令:
sql
DELETE FROM policy_rules WHERE policy_rules.policy_id NOT IN (SELECT id FROM policies);
DELETE FROM routes WHERE routes.account_id NOT IN (SELECT id FROM accounts);
DELETE FROM name_server_groups WHERE name_server_groups.account_id NOT IN (SELECT id FROM accounts);
DROP TABLE IF EXISTS rules;- 在配置中启用Postgres。 更新
config.yaml:
shell
server:
store:
engine: "postgres"
dsn: "host=<PG_HOST> user=<PG_USER> password=<PG_PASSWORD> dbname=<PG_DB_NAME> port=<PG_PORT>"
encryptionKey: "your_encryption_key"WARNING
encryptionKey很重要,它用于加密和解密您的数据,如果未配置每次启动会生成一个随机密钥,这会导致之前的数据无法解密,控制台将会报错。
- 重启服务器并确认:
shell
docker compose restart netbird-server
docker compose logs netbird-server您应该会看到类似这样的条目:
shell
INFO management/server/store.go:109: using Postgres store engine回滚到 SQLite 存储
要回滚到 SQLite 存储,请按照以下步骤操作:
恢复store.db备份
shell
docker compose cp backup/. netbird-server:/var/lib/netbird/- 将数据存储引擎切换回 SQLite。
shell
server:
store:
engine: "sqlite"- 重启服务器并确认:
shell
docker compose restart netbird-server
docker compose logs netbird-server您应该会看到类似这样的条目:
shell
INFO management/server/store.go:109: using SQLite store engine可选操作:将 Postgres 数据回滚到 SQLite
此步骤为可选步骤,仅当您希望在运行相同 NetBird 版本的情况下将数据从 Postgres 回滚到 SQLite 时才应使用。迁移 Postgres 数据需要使用 git config、git configpg_dump和sedgit config 等sqlite3工具。请确保在继续操作之前已安装这些工具。
- 导出Postgres数据
shell
pg_dump --data-only --column-inserts "postgresql://<PG_USER>:<PG_PASSWORD>@<PG_HOST>:<PG_PORT>/<PG_DB_NAME>" > data.sql- 将导出的 Postgres 数据 SQL 转换为 SQLite 格式
shell
sed \
-e 's/\\\\:/\:/g' \
-e 's/\\\\//g' \
-e 's/\\\\;/;/g' \
-e '/^SET /d' \
-e '/setval/d' \
-e "s/'true'/1/g" \
-e "s/'false'/0/g" \
-e 's/public\.//' \
-e '/^[[:space:]]*SELECT/d' data.sql > data.sql- 从 SQLite 备份生成数据库架构
shell
sqlite3 backup/store.db '.schema' > schema.sql- 使用从 Postgres 导出的数据创建 SQLite 数据库
shell
sqlite3 store.db '.read schema.sql' && sqlite3 store.db '.read data.sql'- 将数据库复制到容器
shell
docker compose cp store.db netbird-server:/var/lib/netbird/store.db6.按照上面“回滚到 SQLite 存储”部分中的说明,将存储引擎切换回 SQLite 。
