feat(postgres): in-panel backup/restore and consistent CLI backend

Two PostgreSQL gaps on the panel:

1. x-ui setting and other CLI subcommands read XUI_DB_TYPE/XUI_DB_DSN from
   the process environment, which systemd injects via EnvironmentFile but a
   plain shell invocation does not. On a PostgreSQL install the CLI silently
   fell back to SQLite, so changes made from the management menu never
   reached the panel's database. Load the systemd EnvironmentFile
   (/etc/default/x-ui and distro equivalents) at startup; godotenv.Load does
   not override existing vars, so it stays a no-op for the managed service.

2. DB backup/restore (panel endpoints and the Telegram bot) only handled the
   SQLite file, so on PostgreSQL Back Up returned a stale/absent x-ui.db and
   Restore silently did nothing. Add pg_dump/pg_restore based backup/restore:
   - GetDb/ImportDB run pg_dump (custom format) / pg_restore, passing
     credentials via the PG* environment instead of argv.
   - getDb downloads x-ui.dump on Postgres, x-ui.db on SQLite.
   - Telegram backup sends the matching file via GetDb.
   - BackupModal shows a Postgres note and accepts .dump; the dist page
     injects window.X_UI_DB_TYPE; new strings translated for all locales.
   - install.sh installs postgresql-client for the external-DSN path and
     points the user to in-panel Backup & Restore.

Closes #4658
This commit is contained in:
MHSanaei
2026-05-31 17:53:34 +02:00
parent a2f20f85f3
commit cc34dc381c
24 changed files with 311 additions and 29 deletions
+43
View File
@@ -218,6 +218,41 @@ EOF
return 0
}
ensure_pg_client() {
if command -v pg_dump > /dev/null 2>&1 && command -v pg_restore > /dev/null 2>&1; then
return 0
fi
echo -e "${yellow}Installing PostgreSQL client tools (pg_dump/pg_restore) for in-panel backup...${plain}" >&2
case "${release}" in
ubuntu | debian | armbian)
apt-get update >&2 && apt-get install -y -q postgresql-client >&2 || return 1
;;
fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
dnf install -y -q postgresql >&2 || return 1
;;
centos)
if [[ "${VERSION_ID}" =~ ^7 ]]; then
yum install -y postgresql >&2 || return 1
else
dnf install -y -q postgresql >&2 || return 1
fi
;;
arch | manjaro | parch)
pacman -Sy --noconfirm postgresql >&2 || return 1
;;
opensuse-tumbleweed | opensuse-leap)
zypper -q install -y postgresql >&2 || return 1
;;
alpine)
apk add --no-cache postgresql-client >&2 || return 1
;;
*)
return 1
;;
esac
command -v pg_dump > /dev/null 2>&1 && command -v pg_restore > /dev/null 2>&1
}
install_acme() {
echo -e "${green}Installing acme.sh for SSL certificate management...${plain}"
cd ~ || return 1
@@ -941,6 +976,7 @@ EOF
umask 022
export XUI_DB_TYPE=postgres
export XUI_DB_DSN="${xui_dsn}"
ensure_pg_client || echo -e "${yellow}⚠ Could not install pg_dump/pg_restore. In-panel database backup/restore will be unavailable until you install the postgresql-client package.${plain}"
fi
fi
@@ -989,6 +1025,13 @@ EOF
echo -e "${yellow}⚠ SSL Certificate: Skipped — panel is HTTP-only. Use a reverse proxy or SSH tunnel.${plain}"
fi
if [[ "$db_choice" == "2" ]]; then
echo ""
echo -e "${green}PostgreSQL backup & restore is built into the panel:${plain}"
echo -e " ${blue}${SSL_SCHEME}://${SSL_HOST}:${config_port}/${config_webBasePath}${plain} → Backup & Restore"
echo -e "${yellow} Back Up downloads a pg_dump .dump file; Restore reloads it via pg_restore.${plain}"
fi
if [[ "$db_choice" == "2" && "$pg_local_installed" == "1" ]]; then
echo ""
echo -e "${green}═══════════════════════════════════════════${plain}"