diff --git a/install.sh b/install.sh index c5bfd8b..1351280 100755 --- a/install.sh +++ b/install.sh @@ -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 diff --git a/scripts/nexus_cluster_sync.php b/scripts/nexus_cluster_sync.php index af9a014..7a64955 100644 --- a/scripts/nexus_cluster_sync.php +++ b/scripts/nexus_cluster_sync.php @@ -44,3 +44,4 @@ try { echo date('[Y-m-d H:i:s] ') . "[CLUSTER-CRON-ERROR] " . $e->getMessage() . "\n"; exit(1); } + diff --git a/src/ClusterManager.php b/src/ClusterManager.php index 5e77e93..462b7cc 100644 --- a/src/ClusterManager.php +++ b/src/ClusterManager.php @@ -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