feat: add backup/restore to x-ui.sh and update assets

This commit is contained in:
root 2026-04-26 19:53:15 +08:00
parent 3a4c6f7094
commit 64e8f18997
3 changed files with 89 additions and 2 deletions

View file

@ -26,7 +26,7 @@
"js/model/inbound.js": "js/model/inbound.afa36664.js",
"js/model/outbound.js": "js/model/outbound.5b13ad17.js",
"js/model/reality_targets.js": "js/model/reality_targets.6ca38c21.js",
"js/model/setting.js": "js/model/setting.a37c1aec.js",
"js/model/setting.js": "js/model/setting.e40c3f88.js",
"js/subscription.js": "js/subscription.84ade419.js",
"js/util/index.js": "js/util/index.7ced4edf.js",
"js/websocket.js": "js/websocket.938f634e.js",

View file

@ -81,6 +81,11 @@ class AllSetting {
this.ldapDefaultExpiryDays = 0;
this.ldapDefaultLimitIP = 0;
this.backupEnabled = false;
this.backupFrequency = "daily";
this.backupHour = 3;
this.backupMaxCount = 10;
this.dbType = "sqlite";
this.dbHost = "127.0.0.1";
this.dbPort = "3306";

84
x-ui.sh
View file

@ -3588,6 +3588,61 @@ db_switch_to_sqlite() {
}
# Database management menu
backup_db() {
echo -e "${green}Creating database backup...${plain}"
local xui_bin="${xui_folder}/x-ui"
if [[ ! -x "$xui_bin" ]]; then
echo -e "${red}x-ui binary not found at $xui_bin${plain}"
return 1
fi
"$xui_bin" backup
}
restore_db() {
local backup_file="$1"
if [[ -z "$backup_file" ]]; then
echo -e "${red}Usage: x-ui restore <backup-filename>${plain}"
list_backups
return 1
fi
local backup_dir="/etc/x-ui/backups"
local full_path="${backup_dir}/${backup_file}"
if [[ ! -f "$full_path" ]]; then
echo -e "${red}Backup file not found: $full_path${plain}"
list_backups
return 1
fi
echo -e "${yellow}WARNING: Restore will stop the panel and replace the database.${plain}"
read -p "Continue? (y/n) " confirm
if [[ "$confirm" != "y" ]]; then
echo "Cancelled."
return 0
fi
echo "Stopping panel..."
stop
echo "Restoring from $backup_file..."
local xui_bin="${xui_folder}/x-ui"
"$xui_bin" restore --file="$backup_file"
echo "Starting panel..."
start
echo -e "${green}Restore completed.${plain}"
}
list_backups() {
local backup_dir="/etc/x-ui/backups"
if [[ ! -d "$backup_dir" ]]; then
echo "No backups found."
return 0
fi
local count=$(ls -1 "$backup_dir" 2>/dev/null | wc -l)
if [[ "$count" -eq 0 ]]; then
echo "No backups found."
return 0
fi
echo -e "${green}Backups in ${backup_dir}:${plain}"
ls -lh "$backup_dir" | awk 'NR>1 {print $5, $6, $7, $8, $9}'
}
db_menu() {
local current_type=$(read_json_dbtype)
@ -3612,9 +3667,12 @@ db_menu() {
${green}14.${plain} 查看 MariaDB 允许 IP │
${green}15.${plain} 添加 MariaDB 允许 IP │
${green}16.${plain} 移除 MariaDB 允许 IP │
${green}17.${plain} 创建数据库备份 │
${green}18.${plain} 从备份恢复数据库 │
${green}19.${plain} 列出所有备份 │
╚════════════════════════════════════════════════╝
"
read -rp "请输入选择 [0-16]" num
read -rp "请输入选择 [0-19]" num
case "${num}" in
0)
show_menu
@ -3681,6 +3739,21 @@ db_menu() {
remove_mariadb_remote_ip
db_menu
;;
17)
backup_db
db_menu
;;
18)
list_backups
echo ""
read -p "Enter backup filename to restore: " restore_filename
restore_db "$restore_filename"
db_menu
;;
19)
list_backups
db_menu
;;
*)
echo -e "${red}无效选项,请选择有效数字。${plain}\n"
db_menu
@ -3870,6 +3943,15 @@ if [[ $# > 0 ]]; then
"update-all-geofiles")
check_install 0 && update_all_geofiles 0 && restart 0
;;
"backup")
check_install 0 && backup_db
;;
"restore")
check_install 0 && restore_db "${2}"
;;
"list-backups")
check_install 0 && list_backups
;;
*) show_usage ;;
esac
else