Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions .github/workflows/restore-staging-mongodb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
name: Restore Staging MongoDB from Production

on:
workflow_dispatch:
inputs:
anonymize_data:
description: 'Anonymize PII data after restore'
required: true
type: boolean
default: true
skip_snapshot:
description: 'Skip snapshot creation (use existing volume)'
required: false
type: boolean
default: false

env:
AWS_REGION: us-west-2

jobs:
restore-mongodb:
name: Clone Production EBS to Staging
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Verify AWS authentication
run: |
aws sts get-caller-identity
echo "✅ AWS authentication successful"

- name: Install Ansible
run: |
sudo apt-get update
sudo apt-get install -y ansible
ansible --version

- name: Verify prerequisites
run: |
echo "Checking AWS CLI..."
aws --version

echo "Checking Python..."
python3 --version

echo "✅ All prerequisites met"

- name: Run MongoDB restore playbook
working-directory: ansible/playbook
run: |
ansible-playbook -i staging/inventory mongodb-restore.yml \
-e "anonymize_data=${{ github.event.inputs.anonymize_data }}" \
-v
timeout-minutes: 30

- name: Get restore summary
if: success()
id: summary
run: |
echo "restore_complete=true" >> $GITHUB_OUTPUT
echo "timestamp=$(date -u +'%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_OUTPUT

- name: Post results to summary
if: success()
run: |
cat >> $GITHUB_STEP_SUMMARY << 'EOF'
## ✅ MongoDB Staging Restore Completed

**Timestamp:** ${{ steps.summary.outputs.timestamp }}

**Configuration:**
- Source: Production MongoDB EBS Volume
- Destination: Staging MongoDB Instance
- Region: ${{ env.AWS_REGION }}
- Data Anonymized: ${{ github.event.inputs.anonymize_data }}

**Next Steps:**
1. Verify MongoDB is running on staging
2. Test application connectivity
3. Verify data integrity
4. Clean up old volumes if needed

**Useful Commands:**
```bash
# SSH into staging
ssh ec2-user@<staging-ip>

# Check MongoDB status
sudo systemctl status mongod

# Verify data
mongosh
use userdb
db.users.countDocuments()
```
EOF

- name: Handle failure
if: failure()
run: |
cat >> $GITHUB_STEP_SUMMARY << 'EOF'
## ❌ MongoDB Staging Restore Failed

Please check the workflow logs for detailed error information.

**Common Issues:**
- AWS permissions insufficient
- SSM Agent not running on instances
- MongoDB not installed on staging
- Network connectivity issues

**Troubleshooting:**
1. Check AWS IAM permissions
2. Verify EC2 instance tags
3. Check SSM Agent status
4. Review Ansible output logs
EOF

notify:
name: Send Notification
runs-on: ubuntu-latest
needs: restore-mongodb
if: always()

steps:
- name: Notify on success
if: needs.restore-mongodb.result == 'success'
run: |
echo "✅ MongoDB staging restore completed successfully"
# Add your notification logic here (Slack, Teams, email, etc.)

- name: Notify on failure
if: needs.restore-mongodb.result == 'failure'
run: |
echo "❌ MongoDB staging restore failed"
# Add your notification logic here (Slack, Teams, email, etc.)
Loading