Picture this: Your AI project deadline looms tomorrow. You fire up Ollama to fine-tune your models. Nothing happens. Your drive crashed overnight, taking months of work with it. Your coffee goes cold as reality sets in—you never backed up your Ollama installation.
Don't become another cautionary tale. This guide shows you how to create bulletproof backup strategies for Ollama and restore your AI infrastructure after disasters strike.
Why Ollama Disaster Recovery Matters
Ollama stores critical data across multiple locations. Models, configurations, and custom fine-tuning work can vanish instantly. Hardware failures, software corruption, and human errors threaten your AI projects daily.
What you'll lose without proper backups:
- Downloaded language models (multi-gigabyte files)
- Custom model configurations
- Fine-tuning parameters and weights
- API endpoint settings
- Integration configurations
What you'll gain from this guide:
- Automated backup procedures
- Quick restoration methods
- Zero-downtime recovery strategies
- Best practices for data protection
Understanding Ollama's File Structure
Before creating backups, you need to understand where Ollama stores its data. The storage locations vary by operating system.
Linux and macOS Storage Locations
# Default Ollama data directory
~/.ollama/
# Model files location
~/.ollama/models/
# Configuration files
~/.ollama/config/
# Logs and temporary files
~/.ollama/logs/
Windows Storage Locations
# Default Ollama data directory
%USERPROFILE%\.ollama\
# Model files location
%USERPROFILE%\.ollama\models\
# Configuration files
%USERPROFILE%\.ollama\config\
Checking Your Ollama Installation
Verify your Ollama installation and data locations:
# Check Ollama version and status
ollama --version
# List installed models
ollama list
# Show model details and sizes
ollama show llama2:7b --verbose
Creating Comprehensive Ollama Backups
Method 1: Simple Directory Backup
The simplest approach copies the entire Ollama directory. This method preserves all models, configurations, and settings.
#!/bin/bash
# Simple Ollama backup script
# Set backup directory with timestamp
BACKUP_DIR="/path/to/backups/ollama-backup-$(date +%Y%m%d-%H%M%S)"
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Copy entire Ollama directory
cp -r ~/.ollama/* "$BACKUP_DIR/"
# Verify backup completed successfully
if [ $? -eq 0 ]; then
echo "Backup completed successfully: $BACKUP_DIR"
# Calculate backup size
du -sh "$BACKUP_DIR"
else
echo "Backup failed!"
exit 1
fi
Pros:
- Captures everything
- Simple to implement
- Guaranteed completeness
Cons:
- Large backup sizes
- Longer backup times
- Storage intensive
Method 2: Selective Model Backup
Back up only essential models and configurations to save space and time.
#!/bin/bash
# Selective Ollama backup script
BACKUP_DIR="/path/to/backups/ollama-selective-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP_DIR"
# List of critical models to backup
CRITICAL_MODELS=("llama2:7b" "codellama:13b" "mistral:7b")
# Create models directory in backup
mkdir -p "$BACKUP_DIR/models"
# Backup selected models only
for model in "${CRITICAL_MODELS[@]}"; do
echo "Backing up model: $model"
# Find model files
model_path=$(ollama show "$model" --template 2>/dev/null | grep -o '/.*\.bin')
if [ -n "$model_path" ]; then
# Copy model files
cp -r "$(dirname "$model_path")" "$BACKUP_DIR/models/"
echo "✓ Backed up $model"
else
echo "✗ Model $model not found"
fi
done
# Always backup configuration files
cp -r ~/.ollama/config "$BACKUP_DIR/" 2>/dev/null
echo "Selective backup completed: $BACKUP_DIR"
Method 3: Automated Cloud Backup
Integrate with cloud storage services for offsite protection.
#!/bin/bash
# Automated cloud backup with rsync
# Configuration
LOCAL_OLLAMA_DIR="$HOME/.ollama"
CLOUD_BACKUP_DIR="/path/to/cloud/storage/ollama-backups"
LOG_FILE="/var/log/ollama-backup.log"
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Create timestamped backup
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BACKUP_TARGET="$CLOUD_BACKUP_DIR/ollama-$TIMESTAMP"
log_message "Starting Ollama backup to $BACKUP_TARGET"
# Sync Ollama directory to cloud storage
rsync -av \
--progress \
--exclude="*.tmp" \
--exclude="logs/" \
"$LOCAL_OLLAMA_DIR/" \
"$BACKUP_TARGET/"
if [ $? -eq 0 ]; then
log_message "Backup completed successfully"
# Clean up old backups (keep last 7 days)
find "$CLOUD_BACKUP_DIR" -name "ollama-*" -type d -mtime +7 -exec rm -rf {} \;
log_message "Old backups cleaned up"
else
log_message "Backup failed!"
exit 1
fi
Automated Backup Scheduling
Setting Up Cron Jobs (Linux/macOS)
Automate your backups with cron scheduling:
# Edit crontab
crontab -e
# Add daily backup at 2 AM
0 2 * * * /path/to/ollama-backup.sh >> /var/log/ollama-backup.log 2>&1
# Add weekly full backup on Sundays at 3 AM
0 3 * * 0 /path/to/ollama-full-backup.sh >> /var/log/ollama-backup.log 2>&1
Windows Task Scheduler
Create scheduled tasks using PowerShell:
# PowerShell script for Windows backup automation
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\OllamaBackup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Register-ScheduledTask -Action $action -Trigger $trigger -Settings $settings -TaskName "OllamaBackup" -Description "Daily Ollama backup"
Ollama Restoration Procedures
Complete System Restoration
Restore your entire Ollama installation from backup:
#!/bin/bash
# Complete Ollama restoration script
# Stop Ollama service if running
sudo systemctl stop ollama 2>/dev/null || true
pkill ollama 2>/dev/null || true
# Backup location (modify as needed)
BACKUP_SOURCE="/path/to/backups/ollama-backup-20250115-142300"
# Verify backup exists
if [ ! -d "$BACKUP_SOURCE" ]; then
echo "Error: Backup directory not found: $BACKUP_SOURCE"
exit 1
fi
# Remove existing Ollama data (be careful!)
echo "Removing existing Ollama data..."
rm -rf ~/.ollama/*
# Restore from backup
echo "Restoring Ollama data from backup..."
cp -r "$BACKUP_SOURCE"/* ~/.ollama/
# Set proper permissions
chmod -R 755 ~/.ollama
chown -R $USER:$USER ~/.ollama
# Restart Ollama service
sudo systemctl start ollama 2>/dev/null || {
echo "Starting Ollama manually..."
ollama serve &
sleep 5
}
# Verify restoration
echo "Verifying restoration..."
ollama list
if [ $? -eq 0 ]; then
echo "✓ Ollama restoration completed successfully"
else
echo "✗ Restoration failed - check logs"
exit 1
fi
Selective Model Restoration
Restore specific models without affecting others:
#!/bin/bash
# Selective model restoration
BACKUP_SOURCE="/path/to/backups/ollama-backup-20250115-142300"
MODEL_TO_RESTORE="llama2:7b"
echo "Restoring model: $MODEL_TO_RESTORE"
# Find model in backup
MODEL_BACKUP_PATH="$BACKUP_SOURCE/models/"
if [ -d "$MODEL_BACKUP_PATH" ]; then
# Copy model files
cp -r "$MODEL_BACKUP_PATH"/* ~/.ollama/models/
# Verify model is available
ollama list | grep "$MODEL_TO_RESTORE"
if [ $? -eq 0 ]; then
echo "✓ Model $MODEL_TO_RESTORE restored successfully"
else
echo "✗ Model restoration failed"
exit 1
fi
else
echo "✗ Model not found in backup"
exit 1
fi
Configuration-Only Restoration
Restore only configuration files while preserving existing models:
#!/bin/bash
# Configuration restoration script
BACKUP_SOURCE="/path/to/backups/ollama-backup-20250115-142300"
CONFIG_BACKUP="$BACKUP_SOURCE/config"
if [ -d "$CONFIG_BACKUP" ]; then
echo "Restoring Ollama configurations..."
# Backup current config
cp -r ~/.ollama/config ~/.ollama/config.backup.$(date +%s) 2>/dev/null
# Restore configuration
cp -r "$CONFIG_BACKUP" ~/.ollama/
echo "✓ Configuration restored successfully"
else
echo "✗ Configuration backup not found"
exit 1
fi
Testing Your Disaster Recovery Plan
Backup Verification Script
Regularly test your backups to ensure they work:
#!/bin/bash
# Backup verification script
BACKUP_DIR="/path/to/backups/ollama-backup-20250115-142300"
echo "Verifying Ollama backup integrity..."
# Check if backup directory exists
if [ ! -d "$BACKUP_DIR" ]; then
echo "✗ Backup directory not found"
exit 1
fi
# Verify essential directories exist
REQUIRED_DIRS=("models" "config")
for dir in "${REQUIRED_DIRS[@]}"; do
if [ -d "$BACKUP_DIR/$dir" ]; then
echo "✓ $dir directory found"
else
echo "✗ $dir directory missing"
exit 1
fi
done
# Check model files
MODEL_COUNT=$(find "$BACKUP_DIR/models" -name "*.bin" -o -name "*.gguf" | wc -l)
echo "✓ Found $MODEL_COUNT model files"
# Verify file sizes (models should be > 1GB typically)
LARGE_FILES=$(find "$BACKUP_DIR/models" -size +1G | wc -l)
echo "✓ Found $LARGE_FILES large model files (>1GB)"
# Calculate total backup size
TOTAL_SIZE=$(du -sh "$BACKUP_DIR" | cut -f1)
echo "✓ Total backup size: $TOTAL_SIZE"
echo "Backup verification completed successfully"
Recovery Time Testing
Measure how long your restoration procedures take:
#!/bin/bash
# Recovery time testing script
echo "Starting recovery time test..."
START_TIME=$(date +%s)
# Simulate restoration (use test environment)
./ollama-restore.sh
END_TIME=$(date +%s)
RECOVERY_TIME=$((END_TIME - START_TIME))
echo "Recovery completed in $RECOVERY_TIME seconds"
echo "Recovery Time Objective (RTO): $RECOVERY_TIME seconds" >> recovery-metrics.log
Best Practices for Ollama Disaster Recovery
The 3-2-1 Backup Rule
Apply the industry-standard backup strategy:
- 3 copies of your data (original + 2 backups)
- 2 different storage types (local disk + cloud/external)
- 1 offsite backup (cloud storage or remote location)
Backup Frequency Guidelines
Tailor backup schedules to your usage patterns:
High-frequency scenarios:
- Daily full backups for active development
- Hourly incremental backups during model training
- Real-time replication for production environments
Standard scenarios:
- Weekly full backups for stable environments
- Daily configuration backups
- Monthly archive backups
Security Considerations
Protect your backup data:
# Encrypt backups using GPG
gpg --symmetric --cipher-algo AES256 --compress-algo 2 \
--output ollama-backup-encrypted.gpg \
ollama-backup.tar.gz
# Decrypt when needed
gpg --decrypt ollama-backup-encrypted.gpg > ollama-backup.tar.gz
Documentation Requirements
Maintain detailed disaster recovery documentation:
- Backup procedures - Step-by-step instructions
- Restoration procedures - Complete recovery workflows
- Contact information - Key personnel and vendors
- Recovery time objectives - Maximum acceptable downtime
- Testing schedules - Regular verification procedures
Monitoring and Alerting
Backup Status Monitoring
Create alerts for backup failures:
#!/bin/bash
# Backup monitoring script with email alerts
BACKUP_LOG="/var/log/ollama-backup.log"
ALERT_EMAIL="admin@yourcompany.com"
# Check if backup completed in last 24 hours
LAST_BACKUP=$(grep "Backup completed successfully" "$BACKUP_LOG" | tail -1 | cut -d' ' -f1-2)
if [ -z "$LAST_BACKUP" ]; then
# Send alert email
echo "Ollama backup failure detected" | \
mail -s "ALERT: Ollama Backup Failed" "$ALERT_EMAIL"
echo "Alert sent: No recent backup found"
exit 1
fi
echo "Latest backup: $LAST_BACKUP"
Storage Space Monitoring
Monitor backup storage usage:
#!/bin/bash
# Storage monitoring for backup directories
BACKUP_DIR="/path/to/backups"
THRESHOLD=80 # Alert at 80% capacity
# Check disk usage
USAGE=$(df "$BACKUP_DIR" | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$USAGE" -gt "$THRESHOLD" ]; then
echo "Warning: Backup storage at ${USAGE}% capacity"
# Clean up old backups
find "$BACKUP_DIR" -name "ollama-*" -type d -mtime +30 -exec rm -rf {} \;
echo "Old backups cleaned up"
fi
Advanced Recovery Scenarios
Database Corruption Recovery
Handle corrupted Ollama model databases:
#!/bin/bash
# Database corruption recovery
echo "Checking for database corruption..."
# Try to list models
ollama list > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Database corruption detected"
# Stop Ollama
pkill ollama
# Remove corrupted database files
rm ~/.ollama/models/manifests.db* 2>/dev/null
rm ~/.ollama/models/blobs.db* 2>/dev/null
# Restart Ollama to rebuild database
ollama serve &
sleep 10
# Verify recovery
ollama list
if [ $? -eq 0 ]; then
echo "✓ Database corruption recovered"
else
echo "✗ Manual intervention required"
fi
fi
Cross-Platform Migration
Migrate Ollama installations between different operating systems:
#!/bin/bash
# Cross-platform migration script
SOURCE_OS="linux"
TARGET_OS="windows"
MIGRATION_DIR="/tmp/ollama-migration"
echo "Preparing cross-platform migration: $SOURCE_OS to $TARGET_OS"
# Create migration package
mkdir -p "$MIGRATION_DIR"
# Copy only essential files (models and configs)
cp -r ~/.ollama/models "$MIGRATION_DIR/"
cp -r ~/.ollama/config "$MIGRATION_DIR/"
# Create migration instructions
cat > "$MIGRATION_DIR/README.txt" << EOF
Ollama Cross-Platform Migration Package
Target OS: $TARGET_OS
Created: $(date)
Installation Instructions:
1. Install Ollama on target system
2. Stop Ollama service
3. Copy models/ to target Ollama directory
4. Copy config/ to target Ollama directory
5. Restart Ollama service
6. Verify with: ollama list
EOF
echo "Migration package created: $MIGRATION_DIR"
Conclusion
Disaster recovery planning protects your Ollama investments from data loss catastrophes. Regular backups, automated scheduling, and tested restoration procedures ensure your AI infrastructure survives hardware failures, software corruption, and human errors.
Key takeaways for bulletproof Ollama disaster recovery:
- Implement automated daily backups using the scripts provided
- Test restoration procedures monthly to verify backup integrity
- Follow the 3-2-1 backup rule with local and cloud storage
- Monitor backup status with alerting for failures
- Document your procedures for team consistency
Start with simple directory backups, then graduate to automated cloud synchronization as your needs grow. The investment in disaster recovery planning pays dividends when disasters strike.
Ready to secure your Ollama infrastructure? Download our complete backup automation toolkit and implement these procedures today. Your future self will thank you when disaster strikes and your AI projects survive unscathed.