feat(cluster): add Asterisk astdb.sqlite3 live contacts sync every 2min

This commit is contained in:
sorcier committed 2026-11-05 22:54:43 +01:00
1 parent 3f29ac6d2e
commit c9ce4d7c74
3 files changed
+31 -2

No files matched your search

+2 -2
View File
@@ -488,10 +488,10 @@ EOF
chmod 0644 /etc/cron.d/nexus-backup
touch /var/log/nexus_backup.log && chown www-data:asterisk /var/log/nexus_backup.log && chmod 664 /var/log/nexus_backup.log
# Cron synchronisation Cluster HA toutes les 5 minutes
# Cron synchronisation Cluster HA toutes les 2 minutes (synchronisation base + astdb contacts)
cat << 'EOF' > /etc/cron.d/nexus-cluster-sync
# Synchronisation périodique du cluster NexusPBX HA vers le nœud Backup
*/5 * * * * www-data /usr/bin/php /var/www/nexus-pbx/scripts/nexus_cluster_sync.php >> /var/log/nexus_cluster.log 2>&1
*/2 * * * * www-data /usr/bin/php /var/www/nexus-pbx/scripts/nexus_cluster_sync.php >> /var/log/nexus_cluster.log 2>&1
EOF
chmod 0644 /etc/cron.d/nexus-cluster-sync
+1
View File
@@ -44,3 +44,4 @@ try {
echo date('[Y-m-d H:i:s] ') . "[CLUSTER-CRON-ERROR] " . $e->getMessage() . "\n";
exit(1);
}
+28
View File
@@ -464,11 +464,30 @@ class ClusterManager
$this->db->exec('PRAGMA wal_checkpoint(FULL);');
$tmpArchive = '/tmp/nexus_cluster_sync_' . time() . '.tar.gz';
$tmpAstdb = '/tmp/astdb_cluster_sync.sqlite3';
$hasAstdb = false;
// Sauvegarde intègre et atomique de la base de contacts Asterisk (astdb.sqlite3)
$astdbPath = '/var/lib/asterisk/astdb.sqlite3';
if (file_exists($astdbPath) && is_readable($astdbPath)) {
@unlink($tmpAstdb);
shell_exec("sqlite3 " . escapeshellarg($astdbPath) . " \".backup " . escapeshellarg($tmpAstdb) . "\" 2>/dev/null || cp -f " . escapeshellarg($astdbPath) . " " . escapeshellarg($tmpAstdb) . " 2>/dev/null || true");
if (file_exists($tmpAstdb) && filesize($tmpAstdb) > 0) {
$hasAstdb = true;
}
}
$cmd = "tar -czf " . escapeshellarg($tmpArchive) . " -C /var/www/nexus-pbx/storage database.sqlite";
if (is_dir('/var/lib/asterisk/sounds/custom')) {
$cmd .= " -C /var/lib/asterisk/sounds custom";
}
if ($hasAstdb) {
$cmd .= " -C /tmp astdb_cluster_sync.sqlite3";
}
shell_exec($cmd);
if ($hasAstdb) {
@unlink($tmpAstdb);
}
if (!file_exists($tmpArchive) || filesize($tmpArchive) === 0) {
@unlink($tmpArchive);
@@ -599,6 +618,15 @@ class ClusterManager
if (is_dir('/var/lib/asterisk/sounds')) {
shell_exec("tar -xzf " . escapeshellarg($tmpArchive) . " -C /var/lib/asterisk/sounds custom 2>/dev/null || true");
}
// Restauration de l'astdb (contacts et IP actives des téléphones PJSIP)
shell_exec("tar -xzf " . escapeshellarg($tmpArchive) . " -C /tmp astdb_cluster_sync.sqlite3 2>/dev/null || true");
if (file_exists('/tmp/astdb_cluster_sync.sqlite3')) {
copy('/tmp/astdb_cluster_sync.sqlite3', '/var/lib/asterisk/astdb.sqlite3');
@chown('/var/lib/asterisk/astdb.sqlite3', 'asterisk');
@chgrp('/var/lib/asterisk/astdb.sqlite3', 'asterisk');
@chmod('/var/lib/asterisk/astdb.sqlite3', 0664);
@unlink('/tmp/astdb_cluster_sync.sqlite3');
}
@unlink($tmpArchive);
// Réouverture de la connexion DB