Created
February 23, 2023 13:06
-
-
Save gador/a740144a6949b1272991cda5e35b8457 to your computer and use it in GitHub Desktop.
mongodb migration for NixOS update to 23.05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env nix-shell | |
#! nix-shell -i bash -p bash | |
kill_mongo () { | |
set +e | |
sleep 5 # wait for previous processes to come to an end | |
echo "terminating mongod ..." | |
pkill mongod | |
sleep 5 | |
pgrep mongod | |
if [ "$?" -eq 0 ]; then | |
# really kill mongod | |
echo "mongod was not yet terminated. Killing it now ..." | |
pkill -9 mongod | |
sleep 5 | |
fi | |
set -e | |
} | |
echo | |
echo "---------------------------------------------" | |
echo "This script backs up the data from Unifi and migrates it to mongodb-4_4" | |
echo "This will cause a build of mongodb-4_2 and mongodb-4_4 on your system," | |
echo "since those are not cached upstream. Prepare for a longer wait time." | |
echo | |
echo "The following variables can be set by environment variables" | |
UNIFI_DIR="${UNIFI_DIR:-/var/lib/unifi}" | |
UNIFI_DB_DIR="${UNIFI_DIR}/data/db" | |
UNIFI_LOG="${UNIFI_LOG:-/var/log/unifi/repair.log}" | |
BACKUP_DIR="${BACKUP_DIR:-/tmp/mongodump}" | |
echo "UNIFI_DIR=$UNIFI_DIR" | |
echo "UNIFI_LOG=$UNIFI_LOG" | |
echo "BACKUP_DIR=$BACKUP_DIR" | |
echo "---------------------------------------------" | |
read -p "Are those correct? " -n 1 -r | |
echo | |
if [[ ! $REPLY =~ ^[Yy]$ ]] | |
then | |
exit 1 | |
fi | |
echo | |
echo "If the migration does not work, please have a look at the log file" | |
echo "($UNIFI_LOG)" | |
echo | |
set -e | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run as root" | |
exit 1 | |
fi | |
# When the system is already upgraded, mongodb cannot read the old data and will fail to start | |
# which causes the unifi service to hang. This will cause a delay when stopping the service | |
echo "---------------------------------------------" | |
echo "Stopping running unifi service. It is normal that this will take up to a few minutes..." | |
echo "---------------------------------------------" | |
systemctl stop unifi.service | |
echo "---------------------------------------------" | |
echo "Backup old data dir ($UNIFI_DB_DIR) ..." | |
echo "---------------------------------------------" | |
cp -avr $UNIFI_DB_DIR ${UNIFI_DB_DIR}_bak | |
# Repair and dump database | |
echo "---------------------------------------------" | |
echo "Starting mongodb-3_4 repair ..." | |
echo "---------------------------------------------" | |
nix --extra-experimental-features "nix-command flakes" shell github:nixos/nixpkgs/22.11#mongodb-3_4 -c "mongod" --dbpath $UNIFI_DB_DIR --logpath $UNIFI_LOG --repair | |
echo "---------------------------------------------" | |
echo "Starting mongodb-3_4 and dump the database to $BACKUP_DIR ..." | |
echo "---------------------------------------------" | |
nix --extra-experimental-features "nix-command flakes" shell github:nixos/nixpkgs/22.11#mongodb-3_4 -c "mongod" --dbpath $UNIFI_DB_DIR --logpath $UNIFI_LOG --journal --fork | |
nix --extra-experimental-features "nix-command flakes" shell github:nixos/nixpkgs/22.11#mongodb-tools -c "mongodump" --out=$BACKUP_DIR | |
kill_mongo | |
echo "---------------------------------------------" | |
echo "Remove old data dir ($UNIFI_DB_DIR) and recreate it ..." | |
echo "---------------------------------------------" | |
rm -rf $UNIFI_DB_DIR | |
mkdir $UNIFI_DB_DIR | |
# mongo 4_4 cannot read the database of mongo 3_4 | |
# Therefore, we need to upgrade to mongo_4_2 first | |
echo "---------------------------------------------" | |
echo "Starting mongodb-4_2 and restore the database ..." | |
echo "---------------------------------------------" | |
export NIXPKGS_ALLOW_UNFREE=1; nix --extra-experimental-features "nix-command flakes" shell --impure github:nixos/nixpkgs/22.11#mongodb-4_2 -c "mongod" --dbpath $UNIFI_DB_DIR --logpath $UNIFI_LOG --journal --fork | |
nix --extra-experimental-features "nix-command flakes" shell github:nixos/nixpkgs/22.11#mongodb-tools -c "mongorestore" $BACKUP_DIR | |
sleep 5 # wait for the restore to be done completly | |
echo "---------------------------------------------" | |
echo "Dump the database to ${BACKUP_DIR}_2 ..." | |
echo "---------------------------------------------" | |
nix --extra-experimental-features "nix-command flakes" shell github:nixos/nixpkgs/22.11#mongodb-tools -c "mongodump" --out=${BACKUP_DIR}_2 | |
kill_mongo | |
echo "---------------------------------------------" | |
echo "Starting mongodb-4_4 and repair the database ..." | |
echo "---------------------------------------------" | |
export NIXPKGS_ALLOW_UNFREE=1; nix --extra-experimental-features "nix-command flakes" shell --impure github:nixos/nixpkgs/22.11#mongodb-4_4 -c "mongod" --dbpath $UNIFI_DB_DIR --logpath $UNIFI_LOG --repair | |
echo "---------------------------------------------" | |
echo "Starting mongodb-4_4 and restore the database from ${BACKUP_DIR}_2 ..." | |
echo "---------------------------------------------" | |
export NIXPKGS_ALLOW_UNFREE=1; nix --extra-experimental-features "nix-command flakes" shell --impure github:nixos/nixpkgs/22.11#mongodb-4_4 -c "mongod" --dbpath $UNIFI_DB_DIR --logpath $UNIFI_LOG --journal --fork | |
nix --extra-experimental-features "nix-command flakes" shell github:nixos/nixpkgs/22.11#mongodb-tools -c "mongorestore" ${BACKUP_DIR}_2 | |
kill_mongo | |
echo "---------------------------------------------" | |
echo "Fixing permissions..." | |
echo "---------------------------------------------" | |
chown -R unifi:unifi $UNIFI_DIR | |
echo "---------------------------------------------" | |
echo "Cleaning up..." | |
echo "---------------------------------------------" | |
rm -rf $BACKUP_DIR | |
rm -rf ${BACKUP_DIR}_2 | |
echo "---------------------------------------------" | |
echo "The original database is still present in ${UNIFI_DB_DIR}_bak. If something didn't work, you can restore it from there" | |
echo "---------------------------------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment