forked from mahdiMGF2/botmirzapanel
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.sh
1655 lines (1391 loc) · 58.1 KB
/
install.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Checking Root Access
if [[ $EUID -ne 0 ]]; then
echo -e "\033[31m[ERROR]\033[0m Please run this script as \033[1mroot\033[0m."
exit 1
fi
# Display Logo
function show_logo() {
clear
echo -e "\033[1;34m"
echo "========================================"
echo " MIRZA INSTALL SCRIPT "
echo "========================================"
echo -e "\033[0m"
echo ""
}
# Display Menu
function show_menu() {
echo -e "\033[1;36m1)\033[0m Install Mirza Bot"
echo -e "\033[1;36m2)\033[0m Update Mirza Bot"
echo -e "\033[1;36m3)\033[0m Remove Mirza Bot"
echo -e "\033[1;36m4)\033[0m Export Database"
echo -e "\033[1;36m5)\033[0m Import Database"
echo -e "\033[1;36m6)\033[0m Configure Automated Backup"
echo -e "\033[1;36m7)\033[0m Renew SSL Certificates"
echo -e "\033[1;36m8)\033[0m Additional Bot Management"
echo -e "\033[1;36m9)\033[0m Exit"
echo ""
read -p "Select an option [1-9]: " option
case $option in
1) install_bot ;;
2) update_bot ;;
3) remove_bot ;;
4) export_database ;;
5) import_database ;;
6) auto_backup ;;
7) renew_ssl ;;
8) manage_additional_bots ;;
9)
echo -e "\033[32mExiting...\033[0m"
exit 0
;;
*)
echo -e "\033[31mInvalid option. Please try again.\033[0m"
show_menu
;;
esac
}
# Install Function
function install_bot() {
echo -e "\e[32mInstalling mirza script ... \033[0m\n"
# Function to add the Ondřej Surý PPA for PHP
add_php_ppa() {
sudo add-apt-repository -y ppa:ondrej/php || {
echo -e "\e[91mError: Failed to add PPA ondrej/php.\033[0m"
return 1
}
}
# Function to add the Ondřej Surý PPA for PHP with locale override
add_php_ppa_with_locale() {
sudo LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php || {
echo -e "\e[91mError: Failed to add PPA ondrej/php with locale override.\033[0m"
return 1
}
}
# Try adding the PPA with the system's default locale settings
if ! add_php_ppa; then
echo "Failed to add PPA with default locale, retrying with locale override..."
if ! add_php_ppa_with_locale; then
echo "Failed to add PPA even with locale override. Exiting..."
exit 1
fi
fi
sudo apt update && sudo apt upgrade -y || {
echo -e "\e[91mError: Failed to update and upgrade packages.\033[0m"
exit 1
}
echo -e "\e[92mThe server was successfully updated ...\033[0m\n"
sudo apt install -y git unzip curl || {
echo -e "\e[91mError: Failed to install required packages.\033[0m"
exit 1
}
DEBIAN_FRONTEND=noninteractive sudo apt install -y php8.2 php8.2-fpm php8.2-mysql || {
echo -e "\e[91mError: Failed to install PHP 8.2 and related packages.\033[0m"
exit 1
}
# List of required packages
PKG=(
lamp-server^
libapache2-mod-php
mysql-server
apache2
php-mbstring
php-zip
php-gd
php-json
php-curl
)
# Installing required packages with error handling
for i in "${PKG[@]}"; do
dpkg -s $i &>/dev/null
if [ $? -eq 0 ]; then
echo "$i is already installed"
else
if ! DEBIAN_FRONTEND=noninteractive sudo apt install -y $i; then
echo -e "\e[91mError installing $i. Exiting...\033[0m"
exit 1
fi
fi
done
echo -e "\n\e[92mPackages Installed, Continuing ...\033[0m\n"
# phpMyAdmin Configuration
echo 'phpmyadmin phpmyadmin/dbconfig-install boolean true' | sudo debconf-set-selections
echo 'phpmyadmin phpmyadmin/app-password-confirm password mirzahipass' | sudo debconf-set-selections
echo 'phpmyadmin phpmyadmin/mysql/admin-pass password mirzahipass' | sudo debconf-set-selections
echo 'phpmyadmin phpmyadmin/mysql/app-pass password mirzahipass' | sudo debconf-set-selections
echo 'phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2' | sudo debconf-set-selections
sudo apt-get install phpmyadmin -y || {
echo -e "\e[91mError: Failed to install phpMyAdmin.\033[0m"
exit 1
}
# Check and remove existing phpMyAdmin configuration
if [ -f /etc/apache2/conf-available/phpmyadmin.conf ]; then
sudo rm -f /etc/apache2/conf-available/phpmyadmin.conf && echo -e "\e[92mRemoved existing phpMyAdmin configuration.\033[0m"
fi
# Create symbolic link for phpMyAdmin configuration
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf || {
echo -e "\e[91mError: Failed to create symbolic link for phpMyAdmin configuration.\033[0m"
exit 1
}
sudo a2enconf phpmyadmin.conf || {
echo -e "\e[91mError: Failed to enable phpMyAdmin configuration.\033[0m"
exit 1
}
sudo systemctl restart apache2 || {
echo -e "\e[91mError: Failed to restart Apache2 service.\033[0m"
exit 1
}
# Additional package installations with error handling
sudo apt-get install -y php-soap || {
echo -e "\e[91mError: Failed to install php-soap.\033[0m"
exit 1
}
sudo apt-get install libapache2-mod-php || {
echo -e "\e[91mError: Failed to install libapache2-mod-php.\033[0m"
exit 1
}
sudo systemctl enable mysql.service || {
echo -e "\e[91mError: Failed to enable MySQL service.\033[0m"
exit 1
}
sudo systemctl start mysql.service || {
echo -e "\e[91mError: Failed to start MySQL service.\033[0m"
exit 1
}
sudo systemctl enable apache2 || {
echo -e "\e[91mError: Failed to enable Apache2 service.\033[0m"
exit 1
}
sudo systemctl start apache2 || {
echo -e "\e[91mError: Failed to start Apache2 service.\033[0m"
exit 1
}
sudo apt-get install ufw -y || {
echo -e "\e[91mError: Failed to install UFW.\033[0m"
exit 1
}
ufw allow 'Apache' || {
echo -e "\e[91mError: Failed to allow Apache in UFW.\033[0m"
exit 1
}
sudo systemctl restart apache2 || {
echo -e "\e[91mError: Failed to restart Apache2 service after UFW update.\033[0m"
exit 1
}
sudo apt-get install -y git || {
echo -e "\e[91mError: Failed to install Git.\033[0m"
exit 1
}
sudo apt-get install -y wget || {
echo -e "\e[91mError: Failed to install Wget.\033[0m"
exit 1
}
sudo apt-get install -y unzip || {
echo -e "\e[91mError: Failed to install Unzip.\033[0m"
exit 1
}
sudo apt install curl -y || {
echo -e "\e[91mError: Failed to install cURL.\033[0m"
exit 1
}
sudo apt-get install -y php-ssh2 || {
echo -e "\e[91mError: Failed to install php-ssh2.\033[0m"
exit 1
}
sudo apt-get install -y libssh2-1-dev libssh2-1 || {
echo -e "\e[91mError: Failed to install libssh2.\033[0m"
exit 1
}
sudo systemctl restart apache2.service || {
echo -e "\e[91mError: Failed to restart Apache2 service.\033[0m"
exit 1
}
# Check and remove existing directory before cloning Git repository
BOT_DIR="/var/www/html/mirzabotconfig"
if [ -d "$BOT_DIR" ]; then
echo -e "\e[93mDirectory $BOT_DIR already exists. Removing...\033[0m"
sudo rm -rf "$BOT_DIR" || {
echo -e "\e[91mError: Failed to remove existing directory $BOT_DIR.\033[0m"
exit 1
}
fi
# Default repository URL
REPO_URL="https://github.com/mahdiMGF2/botmirzapanel.git"
# Check for version flag
if [[ "$1" == -v* ]]; then
VERSION=$(echo "$1" | sed 's/-v//')
REPO_URL="https://github.com/mahdiMGF2/botmirzapanel/archive/refs/tags/$VERSION.zip"
elif [[ "$1" == "-v" && -n "$2" ]]; then
VERSION="$2"
REPO_URL="https://github.com/mahdiMGF2/botmirzapanel/archive/refs/tags/$VERSION.zip"
fi
# Validate the REPO_URL
if ! curl --output /dev/null --silent --head --fail "$REPO_URL"; then
echo -e "\e[91mError: The specified version or URL is invalid.\033[0m"
exit 1
fi
# Clone or download the repository
if [[ $REPO_URL == *.zip ]]; then
TEMP_DIR="/tmp/mirzabot"
mkdir -p "$TEMP_DIR"
curl -L -o "$TEMP_DIR/bot.zip" "$REPO_URL" || {
echo -e "\e[91mError: Failed to download the specified version.\033[0m"
exit 1
}
if ! unzip "$TEMP_DIR/bot.zip" -d "$TEMP_DIR"; then
echo -e "\e[91mError: Failed to unzip the repository. Please check the version or URL.\033[0m"
rm -rf "$TEMP_DIR"
exit 1
fi
# Find the extracted directory (assuming only one subdirectory exists)
EXTRACTED_DIR=$(find "$TEMP_DIR" -mindepth 1 -maxdepth 1 -type d)
if [ -z "$EXTRACTED_DIR" ]; then
echo -e "\e[91mError: Extracted directory not found.\033[0m"
exit 1
fi
mv "$EXTRACTED_DIR" "$BOT_DIR" || {
echo -e "\e[91mError: Failed to move files to $BOT_DIR.\033[0m"
exit 1
}
rm -rf "$TEMP_DIR"
else
git clone "$REPO_URL" "$BOT_DIR" || {
echo -e "\e[91mError: Failed to clone the repository.\033[0m"
exit 1
}
fi
sudo chown -R www-data:www-data "$BOT_DIR" || {
echo -e "\e[91mError: Failed to set ownership for $BOT_DIR.\033[0m"
exit 1
}
sudo chmod -R 755 "$BOT_DIR" || {
echo -e "\e[91mError: Failed to set permissions for $BOT_DIR.\033[0m"
exit 1
}
echo -e "\n\033[33mMirza config and script have been installed successfully.\033[0m"
wait
if [ ! -d "/root/confmirza" ]; then
sudo mkdir /root/confmirza || {
echo -e "\e[91mError: Failed to create /root/confmirza directory.\033[0m"
exit 1
}
sleep 1
touch /root/confmirza/dbrootmirza.txt || {
echo -e "\e[91mError: Failed to create dbrootmirza.txt.\033[0m"
exit 1
}
sudo chmod -R 777 /root/confmirza/dbrootmirza.txt || {
echo -e "\e[91mError: Failed to set permissions for dbrootmirza.txt.\033[0m"
exit 1
}
sleep 1
randomdbpasstxt=$(openssl rand -base64 10 | tr -dc 'a-zA-Z0-9' | cut -c1-8)
ASAS="$"
echo "${ASAS}user = 'root';" >> /root/confmirza/dbrootmirza.txt
echo "${ASAS}pass = '${randomdbpasstxt}';" >> /root/confmirza/dbrootmirza.txt
echo "${ASAS}path = '${RANDOM_NUMBER}';" >> /root/confmirza/dbrootmirza.txt
sleep 1
passs=$(cat /root/confmirza/dbrootmirza.txt | grep '$pass' | cut -d"'" -f2)
userrr=$(cat /root/confmirza/dbrootmirza.txt | grep '$user' | cut -d"'" -f2)
sudo mysql -u $userrr -p$passs -e "alter user '$userrr'@'localhost' identified with mysql_native_password by '$passs';FLUSH PRIVILEGES;" || {
echo -e "\e[91mError: Failed to alter MySQL user. Attempting recovery...\033[0m"
# Enable skip-grant-tables at the end of the file
sudo sed -i '$ a skip-grant-tables' /etc/mysql/mysql.conf.d/mysqld.cnf
sudo systemctl restart mysql
# Access MySQL to reset the root user
sudo mysql <<EOF
DROP USER IF EXISTS 'root'@'localhost';
CREATE USER 'root'@'localhost' IDENTIFIED BY '${passs}';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
# Disable skip-grant-tables
sudo sed -i '/skip-grant-tables/d' /etc/mysql/mysql.conf.d/mysqld.cnf
sudo systemctl restart mysql
# Retry MySQL login with the new credentials
echo "SELECT 1" | mysql -u$userrr -p$passs 2>/dev/null || {
echo -e "\e[91mError: Recovery failed. MySQL login still not working.\033[0m"
exit 1
}
}
echo "Folder created successfully!"
else
echo "Folder already exists."
fi
clear
echo " "
echo -e "\e[32m SSL \033[0m\n"
read -p "Enter the domain: " domainname
while [[ ! "$domainname" =~ ^[a-zA-Z0-9.-]+$ ]]; do
echo -e "\e[91mInvalid domain format. Please try again.\033[0m"
read -p "Enter the domain: " domainname
done
DOMAIN_NAME="$domainname"
PATHS=$(cat /root/confmirza/dbrootmirza.txt | grep '$path' | cut -d"'" -f2)
sudo ufw allow 80 || {
echo -e "\e[91mError: Failed to allow port 80 in UFW.\033[0m"
exit 1
}
sudo ufw allow 443 || {
echo -e "\e[91mError: Failed to allow port 443 in UFW.\033[0m"
exit 1
}
echo -e "\033[33mDisable apache2\033[0m"
wait
sudo systemctl stop apache2 || {
echo -e "\e[91mError: Failed to stop Apache2.\033[0m"
exit 1
}
sudo systemctl disable apache2 || {
echo -e "\e[91mError: Failed to disable Apache2.\033[0m"
exit 1
}
sudo apt install letsencrypt -y || {
echo -e "\e[91mError: Failed to install letsencrypt.\033[0m"
exit 1
}
sudo systemctl enable certbot.timer || {
echo -e "\e[91mError: Failed to enable certbot timer.\033[0m"
exit 1
}
sudo certbot certonly --standalone --agree-tos --preferred-challenges http -d $DOMAIN_NAME || {
echo -e "\e[91mError: Failed to generate SSL certificate.\033[0m"
exit 1
}
sudo apt install python3-certbot-apache -y || {
echo -e "\e[91mError: Failed to install python3-certbot-apache.\033[0m"
exit 1
}
sudo certbot --apache --agree-tos --preferred-challenges http -d $DOMAIN_NAME || {
echo -e "\e[91mError: Failed to configure SSL with Certbot.\033[0m"
exit 1
}
echo " "
echo -e "\033[33mEnable apache2\033[0m"
wait
sudo systemctl enable apache2 || {
echo -e "\e[91mError: Failed to enable Apache2.\033[0m"
exit 1
}
sudo systemctl start apache2 || {
echo -e "\e[91mError: Failed to start Apache2.\033[0m"
exit 1
}
clear
printf "\e[33m[+] \e[36mBot Token: \033[0m"
read YOUR_BOT_TOKEN
while [[ ! "$YOUR_BOT_TOKEN" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]]; do
echo -e "\e[91mInvalid bot token format. Please try again.\033[0m"
printf "\e[33m[+] \e[36mBot Token: \033[0m"
read YOUR_BOT_TOKEN
done
printf "\e[33m[+] \e[36mChat id: \033[0m"
read YOUR_CHAT_ID
while [[ ! "$YOUR_CHAT_ID" =~ ^-?[0-9]+$ ]]; do
echo -e "\e[91mInvalid chat ID format. Please try again.\033[0m"
printf "\e[33m[+] \e[36mChat id: \033[0m"
read YOUR_CHAT_ID
done
YOUR_DOMAIN="$DOMAIN_NAME"
while true; do
printf "\e[33m[+] \e[36musernamebot: \033[0m"
read YOUR_BOTNAME
if [ "$YOUR_BOTNAME" != "" ]; then
break
else
echo -e "\e[91mError: Bot username cannot be empty. Please enter a valid username.\033[0m"
fi
done
ROOT_PASSWORD=$(cat /root/confmirza/dbrootmirza.txt | grep '$pass' | cut -d"'" -f2)
ROOT_USER="root"
echo "SELECT 1" | mysql -u$ROOT_USER -p$ROOT_PASSWORD 2>/dev/null || {
echo -e "\e[91mError: MySQL connection failed.\033[0m"
exit 1
}
if [ $? -eq 0 ]; then
wait
randomdbpass=$(openssl rand -base64 10 | tr -dc 'a-zA-Z0-9' | cut -c1-8)
randomdbdb=$(openssl rand -base64 10 | tr -dc 'a-zA-Z' | cut -c1-8)
if [[ $(mysql -u root -p$ROOT_PASSWORD -e "SHOW DATABASES LIKE 'mirzabot'") ]]; then
clear
echo -e "\n\e[91mYou have already created the database\033[0m\n"
else
dbname=mirzabot
clear
echo -e "\n\e[32mPlease enter the database username!\033[0m"
printf "[+] Default user name is \e[91m${randomdbdb}\e[0m ( let it blank to use this user name ): "
read dbuser
if [ "$dbuser" = "" ]; then
dbuser=$randomdbdb
fi
echo -e "\n\e[32mPlease enter the database password!\033[0m"
printf "[+] Default password is \e[91m${randomdbpass}\e[0m ( let it blank to use this password ): "
read dbpass
if [ "$dbpass" = "" ]; then
dbpass=$randomdbpass
fi
mysql -u root -p$ROOT_PASSWORD -e "CREATE DATABASE $dbname;" -e "CREATE USER '$dbuser'@'%' IDENTIFIED WITH mysql_native_password BY '$dbpass';GRANT ALL PRIVILEGES ON * . * TO '$dbuser'@'%';FLUSH PRIVILEGES;" -e "CREATE USER '$dbuser'@'localhost' IDENTIFIED WITH mysql_native_password BY '$dbpass';GRANT ALL PRIVILEGES ON * . * TO '$dbuser'@'localhost';FLUSH PRIVILEGES;" || {
echo -e "\e[91mError: Failed to create database or user.\033[0m"
exit 1
}
echo -e "\n\e[95mDatabase Created.\033[0m"
clear
ASAS="$"
wait
sleep 1
file_path="/var/www/html/mirzabotconfig/config.php"
if [ -f "$file_path" ]; then
rm "$file_path" || {
echo -e "\e[91mError: Failed to delete old config.php.\033[0m"
exit 1
}
echo -e "File deleted successfully."
else
echo -e "File not found."
fi
sleep 1
secrettoken=$(openssl rand -base64 10 | tr -dc 'a-zA-Z0-9' | cut -c1-8)
echo -e "<?php" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}APIKEY = '${YOUR_BOT_TOKEN}';" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}usernamedb = '${dbuser}';" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}passworddb = '${dbpass}';" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}dbname = '${dbname}';" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}domainhosts = '${YOUR_DOMAIN}/mirzabotconfig';" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}adminnumber = '${YOUR_CHAT_ID}';" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}usernamebot = '${YOUR_BOTNAME}';" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}secrettoken = '${secrettoken}';" >> /var/www/html/mirzabotconfig/config.php
echo -e "${ASAS}connect = mysqli_connect('localhost', \$usernamedb, \$passworddb, \$dbname);" >> /var/www/html/mirzabotconfig/config.php
echo -e "if (${ASAS}connect->connect_error) {" >> /var/www/html/mirzabotconfig/config.php
echo -e "die(' The connection to the database failed:' . ${ASAS}connect->connect_error);" >> /var/www/html/mirzabotconfig/config.php
echo -e "}" >> /var/www/html/mirzabotconfig/config.php
echo -e "mysqli_set_charset(${ASAS}connect, 'utf8mb4');" >> /var/www/html/mirzabotconfig/config.php
text_to_save=$(cat <<EOF
\$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
\$dsn = "mysql:host=localhost;dbname=${ASAS}dbname;charset=utf8mb4";
try {
\$pdo = new PDO(\$dsn, \$usernamedb, \$passworddb, \$options);
} catch (\PDOException \$e) {
throw new \PDOException(\$e->getMessage(), (int)\$e->getCode());
}
EOF
)
echo -e "$text_to_save" >> /var/www/html/mirzabotconfig/config.php
echo -e "?>" >> /var/www/html/mirzabotconfig/config.php
sleep 1
curl -F "url=https://${YOUR_DOMAIN}/mirzabotconfig/index.php" \
-F "secret_token=${secrettoken}" \
"https://api.telegram.org/bot${YOUR_BOT_TOKEN}/setWebhook" || {
echo -e "\e[91mError: Failed to set webhook for bot.\033[0m"
exit 1
}
MESSAGE="✅ The bot is installed! for start the bot send /start command."
curl -s -X POST "https://api.telegram.org/bot${YOUR_BOT_TOKEN}/sendMessage" -d chat_id="${YOUR_CHAT_ID}" -d text="$MESSAGE" || {
echo -e "\e[91mError: Failed to send message to Telegram.\033[0m"
exit 1
}
sleep 1
sudo systemctl start apache2 || {
echo -e "\e[91mError: Failed to start Apache2.\033[0m"
exit 1
}
url="https://${YOUR_DOMAIN}/mirzabotconfig/table.php"
curl $url || {
echo -e "\e[91mError: Failed to fetch URL from domain.\033[0m"
exit 1
}
clear
echo " "
echo -e "\e[102mDomain Bot: https://${YOUR_DOMAIN}\033[0m"
echo -e "\e[104mDatabase address: https://${YOUR_DOMAIN}/phpmyadmin\033[0m"
echo -e "\e[33mDatabase name: \e[36m${dbname}\033[0m"
echo -e "\e[33mDatabase username: \e[36m${dbuser}\033[0m"
echo -e "\e[33mDatabase password: \e[36m${dbpass}\033[0m"
echo " "
echo -e "Mirza Bot"
fi
elif [ "$ROOT_PASSWORD" = "" ] || [ "$ROOT_USER" = "" ]; then
echo -e "\n\e[36mThe password is empty.\033[0m\n"
else
echo -e "\n\e[36mThe password is not correct.\033[0m\n"
fi
}
# Update Function
function update_bot() {
echo "Updating Mirza Bot..."
# Server Update
if ! sudo apt update && sudo apt upgrade -y; then
echo -e "\e[91mError updating the server. Exiting...\033[0m"
exit 1
fi
echo -e "\e[92mThe server was successfully updated...\033[0m\n"
# Install Required Tools
if ! sudo apt-get install -y git curl; then
echo -e "\e[91mError installing required packages. Exiting...\033[0m"
exit 1
fi
echo -e "\n\e[92mUpdating Bot...\033[0m\n"
sleep 2
# Check the config.php file and transfer it
CONFIG_PATH="/var/www/html/mirzabotconfig/config.php"
TEMP_CONFIG_PATH="/root/config.php"
if [ -f "$CONFIG_PATH" ]; then
mv "$CONFIG_PATH" "$TEMP_CONFIG_PATH" || {
echo -e "\e[91mFailed to move config.php. Exiting...\033[0m"
exit 1
}
else
echo -e "\e[91mconfig.php not found. Exiting...\033[0m"
exit 1
fi
# Remove the old version of the bot
if ! rm -rf /var/www/html/mirzabotconfig; then
echo -e "\e[91mFailed to remove old bot directory. Exiting...\033[0m"
exit 1
fi
# Clone the new version of the bot
if ! git clone https://github.com/mahdiMGF2/botmirzapanel.git /var/www/html/mirzabotconfig; then
echo -e "\e[91mFailed to clone the repository. Exiting...\033[0m"
exit 1
fi
# Set Ownership and Permissions
sudo chown -R www-data:www-data /var/www/html/mirzabotconfig/
sudo chmod -R 755 /var/www/html/mirzabotconfig/
# Restore Configuration File
if ! mv "$TEMP_CONFIG_PATH" "$CONFIG_PATH"; then
echo -e "\e[91mFailed to restore config.php. Exiting...\033[0m"
exit 1
fi
# Execute the table.php script
URL=$(grep '$domainhosts' "$CONFIG_PATH" | cut -d"'" -f2)
if [ -z "$URL" ]; then
echo -e "\e[91mFailed to extract domain URL from config.php. Exiting...\033[0m"
exit 1
fi
if ! curl "https://$URL/table.php"; then
echo -e "\e[91mFailed to execute table.php. Exiting...\033[0m"
exit 1
fi
clear
echo -e "\n\e[92mMirza Bot has been successfully updated!\033[0m"
}
# Delete Function
function remove_bot() {
echo -e "\e[33mStarting Mirza Bot removal process...\033[0m"
LOG_FILE="/var/log/remove_bot.log"
echo "Log file: $LOG_FILE" > "$LOG_FILE"
# User Confirmation
read -p "Are you sure you want to remove Mirza Bot, MySQL, and all its dependencies? (y/n): " choice
if [[ "$choice" != "y" ]]; then
echo "Aborting..." | tee -a "$LOG_FILE"
exit 0
fi
echo "Removing Mirza Bot..." | tee -a "$LOG_FILE"
# Delete the Bot Directory
BOT_DIR="/var/www/html/mirzabotconfig"
if [ -d "$BOT_DIR" ]; then
sudo rm -rf "$BOT_DIR" && echo -e "\e[92mBot directory removed: $BOT_DIR\033[0m" | tee -a "$LOG_FILE" || {
echo -e "\e[91mFailed to remove bot directory: $BOT_DIR. Exiting...\033[0m" | tee -a "$LOG_FILE"
exit 1
}
else
echo -e "\e[93mBot directory not found: $BOT_DIR\033[0m" | tee -a "$LOG_FILE"
fi
# Delete Configuration File
CONFIG_PATH="/root/config.php"
if [ -f "$CONFIG_PATH" ]; then
sudo shred -u -n 5 "$CONFIG_PATH" && echo -e "\e[92mConfig file securely removed: $CONFIG_PATH\033[0m" | tee -a "$LOG_FILE" || {
echo -e "\e[91mFailed to securely remove config file: $CONFIG_PATH\033[0m" | tee -a "$LOG_FILE"
}
fi
# Delete MySQL and Database Data
echo -e "\e[33mRemoving MySQL and database...\033[0m" | tee -a "$LOG_FILE"
sudo systemctl stop mysql
sudo systemctl disable mysql
sudo systemctl daemon-reload
sudo apt --fix-broken install -y
sudo apt-get purge -y mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*
sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql /var/log/mysql.* /usr/lib/mysql /usr/include/mysql /usr/share/mysql
sudo rm /lib/systemd/system/mysql.service
sudo rm /etc/init.d/mysql
sudo dpkg --remove --force-remove-reinstreq mysql-server mysql-server-8.0
sudo find /etc/systemd /lib/systemd /usr/lib/systemd -name "*mysql*" -exec rm -f {} \;
sudo apt-get purge -y mysql-server mysql-server-8.0 mysql-client mysql-client-8.0
sudo apt-get purge -y mysql-client-core-8.0 mysql-server-core-8.0 mysql-common php-mysql php8.2-mysql php8.3-mysql php-mariadb-mysql-kbs
sudo apt-get autoremove --purge -y
sudo apt-get clean
sudo apt-get update
echo -e "\e[92mMySQL has been completely removed.\033[0m" | tee -a "$LOG_FILE"
# Delete PHPMyAdmin
echo -e "\e[33mRemoving PHPMyAdmin...\033[0m" | tee -a "$LOG_FILE"
if dpkg -s phpmyadmin &>/dev/null; then
sudo apt-get purge -y phpmyadmin && echo -e "\e[92mPHPMyAdmin removed.\033[0m" | tee -a "$LOG_FILE"
sudo apt-get autoremove -y && sudo apt-get autoclean -y
else
echo -e "\e[93mPHPMyAdmin is not installed.\033[0m" | tee -a "$LOG_FILE"
fi
# Delete Apache and PHP Settings
echo -e "\e[33mRemoving Apache and PHP configurations...\033[0m" | tee -a "$LOG_FILE"
sudo a2disconf phpmyadmin.conf &>/dev/null
sudo rm -f /etc/apache2/conf-available/phpmyadmin.conf
sudo systemctl restart apache2
# Remove Unnecessary Packages
echo -e "\e[33mRemoving additional packages...\033[0m" | tee -a "$LOG_FILE"
sudo apt-get remove -y php-soap php-ssh2 libssh2-1-dev libssh2-1 \
&& echo -e "\e[92mRemoved additional PHP packages.\033[0m" | tee -a "$LOG_FILE" || echo -e "\e[93mSome additional PHP packages may not be installed.\033[0m" | tee -a "$LOG_FILE"
# Reset Firewall (without changing SSL rules)
echo -e "\e[33mResetting firewall rules (except SSL)...\033[0m" | tee -a "$LOG_FILE"
sudo ufw delete allow 'Apache'
sudo ufw reload
echo -e "\e[92mMirza Bot, MySQL, and their dependencies have been completely removed.\033[0m" | tee -a "$LOG_FILE"
}
# Function to extract database credentials from config.php
function extract_db_credentials() {
CONFIG_PATH="/var/www/html/mirzabotconfig/config.php"
if [ ! -f "$CONFIG_PATH" ]; then
echo -e "\033[31m[ERROR]\033[0m File config.php not found at $CONFIG_PATH."
return 1
fi
# Extracting credentials with more precision
DB_USER=$(grep '^\$usernamedb' "$CONFIG_PATH" | awk -F"'" '{print $2}')
DB_PASS=$(grep '^\$passworddb' "$CONFIG_PATH" | awk -F"'" '{print $2}')
DB_NAME=$(grep '^\$dbname' "$CONFIG_PATH" | awk -F"'" '{print $2}')
if [ -z "$DB_USER" ] || [ -z "$DB_PASS" ] || [ -z "$DB_NAME" ]; then
echo -e "\033[31m[ERROR]\033[0m Could not extract database credentials from config.php."
return 1
fi
# Add these lines to debug extracted variables
echo "DB_USER: $DB_USER"
echo "DB_PASS: $DB_PASS"
echo "DB_NAME: $DB_NAME"
export DB_USER DB_PASS DB_NAME
return 0
}
# Export Database Function
function export_database() {
echo -e "\033[33mChecking database configuration...\033[0m"
if ! extract_db_credentials; then
return 1
fi
echo -e "\033[33mVerifying database existence...\033[0m"
if ! mysql -u "$DB_USER" -p"$DB_PASS" -e "USE $DB_NAME;" 2>/dev/null; then
echo -e "\033[31m[ERROR]\033[0m Database $DB_NAME does not exist or credentials are incorrect."
return 1
fi
BACKUP_FILE="/root/${DB_NAME}_backup.sql"
echo -e "\033[33mCreating backup at $BACKUP_FILE...\033[0m"
if ! mysqldump -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" > "$BACKUP_FILE"; then
echo -e "\033[31m[ERROR]\033[0m Failed to create database backup."
return 1
fi
echo -e "\033[32mBackup successfully created at $BACKUP_FILE.\033[0m"
}
# Import Database Function
function import_database() {
echo -e "\033[33mChecking database configuration...\033[0m"
if ! extract_db_credentials; then
return 1
fi
echo -e "\033[33mVerifying database existence...\033[0m"
if ! mysql -u "$DB_USER" -p"$DB_PASS" -e "USE $DB_NAME;" 2>/dev/null; then
echo -e "\033[31m[ERROR]\033[0m Database $DB_NAME does not exist or credentials are incorrect."
return 1
fi
while true; do
read -p "Enter the path to the backup file [default: /root/${DB_NAME}_backup.sql]: " BACKUP_FILE
BACKUP_FILE=${BACKUP_FILE:-/root/${DB_NAME}_backup.sql}
if [[ -f "$BACKUP_FILE" && "$BACKUP_FILE" =~ \.sql$ ]]; then
break
else
echo -e "\033[31m[ERROR]\033[0m Invalid file path or format. Please provide a valid .sql file."
fi
done
echo -e "\033[33mImporting backup from $BACKUP_FILE...\033[0m"
if ! mysql -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" < "$BACKUP_FILE"; then
echo -e "\033[31m[ERROR]\033[0m Failed to import database from backup file."
return 1
fi
echo -e "\033[32mDatabase successfully imported from $BACKUP_FILE.\033[0m"
}
# Function for automated backup
function auto_backup() {
echo -e "\033[33mChecking database configuration...\033[0m"
if ! extract_db_credentials; then
return 1
fi
echo -e "\033[33mVerifying database existence...\033[0m"
if ! mysql -u "$DB_USER" -p"$DB_PASS" -e "USE $DB_NAME;" 2>/dev/null; then
echo -e "\033[31m[ERROR]\033[0m Database $DB_NAME does not exist or credentials are incorrect."
return 1
fi
TELEGRAM_TOKEN=$(grep '\$APIKEY' "$CONFIG_PATH" | cut -d"'" -f2)
TELEGRAM_CHAT_ID=$(grep '\$adminnumber' "$CONFIG_PATH" | cut -d"'" -f2)
if [ -z "$TELEGRAM_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
echo -e "\033[31m[ERROR]\033[0m Telegram token or chat ID not found in config.php."
return 1
fi
while true; do
echo -e "\033[36mChoose backup frequency:\033[0m"
echo -e "\033[36m1) Every minute\033[0m"
echo -e "\033[36m2) Every hour\033[0m"
echo -e "\033[36m3) Every day\033[0m"
read -p "Enter your choice (1-3): " frequency
case $frequency in
1) cron_time="* * * * *" ; break ;;
2) cron_time="0 * * * *" ; break ;;
3) cron_time="0 0 * * *" ; break ;;
*)
echo -e "\033[31mInvalid option. Please try again.\033[0m"
;;
esac
done
BACKUP_SCRIPT="/root/auto_backup.sh"
cat <<EOF > "$BACKUP_SCRIPT"
#!/bin/bash
BACKUP_FILE="/root/\${DB_NAME}_\$(date +\"%Y%m%d_%H%M%S\").sql"
if mysqldump -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" > "\$BACKUP_FILE"; then
curl -F document=@"\$BACKUP_FILE" "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendDocument" -F chat_id="$TELEGRAM_CHAT_ID"
rm "\$BACKUP_FILE"
else
echo -e "\033[31m[ERROR]\033[0m Failed to create database backup."
fi
EOF
chmod +x "$BACKUP_SCRIPT"
(crontab -l 2>/dev/null; echo "$cron_time bash $BACKUP_SCRIPT") | crontab -
echo -e "\033[32mAutomated backup configured successfully.\033[0m"
}
# Function to renew SSL certificates
function renew_ssl() {
echo -e "\033[33mStarting SSL renewal process...\033[0m"
if ! command -v certbot &>/dev/null; then
echo -e "\033[31m[ERROR]\033[0m Certbot is not installed. Please install Certbot to proceed."
return 1
fi
if sudo certbot renew; then
echo -e "\033[32mSSL certificates successfully renewed.\033[0m"
sudo systemctl reload apache2 || {
echo -e "\033[31m[WARNING]\033[0m Failed to reload Apache. Please check manually."
}
else
echo -e "\033[31m[ERROR]\033[0m SSL renewal failed. Please check Certbot logs for more details."
return 1
fi
}
# Function to Manage Additional Bots
function manage_additional_bots() {
echo -e "\033[36m1) Install Additional Bot\033[0m"
echo -e "\033[36m2) Update Additional Bot\033[0m"
echo -e "\033[36m3) Remove Additional Bot\033[0m"
echo -e "\033[36m4) Export Additional Bot Database\033[0m"
echo -e "\033[36m5) Import Additional Bot Database\033[0m"
echo -e "\033[36m6) Configure Automated Backup for Additional Bot\033[0m"
echo -e "\033[36m7) Back to Main Menu\033[0m"
echo ""
read -p "Select an option [1-7]: " sub_option
case $sub_option in
1) install_additional_bot ;;
2) update_additional_bot ;;
3) remove_additional_bot ;;
4) export_additional_bot_database ;;
5) import_additional_bot_database ;;
6) configure_backup_additional_bot ;;
7) show_menu ;;
*)
echo -e "\033[31mInvalid option. Please try again.\033[0m"
manage_additional_bots
;;
esac
}
# Added Function for Installing Additional Bot
function install_additional_bot() {
clear
echo -e "\033[33mStarting Additional Bot Installation...\033[0m"
# Check for root credentials file
ROOT_CREDENTIALS_FILE="/root/confmirza/dbrootmirza.txt"
if [[ ! -f "$ROOT_CREDENTIALS_FILE" ]]; then
echo -e "\033[31mError: Root credentials file not found at $ROOT_CREDENTIALS_FILE.\033[0m"
echo -ne "\033[36mPlease enter the root MySQL password: \033[0m"
read -s ROOT_PASS
echo
ROOT_USER="root"
else
ROOT_USER=$(grep '\$user =' "$ROOT_CREDENTIALS_FILE" | awk -F"'" '{print $2}')
ROOT_PASS=$(grep '\$pass =' "$ROOT_CREDENTIALS_FILE" | awk -F"'" '{print $2}')
if [[ -z "$ROOT_USER" || -z "$ROOT_PASS" ]]; then
echo -e "\033[31mError: Could not extract root credentials from file.\033[0m"
return 1
fi
fi
# Request Domain Name
while true; do
echo -ne "\033[36mEnter the domain for the additional bot: \033[0m"
read DOMAIN_NAME
if [[ "$DOMAIN_NAME" =~ ^[a-zA-Z0-9.-]+$ ]]; then
break
else
echo -e "\033[31mInvalid domain format. Please try again.\033[0m"
fi
done
# Stop Apache to free port 80
echo -e "\033[33mStopping Apache to free port 80...\033[0m"
sudo systemctl stop apache2
# Obtain SSL Certificate
echo -e "\033[33mObtaining SSL certificate...\033[0m"
sudo certbot certonly --standalone --agree-tos --preferred-challenges http -d "$DOMAIN_NAME" || {
echo -e "\033[31mError obtaining SSL certificate.\033[0m"
return 1
}
# Restart Apache
echo -e "\033[33mRestarting Apache...\033[0m"
sudo systemctl start apache2