---
name: auto-backup
description: Automatically backup databases, volumes, and configs before major changes. Triggers before migrations, rebuilds, or destructive operations.
---

# Auto-Backup Skill

Automatically creates safety snapshots before any major changes to prevent data loss.

## When to Trigger

AUTOMATICALLY backup before:
- Database migrations
- Docker rebuild/recreate
- Volume changes
- Config file modifications
- Destructive git operations
- Service restarts with config changes

## Backup Targets

### 1. PostgreSQL Databases
```bash
# Dump database before changes
docker exec CONTAINER pg_dump -U USER -d DATABASE > /backup/db_$(date +%Y%m%d_%H%M%S).sql
```

### 2. Docker Volumes
```bash
# Backup volume data
docker run --rm -v VOLUME:/data -v /backup:/backup alpine tar czf /backup/volume_$(date +%Y%m%d_%H%M%S).tar.gz /data
```

### 3. Config Files
```bash
# Backup configs
cp docker-compose.yml docker-compose.yml.bak.$(date +%Y%m%d_%H%M%S)
cp .env .env.bak.$(date +%Y%m%d_%H%M%S)
```

### 4. Git State
```bash
# Save current state
git stash push -m "auto-backup-$(date +%Y%m%d_%H%M%S)"
git rev-parse HEAD > /backup/git_head_$(date +%Y%m%d_%H%M%S)
```

## Backup Location

Default: `/home/zesbe/backups/[project]/[date]/`

Structure:
```
backups/
├── rima/
│   ├── 20260129_185000/
│   │   ├── database.sql
│   │   ├── volumes/
│   │   ├── configs/
│   │   └── git_state.txt
│   └── 20260130_120000/
└── jagavpn/
    └── ...
```

## Auto-Cleanup

- Keep last 5 backups per project
- Delete backups older than 7 days
- Alert if backup size > 1GB

## Rollback Commands

```bash
# Restore database
docker exec -i CONTAINER psql -U USER -d DATABASE < backup.sql

# Restore volume
docker run --rm -v VOLUME:/data -v /backup:/backup alpine tar xzf /backup/volume.tar.gz -C /

# Restore config
cp docker-compose.yml.bak docker-compose.yml
```

## Integration

Before ANY destructive operation, Claude MUST:
1. Identify what will be affected
2. Create appropriate backups
3. Verify backup integrity
4. Proceed with operation
5. Keep backup for 24h minimum

## Usage in Proactive Mode

```
🔄 AUTO-BACKUP: Creating safety snapshot...
├─ Database: rima → backup/rima/20260129/db.sql (56 rows)
├─ Volume: rima_uploads → backup/rima/20260129/uploads.tar.gz (335MB)
├─ Config: docker-compose.yml → backed up
└─ Status: ✅ Ready to proceed

Proceeding with migration...
```
