#!/bin/bash
# AUTOSCRIPT INTERFLAZZ ID

RED='\033[0;31m'
NC='\033[0m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHT='\033[0;37m'

CHATID=$(cat /root/telebotvpn/idteleowner 2>/dev/null)
KEY=$(cat /root/telebotvpn/tokentelebotbackup 2>/dev/null)
if [ -z "$KEY" ]; then
    KEY=$(cat /root/telebotvpn/tokentelebotserver 2>/dev/null)
fi
TIME="10"
URL="https://api.telegram.org/bot$KEY/sendMessage"

IP=$(cat /etc/xray/ipvps 2>/dev/null || wget -qO- ipv4.icanhazip.com)
domain=$(cat /etc/xray/domain 2>/dev/null || hostname -f)
date=$(date +"%d-%B-%Y")
time=$(date +"%H:%M WIB")
DOMAIN_PART=$(cut -d '.' -f1 /etc/xray/domain 2>/dev/null || echo "backup")

clear
echo "Mohon Menunggu, Proses Backup Sedang Berlangsung!"
cd /root
rm -rf /root/backup /root/$DOMAIN_PART.zip /root/restore.tar.gz

backup_dir="/root/backup"
mkdir -p "$backup_dir"

# 1. System user and auth database
cp /etc/passwd "$backup_dir/" >/dev/null 2>&1
cp /etc/group "$backup_dir/" >/dev/null 2>&1
cp /etc/shadow "$backup_dir/" >/dev/null 2>&1
cp /etc/gshadow "$backup_dir/" >/dev/null 2>&1
cp /etc/issue.net "$backup_dir/" >/dev/null 2>&1

# 2. SSH / OpenVPN configs & database (matching menu/backuprestore)
if [ -d "/etc/ssh" ]; then
    cp -r /etc/ssh "$backup_dir/" >/dev/null 2>&1
fi
cp /etc/ssh/.ssh.db "$backup_dir/.ssh.db" >/dev/null 2>&1

# 3. Modular Xray protocols (matching menu/backuprestore)
if [ -d "/etc/xray/vmess" ]; then
    cp -r /etc/xray/vmess "$backup_dir/" >/dev/null 2>&1
fi
if [ -d "/etc/xray/vless" ]; then
    cp -r /etc/xray/vless "$backup_dir/" >/dev/null 2>&1
fi
if [ -d "/etc/xray/trojan" ]; then
    cp -r /etc/xray/trojan "$backup_dir/" >/dev/null 2>&1
fi
if [ -d "/etc/xray/shadowsocks" ]; then
    cp -r /etc/xray/shadowsocks "$backup_dir/" >/dev/null 2>&1
fi

# Metadata & logs
cp /etc/xray/*.log "$backup_dir/" >/dev/null 2>&1
cp /etc/xray/domain "$backup_dir/" >/dev/null 2>&1
cp /etc/xray/ipvps "$backup_dir/" >/dev/null 2>&1
cp /etc/xray/city "$backup_dir/" >/dev/null 2>&1
cp /etc/xray/isp "$backup_dir/" >/dev/null 2>&1
cp /etc/xray/dns "$backup_dir/" >/dev/null 2>&1

# Root DB fallbacks for legacy tools
cp /etc/xray/vmess/.vmess.db "$backup_dir/.vmess.db" >/dev/null 2>&1
cp /etc/xray/vless/.vless.db "$backup_dir/.vless.db" >/dev/null 2>&1
cp /etc/xray/trojan/.trojan.db "$backup_dir/.trojan.db" >/dev/null 2>&1

# Client configs
mkdir -p "$backup_dir/html" >/dev/null 2>&1
cp /var/www/html/*.txt "$backup_dir/html/" >/dev/null 2>&1

# Clean temporary trials from backup db
sed -i '/^#&#/d' "$backup_dir/.ssh.db" >/dev/null 2>&1
sed -i '/^#&#/d' "$backup_dir/.vmess.db" >/dev/null 2>&1
sed -i '/^#&#/d' "$backup_dir/.vless.db" >/dev/null 2>&1
sed -i '/^#&#/d' "$backup_dir/.trojan.db" >/dev/null 2>&1
sed -i '/^#&#/d' "$backup_dir/ssh/.ssh.db" >/dev/null 2>&1
sed -i '/^#&#/d' "$backup_dir/vmess/.vmess.db" >/dev/null 2>&1
sed -i '/^#&#/d' "$backup_dir/vless/.vless.db" >/dev/null 2>&1
sed -i '/^#&#/d' "$backup_dir/trojan/.trojan.db" >/dev/null 2>&1

# Create both tar.gz (for menu/backuprestore) and zip (for bot/upload.php)
tar -czf /root/restore.tar.gz -C /root backup >/dev/null 2>&1
zip -r /root/$DOMAIN_PART.zip backup >/dev/null 2>&1

# Copy to web directory for HTTP download
mkdir -p /var/www/html
cp /root/$DOMAIN_PART.zip /var/www/html/ >/dev/null 2>&1
cp /root/restore.tar.gz /var/www/html/ >/dev/null 2>&1

# Upload to backup server
response=$(curl -k -s -X POST -F "file=@/root/$DOMAIN_PART.zip" https://interflazz.local/backup/upload.php)
url=$(echo "$response" | jq -r '.url' 2>/dev/null)

if [ -z "$url" ] || [ "$url" == "null" ]; then
    url="https://${domain}:81/${DOMAIN_PART}.zip"
fi

TEXT="
──────────────────────
Backup Vps Server Success
──────────────────────
<code>Host :</code> <code>$domain</code>
<code>IP   :</code> <code>$IP</code>
<code>Date :</code> <code>$date</code>
<code>Time :</code> <code>$time</code>
──────────────────────
Link Backup :
$url
────────────────────── "

# Send message to telegram
curl -s --max-time $TIME -d "chat_id=$CHATID&disable_web_page_preview=1&text=$TEXT&parse_mode=html" $URL >/dev/null

sleep 2
rm -rf /root/backup
rm -f /root/$DOMAIN_PART.zip /root/restore.tar.gz

clear
echo -e "
──────────────────────
Backup Vps Server Success
──────────────────────
Host  : $domain
IP    : $IP
Date  : $date
Time  : $time
──────────────────────
Link Backup :
$url
──────────────────────
"

echo ""
