Nexus Repository Manager OSS by Sonatype is a powerful tool for managing software artifacts. It helps you store, retrieve, and distribute components used in development, from libraries and dependencies to Docker images. Given its central role in your development infrastructure, careful handling during upgrades is paramount.
Upgrading your Nexus OSS installation can be a critical yet rewarding process, ensuring you benefit from the latest features, bug fixes, and security updates. This guide is specifically designed for users who have installed our AWS and Azure Marketplace images. Note: This upgrade process is still experimental. It is essential that you proceed with caution and You must proceed before continuing.
Pre-Upgrade Considerations
Before initiating the upgrade process, keep in mind the following:
- Experimental Nature: The upgrade process for the AWS and Azure Marketplace images is still in an experimental stage. Ensure that you thoroughly test the upgrade in a staging environment before deploying it in production.
- Backup Your Nexus Instance: Always create a snapshot backup of your instance. This safeguard will help you restore your system in case anything goes wrong during the upgrade.
- System Requirements: The script checks for at least 3GB of free disk space, so confirm that your instance meets this requirement.
- Running as Root: The upgrade script must be executed with root privileges to ensure all operations (like modifying system files and permissions) complete successfully.
Script Explained
Below is the provided bash script that automates the upgrade process. This script performs the following key actions:
This script is included by default in our nexus images on AWS and Azure MarketPlace
- Root User Check: Verifies that the script is being run as the root user.
- Disk Space Check: Ensures there is sufficient free disk space (minimum 3GB) before proceeding.
- Backup Process: Stops the Nexus service and backs up both the Nexus installation directory and data directory (You are still encouraged to make your own backup).
- Download Latest Nexus: Retrieves the latest Nexus OSS tarball from the official source.
- Extraction and Upgrade: Extracts the downloaded archive, preserves existing configuration and data, and then replaces the old Nexus installation with the new version.
- Permissions and Cleanup: Sets the appropriate permissions for the new installation and cleans up any temporary files.
- Restart Nexus: Restarts the Nexus service to complete the upgrade process.
Code
#!/bin/bash
# Ensure script runs as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" >&2
exit 1
fi
# Legalese Prompt
echo "============================================================================================="
echo "IMPORTANT: Before proceeding, ensure you have taken a snapshot backup of your instance. This script will perform actions that"
echo "might lead to data loss. By proceeding, you acknowledge that any data loss as a result of this script is your responsibility."
echo "============================================================================================="
read -p "Do you accept the above terms? (yes/no): " ACCEPT
if [[ "$ACCEPT" != "yes" ]]; then
echo "Exiting script as terms were not accepted."
exit 1
fi
# Check for at least 3GB of free space
FREE_SPACE=$(df --output=avail / | tail -n1 | awk '{print $1}')
if [[ $FREE_SPACE -lt $((3 * 1024 * 1024)) ]]; then
echo "Error: At least 3GB of free space is required to proceed." >&2
exit 1
fi
# Define variables
NEXUS_INSTALL_DIR="/opt/nexus"
NEXUS_DATA_DIR="/opt/sonatype-work"
NEXUS_BACKUP_DIR="/opt/nexus-backups"
LATEST_NEXUS_URL="https://download.sonatype.com/nexus/3/latest-unix.tar.gz"
LATEST_NEXUS_ARCHIVE="/tmp/nexus-latest.tar.gz"
# Step 1: Backup Nexus
echo "Backing up Nexus..."
systemctl stop nexus.service
mkdir -p "$NEXUS_BACKUP_DIR"
cp -r "$NEXUS_INSTALL_DIR" "$NEXUS_BACKUP_DIR/nexus-backup-$(date +%F)"
cp -r "$NEXUS_DATA_DIR" "$NEXUS_BACKUP_DIR/sonatype-work-backup-$(date +%F)"
if [[ $? -ne 0 ]]; then
echo "Nexus backup failed." >&2
exit 1
else
echo "Nexus backup successful."
fi
# Step 2: Download Latest Nexus
echo "Downloading latest Nexus version..."
wget "$LATEST_NEXUS_URL" -O "$LATEST_NEXUS_ARCHIVE"
if [[ $? -ne 0 ]]; then
echo "Failed to download latest Nexus version." >&2
exit 1
else
echo "Nexus download successful."
fi
# Step 3: Extract and Upgrade Nexus
echo "Extracting and upgrading Nexus..."
tar -xzf "$LATEST_NEXUS_ARCHIVE" -C /tmp
NEW_NEXUS_DIR="/tmp/$(tar -tf "$LATEST_NEXUS_ARCHIVE" | head -1 | cut -f1 -d"/")"
# Preserve configuration and data
cp -r "$NEXUS_INSTALL_DIR/etc" "$NEW_NEXUS_DIR/"
cp -r "$NEXUS_DATA_DIR" "$NEW_NEXUS_DIR/sonatype-work"
# Replace old Nexus installation
mv "$NEXUS_INSTALL_DIR" "$NEXUS_INSTALL_DIR-old"
mv "$NEW_NEXUS_DIR" "$NEXUS_INSTALL_DIR"
# Set permissions
chown -R nexus:nexus "$NEXUS_INSTALL_DIR"
# Cleanup
rm -f "$LATEST_NEXUS_ARCHIVE"
echo "Cleanup complete."
# Step 4: Restart Nexus
echo "Restarting Nexus..."
systemctl start nexus.service
if [[ $? -eq 0 ]]; then
echo "Nexus restarted successfully."
else
echo "Failed to restart Nexus." >&2
exit 1
fi
# Completion
echo "============================================================================================="
echo "Upgrade process completed successfully. Please log in to Nexus to verify the upgrade."
echo "============================================================================================="
Conclusion
Upgrading your Nexus OSS installation is a delicate process that demands careful preparation, mainly when using experimental AWS and Azure Marketplace images. Following this comprehensive guide and utilizing the provided script can streamline the upgrade process while minimizing risks. Remember: Always back up your current installation and test the upgrade process in a controlled environment before deploying to production.
Happy upgrading!
Disclaimer:
All product and company names are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. Sonatype Nexus Repository Manager OSS™, Nexus Repository Manager Pro™, Nexus Lifecycle™, Nexus Auditor™, Nexus Firewall™, IQ Server™, and all Nexus-related logos as well as Sonatype CLM are trademarks or registered trademarks of Sonatype, Inc., in the United States and other countries.
Do you still need help?
Look, our Tech Support Staff live and breathe Cloud Engineering. Let them handle the details, so you can focus on the big picture.
Contact Support