# !/bin/bash
# ████████╗░█████╗░██╗░░██╗░█████╗░██╗░░░██╗██████╗░███╗░░██╗
# ╚══██╔══╝██╔══██╗██║░██╔╝██╔══██╗██║░░░██║██╔══██╗████╗░██║
# ░░░██║░░░██║░░██║█████═╝░██║░░██║╚██╗░██╔╝██████╔╝██╔██╗██║
# ░░░██║░░░██║░░██║██╔═██╗░██║░░██║░╚████╔╝░██╔═══╝░██║╚████║
# ░░░██║░░░╚█████╔╝██║░╚██╗╚█████╔╝░░╚██╔╝░░██║░░░░░██║░╚███║
# ░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝░╚════╝░░░░╚═╝░░░╚═╝░░░░░╚═╝░░╚══╝

clear
RED='\033[0;31m'
NC='\033[0m'
gray="\e[1;30m"
Blue="\033[0;34m"
green='\033[0;32m'
grenbo="\e[92;1m"
YELL='\033[0;33m'
red() { echo -e "\\033[32;1m${*}\\033[0m"; }
# Getting
MYIP=$(curl -sS ipv4.icanhazip.com)
echo -e "\e[32mloading...\e[0m"
clear
IP=$(wget -qO- icanhazip.com)
dateToday=$(date +"%Y-%m-%d")
Name=$(curl https://autoscript.interflazz.my.id/tunnel/register | grep $MYIP | awk '{print $2}')

setup_bot() {
    clear    
    switch=$(grep -i "switch" /root/.bckupbot | awk '{print $2}')
    echo -e ""
    echo -e "▀▀█▀▀ ▒█▀▀▀ ▒█░░░ ▒█▀▀▀ ▒█▀▀█ ▒█▀▀▀█ ▀▀█▀▀\033[0m" 
    echo -e "░▒█░░ ▒█▀▀▀ ▒█░░░ ▒█▀▀▀ ▒█▀▀▄ ▒█░░▒█ ░▒█░░\033[0m" 
    echo -e "░▒█░░ ▒█▄▄▄ ▒█▄▄█ ▒█▄▄▄ ▒█▄▄█ ▒█▄▄▄█ ░▒█░░\033[0m"
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    echo "Pergi ke @BotFather dan type /newbot untuk membuat bot baru"
    echo "Pergi ke @MissRose_bot dan type /id untuk mendapatkan ID telegram"
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    read -p "Input bot Token : " getToken
    read -p "Input admin ID  : " adminID
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    echo "$getToken" >/root/.bckupbot
    echo "$adminID" >>/root/.bckupbot
    echo "switch $switch" >>/root/.bckupbot
    read -n 1 -s -r -p "Press any key to back on Bot menu"
    botbckpBot_menu
}

botBackup() {
    bottoken=$(sed -n '1p' /root/.bckupbot | awk '{print $1}')
    adminid=$(sed -n '2p' /root/.bckupbot | awk '{print $1}')
    echo -e "[ ${green}INFO${NC} ] Create password for database"
	read -t 10 -p "Input Your password : " InputPass
	sleep 1
    echo -e "[ ${green}INFO${NC} ] • VPS Data Backup... "
    sleep 1
    echo -e "[ ${green}INFO${NC} ] • Directory Created... "

    cd /root
    rm -rf /root/backup
    mkdir -p /root/backup/ > /dev/null 2>&1
    mkdir -p /root/backup/xray/ > /dev/null 2>&1
    mkdir -p /root/backup/html/ > /dev/null 2>&1
    mkdir -p /root/backup/limit/ssh/ip > /dev/null 2>&1
    mkdir -p /root/backup/limit/vmess/ip > /dev/null 2>&1
    mkdir -p /root/backup/limit/vless/ip > /dev/null 2>&1
    mkdir -p /root/backup/limit/trojan/ip > /dev/null 2>&1
    mkdir -p /root/backup/quota/vmess > /dev/null 2>&1
    mkdir -p /root/backup/quota/vless > /dev/null 2>&1
    mkdir -p /root/backup/quota/trojan > /dev/null 2>&1
    mkdir -p /root/backup/quotax/vmess > /dev/null 2>&1
    mkdir -p /root/backup/quotax/vless > /dev/null 2>&1
    mkdir -p /root/backup/quotax/trojan > /dev/null 2>&1
    cp /etc/passwd /root/backup/ 
    cp /etc/group /root/backup/ 
    cp /etc/shadow /root/backup/ 
    cp /etc/gshadow /root/backup/ 
    cp -r /etc/kyt/limit/ssh/ip/* /root/backup/limit/ssh/ip > /dev/null 2>&1
    cp -r /etc/kyt/limit/vmess/ip/* /root/backup/limit/vmess/ip > /dev/null 2>&1
    cp -r /etc/kyt/limit/vless/ip/* /root/backup/limit/vless/ip > /dev/null 2>&1
    cp -r /etc/kyt/limit/trojan/ip/* /root/backup/limit/trojan/ip > /dev/null 2>&1
    cp -r /etc/vmess/* /root/backup/quota/vmess > /dev/null 2>&1
    cp -r /etc/vless/* /root/backup/quota/vless > /dev/null 2>&1
    cp -r /etc/trojan/* /root/backup/quota/trojan > /dev/null 2>&1
    cp -r /etc/limit/vmess/* /root/backup/quotax/vmess > /dev/null 2>&1
    cp -r /etc/limit/vless/* /root/backup/quotax/vless > /dev/null 2>&1
    cp -r /etc/limit/trojan/* /root/backup/quotax/trojan > /dev/null 2>&1
    cp /etc/xray/*.json /root/backup/xray/
    cp /var/www/html/*.txt /root/backup/html/ > /dev/null 2>&1
    cp -r /etc/ssh/.ssh.db /root/backup/.ssh.db > /dev/null 2>&1
    cp -r /etc/vmess/.vmess.db /root/backup/.vmess.db > /dev/null 2>&1
    cp -r /etc/vless/.vless.db /root/backup/.vless.db > /dev/null 2>&1 
    cp -r /etc/trojan/.trojan.db /root/backup/.trojan.db > /dev/null 2>&1
    sed -i '/^#&/d' /root/backup/.ssh.db > /dev/null 2>&1
    sed -i '/^#&/d' /root/backup/.vmess.db > /dev/null 2>&1
    sed -i '/^#&/d' /root/backup/.vless.db > /dev/null 2>&1
    sed -i '/^#&/d' /root/backup/.trojan.db > /dev/null 2>&1
    cd /root
    zip -rP "$InputPass" "$IP-$Name-$dateToday.zip" backup >/dev/null 2>&1
    echo -e "[ ${green}INFO${NC} ] • Sending Via Bot... "
    curl -Ss --request POST \
        --url "https://api.telegram.org/bot${bottoken}/sendDocument?chat_id=${adminid}&caption=Here Your Backup Today : ${dateToday}" \
        --header 'content-type: multipart/form-data' \
        --form document=@"/root/$IP-$Name-$dateToday.zip" >/root/t1

    fileId=$(grep -o '"file_id":"[^"]*' /root/t1 | grep -o '[^"]*$')

    curl -Ss --request GET \
        --url "https://api.telegram.org/bot${bottoken}/getfile?file_id=${fileId}" >/root/t1

    filePath=$(grep -o '"file_path":"[^"]'* /root/t1 | grep -o '[^"]*$')

    curl -Ss --request GET \
        --url "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${adminid}&text=File ID   : <code>${fileId}</code>&parse_mode=html" &>/dev/null
    curl -Ss --request GET \
        --url "https://api.telegram.org/bot${bottoken}/sendMessage?chat_id=${adminid}&text=File Path : <code>${filePath}</code>&parse_mode=html" &>/dev/null

    echo -e "[ ${green}INFO${NC} ] • Completed... "

    rm -rf /root/backup
    rm -r /root/$IP-$Name-$dateToday.zip
    rm -f /root/t1
    read -n 1 -s -r -p "Press any key to back on menu"
    botbckpBot_menu
}

restoreBot() {
    bottoken=$(sed -n '1p' /root/.bckupbot | awk '{print $1}')
    read -p "Input File ID   : " fileId
    read -p "Input File PATH : " filePath
    cd /root
    curl -Ss --request GET \
        --url "https://api.telegram.org/file/bot${bottoken}/${filePath}?file_id=${fileId}" > backup.zip
    echo -e "[ ${green}INFO${NC} ] • Restore Data..."
	read -rp "Password File  : " InputPass
	echo -e "[ ${green}INFO${NC} ] • Getting your data..."
	unzip -P $InputPass /root/backup.zip &> /dev/null
    echo -e "[ ${green}INFO${NC} ] • Starting to restore data..."
    rm -f /root/backup.zip &> /dev/

    mkdir -p /etc/xray/ > /dev/null 2>&1
    cd /root/backup/ > /dev/null 2>&1
    cp /root/backup/passwd /etc/
    cp /root/backup/group /etc/
    cp /root/backup/shadow /etc/
    cp /root/backup/gshadow /etc/
    cp -r /root/backup/limit/ssh/ip/* /etc/kyt/limit/ssh/ip > /dev/null 2>&1
    cp -r /root/backup/limit/vmess/ip/* /etc/kyt/limit/vmess/ip > /dev/null 2>&1
    cp -r /root/backup/limit/vless/ip/* /etc/kyt/limit/vless/ip > /dev/null 2>&1
    cp -r /root/backup/limit/trojan/ip/* /etc/kyt/limit/trojan/ip > /dev/null 2>&1
    cp -r /root/backup/quota/vmess/* /etc/vmess/ > /dev/null 2>&1
    cp -r /root/backup/quota/vless/* /etc/vless/ > /dev/null 2>&1
    cp -r /root/backup/quota/trojan/* /etc/trojan/ > /dev/null 2>&1
    cp -r /root/backup/quotax/vmess/* /etc/limit/vmess/ > /dev/null 2>&1
    cp -r /root/backup/quotax/vless/* /etc/limit/vless/ > /dev/null 2>&1
    cp -r /root/backup/quotax/trojan/* /etc/limit/trojan/ > /dev/null 2>&1
    cp /root/backup/xray/* /etc/xray/
    cp /root/backup/html/* /var/www/html/ > /dev/null 2>&1
    cp /root/backup/.ssh.db /etc/ssh/ > /dev/null 2>&1
    cp /root/backup/.vmess.db /etc/vmess/ > /dev/null 2>&1
    cp /root/backup/.vless.db /etc/vless/ > /dev/null 2>&1
    cp /root/backup/.trojan.db /etc/trojan/ > /dev/null 2>&1
    systemctl restart xray
    systemctl restart ssh
    rm -rf /root/backup
    rm -f backup.zip
    echo -e "[ ${green}INFO${NC} ] • Done Restore... "
    read -n 1 -s -r -p "Press any key to back on menu"
    botbckpBot_menu
}

autoBackup() {
    switch=$(grep -i "switch" /root/.bckupbot | awk '{print $2}')
    if [ "${switch}" == "on" ]; then
        sed -i 's/switch on/switch off/g' /root/.bckupbot
        sed -i "/autobackup/d" /etc/crontab
        service cron restart >/dev/null 2>&1
        service cron reload >/dev/null 2>&1
        echo "Turn Off"
    else
        sed -i 's/switch off/switch on/g' /root/.bckupbot
        echo "5 0 * * * root autobackup" >>/etc/crontab
        service cron restart >/dev/null 2>&1
        service cron reload >/dev/null 2>&1
        echo "Turn On"
    fi
    sleep 1
    read -n 1 -s -r -p "Press any key to back on Bot menu"
    botbckpBot_menu
}

botbckpBot_menu() {
    switch=$(grep -i "switch" /root/.bckupbot | awk '{print $2}')
    clear
    if [ "${switch}" == "on" ]; then
        sts="\033[0;32m[ON]\033[0m"
    else
        sts="\033[1;31m[OFF]\033[0m"
    fi
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
	echo -e "\E[40;1;37m      MENU SETTING UP BOT AUTOBACKUP      \E[0m"
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    echo -e "${NC}  Status AutoBackup : $sts"
    echo -e "${NC}"   
    echo -e "${NC}  [01] SetUp Bot Telegram \e[0m"
    echo -e "${NC}  [02] SetUp autobackup Telegram \e[0m"
    echo -e "${NC}  [03] Backup Server & Telegram \e[0m"
    echo -e "${NC}  [04] Restore Data Server \e[0m"
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    echo -e ""
    read -p "Select From Options ➤ " botch

    case "$botch" in
    1)
        clear
        setup_bot
        ;;
    2)
        clear
        autoBackup
        ;;
    3)
        clear
        botBackup
        ;;
    4)
        clear
        restoreBot
        ;;
    0) clear ; menu ; exit ;;
    x) exit ;;
    *) echo "Pilihan anda tidak tersedia! " ; sleep 1 ; mbot-backup ;;
    esac
}
clear
[[ ! -f /root/.bckupbot ]] && {
    clear
    echo -e ""
    echo -e "▀▀█▀▀ ▒█▀▀▀ ▒█░░░ ▒█▀▀▀ ▒█▀▀█ ▒█▀▀▀█ ▀▀█▀▀\033[0m" 
    echo -e "░▒█░░ ▒█▀▀▀ ▒█░░░ ▒█▀▀▀ ▒█▀▀▄ ▒█░░▒█ ░▒█░░\033[0m" 
    echo -e "░▒█░░ ▒█▄▄▄ ▒█▄▄█ ▒█▄▄▄ ▒█▄▄█ ▒█▄▄▄█ ░▒█░░\033[0m"
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    echo "Pergi ke @BotFather dan type /newbot untuk membuat bot baru"
    echo "Pergi ke @MissRose_bot dan type /id untuk mendapatkan ID telegram"
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    read -p "Input bot Token : " getToken
    read -p "Input admin ID  : " adminID
    echo -e "\e[33m──────────────────────────────────────────\033[0m"
    echo "$getToken" > /root/.bckupbot
    echo "$adminID" > /root/.bckupbot
    echo "switch on" > /root/.bckupbot
}
botbckpBot_menu