-
Notifications
You must be signed in to change notification settings - Fork 34
/
update.sh
156 lines (129 loc) · 4.95 KB
/
update.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# shellcheck disable=SC2034
target_version="6.1.5"
# Define text colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
RESET='\033[0m' # Reset text color
install_dir="/opt/Hiddify-Telegram-Bot"
# Function to display colored messages
function display_message() {
echo -e "$1"
}
# Function to gracefully stop the bot
function stop_bot() {
display_message "${GREEN}Stopping the bot gracefully...${RESET}"
pkill -15 -f hiddifyTelegramBot.py
}
add_cron_job_if_not_exists() {
local cron_job="$1"
local current_crontab
# Normalize the cron job formatting (remove extra spaces)
cron_job=$(echo "$cron_job" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')
# Check if the cron job already exists in the current user's crontab
current_crontab=$(crontab -l 2>/dev/null || true)
if [[ -z "$current_crontab" ]]; then
# No existing crontab, so add the new cron job
(echo "$cron_job") | crontab -
elif ! (echo "$current_crontab" | grep -Fq "$cron_job"); then
# Cron job doesn't exist, so append it to the crontab
(echo "$current_crontab"; echo "$cron_job") | crontab -
fi
}
# Function to reinstall the bot
function reinstall_bot() {
display_message "${YELLOW}This version is deprecated, and you need to reinstall the bot.${RESET}"
# Ask for confirmation
read -r -p "Do you want to reinstall the bot? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
display_message "Reinstalling the bot..."
# Change to the installation directory
cd /opt || {
display_message "${RED}Failed to change directory to /opt.${RESET}"
exit 1
}
# Remove the old bot
rm -rf /opt/Hiddify-Telegram-Bot
# Run the installation script
bash -c "$(curl -Lfo- https://raw.githubusercontent.com/B3H1Z/Hiddify-Telegram-Bot/main/install.sh)"
display_message "${GREEN}Bot has been reinstalled.${RESET}"
;;
*)
display_message "Bot has not been reinstalled."
;;
esac
}
# current_version_first_part=$(echo "current_version" | cut -d '.' -f 1)
# target_version_first_part=$(echo "target_version" | cut -d '.' -f 1)
# if [ "$current_version_first_part" = "4" ] && [ "$target_version_first_part" = "5" ]; then
# echo "Version is 4, running update.py to update to version 5."
# python3 /opt/Hiddify-Telegram-Bot/update.py --update-v4-v5
# echo "Update.py has been run."
# else
# echo "Version is not 4."
# fi
branch="main"
if [ "$0" == "--pre-release" ]; then
branch="pre-release"
fi
echo "Selected branch: $branch"
# Function to update and restart the bot
function update_bot() {
display_message "${GREEN}Updating the bot...${RESET}"
git stash
if git pull origin "$branch"; then
pip install -r requirements.txt || display_error_and_exit "Failed to install requirements."
nohup python3 hiddifyTelegramBot.py >>bot.log 2>&1 &
display_message "${GREEN}Bot has been updated and restarted.${RESET}"
else
if git pull --rebase origin "$branch"; then
pip install -r requirements.txt || display_error_and_exit "Failed to install requirements."
> $install_dir/bot.log
nohup python3 hiddifyTelegramBot.py >>bot.log 2>&1 &
display_message "${GREEN}Bot has been updated and restarted.${RESET}"
else
display_message "${RED}Failed to update the bot. Check the Git repository for errors.${RESET}"
exit 1
fi
display_message "${RED}Failed to update the bot. Check the Git repository for errors.${RESET}"
exit 1
fi
}
# get backup of Database/hidyBot.db
function get_backup() {
display_message "${GREEN}Getting backup of Database/hidyBot.db...${RESET}"
if cp /opt/Hiddify-Telegram-Bot/Database/hidyBot.db /opt/Hiddify-Telegram-Bot/Database/hidyBot.db.bak; then
display_message "${GREEN}Backup of Database/hidyBot.db has been taken.${RESET}"
else
display_message "${RED}Failed to get backup of Database/hidyBot.db.${RESET}"
fi
}
# Stop the bot gracefully before proceeding
stop_bot
# Wait for a few seconds
display_message "Please wait for 5 seconds ..."
sleep 5
# If version.py does not exist, offer to reinstall the bot; otherwise, update it
if [ ! -f /opt/Hiddify-Telegram-Bot/version.py ]; then
reinstall_bot
else
current_version=$(python3 /opt/Hiddify-Telegram-Bot/version.py --version)
get_backup
update_bot
# Add cron job for reboot
add_cron_job_if_not_exists "@reboot cd $install_dir && ./restart.sh"
# Add cron job to run every 6 hours
add_cron_job_if_not_exists "0 */6 * * * cd $install_dir && python3 crontab.py --backup"
# Add cron job to run at 12:00 PM daily
add_cron_job_if_not_exists "0 12 * * * cd $install_dir && python3 crontab.py --reminder"
echo -e "${YELLOW}Current version: $current_version${RESET}"
echo -e "${YELLOW}Target version: $target_version${RESET}"
if python3 /opt/Hiddify-Telegram-Bot/update.py --current-version "$current_version" --target-version "$target_version"; then
echo "update.py has been run."
else
echo "update.py has not been run."
exit 1
fi
fi