diff --git a/backup.sh b/backup.sh index 4c09f8e..d709de4 100644 --- a/backup.sh +++ b/backup.sh @@ -1,40 +1,170 @@ -read -p 'Bot token: ' tk -read -p 'Chat id: ' chatid -if [[ ! $chatid =~ ^\-?[0-9]+$ ]]; then - echo "${chatid} is not a number." - exit 1 -fi +#!/bin/bash + +# Bot token +# گرفتن توکن ربات از کاربر و ذخیره آن در متغیر tk +while [[ -z "$tk" ]]; do + read -p 'Bot token: ' tk + if [[ $tk == $'\0' ]]; then + echo "Invalid input. Token cannot be empty." + unset tk + fi +done + +# Chat id +# گرفتن Chat ID از کاربر و ذخیره آن در متغیر chatid +while [[ -z "$chatid" ]]; do + read -p 'Chat id: ' chatid + if [[ $chatid == $'\0' ]]; then + echo "Invalid input. Chat id cannot be empty." + unset chatid + elif [[ ! $chatid =~ ^\-?[0-9]+$ ]]; then + echo "${chatid} is not a number." + unset chatid + fi +done + +# Caption +# گرفتن عنوان برای فایل پشتیبان و ذخیره آن در متغیر caption read -p 'Caption (for example, your domain, to identify the database file more easily): ' caption -read -p 'Cronjob (only hours are supported ) (e.g : 6) : ' cron -if [[ ! $cron =~ ^[0-9]+$ ]]; then - echo "${cron} is not a number." - exit 1 + +# Cronjob +# تعیین زمانی برای اجرای این اسکریپت به صورت دوره‌ای +while true; do + read -p 'Cronjob (minutes and hours) (e.g : 30 6 or 0 12) : ' minute hour + if [[ $minute == 0 ]] && [[ $hour == 0 ]]; then + cron_time="* * * * *" + break + elif [[ $minute == 0 ]] && [[ $hour =~ ^[0-9]+$ ]] && [[ $hour -lt 24 ]]; then + cron_time="0 */${hour} * * *" + break + elif [[ $hour == 0 ]] && [[ $minute =~ ^[0-9]+$ ]] && [[ $minute -lt 60 ]]; then + cron_time="*/${minute} * * * *" + break + elif [[ $minute =~ ^[0-9]+$ ]] && [[ $hour =~ ^[0-9]+$ ]] && [[ $hour -lt 24 ]] && [[ $minute -lt 60 ]]; then + cron_time="*/${minute} */${hour} * * *" + break + else + echo "Invalid input, please enter a valid cronjob format (minutes and hours, e.g: 0 6 or 30 12)" + fi +done + + +# x-ui or marzban or hiddify +# گرفتن نوع نرم افزاری که می‌خواهیم پشتیبانی از آن بگیریم و ذخیره آن در متغیر xmh +while [[ -z "$xmh" ]]; do + read -p 'x-ui or marzban or hiddify? [x/m/h] : ' xmh + if [[ $xmh == $'\0' ]]; then + echo "Invalid input. Please choose x, m or h." + unset xmh + elif [[ ! $xmh =~ ^[xmh]$ ]]; then + echo "${xmh} is not a valid option. Please choose x, m or h." + unset xmh + fi +done + +while [[ -z "$crontabs" ]]; do + read -p 'Would you like the previous crontabs to be cleared? [y/n] : ' crontabs + if [[ $crontabs == $'\0' ]]; then + echo "Invalid input. Please choose y or n." + unset crontabs + elif [[ ! $crontabs =~ ^[yn]$ ]]; then + echo "${crontabs} is not a valid option. Please choose y or n." + unset crontabs + fi +done + +if [[ "$crontabs" == "y" ]]; then +# remove cronjobs +sudo crontab -l | grep -vE '/root/ac-backup.+\.sh' | crontab - +fi + + +# m backup +# ساخت فایل پشتیبانی برای نرم‌افزار Marzban و ذخیره آن در فایل ac-backup.zip +if [[ "$xmh" == "m" ]]; then + +if dir=$(find /opt /root -type d -iname "marzban" -print -quit); then + echo "The folder exists at $dir" +else + echo "The folder does not exist." + exit 1 +fi + + +ZIP="zip -r /root/ac-backup-m.zip ${dir}/* /var/lib/marzban/*" +ACh1992="marzban backup" + +# x-ui backup +# ساخت فایل پشتیبانی برای نرم‌افزار X-UI و ذخیره آن در فایل ac-backup.zip +elif [[ "$xmh" == "x" ]]; then + +if dbDir=$(find /etc -type d -iname "x-ui*" -print -quit); then + echo "The folder exists at $dbDir" +else + echo "The folder does not exist." + exit 1 +fi + +if configDir=$(find /usr/local -type d -iname "x-ui*" -print -quit); then + echo "The folder exists at $configDir" +else + echo "The folder does not exist." + exit 1 fi -read -p 'x-ui or marzban? [x/m] : ' xm +ZIP="zip /root/ac-backup-x.zip ${dbDir}/x-ui.db ${configDir}/config.json /root/*.key /root/*.crt" +ACh1992="x-ui backup" -if [[ "$xm" == "m" || "$xm" == "M" ]]; then -ZIP="zip -r /root/ac-backup.zip /root/marzban/* /var/lib/marzban/*" -ACLover="marzban backup" -elif [[ "$xm" == "x" || "$xm" == "X" ]]; then -ZIP="zip /root/ac-backup.zip /etc/x-ui/x-ui.db /usr/local/x-ui/bin/config.json /root/*.key /root/*.crt" -ACLover="x-ui backup" +# hiddify backup +# ساخت فایل پشتیبانی برای نرم‌افزار Hiddify و ذخیره آن در فایل ac-backup.zip +elif [[ "$xmh" == "h" ]]; then + +if ! find /opt/hiddify-config/hiddify-panel/ -type d -iname "backup" -print -quit; then + echo "The folder does not exist." + exit 1 +fi + +ZIP=$(cat </root/ac-backup.sh < "/root/ac-backup-${xmh}.sh" </dev/null 2>&1"; } | crontab -u root - -bash /root/ac-backup.sh -echo "\nDone" +# Add cronjob +# افزودن کرانجاب جدید برای اجرای دوره‌ای این اسکریپت +{ crontab -l -u root; echo "${cron_time} /bin/bash /root/ac-backup-${xmh}.sh >/dev/null 2>&1"; } | crontab -u root - + +# run the script +# اجرای این اسکریپت +bash "/root/ac-backup-${xmh}.sh" + +# Done +# پایان اجرای اسکریپت +echo -e "\nDone\n"