Automate Backups and Disaster Recovery in DevOps 

Backups and disaster recovery (DR) are critical to ensuring business continuity in case of failures, data loss, or unexpected incidents. Automating these processes helps reduce the risk of human error, improve reliability, and save time in managing backups. In this post, we’ll explore how to automate backups and disaster recovery for databases, file systems, and cloud resources using AWS Backup, Bash scripts, and Ansible, with hands-on examples and best practices.


Disaster Recovery

Disaster Recovery (DR) is the strategy, processes, and tools used to restore IT systems, data, and infrastructure after a cyberattack, natural disaster, hardware failure, or human error. The goal is to minimize downtime and data loss, ensuring business continuity.

Why Automate Backups and Disaster Recovery?

Key Benefits

  1. Data Protection:
    • Protect against data loss caused by hardware failures, cyberattacks, or accidental deletions.
  2. Improved Efficiency:
  3. Faster Recovery:
    • Automate recovery processes to minimize downtime in case of disasters.
  4. Compliance:

For a deeper understanding of disaster recovery concepts, refer to AWS Disaster Recovery Guide.


Backup Automation Use Cases

1. Database Backups

  • Automate backups for relational databases like MySQL, PostgreSQL, and MongoDB.
  • Schedule periodic backups and validate data integrity.

2. File System Backups

  • Create regular snapshots of file systems for business-critical data.

3. Cloud Resource Backups

  • Automate backups for EC2 instances, EBS volumes, and RDS databases using cloud-native tools.

Step-by-Step Guide Automate Backups and Disaster Recovery in DevOps 

We’ll implement automation using Bash scripts, Ansible, and AWS Backup.


1. Automate Database Backups

1.1: Automating MySQL Backups with Bash

  1. Create a backup script mysql_backup.sh:
    #!/bin/bash
    
    # Configuration
    DB_USER="root"
    DB_PASSWORD="password"
    DB_NAME="my_database"
    BACKUP_DIR="/backups/mysql"
    TIMESTAMP=$(date +"%Y%m%d%H%M%S")
    BACKUP_FILE="${BACKUP_DIR}/${DB_NAME}_${TIMESTAMP}.sql"
    
    # Ensure backup directory exists
    mkdir -p "$BACKUP_DIR"
    
    # Create the backup
    mysqldump -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" > "$BACKUP_FILE"
    
    # Check if backup was successful
    if [[ $? -eq 0 ]]; then
        echo "Backup successful: $BACKUP_FILE"
    else
        echo "Backup failed!"
        exit 1
    fi
    
    # Remove backups older than 7 days
    find "$BACKUP_DIR" -type f -mtime +7 -exec rm {} \;
    
  2. Make the script executable:
    chmod +x mysql_backup.sh
    
  3. Schedule periodic backups with cron:
    crontab -e
    

    Add the following to schedule daily backups at 2 AM:

    0 2 * * * /path/to/mysql_backup.sh >> /var/log/mysql_backup.log 2>&1
    

2. Automate File System Backups

2.1: Automating File System Backups with rsync

  1. Create a backup script filesystem_backup.sh:
    #!/bin/bash
    
    # Configuration
    SOURCE_DIR="/data"
    BACKUP_DIR="/backups/filesystem"
    TIMESTAMP=$(date +"%Y%m%d%H%M%S")
    BACKUP_PATH="${BACKUP_DIR}/${TIMESTAMP}"
    
    # Ensure backup directory exists
    mkdir -p "$BACKUP_PATH"
    
    # Perform the backup
    rsync -av --delete "$SOURCE_DIR/" "$BACKUP_PATH/"
    
    # Remove backups older than 7 days
    find "$BACKUP_DIR" -type d -mtime +7 -exec rm -rf {} \;
    
    echo "Backup completed: $BACKUP_PATH"
    
  2. Schedule the script with cron:
    crontab -e
    

    Add:

    0 1 * * * /path/to/filesystem_backup.sh >> /var/log/filesystem_backup.log 2>&1
    

3. Automate Cloud Backups with AWS Backup

AWS Backup is a fully managed service for automating cloud backups.

3.1: Configure AWS Backup

  1. Create a backup plan using AWS CLI:
    aws backup create-backup-plan --backup-plan \
    '{
        "BackupPlanName": "MyBackupPlan",
        "Rules": [
            {
                "RuleName": "DailyBackup",
                "TargetBackupVaultName": "Default",
                "ScheduleExpression": "cron(0 2 * * ? *)",
                "Lifecycle": {
                    "DeleteAfterDays": 30
                }
            }
        ]
    }'
    
  2. Assign resources to the backup plan:
    aws backup create-backup-selection --backup-plan-id <backup-plan-id> \
    --backup-selection '{"SelectionName": "MyBackupSelection", "IamRoleArn": "<IAM-Role-ARN>", "Resources": ["arn:aws:ec2:region:account-id:volume/volume-id"]}'
    
  3. Monitor backups:
    aws backup list-backups
    

For more details, visit the AWS Backup Documentation.


4. Automate Disaster Recovery

4.1: Disaster Recovery with Ansible

  1. Write an Ansible playbook dr_playbook.yml:
    ---
    - hosts: all
      become: yes
      tasks:
        - name: Restore MySQL database
          shell: mysql -u root -p{{ mysql_password }} < /backups/mysql/latest.sql
    
        - name: Restore files
          synchronize:
            src: /backups/filesystem/latest/
            dest: /data/
    
  2. Run the playbook:
    ansible-playbook -i inventory dr_playbook.yml --extra-vars "mysql_password=yourpassword"
    

Advanced Backup Features

1. Incremental Backups

  • Use tools like rsnapshot for efficient incremental backups:
    apt install rsnapshot
    rsnapshot configtest
    rsnapshot daily
    

2. Multi-Region Backups

  • Replicate backups across AWS regions:
    aws s3 sync s3://source-bucket s3://destination-bucket --region us-west-1
    

3. Validation and Reporting

  • Validate backups using checksum tools like md5sum or sha256sum.
  • Generate automated reports with tools like Grafana.

High availability disaster recovery​

Best Practices

  1. Test Backups Regularly:
    • Ensure backups can be restored without issues by performing regular recovery tests.
  2. Encrypt Backups:
    • Encrypt backups using GPG or AWS KMS:
      gpg --encrypt --recipient [email protected] backup.sql
      
  3. Monitor Backup Health:
    • Use tools like Prometheus and Grafana to monitor backup job statuses.
  4. Implement Retention Policies:
    • Retain backups for an appropriate duration based on compliance requirements.
  5. Automate Notifications:
    • Send email or Slack notifications for backup job statuses.

Official Links


Conclusion

Automating backups and disaster recovery ensures data safety, minimizes downtime, and improves operational efficiency. By integrating tools like Bash scripts, Ansible, and AWS Backup, you can achieve a robust backup and recovery strategy tailored to your infrastructure.

Related articles

Software as a Service (SaaS)

Software as a Service (SaaS) Software as a Service (SaaS) is a cloud computing model that delivers software applications...

Top Kubernetes Monitoring Tools & Best Practices (2025) Open‑Source, Free, GitHub‑Ready

Top Kubernetes Monitoring Tools & Best Practices for 2025 (Open‑Source, Free, GitHub‑First) Why monitoring Kubernetes in 2025 needs an...

How to Create and Manage RDS Databases on AWS

📊 How to Create and Manage RDS Databases on AWS: A Complete Guide Managing databases efficiently is a cornerstone...

Zero Downtime Deployment | Techniques and Automation

Zero Downtime Deployment : Techniques and Automation Zero downtime deployment ensures that applications remain accessible to users during updates...