forked from iamtheted/Marzban-Backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
229 lines (188 loc) · 6.99 KB
/
backup.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
# Bot token
# گرفتن توکن ربات از کاربر و ذخیره آن در متغیر tk
while [[ -z "$tk" ]]; do
echo "Bot token: "
read -r 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
echo "Chat id: "
read -r 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
echo "Caption (for example, your domain, to identify the database file more easily): "
read -r caption
# Cronjob
# تعیین زمانی برای اجرای این اسکریپت به صورت دورهای
while true; do
echo "Cronjob (minutes and hours) (e.g : 30 6 or 0 12) : "
read -r 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
echo "x-ui or marzban or hiddify? [x/m/h] : "
read -r 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
echo "Would you like the previous crontabs to be cleared? [y/n] : "
read -r 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
if [ -d "/var/lib/marzban/mysql" ]; then
sed -i -e 's/\s*=\s*/=/' -e 's/\s*:\s*/:/' -e 's/^\s*//' /opt/marzban/.env
docker exec marzban-mysql-1 bash -c "mkdir -p /var/lib/mysql/db-backup"
source /opt/marzban/.env
cat > "/var/lib/marzban/mysql/ac-backup.sh" <<EOL
#!/bin/bash
USER="root"
PASSWORD="$MYSQL_ROOT_PASSWORD"
databases=\$(mysql --user=\$USER --password=\$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
for db in \$databases; do
if [[ "\$db" != "information_schema" ]] && [[ "\$db" != "mysql" ]] && [[ "\$db" != "performance_schema" ]] && [[ "\$db" != "sys" ]] ; then
echo "Dumping database: \$db"
mysqldump --force --opt --user=\$USER --password=\$PASSWORD --databases \$db > /var/lib/mysql/db-backup/\$db.sql
fi
done
EOL
chmod +x /var/lib/marzban/mysql/ac-backup.sh
ZIP=$(cat <<EOF
docker exec marzban-mysql-1 bash -c "/var/lib/mysql/ac-backup.sh"
zip -r /root/ac-backup-m.zip /opt/marzban/* /var/lib/marzban/* /opt/marzban/.env -x /var/lib/marzban/mysql/\*
zip -r /root/ac-backup-m.zip /var/lib/marzban/mysql/db-backup/*
rm -rf /var/lib/marzban/mysql/db-backup/*
EOF
)
else
ZIP="zip -r /root/ac-backup-m.zip ${dir}/* /var/lib/marzban/* /opt/marzban/.env"
fi
ACLover="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
ZIP="zip /root/ac-backup-x.zip ${dbDir}/x-ui.db ${configDir}/config.json"
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
)
ACLover="hiddify backup"
else
echo "Please choose m or x or h only !"
exit 1
fi
trim() {
# remove leading and trailing whitespace/lines
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
echo -n "$var"
}
IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
caption="${caption}\n\n${ACLover}\n<code>${IP}</code>\nCreated by @AC_Lover - https://github.com/AC-Lover/backup"
comment=$(echo -e "$caption" | sed 's/<code>//g;s/<\/code>//g')
comment=$(trim "$comment")
# install zip
# نصب پکیج zip
sudo apt install zip -y
# send backup to telegram
# ارسال فایل پشتیبانی به تلگرام
cat > "/root/ac-backup-${xmh}.sh" <<EOL
rm -rf /root/ac-backup-${xmh}.zip
$ZIP
echo -e "$comment" | zip -z /root/ac-backup-${xmh}.zip
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
# 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"