-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpbxreplica.sh
655 lines (583 loc) · 19.4 KB
/
vpbxreplica.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
#!/bin/bash
# This code is the property of VitalPBX LLC Company
# License: Proprietary
# Date: 28-Sep-2023
# VitalPBX Replica with MariaDB Replica and Lsync
#
set -e
function jumpto
{
label=$start
cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
eval "$cmd"
exit
}
echo -e "\n"
echo -e "************************************************************"
echo -e "* Welcome to the VitalPBX Replicay installation *"
echo -e "* All options are mandatory *"
echo -e "************************************************************"
filename="config.txt"
if [ -f $filename ]; then
echo -e "config file"
n=1
while read line; do
case $n in
1)
ip_master=$line
;;
2)
ip_standby=$line
;;
esac
n=$((n+1))
done < $filename
echo -e "IP Server 1.............. > $ip_master"
echo -e "IP Server 2.............. > $ip_standby"
fi
while [[ $ip_master == '' ]]
do
read -p "IP Server 1............. > " ip_master
done
while [[ $ip_standby == '' ]]
do
read -p "IP Server 2............. > " ip_standby
done
echo -e "************************************************************"
echo -e "* Check Information *"
echo -e "* Make sure you have internet on both servers *"
echo -e "************************************************************"
while [[ $veryfy_info != yes && $veryfy_info != no ]]
do
read -p "Are you sure to continue with this settings? (yes,no) > " veryfy_info
done
if [ "$veryfy_info" = yes ] ;then
echo -e "************************************************************"
echo -e "* Starting to run the scripts *"
echo -e "************************************************************"
else
exit;
fi
cat > config.txt << EOF
$ip_master
$ip_standby
EOF
start="rename_tenant_id_in_server2"
case $step in
1)
start="rename_tenant_id_in_server2"
;;
2)
start="configuring_firewall"
;;
3)
start="create_lsyncd_config_file"
;;
4)
start="create_mariadb_replica"
;;
esac
jumpto $start
echo -e "*** Done Step 1 ***"
echo -e "1" > step.txt
rename_tenant_id_in_server2:
echo -e "************************************************************"
echo -e "* Remove Tenant in Server 2 *"
echo -e "************************************************************"
remote_tenant_id=`ssh root@$ip_standby "ls /var/lib/vitalpbx/static/"`
ssh root@$ip_standby "rm -rf /var/lib/vitalpbx/static/$remote_tenant_id"
echo -e "*** Done Step 2 ***"
echo -e "2" > step.txt
configuring_firewall:
echo -e "************************************************************"
echo -e "* Configuring Temporal Firewall *"
echo -e "************************************************************"
#Create temporal Firewall Rules in Server 1 and 2
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload
ssh root@$ip_standby "firewall-cmd --permanent --zone=public --add-port=3306/tcp"
ssh root@$ip_standby "firewall-cmd --reload"
echo -e "************************************************************"
echo -e "* Configuring Permanent Firewall *"
echo -e "* Creating Firewall Services in VitalPBX in Server 1 *"
echo -e "************************************************************"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('MariaDB Client', 'tcp', '3306')"
echo -e "************************************************************"
echo -e "* Configuring Permanent Firewall *"
echo -e "* Creating Firewall Rules in VitalPBX in Server 1 *"
echo -e "************************************************************"
last_index=$(mysql -uroot ombutel -e "SELECT MAX(\`index\`) AS Consecutive FROM ombu_firewall_rules" | awk 'NR==2')
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'MariaDB Client'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_whitelist (host, description, \`default\`) VALUES ('$ip_master', 'Server 1 IP', 'no')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_whitelist (host, description, \`default\`) VALUES ('$ip_standby', 'Server 2 IP', 'no')"
echo -e "*** Done Step 3 ***"
echo -e "3" > step.txt
create_lsyncd_config_file:
echo -e "************************************************************"
echo -e "* Configure Sync in Server 1 *"
echo -e "************************************************************"
if [ ! -d "/usr/share/vitxi/" ] ;then
mkdir /usr/share/vitxi
mkdir /usr/share/vitxi/backend
mkdir /usr/share/vitxi/backend/storage
fi
if [ ! -d "/var/lib/vitxi/" ] ;then
mkdir /var/lib/vitxi
fi
if [ ! -d "/home/sync/var/spool/asterisk/sqlite3_temp" ] ;then
mkdir -p /home/sync/var/spool/asterisk/sqlite3_temp
fi
ssh root@$ip_standby [[ ! -d /home/sync/var/spool/asterisk/sqlite3_temp ]] && ssh root@$ip_standby "mkdir -p /home/sync/var/spool/asterisk/sqlite3_temp" || echo "Path exist";
if [ ! -d "/etc/lsyncd" ] ;then
mkdir /etc/lsyncd
fi
if [ ! -d "/var/log/lsyncd" ] ;then
mkdir /var/log/lsyncd
touch /var/log/lsyncd/lsyncd.{log,status}
fi
cat > /etc/lsyncd/lsyncd.conf.lua << EOF
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
statusInterval = 20,
nodaemon = false,
insist = true
}
sync {
default.rsyncssh,
source = "/var/spool/asterisk/monitor",
host = "$ip_standby",
targetdir = "/var/spool/asterisk/monitor",
rsync = {
owner = true,
group = true
}
}
sync {
default.rsyncssh,
source = "/var/lib/asterisk/",
host = "$ip_standby",
targetdir = "/var/lib/asterisk/",
rsync = {
binary = "/usr/bin/rsync",
owner = true,
group = true,
archive = "true",
_extra = {
"--include=astdb.sqlite3",
"--exclude=*"
}
}
}
sync {
default.rsyncssh,
source = "/usr/share/vitxi/backend/",
host = "$ip_standby",
targetdir = "/usr/share/vitxi/backend/",
rsync = {
binary = "/usr/bin/rsync",
owner = true,
group = true,
archive = "true",
_extra = {
"--include=.env",
"--exclude=*"
}
}
}
sync {
default.rsyncssh,
source = "/usr/share/vitxi/backend/storage/",
host = "$ip_standby",
targetdir = "/usr/share/vitxi/backend/storage/",
rsync = {
owner = true,
group = true
}
}
sync {
default.rsyncssh,
source = "/var/lib/vitxi/",
host = "$ip_standby",
targetdir = "/var/lib/vitxi/",
rsync = {
binary = "/usr/bin/rsync",
owner = true,
group = true,
archive = "true",
_extra = {
"--include=wizard.conf",
"--exclude=*"
}
}
}
sync {
default.rsyncssh,
source="/var/lib/asterisk/",
host = "$ip_standby",
targetdir = "/home/sync/var/spool/asterisk/sqlite3_temp/",
rsync = {
binary = "/usr/bin/rsync",
owner = true,
group = true,
archive = "true",
_extra = {
"--include=astdb.sqlite3",
"--exclude=*"
}
}
}
sync {
default.rsyncssh,
source = "/var/lib/asterisk/agi-bin/",
host = "$ip_standby",
targetdir = "/var/lib/asterisk/agi-bin/",
rsync = {
owner = true,
group = true
}
}
sync {
default.rsyncssh,
source = "/var/lib/asterisk/priv-callerintros/",
host = "$ip_standby",
targetdir = "/var/lib/asterisk/priv-callerintros",
rsync = {
owner = true,
group = true
}
}
sync {
default.rsyncssh,
source = "/var/lib/asterisk/sounds/",
host = "$ip_standby",
targetdir = "/var/lib/asterisk/sounds/",
rsync = {
owner = true,
group = true
}
}
sync {
default.rsyncssh,
source = "/var/lib/vitalpbx",
host = "$ip_standby",
targetdir = "/var/lib/vitalpbx",
rsync = {
binary = "/usr/bin/rsync",
owner = true,
group = true,
archive = "true",
_extra = {
"--exclude=*.lic",
"--exclude=*.dat",
"--exclude=dbsetup-done",
"--exclude=cache"
}
}
}
sync {
default.rsyncssh,
source = "/etc/asterisk",
host = "$ip_standby",
targetdir = "/etc/asterisk",
rsync = {
owner = true,
group = true
}
}
EOF
systemctl enable lsyncd.service
systemctl start lsyncd.service
echo -e "*** Done Step 4 ***"
echo -e "4" > step.txt
create_mariadb_replica:
echo -e "************************************************************"
echo -e "* Create MariaDB replica *"
echo -e "************************************************************"
#Configuration of the First Master Server (Master)
cat > /etc/mysql/mariadb.conf.d/50-server.cnf << EOF
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
#
# * Replica Settings
#
server_id=1
log-basename=master
log-bin
binlog-format=row
#
# * Basic Settings
#
user = mysql
pid-file = /run/mysqld/mysqld.pid
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
lc-messages = en_US
skip-external-locking
# Broken reverse DNS slows down connections considerably and name resolve is
# safe to skip if there are no "host by domain name" access grants
#skip-name-resolve
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 0.0.0.0
#
# * Fine Tuning
#
#key_buffer_size = 128M
#max_allowed_packet = 1G
#thread_stack = 192K
#thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
#myisam_recover_options = BACKUP
#max_connections = 100
#table_cache = 64
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# Recommend only changing this at runtime for short testing periods if needed!
#general_log_file = /var/log/mysql/mysql.log
#general_log = 1
# When running under systemd, error logging goes via stdout/stderr to journald
# and when running legacy init error logging goes to syslog due to
# /etc/mysql/conf.d/mariadb.conf.d/50-mysqld_safe.cnf
# Enable this if you want to have error logging into a separate file
#log_error = /var/log/mysql/error.log
# Enable the slow query log to see queries with especially long duration
#slow_query_log_file = /var/log/mysql/mariadb-slow.log
#long_query_time = 10
#log_slow_verbosity = query_plan,explain
#log-queries-not-using-indexes
#min_examined_row_limit = 1000
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
#max_binlog_size = 100M
#
# * SSL/TLS
#
# For documentation, please read
# https://mariadb.com/kb/en/securing-connections-for-client-and-server/
#ssl-ca = /etc/mysql/cacert.pem
#ssl-cert = /etc/mysql/server-cert.pem
#ssl-key = /etc/mysql/server-key.pem
#require-secure-transport = on
#
# * Character sets
#
# MySQL/MariaDB default is Latin1, but in Debian we rather default to the full
# utf8 4-byte character set. See also client.cnf
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
# Most important is to give InnoDB 80 % of the system RAM for buffer use:
# https://mariadb.com/kb/en/innodb-system-variables/#innodb_buffer_pool_size
#innodb_buffer_pool_size = 8G
# this is only for embedded server
[embedded]
# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
log-bin
server_id=1
log-basename=master
binlog-format=mixed
# This group is only read by MariaDB-10.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.5]
EOF
systemctl restart mariadb
#Create a new user on the Master
mysql -uroot -e "CREATE USER 'vitalpbx_replica' @'%' IDENTIFIED BY 'vitalpbx_replica';"
mysql -uroot -e "GRANT REPLICATION SLAVE ON *.* TO 'vitalpbx_replica'@'$ip_standby' IDENTIFIED BY 'vitalpbx_replica';"
mysql -uroot -e "FLUSH PRIVILEGES;"
#Get bin_log on Master-1
file_server_1=`mysql -uroot -e "show master status" | awk 'NR==2 {print $1}'`
position_server_1=`mysql -uroot -e "show master status" | awk 'NR==2 {print $2}'`
#Once the data has been copied, you can release the lock on the master by running UNLOCK TABLES
mysql -uroot -e "UNLOCK TABLES;"
#Now on the Master-1 server, do a dump of the database MySQL and import it to Master-2
mysqldump -u root --all-databases > all_databases.sql
scp all_databases.sql root@$ip_standby:/tmp/all_databases.sql
cat > /tmp/mysqldump.sh << EOF
#!/bin/bash
mysql -u root < /tmp/all_databases.sql
EOF
scp /tmp/mysqldump.sh root@$ip_standby:/tmp/mysqldump.sh
ssh root@$ip_standby "chmod +x /tmp/mysqldump.sh"
ssh root@$ip_standby "/tmp/./mysqldump.sh"
#Configuration of the Second Master Server (Replica)
cat > /tmp/50-server.cnf << EOF
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
#
# * Replica Settings
#
server_id=2
log-basename=replica
log-bin
binlog-format=row
slave-skip-errors = 1062
#
# * Basic Settings
#
user = mysql
pid-file = /run/mysqld/mysqld.pid
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
lc-messages = en_US
skip-external-locking
# Broken reverse DNS slows down connections considerably and name resolve is
# safe to skip if there are no "host by domain name" access grants
#skip-name-resolve
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
#
# * Fine Tuning
#
#key_buffer_size = 128M
#max_allowed_packet = 1G
#thread_stack = 192K
#thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
#myisam_recover_options = BACKUP
#max_connections = 100
#table_cache = 64
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# Recommend only changing this at runtime for short testing periods if needed!
#general_log_file = /var/log/mysql/mysql.log
#general_log = 1
# When running under systemd, error logging goes via stdout/stderr to journald
# and when running legacy init error logging goes to syslog due to
# /etc/mysql/conf.d/mariadb.conf.d/50-mysqld_safe.cnf
# Enable this if you want to have error logging into a separate file
#log_error = /var/log/mysql/error.log
# Enable the slow query log to see queries with especially long duration
#slow_query_log_file = /var/log/mysql/mariadb-slow.log
#long_query_time = 10
#log_slow_verbosity = query_plan,explain
#log-queries-not-using-indexes
#min_examined_row_limit = 1000
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
#max_binlog_size = 100M
#
# * SSL/TLS
#
# For documentation, please read
# https://mariadb.com/kb/en/securing-connections-for-client-and-server/
#ssl-ca = /etc/mysql/cacert.pem
#ssl-cert = /etc/mysql/server-cert.pem
#ssl-key = /etc/mysql/server-key.pem
#require-secure-transport = on
#
# * Character sets
#
# MySQL/MariaDB default is Latin1, but in Debian we rather default to the full
# utf8 4-byte character set. See also client.cnf
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
# Most important is to give InnoDB 80 % of the system RAM for buffer use:
# https://mariadb.com/kb/en/innodb-system-variables/#innodb_buffer_pool_size
#innodb_buffer_pool_size = 8G
# this is only for embedded server
[embedded]
# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
# This group is only read by MariaDB-10.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.5]
EOF
scp /tmp/50-server.cnf root@$ip_standby:/etc/mysql/mariadb.conf.d/50-server.cnf
ssh root@$ip_standby "systemctl restart mariadb"
#Create a new user on the Replica
cat > /tmp/grand.sh << EOF
#!/bin/bash
mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='$ip_master', MASTER_USER='vitalpbx_replica', MASTER_PASSWORD='vitalpbx_replica', MASTER_LOG_FILE='$file_server_1', MASTER_LOG_POS=$position_server_1;"
mysql -uroot -e "START SLAVE;"
EOF
scp /tmp/grand.sh root@$ip_standby:/tmp/grand.sh
ssh root@$ip_standby "chmod +x /tmp/grand.sh"
ssh root@$ip_standby "/tmp/./grand.sh"
ssh root@$ip_standby "rm /tmp/grand.sh"
echo -e "*** Done Step 5 ***"
echo -e "5" > step.txt
echo -e "************************************************************"
echo -e "* Create Scripts to copy SQLite Asterisk database *"
echo -e "* Stop Asterisk, copy asterisk database *"
echo -e "* and start Asterisk again *"
echo -e "************************************************************"
#Stops Asterisk to avoid duplicate logs in the call queues table (queues_log)
ssh root@$ip_standby "systemctl stop asterisk"
ssh root@$ip_standby "systemctl disable asterisk"
cat > /usr/local/bin/vpbxstart << EOF
#!/bin/bash
# This code is the property of VitalPBX LLC Company
# License: Proprietary
# Date: 28-Sep-2023
# Stop Asterisk, copy asterisk database and start Asterisk again
#
systemctl stop asterisk
/bin/cp -Rf /home/sync/var/spool/asterisk/sqlite3_temp/astdb.sqlite3 /var/lib/asterisk/astdb.sqlite3
systemctl start asterisk
systemctl enable asterisk
EOF
chmod +x /usr/local/bin/vpbxstart
scp /usr/local/bin/vpbxstart root@$ip_standby:/usr/local/bin/vpbxstart
ssh root@$ip_standby 'chmod +x /usr/local/bin/vpbxstart'
vitalpbx_cluster_ok:
echo -e "************************************************************"
echo -e "* VitalPBX Replica OK *"
echo -e "************************************************************"