Skip to content

Instantly share code, notes, and snippets.

@Kenya-West
Created February 2, 2025 11:26
Show Gist options
  • Save Kenya-West/9e565e1e0a2be43d0ff41a6393f7a4f0 to your computer and use it in GitHub Desktop.
Save Kenya-West/9e565e1e0a2be43d0ff41a6393f7a4f0 to your computer and use it in GitHub Desktop.
Automatically download mongo-tools files from MongoDB website (only for Ubuntu)

Mongo-tools downloader

The problem

Let's assume I do not like to set and maintain MongoDB GPG keys and their repsitory lists in my Ubuntu host. I just need to download binaries and they should work.

The solution

This script only download a .tgz archive that contains requested binaries, to directory this script was run from.

Credits

OpenAI and their o3-mini neural network model.

Notes

Only for Ubuntu!

#!/bin/bash
set -e
# Fetch the latest version number from the MongoDB download page.
latest=$(curl -s "https://www.mongodb.com/try/download/database-tools" \
| grep -oP 'mongodb-database-tools-ubuntu2204-x86_64-\K[0-9\.]+(?=\.tgz)' \
| head -n1)
echo "Latest version: $latest"
# Determine the host Ubuntu codename.
host_codename=$(lsb_release -sc)
echo "Host codename: $host_codename"
# Map host codename to a supported LTS codename.
case "$host_codename" in
noble|bionic|focal|jammy)
lts_codename="$host_codename"
;;
kinetic|lunar|mantic)
lts_codename="jammy" # nearest LTS for non-LTS 22.x releases
;;
groovy)
lts_codename="focal" # for groovy (20.10), use focal (20.04 LTS)
;;
*)
lts_codename="jammy"
;;
esac
echo "Using LTS codename: $lts_codename"
# Map the chosen LTS codename to the version suffix used in the download URL.
case "$lts_codename" in
noble)
ubuntu_suffix="2404"
;;
jammy)
ubuntu_suffix="2204"
;;
focal)
ubuntu_suffix="2004"
;;
bionic)
ubuntu_suffix="1804"
;;
*)
ubuntu_suffix="2204"
;;
esac
echo "Ubuntu download suffix: $ubuntu_suffix"
# Determine the host architecture.
arch=$(uname -m)
echo "Detected architecture: $arch"
case "$arch" in
x86_64)
arch_str="x86_64"
;;
aarch64)
arch_str="arm64"
;;
*)
echo "Unknown architecture; defaulting to x86_64"
arch_str="x86_64"
;;
esac
echo "Architecture string for URL: $arch_str"
# Build the download URL dynamically.
filename="mongodb-database-tools-ubuntu${ubuntu_suffix}-${arch_str}-${latest}.tgz"
URL="https://fastdl.mongodb.org/tools/db/${filename}"
echo "Download URL: $URL"
# Check if the file already exists; if so, skip the download.
if [ -f "$filename" ]; then
echo "File '$filename' already exists. Skipping download."
else
echo "Downloading $filename ..."
wget "$URL"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment