added .env export function

This commit is contained in:
Merlin Dienst 2026-06-09 15:05:38 +02:00
parent b1d40f3409
commit 75299998f3
7 changed files with 67 additions and 2 deletions

View file

@ -57,6 +57,11 @@ from open_webui.internal.config import (
def get_config():
return _state.snapshot
def get_config_as_envs():
all_configs = ""
for config in PERSISTENT_CONFIG_REGISTRY:
all_configs = f'{all_configs}{config.get_as_env()}\n'
return all_configs
def save_to_db(data):
_state.persist(data)

View file

@ -175,6 +175,13 @@ class ConfigVar:
self.config_value = self.value
await STATE.persist_async()
def get_as_env(self) -> str:
if isinstance(self.value, list):
value = ",".join(map(str, self.value))
else:
value = str(self).replace('\n', '\\n').replace('"', '\\"')
return f'{self.env_name}="{value}"'
# ── AppConfig ──────────────────────────────────────────────────────────

View file

@ -6,8 +6,9 @@ from typing import Optional
import aiohttp
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import PlainTextResponse
from mcp.shared.auth import OAuthMetadata
from open_webui.config import BannerModel, async_save_config, get_config, save_config
from open_webui.config import BannerModel, async_save_config, get_config, get_config_as_envs, save_config
from open_webui.env import AIOHTTP_CLIENT_SESSION_SSL, AIOHTTP_CLIENT_TIMEOUT
from open_webui.models.oauth_sessions import OAuthSessions
from open_webui.utils.auth import get_admin_user, get_verified_user
@ -62,6 +63,10 @@ async def import_config(request: Request, form_data: ImportConfigForm, user=Depe
async def export_config(user=Depends(get_admin_user)):
return get_config()
@router.get('/export/env')
async def export_config_as_env(user=Depends(get_admin_user)):
return PlainTextResponse(get_config_as_envs(), media_type='text/plain')
############################
# Connections Config

View file

@ -58,6 +58,33 @@ export const exportConfig = async (token: string) => {
return res;
};
export const exportConfigAsEnv = async (token: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/configs/export/env`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.text();
})
.catch((err) => {
console.error(err);
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
export const getConnectionsConfig = async (token: string) => {
let error = null;

View file

@ -8,7 +8,7 @@
import { toast } from 'svelte-sonner';
import { getAllUserChats } from '$lib/apis/chats';
import { getAllUsers } from '$lib/apis/users';
import { exportConfig, importConfig } from '$lib/apis/configs';
import { exportConfig, exportConfigAsEnv, importConfig } from '$lib/apis/configs';
const i18n = getContext('i18n');
@ -113,6 +113,25 @@
</button>
</div>
</div>
<div>
<div class="py-0.5 flex w-full justify-between">
<div class="self-center text-xs">{$i18n.t('Export Config as .env')}</div>
<button
class="p-1 px-3 text-xs flex rounded-sm transition"
on:click={async () => {
const config = await exportConfigAsEnv(localStorage.token);
const blob = new Blob([config], {
type: 'application/txt'
});
saveAs(blob, `config-${Date.now()}.env`);
}}
type="button"
>
<span class="self-center">{$i18n.t('Export')}</span>
</button>
</div>
</div>
</div>
{#if $config?.features.enable_admin_export ?? true}

View file

@ -903,6 +903,7 @@
"Export chat (.json)": "Chat exportieren (.json)",
"Export Chats": "Chats exportieren",
"Export Config": "Konfiguration exportieren",
"Export Config as .env": "Konfiguration als .env exportieren",
"Export Models": "Modelle exportieren",
"Export Prompts": "Prompts exportieren",
"Export to CSV": "Nach CSV exportieren",

View file

@ -903,6 +903,7 @@
"Export chat (.json)": "",
"Export Chats": "",
"Export Config": "",
"Export Config as .env": "",
"Export Models": "",
"Export Prompts": "",
"Export to CSV": "",