Files
CKB/BACKUP_README.md

7.5 KiB

🗃️ CKB Database Backup & Restore Guide

Panduan lengkap untuk backup dan restore database Docker CKB menggunakan mysqldump.

📋 Informasi Database

  • Container Name: ckb-mysql-dev
  • Database Name: ckb_db
  • Database Type: MariaDB 10.6
  • Port: 3306
  • Username: root / laravel
  • Password: root / password

🛠️ Available Scripts

1. backup_db.sh - Simple Backup

Script backup sederhana dengan kompresi opsional.

./backup_db.sh

Features:

  • Backup database dengan timestamp
  • Kompresi opsional (gzip)
  • Validasi container status
  • Progress indicator

2. restore_db.sh - Database Restore

Script restore dengan interface interaktif.

./restore_db.sh

Features:

  • List backup files yang tersedia
  • Support compressed & uncompressed files
  • Konfirmasi sebelum restore
  • Automatic decompression

3. backup_advanced.sh - Advanced Backup

Script backup lengkap dengan berbagai opsi.

./backup_advanced.sh

Features:

  • Multiple backup types (Full, Structure, Data)
  • Automatic cleanup old backups
  • Colored output
  • Backup statistics
  • Compression with ratio info

🚀 Quick Start

Basic Backup

# Simple backup
./backup_db.sh

# Manual backup command
docker exec ckb-mysql-dev mysqldump -u root -proot ckb_db > backup.sql

Advanced Backup

# Interactive advanced backup
./backup_advanced.sh

# Full backup with all options
docker exec ckb-mysql-dev mysqldump -u root -proot \
  --single-transaction \
  --routines \
  --triggers \
  --add-drop-table \
  ckb_db > full_backup.sql

Restore Database

# Interactive restore
./restore_db.sh

# Manual restore
docker exec -i ckb-mysql-dev mysql -u root -proot ckb_db < backup.sql

# Restore compressed backup
gunzip -c backup.sql.gz | docker exec -i ckb-mysql-dev mysql -u root -proot ckb_db

📂 Backup Types

1. Full Backup (Default)

docker exec ckb-mysql-dev mysqldump -u root -proot \
  --single-transaction \
  --routines \
  --triggers \
  --add-drop-table \
  ckb_db > full_backup.sql

2. Structure Only

docker exec ckb-mysql-dev mysqldump -u root -proot \
  --no-data \
  --routines \
  --triggers \
  ckb_db > structure_backup.sql

3. Data Only

docker exec ckb-mysql-dev mysqldump -u root -proot \
  --no-create-info \
  --single-transaction \
  ckb_db > data_backup.sql

4. Compressed Backup

docker exec ckb-mysql-dev mysqldump -u root -proot ckb_db | gzip > backup.sql.gz

⚙️ Manual Commands

Basic Commands

# Check container status
docker ps | grep ckb-mysql-dev

# Backup with timestamp
docker exec ckb-mysql-dev mysqldump -u root -proot ckb_db > ckb_backup_$(date +%Y%m%d_%H%M%S).sql

# Backup all databases
docker exec ckb-mysql-dev mysqldump -u root -proot --all-databases > all_databases.sql

# Backup specific tables
docker exec ckb-mysql-dev mysqldump -u root -proot ckb_db table1 table2 > specific_tables.sql

Advanced Options

# Backup with extended options
docker exec ckb-mysql-dev mysqldump -u root -proot \
  --single-transaction \
  --routines \
  --triggers \
  --events \
  --add-drop-table \
  --add-drop-trigger \
  --hex-blob \
  --complete-insert \
  ckb_db > advanced_backup.sql

# Backup without locking tables (for MyISAM)
docker exec ckb-mysql-dev mysqldump -u root -proot \
  --lock-tables=false \
  ckb_db > no_lock_backup.sql

# Backup with conditions
docker exec ckb-mysql-dev mysqldump -u root -proot \
  --where="created_at >= '2023-01-01'" \
  ckb_db table_name > conditional_backup.sql

🔄 Automated Backups

Cron Job Setup

# Edit crontab
crontab -e

# Daily backup at 2 AM
0 2 * * * /path/to/backup_db.sh

# Weekly full backup on Sunday at 3 AM
0 3 * * 0 /path/to/backup_advanced.sh

# Monthly cleanup (keep last 30 days)
0 4 1 * * find /path/to/backups -name "*.sql*" -mtime +30 -delete

Systemd Timer (Alternative to Cron)

Create /etc/systemd/system/ckb-backup.service:

[Unit]
Description=CKB Database Backup
After=docker.service

[Service]
Type=oneshot
ExecStart=/path/to/backup_db.sh
User=root

Create /etc/systemd/system/ckb-backup.timer:

[Unit]
Description=Run CKB backup daily
Requires=ckb-backup.service

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Enable timer:

sudo systemctl enable ckb-backup.timer
sudo systemctl start ckb-backup.timer

📊 Backup Management

Check Backup Size

# Show backup directory size
du -sh backups/

# List all backups with sizes
ls -lah backups/

# Show compression ratio
for file in backups/*.sql.gz; do
  original_size=$(gunzip -l "$file" | tail -1 | awk '{print $2}')
  compressed_size=$(stat -c%s "$file")
  ratio=$(echo "scale=1; (1 - $compressed_size/$original_size) * 100" | bc)
  echo "$(basename "$file"): ${ratio}% compression"
done

Cleanup Old Backups

# Delete backups older than 30 days
find backups/ -name "*.sql*" -mtime +30 -delete

# Keep only last 10 backups
ls -1t backups/ckb_backup_*.sql* | tail -n +11 | xargs rm -f

# Clean by size (keep if total < 1GB)
total_size=$(du -s backups/ | cut -f1)
if [ $total_size -gt 1048576 ]; then
  # Delete oldest files until under 1GB
  echo "Cleaning up old backups..."
fi

🚨 Troubleshooting

Common Issues

  1. Container not running
# Start container
docker-compose up -d db

# Check logs
docker logs ckb-mysql-dev
  1. Permission denied
# Fix script permissions
chmod +x *.sh

# Fix backup directory permissions
sudo chown -R $USER:$USER backups/
  1. Out of disk space
# Check disk usage
df -h

# Clean old backups
find backups/ -name "*.sql*" -mtime +7 -delete
  1. MySQL connection error
# Test connection
docker exec ckb-mysql-dev mysql -u root -proot -e "SELECT 1;"

# Check MySQL status
docker exec ckb-mysql-dev mysqladmin -u root -proot status

🔐 Security Notes

  1. Never store passwords in plain text - Consider using MySQL config files
  2. Encrypt sensitive backups - Use GPG for production environments
  3. Secure backup storage - Store backups in secure, offsite locations
  4. Regular restore testing - Test backups regularly to ensure they work

📈 Best Practices

  1. Regular Backups: Daily for production, weekly for development
  2. Multiple Backup Types: Keep both full and incremental backups
  3. Offsite Storage: Store backups in different physical/cloud locations
  4. Compression: Use gzip to save storage space
  5. Retention Policy: Define how long to keep backups
  6. Monitoring: Monitor backup success/failure
  7. Documentation: Document backup procedures and recovery steps
  8. Testing: Regularly test restore procedures

📝 File Structure

backups/
├── ckb_backup_20231225_143022.sql.gz    # Full backup (compressed)
├── ckb_structure_20231225_143022.sql    # Structure only
├── ckb_data_20231225_143022.sql.gz      # Data only (compressed)
└── README.md                            # This documentation

Scripts:
├── backup_db.sh                         # Simple backup script
├── backup_advanced.sh                   # Advanced backup with options
├── restore_db.sh                        # Interactive restore script
└── BACKUP_README.md                     # This documentation

Made with ❤️ for CKB Project