Database Rollback
Database Backup Before Deployment
# Full database backup
mysqldump -u root -p magento_db > backup_$(date +%Y%m%d_%H%M%S).sql
# Backup with compression
mysqldump -u root -p magento_db | gzip > backup_$(date +%Y%m%d_%H%M%S).sql.gz
# Backup specific tables
mysqldump -u root -p magento_db catalog_product_entity sales_order > partial_backup.sql
# Restore from backup
mysql -u root -p magento_db < backup_20240101_120000.sql
# Restore compressed backup
gunzip < backup_20240101_120000.sql.gz | mysql -u root -p magento_db
Schema Rollback with Patches
# Check applied patches
bin/magento module:status
# List data patches
bin/magento setup:patch:status
# Revert a specific patch
bin/magento setup:patch:revert <patch_name>
Transaction-Based Rollback
<?php
// Before making changes
$connection->beginTransaction();
try {
// Make changes
$connection->query(
"UPDATE catalog_product_entity SET name = ? WHERE entity_id = ?",
[$newName, $productId]
);
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
throw $e;
}
Database Point-in-Time Recovery
# Enable binary logging in MySQL
# my.cnf:
# log-bin=mysql-bin
# binlog_format=ROW
# Find position in binary log
mysqlbinlog --start-datetime="2024-01-01 12:00:00" \
--stop-datetime="2024-01-01 13:00:00" \
mysql-bin.000001
# Restore to specific point
mysqlbinlog --stop-datetime="2024-01-01 12:30:00" mysql-bin.000001 | mysql -u root -p
Key Takeaway
Backup database before every deployment. Use mysqldump for backups, binary logs for point-in-time recovery, and transactions for safe changes.
Code Rollback
Git-Based Rollback
# See recent commits
git log --oneline -10
# Revert specific commit
git revert abc1234
# Revert merge commit
# Option 1: Keep history
git revert -m 1 <merge_commit>
# Option 2: Reset to previous
# (DANGER: rewrites history)
git reset --hard HEAD~1
# Rollback to specific tag
git checkout v2.4.5
# Rollback to specific commit
git checkout abc1234
Deployment Rollback Script
#!/bin/bash
# rollback.sh
DEPLOY_DIR="/var/www/magento"
BACKUP_DIR="/var/backups/magento"
rollback_to_backup() {
local backup_path=$1
echo "Rolling back to $backup_path..."
# Enable maintenance mode
cd $DEPLOY_DIR && bin/magento maintenance:enable
# Restore code
rm -rf $DEPLOY_DIR/app/code/Vendor/
cp -r $backup_path/app/code/Vendor/ $DEPLOY_DIR/app/code/
# Restore generated code
rm -rf $DEPLOY_DIR/generated/
cp -r $backup_path/generated/ $DEPLOY_DIR/
# Restore static content
rm -rf $DEPLOY_DIR/pub/static/
cp -r $backup_path/pub/static/ $DEPLOY_DIR/pub/
# Clear cache
cd $DEPLOY_DIR && bin/magento cache:flush
# Disable maintenance mode
cd $DEPLOY_DIR && bin/magento maintenance:disable
echo "Rollback complete!"
}
# Usage
rollback_to_backup "/var/backups/magento/20240101_120000"
Composer-Based Rollback
# Check installed versions
composer show | grep magento
# Downgrade a package
composer require vendor/module:2.4.5 --no-update
composer update vendor/module
# Restore composer.lock from backup
cp /backups/composer.lock.bak composer.lock
composer install
Key Takeaway
Use git revert for safe rollbacks, deployment backups for full code restoration, and composer for package-specific rollbacks.
Full Environment Rollback
Blue-Green Rollback
# Instant rollback by switching traffic
# Current: blue (v1) -> live
# Deployed: green (v2) -> idle
# Switch back to blue
# Update load balancer configuration
# Traffic now goes to blue (v1)
# No code changes needed - just traffic switch
Complete Rollback Script
#!/bin/bash
# full-rollback.sh
set -e
DEPLOY_DIR="/var/www/magento"
BACKUP_DIR="/var/backups/magento"
TIMESTAMP=$1
if [ -z "$TIMESTAMP" ]; then
echo "Usage: $0 <timestamp>"
echo "Available backups:"
ls -la $BACKUP_DIR/
exit 1
fi
BACKUP_PATH="$BACKUP_DIR/$TIMESTAMP"
if [ ! -d "$BACKUP_PATH" ]; then
echo "Backup not found: $BACKUP_PATH"
exit 1
fi
echo "=== Starting Full Rollback ==="
echo "Rolling back to: $TIMESTAMP"
# 1. Enable maintenance mode
echo "1. Enabling maintenance mode..."
cd $DEPLOY_DIR && bin/magento maintenance:enable
# 2. Restore database
echo "2. Restoring database..."
if [ -f "$BACKUP_PATH/database.sql.gz" ]; then
gunzip < $BACKUP_PATH/database.sql.gz | mysql -u root -p magento_db
fi
# 3. Restore code
echo "3. Restoring code..."
rm -rf $DEPLOY_DIR/app/code/Vendor/
cp -r $BACKUP_PATH/app/code/Vendor/ $DEPLOY_DIR/app/code/
# 4. Restore configuration
echo "4. Restoring configuration..."
cp $BACKUP_PATH/env.php $DEPLOY_DIR/app/etc/env.php
# 5. Restore generated code
echo "5. Restoring generated code..."
rm -rf $DEPLOY_DIR/generated/
cp -r $BACKUP_PATH/generated/ $DEPLOY_DIR/
# 6. Restore static content
echo "6. Restoring static content..."
rm -rf $DEPLOY_DIR/pub/static/
cp -r $BACKUP_PATH/pub/static/ $DEPLOY_DIR/pub/
# 7. Clear cache
echo "7. Clearing cache..."
cd $DEPLOY_DIR && bin/magento cache:flush
# 8. Disable maintenance mode
echo "8. Disabling maintenance mode..."
cd $DEPLOY_DIR && bin/magento maintenance:disable
# 9. Verify
echo "9. Verifying rollback..."
HTTP_STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://localhost/)
if [ $HTTP_STATUS -eq 200 ]; then
echo "=== Rollback Successful ==="
else
echo "=== WARNING: Health check returned $HTTP_STATUS ==="
fi
Key Takeaway
Full rollback restores database, code, configuration, generated code, and static content. Always enable maintenance mode and verify after rollback.
Rollback Testing
Rollback Testing Strategy
## Pre-Production Rollback Test
### Test Checklist
- [ ] Backup created successfully
- [ ] Backup can be restored in test environment
- [ ] Rollback script executes without errors
- [ ] Site works correctly after rollback
- [ ] Database data is correct after rollback
- [ ] Static content is correct after rollback
- [ ] Cache is cleared and warmed
Automated Rollback Testing
# GitHub Actions rollback test
rollback-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Test Environment
run: |
docker-compose -f docker-compose.test.yml up -d
sleep 30
- name: Create Backup
run: |
docker-compose exec magento bin/magento backup:db
docker-compose exec magento bin/magento backup:media
docker-compose exec magento bin/magento backup:code
- name: Make Changes
run: |
# Simulate deployment changes
docker-compose exec magento bin/magento setup:upgrade
- name: Rollback
run: |
docker-compose exec magento bin/magento setup:rollback \
--db-backup=magento-db-backup-*.sql.gz
- name: Verify Rollback
run: |
HTTP_STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://localhost/)
if [ $HTTP_STATUS -ne 200 ]; then
echo "Rollback verification failed"
exit 1
fi
Rollback Runbook
## Emergency Rollback Runbook
### Symptoms Requiring Rollback
- Site returns 500 errors
- Critical functionality broken
- Performance severe degradation
- Security vulnerability introduced
### Rollback Steps
1. Announce rollback in #incident channel
2. Enable maintenance mode
3. Identify backup timestamp to restore
4. Run rollback script: ./rollback.sh <timestamp>
5. Verify site functionality
6. Disable maintenance mode
7. Monitor for issues
8. Post-incident review
### Rollback Contacts
- Primary: On-call engineer
- Secondary: Team lead
- Escalation: Engineering manager
Key Takeaway
Test rollbacks regularly. Create automated rollback tests. Maintain runbooks for emergency procedures. Verify rollback success with health checks.
Quiz
1. What should you backup before every deployment?
2. What is the difference between git revert and git reset?
3. Why enable maintenance mode during rollback?
4. What is point-in-time recovery?
5. How often should rollback procedures be tested?
Flashcards
Question
What to backup before deployment?
Click to reveal answer
Answer
Database (mysqldump), code (tar/git), configuration (env.php)
Question
git revert vs git reset?
Click to reveal answer
Answer
Revert: creates new commit, safe for shared. Reset: rewrites history, dangerous.
Question
What is point-in-time recovery?
Click to reveal answer
Answer
Restore database to specific moment using MySQL binary logs
Question
Why test rollbacks?
Click to reveal answer
Answer
Ensure procedures work when needed and team is prepared for emergencies
Question
What is blue-green rollback?
Click to reveal answer
Answer
Switch traffic to previous environment instantly without code changes
Question
Rollback verification step?
Click to reveal answer
Answer
Health check after rollback to confirm site is working correctly
Revision Notes
Key Takeaways
- 1. Always backup database and code before deployment
- 2. Use git revert for safe rollbacks, git reset for local-only
- 3. Enable maintenance mode during rollback
- 4. Test rollbacks regularly with automated tests
- 5. Maintain rollback runbooks for emergencies
Interview Tips
- • Describe your rollback strategy and procedures
- • Explain database backup and recovery approaches
- • Compare different rollback methods
- • Discuss rollback testing strategies
Cheat Sheet
Rollback Procedures
Backup:
mysqldump for database
tar for code
Git Rollback:
git revert
git reset --hard
Full Rollback:
- Maintenance mode
- Restore database
- Restore code
- Clear cache
- Disable maintenance
- Verify
Testing:
Automated rollback tests
Monthly verification
Runbook maintenance