Skip to content

Commit

Permalink
Update backup.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
ach1992 authored Jun 4, 2023
1 parent 85fd400 commit bfcae1f
Showing 1 changed file with 155 additions and 25 deletions.
180 changes: 155 additions & 25 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
cd /opt/hiddify-config/hiddify-panel/
if [ $(find /opt/hiddify-config/hiddify-panel/backup -type f | wc -l) -gt 100 ]; then
find /opt/hiddify-config/hiddify-panel/backup -type f -delete
fi
python3 -m hiddifypanel backup
cd /opt/hiddify-config/hiddify-panel/backup
latest_file=\$(ls -t *.json | head -n1)
rm -f /root/ac-backup-h.zip
zip /root/ac-backup-h.zip /opt/hiddify-config/hiddify-panel/backup/\$latest_file
EOF
)
ACh1992="hiddify backup"
else
echo "Please choose m or x only !"
echo "Please choose m or x or h only !"
exit 1
fi

export IP=$(hostname -I)
caption="${caption}\n\n${ACLover}\n<code>${IP}</code>"
caption="${caption}\n\n${ACh1992}\n<code>${IP}</code>\nCreated by @ACh1992 - https://github.com/ach1992"

# install zip
# نصب پکیج zip
sudo apt install zip -y
sudo apt install curl -y

cat >/root/ac-backup.sh <<EOL
# send backup to telegram
# ارسال فایل پشتیبانی به تلگرام
cat > "/root/ac-backup-${xmh}.sh" <<EOL
$ZIP
curl -F chat_id="${chatid}" -F caption=\$'${caption}' -F parse_mode="HTML" -F document=@"/root/ac-backup.zip" https://api.telegram.org/bot${tk}/sendDocument
curl -F chat_id="${chatid}" -F caption=\$'${caption}' -F parse_mode="HTML" -F document=@"/root/ac-backup-${xmh}.zip" https://api.telegram.org/bot${tk}/sendDocument
EOL

{ crontab -l -u root; echo "0 */${cron} * * * /bin/bash /root/ac-backup.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"

0 comments on commit bfcae1f

Please sign in to comment.