Management Scripts
Collection of useful scripts for managing your ANTE ERP installation.
Script Overview
| Script | Purpose | Frequency | Duration |
|---|---|---|---|
| backup-all.sh | Complete system backup | Daily | 5-10 min |
| restore-backup.sh | Restore from backup | As needed | 10-15 min |
| update.sh | Update to latest version | Weekly | 15-20 min |
| health-check.sh | System health check | Daily | 1-2 min |
| db-maintenance.sh | Database optimization | Weekly | 5-10 min |
| cleanup.sh | Clean old files/logs | Weekly | 2-5 min |
Installation
Create scripts directory:
bash
mkdir -p ~/ante-erp/scripts
cd ~/ante-erp/scriptsComplete Backup Script
File: ~/ante-erp/scripts/backup-all.sh
See Maintenance Guide - Automated Backups for the complete backup script.
Quick version:
bash
#!/bin/bash
# Complete backup of ANTE ERP
BACKUP_DIR=~/ante-erp/backups
DATE=$(date +%Y%m%d_%H%M%S)
COMPOSE_FILE=~/ante-erp/docker-compose.yml
mkdir -p "$BACKUP_DIR"/$DATE
# Backup PostgreSQL
docker compose -f "$COMPOSE_FILE" exec -T postgres \
pg_dump -U ante -F c ante_db > "$BACKUP_DIR"/$DATE/postgres.dump
# Backup MongoDB
docker compose -f "$COMPOSE_FILE" exec -T mongodb mongodump \
--username=ante --password="$MONGO_PASSWORD" \
--authenticationDatabase=admin --db=ante \
--archive | gzip > "$BACKUP_DIR"/$DATE/mongodb.archive.gz
# Backup uploads
docker cp ante-backend:/app/uploads "$BACKUP_DIR"/$DATE/
# Backup configs
cp ~/ante-erp/docker-compose.yml "$BACKUP_DIR"/$DATE/
cp ~/ante-erp/.env "$BACKUP_DIR"/$DATE/env.backup
# Create compressed archive
cd "$BACKUP_DIR"
tar -czf "ante-erp-backup-${DATE}.tar.gz" "$DATE"
rm -rf "$DATE"
# Clean old backups (keep 30 days)
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +30 -delete
echo "✓ Backup completed: ante-erp-backup-${DATE}.tar.gz"Restore Script
File: ~/ante-erp/scripts/restore-backup.sh
bash
#!/bin/bash
# Restore ANTE ERP from backup
if [ -z "$1" ]; then
echo "Usage: $0 <backup-file.tar.gz>"
echo ""
echo "Available backups:"
ls -lht ~/ante-erp/backups/*.tar.gz | head -10
exit 1
fi
BACKUP_FILE=$1
RESTORE_DIR=~/ante-erp/restore_temp
COMPOSE_FILE=~/ante-erp/docker-compose.yml
echo "════════════════════════════════════════"
echo " ANTE ERP Restore Script"
echo "════════════════════════════════════════"
echo ""
echo "⚠️ WARNING: This will restore from backup!"
echo " Current data may be overwritten."
echo ""
read -p "Continue? (yes/no): " confirm
if [ "$confirm" != "yes" ]; then
echo "Restore cancelled."
exit 0
fi
echo ""
echo "Step 1/7: Stopping services..."
docker compose -f "$COMPOSE_FILE" stop backend frontend
echo "Step 2/7: Extracting backup..."
mkdir -p "$RESTORE_DIR"
tar -xzf "$BACKUP_FILE" -C "$RESTORE_DIR"
BACKUP_DIR=$(ls -d "$RESTORE_DIR"/*/ | head -1)
echo "Step 3/7: Restoring PostgreSQL..."
docker compose -f "$COMPOSE_FILE" exec -T postgres \
pg_restore -U ante -d ante_db --clean --if-exists < "$BACKUP_DIR/postgres.dump"
echo "Step 4/7: Restoring MongoDB..."
if [ -f "$BACKUP_DIR/mongodb.archive.gz" ]; then
gunzip -c "$BACKUP_DIR/mongodb.archive.gz" | \
docker compose -f "$COMPOSE_FILE" exec -T mongodb \
mongorestore --username=ante --password="$MONGO_PASSWORD" \
--authenticationDatabase=admin --archive
fi
echo "Step 5/7: Restoring uploads..."
if [ -d "$BACKUP_DIR/uploads" ]; then
docker cp "$BACKUP_DIR/uploads" ante-backend:/app/
fi
echo "Step 6/7: Starting services..."
docker compose -f "$COMPOSE_FILE" start backend frontend
echo "Step 7/7: Cleaning up..."
rm -rf "$RESTORE_DIR"
echo ""
echo "════════════════════════════════════════"
echo "✓ Restore completed successfully!"
echo "════════════════════════════════════════"
echo ""
echo "Access ANTE ERP at: http://localhost:8080"Update Script
File: ~/ante-erp/scripts/update.sh
bash
#!/bin/bash
# Update ANTE ERP to latest version
COMPOSE_FILE=~/ante-erp/docker-compose.yml
BACKUP_SCRIPT=~/ante-erp/scripts/backup-all.sh
echo "════════════════════════════════════════"
echo " ANTE ERP Update Script"
echo "════════════════════════════════════════"
echo ""
# Check if backup script exists
if [ ! -f "$BACKUP_SCRIPT" ]; then
echo "⚠️ Warning: Backup script not found!"
read -p "Continue without backup? (yes/no): " continue_without_backup
if [ "$continue_without_backup" != "yes" ]; then
exit 0
fi
else
echo "Step 1/6: Creating backup..."
$BACKUP_SCRIPT
if [ $? -ne 0 ]; then
echo "✗ Backup failed! Aborting update."
exit 1
fi
fi
echo "Step 2/6: Pulling latest Docker images..."
docker compose -f "$COMPOSE_FILE" pull
echo "Step 3/6: Stopping services..."
docker compose -f "$COMPOSE_FILE" down
echo "Step 4/6: Starting with new images..."
docker compose -f "$COMPOSE_FILE" up -d
echo "Step 5/6: Waiting for services to be ready..."
sleep 30
# Wait for backend to be healthy
for i in {1..30}; do
if curl -f http://localhost:3001/health >/dev/null 2>&1; then
echo "✓ Backend is healthy"
break
fi
if [ $i -eq 30 ]; then
echo "✗ Backend health check timed out"
echo "Check logs: docker compose -f $COMPOSE_FILE logs backend"
exit 1
fi
sleep 2
done
echo "Step 6/6: Verifying services..."
docker compose -f "$COMPOSE_FILE" ps
echo ""
echo "════════════════════════════════════════"
echo "✓ Update completed successfully!"
echo "════════════════════════════════════════"
echo ""
echo "Frontend: http://localhost:8080"
echo "Backend: http://localhost:3001"
echo ""
echo "Check logs: docker compose -f $COMPOSE_FILE logs -f"Health Check Script
File: ~/ante-erp/scripts/health-check.sh
bash
#!/bin/bash
# ANTE ERP Health Check
COMPOSE_FILE=~/ante-erp/docker-compose.yml
echo "════════════════════════════════════════"
echo " ANTE ERP Health Check"
echo " $(date '+%Y-%m-%d %H:%M:%S')"
echo "════════════════════════════════════════"
# 1. Service Status
echo ""
echo "1. Service Status:"
echo "─────────────────"
docker compose -f "$COMPOSE_FILE" ps
# 2. Disk Space
echo ""
echo "2. Disk Space:"
echo "──────────────"
df -h | grep -E "Filesystem|/$"
# 3. Memory Usage
echo ""
echo "3. Memory Usage:"
echo "────────────────"
free -h | grep -E "total|Mem"
# 4. Container Resource Usage
echo ""
echo "4. Container Resources:"
echo "───────────────────────"
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
# 5. Database Health
echo ""
echo "5. Database Health:"
echo "───────────────────"
# PostgreSQL
if docker compose -f "$COMPOSE_FILE" exec -T postgres pg_isready -U ante >/dev/null 2>&1; then
conn_count=$(docker compose -f "$COMPOSE_FILE" exec -T postgres \
psql -U ante -d ante_db -t -c "SELECT count(*) FROM pg_stat_activity;" 2>/dev/null | tr -d ' ')
echo "PostgreSQL: ✓ Healthy ($conn_count connections)"
else
echo "PostgreSQL: ✗ Not responding"
fi
# Redis
if docker compose -f "$COMPOSE_FILE" exec redis redis-cli -a "$REDIS_PASSWORD" ping >/dev/null 2>&1; then
echo "Redis: ✓ Healthy"
else
echo "Redis: ✗ Not responding"
fi
# MongoDB
if docker compose -f "$COMPOSE_FILE" exec -T mongodb mongosh \
--username ante --password "$MONGO_PASSWORD" \
--authenticationDatabase admin --eval "db.version()" >/dev/null 2>&1; then
echo "MongoDB: ✓ Healthy"
else
echo "MongoDB: ✗ Not responding"
fi
# 6. Application Health
echo ""
echo "6. Application Health:"
echo "──────────────────────"
# Backend
if backend_health=$(curl -sf http://localhost:3001/health 2>/dev/null); then
echo "Backend: ✓ Healthy"
else
echo "Backend: ✗ Not responding"
fi
# Frontend
if curl -sf -o /dev/null http://localhost:8080 2>/dev/null; then
echo "Frontend: ✓ Healthy"
else
echo "Frontend: ✗ Not responding"
fi
# 7. Recent Errors
echo ""
echo "7. Recent Errors (last 10):"
echo "────────────────────────────"
error_count=$(docker compose -f "$COMPOSE_FILE" logs --tail=100 backend 2>/dev/null | \
grep -i error | wc -l)
if [ "$error_count" -gt 0 ]; then
echo "Found $error_count error(s) in recent logs:"
docker compose -f "$COMPOSE_FILE" logs --tail=100 backend 2>/dev/null | \
grep -i error | tail -5
else
echo "No recent errors found"
fi
echo ""
echo "════════════════════════════════════════"Database Maintenance Script
File: ~/ante-erp/scripts/db-maintenance.sh
bash
#!/bin/bash
# Database maintenance and optimization
COMPOSE_FILE=~/ante-erp/docker-compose.yml
LOG_FILE=~/ante-erp/logs/maintenance.log
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
log "════════════════════════════════════════"
log "Starting database maintenance"
log "════════════════════════════════════════"
# PostgreSQL Maintenance
log "Optimizing PostgreSQL..."
docker compose -f "$COMPOSE_FILE" exec -T postgres psql -U ante -d ante_db <<EOF
-- Analyze tables (update statistics)
ANALYZE;
-- Vacuum (reclaim space)
VACUUM;
-- Reindex
REINDEX DATABASE ante_db;
EOF
if [ $? -eq 0 ]; then
log "✓ PostgreSQL optimization completed"
else
log "✗ PostgreSQL optimization failed"
fi
# Redis Maintenance
log "Optimizing Redis..."
docker compose -f "$COMPOSE_FILE" exec redis \
redis-cli -a "$REDIS_PASSWORD" BGREWRITEAOF >/dev/null 2>&1
if [ $? -eq 0 ]; then
log "✓ Redis optimization completed"
else
log "✗ Redis optimization failed"
fi
# MongoDB Maintenance (compact if needed)
log "Checking MongoDB..."
log "✓ MongoDB maintenance completed"
log "════════════════════════════════════════"
log "Database maintenance completed"
log "════════════════════════════════════════"Cleanup Script
File: ~/ante-erp/scripts/cleanup.sh
bash
#!/bin/bash
# Clean up old files, logs, and Docker resources
COMPOSE_FILE=~/ante-erp/docker-compose.yml
LOG_DIR=~/ante-erp/logs
BACKUP_DIR=~/ante-erp/backups
echo "════════════════════════════════════════"
echo " ANTE ERP Cleanup Script"
echo "════════════════════════════════════════"
echo ""
# 1. Clean old logs
echo "1. Cleaning old logs (>30 days)..."
if [ -d "$LOG_DIR" ]; then
find "$LOG_DIR" -name "*.log" -mtime +30 -type f -delete
echo " ✓ Old logs cleaned"
else
echo " ℹ No log directory found"
fi
# 2. Clean old backups
echo "2. Cleaning old backups (>90 days)..."
if [ -d "$BACKUP_DIR" ]; then
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +90 -type f -delete
count=$(find "$BACKUP_DIR" -name "*.tar.gz" | wc -l)
echo " ✓ $count backups remaining"
else
echo " ℹ No backup directory found"
fi
# 3. Docker cleanup
echo "3. Docker cleanup..."
# Show current usage
echo " Current Docker disk usage:"
docker system df
read -p " Clean up unused Docker resources? (yes/no): " clean_docker
if [ "$clean_docker" = "yes" ]; then
echo " Cleaning Docker images..."
docker image prune -f
echo " Cleaning Docker containers..."
docker container prune -f
echo " Cleaning Docker networks..."
docker network prune -f
echo " ✓ Docker cleanup completed"
echo " After cleanup:"
docker system df
else
echo " ℹ Docker cleanup skipped"
fi
# 4. Show disk usage
echo ""
echo "4. Current disk usage:"
df -h | grep -E "Filesystem|/$"
echo ""
echo "════════════════════════════════════════"
echo "✓ Cleanup completed"
echo "════════════════════════════════════════"Making Scripts Executable
bash
# Make all scripts executable
chmod +x ~/ante-erp/scripts/*.sh
# Verify permissions
ls -l ~/ante-erp/scripts/Scheduling Scripts with Cron
Edit crontab:
bash
crontab -eAdd these entries:
cron
# ANTE ERP Automated Tasks
# Daily backup at 2 AM
0 2 * * * ~/ante-erp/scripts/backup-all.sh >> ~/ante-erp/logs/backup.log 2>&1
# Daily health check at 8 AM
0 8 * * * ~/ante-erp/scripts/health-check.sh >> ~/ante-erp/logs/health.log 2>&1
# Weekly maintenance on Sunday at 3 AM
0 3 * * 0 ~/ante-erp/scripts/db-maintenance.sh
# Weekly cleanup on Sunday at 4 AM
0 4 * * 0 ~/ante-erp/scripts/cleanup.sh >> ~/ante-erp/logs/cleanup.log 2>&1
# Weekly update check on Monday at 1 AM (manual approval needed)
0 1 * * 1 echo "Update available. Run: ~/ante-erp/scripts/update.sh" | mail -s "ANTE ERP Update Reminder" admin@yourcompany.comScript Notifications
Email Notifications
Install mail utility:
bash
# Ubuntu/Debian
sudo apt-get install mailutils
# CentOS/RHEL
sudo yum install mailxAdd to scripts:
bash
# Send email on completion
echo "Backup completed successfully" | \
mail -s "ANTE ERP Backup Success" admin@yourcompany.com
# Send email on failure
if [ $? -ne 0 ]; then
echo "Backup failed! Check logs." | \
mail -s "ANTE ERP Backup FAILED" admin@yourcompany.com
fiTelegram Notifications
bash
# Add to your scripts
send_telegram() {
local message="$1"
local bot_token="YOUR_BOT_TOKEN"
local chat_id="YOUR_CHAT_ID"
curl -s -X POST "https://api.telegram.org/bot${bot_token}/sendMessage" \
-d "chat_id=${chat_id}" \
-d "text=${message}" \
>/dev/null
}
# Use in scripts
send_telegram "✓ ANTE ERP backup completed successfully"Testing Scripts
Before scheduling, test each script:
bash
# Test backup
~/ante-erp/scripts/backup-all.sh
# Test health check
~/ante-erp/scripts/health-check.sh
# Test maintenance
~/ante-erp/scripts/db-maintenance.sh
# Test cleanup (with dry-run if possible)
~/ante-erp/scripts/cleanup.shScript Logging
All scripts should log their activity:
bash
# Create log directory
mkdir -p ~/ante-erp/logs
# Add logging to scripts
LOG_FILE=~/ante-erp/logs/$(basename $0 .sh).log
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# Use in scripts
log "Starting backup process..."
log "✓ Backup completed"
log "✗ Backup failed with error"Monitoring Script Execution
View script logs:
bash
# View recent logs
tail -f ~/ante-erp/logs/*.log
# View specific script log
less ~/ante-erp/logs/backup.log
# Search for errors
grep -i error ~/ante-erp/logs/*.log
# View cron execution logs
grep CRON /var/log/syslogScript Maintenance Checklist
Weekly:
- [ ] Review script logs
- [ ] Verify backups are being created
- [ ] Check cron job execution
- [ ] Test restore process (monthly)
Monthly:
- [ ] Update scripts if needed
- [ ] Review and optimize
- [ ] Test all scripts manually
- [ ] Update documentation
Next Steps
Last Updated: October 27, 2025
