How to Migrate Sonatype Nexus Repository to a New Server: Step-by-Step Guide


2 views

Migrating a Sonatype Nexus repository involves transferring both the application data and configuration to a new machine. The key components to migrate include:

  • Nexus application binaries
  • Repository storage directories
  • Configuration files
  • Database (if using external database)

Before starting the migration, ensure you have:

# Create a backup of your current Nexus instance
sudo systemctl stop nexus
sudo tar -czvf nexus_backup.tar.gz /opt/sonatype/nexus /nexus-data

Install the same version of Nexus on your target machine:

# Download Nexus (example for version 3.38.1-01)
wget https://download.sonatype.com/nexus/3/nexus-3.38.1-01-unix.tar.gz
tar -xvf nexus-3.38.1-01-unix.tar.gz
mv nexus-3.38.1-01 /opt/sonatype/nexus

Copy the repository storage from the old server to the new one:

# On old server
rsync -avz /nexus-data user@new-server:/nexus-data

# Or using SCP
scp -r /nexus-data user@new-server:/nexus-data

Transfer the configuration files:

# Copy the etc directory
scp -r /opt/sonatype/nexus/etc user@new-server:/opt/sonatype/nexus/

For external databases, dump and restore:

# PostgreSQL example
pg_dump nexus_db > nexus_db_backup.sql
psql -h new-db-server -U postgres -d nexus_db < nexus_db_backup.sql

Modify the nexus.properties file on the new server:

# Edit the properties file
vi /opt/sonatype/nexus/etc/nexus-default.properties

# Update paths if needed
nexus.scripts.allowCreation=true
nexus.data=/nexus-data

Start the service and verify the migration:

# Start Nexus
/opt/sonatype/nexus/bin/nexus start

# Check logs
tail -f /nexus-data/log/nexus.log
  • Permission issues: Ensure the nexus user owns all files
  • Path mismatches: Verify all paths in configuration files
  • Version conflicts: Use identical Nexus versions for migration

For frequent migrations, consider creating a script:

#!/bin/bash
# Migration script example
OLD_SERVER="old-nexus.example.com"
NEW_SERVER="new-nexus.example.com"

echo "Stopping Nexus on old server..."
ssh $OLD_SERVER "systemctl stop nexus"

echo "Transferring data..."
rsync -avz $OLD_SERVER:/nexus-data /nexus-data

echo "Starting Nexus on new server..."
systemctl start nexus

Before starting the migration process, ensure you have:

  • Administrative access to both source and destination servers
  • Nexus installed on the target machine (same or newer version)
  • Enough disk space for repository data transfer
  • Scheduled downtime window for the migration

The most critical step is creating a complete backup of your current Nexus instance:

# Stop Nexus service
sudo systemctl stop nexus

# Backup the entire sonatype-work directory
tar -czvf nexus_backup_$(date +%Y%m%d).tar.gz /opt/sonatype/sonatype-work

Use rsync for efficient transfer of large repository data:

rsync -avz -e "ssh -p 22" /opt/sonatype/sonatype-work/ user@new-server:/opt/sonatype/sonatype-work/

For very large repositories, consider using these optimizations:

rsync -avz --partial --progress --bwlimit=50000 -e "ssh -p 22" \
/opt/sonatype/sonatype-work/ user@new-server:/opt/sonatype/sonatype-work/

Critical configuration files to migrate:

  • nexus.properties
  • nexus.vmoptions
  • karaf configuration files
  • SSL certificates if using HTTPS

For PostgreSQL databases (common setup):

# On source server
pg_dump -U nexus -h localhost nexus > nexus_db_backup.sql

# On target server
psql -U nexus -h localhost nexus < nexus_db_backup.sql

After migration, run these checks:

# Check blob store integrity
find /opt/sonatype/sonatype-work/nexus3/blobs -type f -exec md5sum {} \; > blobs.md5

# Verify repository metadata
nexus-repository-cli list-repositories --format json | jq '.'

Permission problems: Ensure the nexus user owns all files:

chown -R nexus:nexus /opt/sonatype/sonatype-work

UUID conflict: If both instances try to use same cluster ID:

rm /opt/sonatype/sonatype-work/nexus3/instance/instance.properties

Storage location mismatch: Update nexus.properties if paths changed:

nexus.scripts.allowCreation=true
nexus.datastore.enabled=true
nexus.h2.file=/opt/sonatype/sonatype-work/nexus3/data/nexus

For frequent migrations, consider this Ansible playbook snippet:

- name: Migrate Nexus repository
  hosts: new_nexus_server
  tasks:
    - name: Stop Nexus service
      service:
        name: nexus
        state: stopped
    
    - name: Transfer repository data
      synchronize:
        src: /opt/sonatype/sonatype-work/
        dest: /opt/sonatype/sonatype-work/
    
    - name: Fix permissions
      file:
        path: /opt/sonatype/sonatype-work
        owner: nexus
        group: nexus
        recurse: yes
    
    - name: Start Nexus service
      service:
        name: nexus
        state: started
  • Verify all repositories are visible in the UI
  • Check scheduled tasks are properly configured
  • Test artifact uploads and downloads
  • Validate API endpoints
  • Update DNS records if changing server IP