TekOnline

Migrating Drone CI Database to Preserve Build History

When migrating Drone CI to a new server, you’ll want to preserve your build history. Here’s how to migrate the Drone PostgreSQL database volume.

Prerequisites

  • SSH access to both source and destination servers
  • Docker installed on both servers
  • Drone already set up on destination (or you’ll set it up after migration)

Step 1: Find the Drone Database Volume

On your source server, identify the Drone database container and volume:

# Find the database container
docker ps | grep drone | grep db

# Get the volume name
docker inspect drone-ci-db-1 | grep -A 10 Mounts

Look for the volume name in the output. It’s typically something like drone-ci_drone-db-data or drone_drone-db-data.

Step 2: Stop Drone on Destination

Stop Drone on the destination server to allow database migration:

ssh destination-server "cd ~/drone-ci && docker compose down"

Step 3: Backup the Database Volume

Create a backup of the database volume on the source server:

VOLUME_NAME="drone-ci_drone-db-data"  # Use your actual volume name
BACKUP_FILE="drone-db-backup-$(date +%Y%m%d-%H%M%S).tar.gz"

ssh source-server "docker run --rm -v ${VOLUME_NAME}:/data -v \$(pwd):/backup alpine tar czf /backup/${BACKUP_FILE} -C /data ."

Step 4: Transfer Backup to Destination

Transfer the backup file:

scp source-server:~/${BACKUP_FILE} destination-server:~/${BACKUP_FILE}

Step 5: Check PostgreSQL Version Compatibility

Critical: Check PostgreSQL versions match. Drone databases are version-specific.

# On source server
ssh source-server "docker exec drone-ci-db-1 psql --version"

# On destination server (check docker-compose.yml)
ssh destination-server "grep 'postgres:' ~/drone-ci/drone-docker-compose.yml"

If versions don’t match, update your drone-docker-compose.yml on the destination to match the source version.

Step 6: Restore Database on Destination

Remove the existing volume (if it exists) and restore:

# Remove existing volume
ssh destination-server "docker volume rm drone-ci_drone-db-data 2>/dev/null || echo 'Volume does not exist'"

# Create new volume
ssh destination-server "docker volume create drone-ci_drone-db-data"

# Restore data
ssh destination-server "docker run --rm -v drone-ci_drone-db-data:/data -v \$(pwd):/backup alpine tar xzf /backup/${BACKUP_FILE} -C /data"

Note: The volume name must match what’s in your docker-compose.yml. Docker Compose prefixes volumes with the project name (e.g., drone-ci_drone-db-data).

Step 7: Restart Drone

Start Drone on the destination server:

ssh destination-server "cd ~/drone-ci && docker compose up -d"

Wait a few seconds for the database to initialize, then verify:

ssh destination-server "docker exec drone-ci-db-1 psql -U drone -d drone -c 'SELECT COUNT(*) as build_count FROM builds;'"

You should see your build count matching the source server.

Common Issues

PostgreSQL Version Mismatch

Error: database files are incompatible with server

Solution: Update docker-compose.yml to use the same PostgreSQL version as the source. For example, change postgres:16 to postgres:17.

Volume Name Mismatch

Error: Database appears empty after migration

Solution: Ensure the volume name matches exactly what Docker Compose expects. Check with:

docker inspect drone-ci-db-1 | grep -A 5 Mounts

The volume name includes the compose project name prefix (e.g., drone-ci_drone-db-data).

Collation Version Warning

Warning: database has a collation version mismatch

This is harmless and won’t affect functionality. You can ignore it or run:

docker exec drone-ci-db-1 psql -U drone -d drone -c "ALTER DATABASE drone REFRESH COLLATION VERSION;"

Verification Checklist

  • [ ] Build count matches source server
  • [ ] Drone web UI accessible
  • [ ] Previous builds visible in UI
  • [ ] Repositories still configured
  • [ ] Database container healthy

Quick One-Liner Migration

If you’re comfortable with the process, here’s a condensed version:

VOLUME="drone-ci_drone-db-data"
BACKUP="drone-db-backup-$(date +%Y%m%d-%H%M%S).tar.gz"

# Backup
ssh source "docker run --rm -v ${VOLUME}:/data -v \$(pwd):/backup alpine tar czf /backup/${BACKUP} -C /data ."

# Transfer
scp source:~/${BACKUP} dest:~/${BACKUP}

# Restore
ssh dest "docker compose -f ~/drone-ci/drone-docker-compose.yml down && \
          docker volume rm drone-ci_drone-db-data 2>/dev/null; \
          docker volume create drone-ci_drone-db-data && \
          docker run --rm -v drone-ci_drone-db-data:/data -v \$(pwd):/backup alpine tar xzf /backup/${BACKUP} -C /data && \
          docker compose -f ~/drone-ci/drone-docker-compose.yml up -d"

Summary

Migrating Drone’s database volume is straightforward:

  1. Backup the volume
  2. Transfer it
  3. Ensure PostgreSQL versions match
  4. Restore to the correct volume name
  5. Restart Drone

The key gotcha is PostgreSQL version compatibility – always verify versions match before restoring.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *