Cloud and AWS Intermediate

AWS S3 Backup and Sync

Backs up and syncs local files to AWS S3. Uploads to S3 buckets using AWS CLI.

Published: May 10, 2024

Detailed Information

This script backs up and syncs local files to AWS S3. Performs secure upload to S3 buckets using AWS CLI.

What Does This Script Do?

This script performs S3 backup:

  • Syncs local files to S3
  • Updates changed files
  • Deletes removed files from S3 (--delete)
  • Performs secure upload

Why Should You Use It?

AWS S3 backup is ideal for cloud storage:

  • Cloud Storage: Secure cloud backup
  • Automation: Automatic synchronization
  • Scalability: Unlimited storage
  • Security: AWS security features

How to Use

Step-by-Step Usage Guide

1. Install AWS CLI

pip install awscli
aws configure

2. Create Script File

nano s3_backup.sh

3. Make Executable

chmod +x s3_backup.sh

4. Run Script

./s3_backup.sh /backup/files my-backup-bucket backups/

Requirements

Requirements

  • AWS CLI: AWS command line tool
  • AWS Credentials: AWS access keys
  • S3 Bucket: AWS S3 bucket must be created

Use Cases

Use Cases

1. Cloud Backup

Back up critical data to AWS S3.

2. Automatic Synchronization

Regularly sync files to S3.

Examples

Usage Examples

Example 1: File Backup

./s3_backup.sh /home/user/documents my-bucket documents/

Code

#!/bin/bash

# AWS S3 Backup and Sync Script

if [ -z "$1" ] || [ -z "$2" ]; then
    echo "Usage: $0 <local_path> <s3_bucket> [s3_path]"
    echo "Example: $0 /backup/files my-backup-bucket backups/"
    exit 1
fi

LOCAL_PATH="$1"
S3_BUCKET="$2"
S3_PATH="${3:-}"

if [ ! -d "$LOCAL_PATH" ]; then
    echo "Error: Local path not found: $LOCAL_PATH"
    exit 1
fi

if ! command -v aws &> /dev/null; then
    echo "Error: AWS CLI not installed"
    echo "Install: pip install awscli"
    exit 1
fi

echo "======================================"
echo "   AWS S3 BACKUP SYNC"
echo "======================================"
echo "Local Path: $LOCAL_PATH"
echo "S3 Bucket: s3://$S3_BUCKET/$S3_PATH"
echo "Started: $(date)"
echo ""

# Sync files
echo "Syncing files to S3..."
aws s3 sync "$LOCAL_PATH" "s3://$S3_BUCKET/$S3_PATH" --delete

if [ $? -eq 0 ]; then
    echo ""
    echo "✓ Sync completed successfully!"
    echo "Completed: $(date)"
else
    echo ""
    echo "✗ Sync failed!"
    exit 1
fi

Usage

chmod +x s3_backup.sh
./s3_backup.sh /backup/files my-backup-bucket backups/

Troubleshooting

Troubleshooting

Problem: "aws: command not found"

Solution: Install AWS CLI:

pip install awscli
# or
sudo apt-get install awscli

Tags

aws s3 cloud backup sync