Support for downgrading already existing MongoDB 5 deployments to 4.4

This commit is contained in:
Karmaa
2025-03-17 21:29:23 -05:00
parent 296fa5031b
commit b131196463

View File

@@ -6,7 +6,46 @@ mkdir -p /data/db /var/log/mongodb /var/run/mongodb
chown -R mongodb:mongodb /data/db /var/log/mongodb /var/run/mongodb
chmod 755 /data/db /var/log/mongodb /var/run/mongodb
# Start MongoDB
# Check if we need to migrate from MongoDB 5.0
if [ -f "/data/db/diagnostic.data/metrics.2" ] || [ -f "/data/db/WiredTiger.wt" ]; then
echo "Existing MongoDB data detected, checking version..."
# Start MongoDB temporarily with --repair to handle version mismatch
gosu mongodb mongod --dbpath $MONGODB_DATA_DIR --logpath $MONGODB_LOG_DIR/mongodb.log --repair
# Start MongoDB with specific options for migration
echo "Starting MongoDB for migration..."
gosu mongodb mongod --dbpath $MONGODB_DATA_DIR --logpath $MONGODB_LOG_DIR/mongodb.log --bind_ip 0.0.0.0 &
MONGO_PID=$!
# Wait for MongoDB to be ready
echo "Waiting for MongoDB to start for migration..."
until gosu mongodb mongo --eval "print(\"waited for connection\")" > /dev/null 2>&1; do
sleep 1
if ! kill -0 $MONGO_PID 2>/dev/null; then
echo "MongoDB failed to start for migration. Checking logs:"
cat $MONGODB_LOG_DIR/mongodb.log
exit 1
fi
done
echo "Setting featureCompatibilityVersion to 4.4..."
# Try to set featureCompatibilityVersion
if gosu mongodb mongo --eval 'db.adminCommand({setFeatureCompatibilityVersion: "4.4"})' > /dev/null 2>&1; then
echo "Successfully set featureCompatibilityVersion to 4.4"
else
echo "Failed to set featureCompatibilityVersion. Attempting repair..."
gosu mongodb mongod --dbpath $MONGODB_DATA_DIR --repair
fi
# Stop MongoDB after migration
kill $MONGO_PID
while kill -0 $MONGO_PID 2>/dev/null; do
sleep 1
done
fi
# Start MongoDB normally
echo "Starting MongoDB..."
gosu mongodb mongod --dbpath $MONGODB_DATA_DIR --logpath $MONGODB_LOG_DIR/mongodb.log --bind_ip 0.0.0.0 &
MONGO_PID=$!