Skip to content

Commit 0706b0b

Browse files
authored
feat(x-ui.sh): add migrateDB command for SQLite .db <-> .dump (#4910)
* feat(x-ui.sh): add migrateDB command and menu for SQLite .db <-> .dump Adds an "x-ui migrateDB <file>" subcommand and a PostgreSQL-menu option (9) that convert between a SQLite .db and a portable .dump file. Direction is auto-detected from the extension and delegated to the bundled binary (x-ui migrate-db --dump/--restore), so no external sqlite3 client is needed. Depends on the matching binary support, so it is only usable from the next panel release. * fix(x-ui.sh): address review feedback on migrateDB Per Copilot review on PR #4910: - Probe the bundled binary for migrate-db --dump support and fail with a clear upgrade message instead of a raw "flag not defined" error on old builds. - Prompt before overwriting an existing .dump in dump mode (parity with restore). - Refuse to restore into the live database path while x-ui is running, to avoid corrupting the running panel. - Fix the usage/synopsis strings to show input is optional ([file] not <file>).
1 parent db118cb commit 0706b0b

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

x-ui.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,6 +2795,7 @@ postgresql_menu() {
27952795
echo -e "${green}\t6.${plain} Restart PostgreSQL"
27962796
echo -e "${green}\t7.${plain} ${green}Enable${plain} Autostart on boot"
27972797
echo -e "${green}\t8.${plain} View PostgreSQL Log"
2798+
echo -e "${green}\t9.${plain} Convert SQLite ${green}.db <-> .dump${plain}"
27982799
echo -e "${green}\t0.${plain} Back to Main Menu"
27992800
read -rp "Choose an option: " choice
28002801
case "$choice" in
@@ -2833,13 +2834,110 @@ postgresql_menu() {
28332834
postgresql_log
28342835
postgresql_menu
28352836
;;
2837+
9)
2838+
migrate_db_prompt
2839+
postgresql_menu
2840+
;;
28362841
*)
28372842
echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
28382843
postgresql_menu
28392844
;;
28402845
esac
28412846
}
28422847

2848+
# Convert between the panel's SQLite database and a portable .dump (SQL text)
2849+
# file using the bundled x-ui binary. With no arguments it dumps the installed
2850+
# panel database; an optional second argument overrides the output path.
2851+
# x-ui migrateDB [file.db|file.dump] [output]
2852+
migrate_db() {
2853+
local input="$1" output="$2"
2854+
local default_db="/etc/x-ui/x-ui.db"
2855+
local bin="${xui_folder}/x-ui"
2856+
2857+
[[ -z "$input" ]] && input="$default_db"
2858+
2859+
if [[ ! -x "$bin" ]]; then
2860+
LOGE "x-ui binary not found at ${bin}. Is the panel installed?"
2861+
return 1
2862+
fi
2863+
2864+
if ! "$bin" migrate-db -h 2>&1 | grep -q -- '-dump'; then
2865+
LOGE "This x-ui build does not support .db <-> .dump conversion yet."
2866+
LOGE "Update the panel first (x-ui update) to a version with 'migrate-db --dump/--restore'."
2867+
return 1
2868+
fi
2869+
2870+
if [[ ! -f "$input" ]]; then
2871+
LOGE "Input file not found: ${input}"
2872+
echo -e "Usage: ${green}x-ui migrateDB [file.db|file.dump] [output]${plain}"
2873+
return 1
2874+
fi
2875+
2876+
local mode
2877+
case "$input" in
2878+
*.db | *.sqlite | *.sqlite3)
2879+
mode="dump"
2880+
;;
2881+
*.dump | *.sql)
2882+
mode="restore"
2883+
;;
2884+
*)
2885+
if head -c 16 "$input" | grep -q "SQLite format 3"; then
2886+
mode="dump"
2887+
else
2888+
mode="restore"
2889+
fi
2890+
;;
2891+
esac
2892+
2893+
if [[ "$mode" == "dump" ]]; then
2894+
[[ -z "$output" ]] && output="${input%.*}.dump"
2895+
if [[ -f "$output" ]]; then
2896+
confirm "Output ${output} already exists and will be overwritten. Continue?" "n" || return 0
2897+
fi
2898+
LOGI "Dumping SQLite database to SQL text:"
2899+
echo -e " ${green}${input}${plain} -> ${green}${output}${plain}"
2900+
if "$bin" migrate-db --src "$input" --dump "$output"; then
2901+
LOGI "Done. Wrote ${output}."
2902+
else
2903+
LOGE "Dump failed."
2904+
return 1
2905+
fi
2906+
else
2907+
[[ -z "$output" ]] && output="${input%.*}.db"
2908+
if [[ "$output" == "$default_db" ]] && check_status > /dev/null 2>&1; then
2909+
LOGE "Refusing to restore into the live database (${default_db}) while x-ui is running."
2910+
LOGE "Stop the panel first (x-ui stop) or choose a different output path."
2911+
return 1
2912+
fi
2913+
if [[ -f "$output" ]]; then
2914+
confirm "Output ${output} already exists and will be overwritten. Continue?" "n" || return 0
2915+
rm -f "$output"
2916+
fi
2917+
LOGI "Rebuilding SQLite database from SQL text:"
2918+
echo -e " ${green}${input}${plain} -> ${green}${output}${plain}"
2919+
if "$bin" migrate-db --restore "$input" --out "$output"; then
2920+
LOGI "Done. Created ${output}."
2921+
else
2922+
LOGE "Restore failed."
2923+
rm -f "$output"
2924+
return 1
2925+
fi
2926+
fi
2927+
}
2928+
2929+
# Interactive wrapper around migrate_db for the menu: prompts for the paths and
2930+
# lets migrate_db auto-detect the direction.
2931+
migrate_db_prompt() {
2932+
local default_db="/etc/x-ui/x-ui.db"
2933+
local input output
2934+
echo -e "Convert between a SQLite ${green}.db${plain} and a portable ${green}.dump${plain} (direction auto-detected)."
2935+
read -rp "Input file [${default_db}]: " input
2936+
input="${input:-$default_db}"
2937+
read -rp "Output file (leave empty to auto-name next to input): " output
2938+
migrate_db "$input" "$output"
2939+
}
2940+
28432941
show_usage() {
28442942
echo -e "┌────────────────────────────────────────────────────────────────┐
28452943
${blue}x-ui control menu usages (subcommands):${plain}
@@ -2857,6 +2955,7 @@ show_usage() {
28572955
${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
28582956
${blue}x-ui update${plain} - Update │
28592957
${blue}x-ui update-all-geofiles${plain} - Update all geo files │
2958+
${blue}x-ui migrateDB [file]${plain} - Convert .db <-> .dump (SQLite) │
28602959
${blue}x-ui legacy${plain} - Legacy version │
28612960
${blue}x-ui install${plain} - Install │
28622961
${blue}x-ui uninstall${plain} - Uninstall │
@@ -3045,6 +3144,9 @@ if [[ $# > 0 ]]; then
30453144
"update-all-geofiles")
30463145
check_install 0 && update_all_geofiles 0 && restart 0
30473146
;;
3147+
"migrateDB")
3148+
migrate_db "$2" "$3"
3149+
;;
30483150
*) show_usage ;;
30493151
esac
30503152
else

0 commit comments

Comments
 (0)