DEV Community

Cover image for Automated Backup to S3
James Ackah
James Ackah

Posted on

Automated Backup to S3

Automated Backup to S3

This project is a Bash script designed to automate the process of creating backups, uploading them to an Amazon S3 bucket, and sending email notifications to keep administrators informed about the status of the backup process.

Features

  • Automated Backup: Creates a compressed archive of the specified directory.
  • S3 Integration: Uploads the backup file to a designated Amazon S3 bucket.
  • Email Notifications: Sends email alerts for each stage of the process (start, success, or failure).
  • File Cleanup: Deletes local backup files after successful upload to S3.

Prerequisites

Before using the script, ensure the following requirements are met:

  1. AWS CLI: Installed and configured with appropriate credentials.
  2. Email Client: mutt installed and configured to send emails.
  3. Bash Shell: Running on a Linux/Unix system with Bash.
  4. Directories:
    • Source directory to be backed up: /home/ubuntu/System_Data
    • Backup destination: /home/ubuntu/system_backup/

Usage

  1. Clone or Create the Script:
    Save the provided Bash script as backup.sh on your system.

  2. Modify Configuration:
    Update the script variables as needed:

    • BACKUP_SOURCE: Path to the directory to be backed up.
    • BACKUP_DEST: Path where the backup file will be stored temporarily.
    • S3_BUCKET: S3 bucket where the backup will be uploaded.
    • ADMIN_EMAIL: Email address to receive notifications.
  3. Set Execute Permissions:
    Make the script executable:

   chmod +x backup.sh
Enter fullscreen mode Exit fullscreen mode
  1. Run the Script: Execute the script manually or schedule it as a cron job:
   ./backup.sh
Enter fullscreen mode Exit fullscreen mode
  1. Set Up Cron Job (Optional): Automate the backup by adding a cron job:
   crontab -e
Enter fullscreen mode Exit fullscreen mode

Add the following line to run the script daily at midnight:

   0 0 * * * /path/to/backup.sh
Enter fullscreen mode Exit fullscreen mode

Script Workflow

  1. Send an email notification that the backup process has started.
  2. Creates a compressed archive (.tar.gz) of the specified directory.
  3. Check if the backup creation was successful:
    • If successful, send a success email.
    • If failed, send a failure email and terminate the script.
  4. Upload the backup file to the specified S3 bucket.
  5. Check if the upload was successful:
    • If successful, send a success email and delete the local backup file.
    • If failed, send a failure email and terminate the script.

Example Configuration

BACKUP_SOURCE="/home/ubuntu/System_Data"
BACKUP_DEST="/home/ubuntu/system_backup/"
BACKUP_FILE="backup_$(date +%Y%m%d).tar.gz"
S3_BUCKET="s3://linuxacademy-awscli-backup"
ADMIN_EMAIL="jamesblay80@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Dependencies

  • tar: Used to compress the backup files.
  • aws-cli: Used to upload files to S3.
  • mutt: Used to send email notifications.

Error Handling

  • The script uses $? to check the success of each operation.
  • If any operation fails, the script sends a failure notification and exits with a non-zero status.

Notes

  • Ensure the AWS CLI is configured with credentials that have appropriate permissions to upload to the specified S3 bucket.
  • Email notifications require mutt to be properly configured on the server.

Contact

For questions or assistance, please get in touch with James Ackah-Blay at jamesblay80@gmail.com.``

Top comments (0)