OPNsense Forum

Archive => 21.1 Legacy Series => Topic started by: BrandonG777 on June 07, 2021, 07:32:28 PM

Title: Unifi Controller Corrupt Database Quick Fix Script
Post by: BrandonG777 on June 07, 2021, 07:32:28 PM
So despite a UPS and other measures I've taken to ensure my OPNsense box never loses power abruptly I still end up unplugging a wrong cord or a battery fails or whatever and the Unifi Controller mongodb seems to instantly corrupt. Maybe there is a way of fixing this but restoring from an automated backup has been the only method I have attempted. Anyway, here is the script I wrote that will fix the corrupted database quick.

#!/usr/local/bin/bash
service unifi stop
pkg remove -y unifi6
mv /usr/local/share/java/unifi /usr/local/share/java/unifi.bad
pkg install -y unifi6 os-unifi-maxit
cp -pRv /usr/local/share/java/unifi.bad/data/backup /usr/local/share/java/unifi/data/backup
service unifi start
rm -rf /usr/local/share/java/unifi.bad
ipaddr=$(ifconfig em1 | grep 'inet' | awk '$1 == "inet" { print $2 }')         
echo "Now to go https://$ipaddr:8443 and restore your backup."
read -p "Press enter to continue" continue
service unifi start


I place this in a file called /root/unifi-fix.sh then make it executable "chmod +x unifi-fix.sh" and then execute it ./unifi-fix.sh