πŸš€Automated Backup & Restore to AWS S3 using AWS CLI & Cron Jobs

Gujjar ApurvGujjar Apurv
4 min read

πŸ“Œ Introduction

Data loss can be costly whether due to accidental deletion, hardware failure, or system crashes . In this blog, we will set up an automated backup and restore system using AWS S3, AWS CLI, and cron jobs, starting completely from scratch.

You will learn:

  • How to configure AWS CLI for S3

  • How to create a shell script for backup & restore

  • How to automate backups using cron

  • How to verify backups in real-world scenarios

πŸ—‚ Agenda

πŸ›  Prerequisites
πŸ“₯ Install AWS CLI
πŸ‘€ Create IAM User & Configure CLI
πŸ“¦ Create S3 Bucket
πŸ“‚ Prepare Local Folder
πŸ–₯ Create Backup & Restore Script
πŸ”„ Backup & Restore (Manual)
⏱ Automate with Cron
πŸ§ͺ Case Studies (Existing Data + New File)
🏁 Conclusion

πŸ›  Step 1 – Prerequisites

You need:

  • AWS Account

  • Ubuntu server / EC2 instance

  • AWS CLI installed

  • IAM User with S3 access

  • Basic Linux command knowledge

πŸ“₯ Step 2 – Install AWS CLI

sudo apt update
sudo apt install -y unzip tar gzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

πŸ‘€ Step 3 – Create IAM User & Configure CLI

  1. Go to AWS Console β†’ IAM

  2. Create a user backup-user with Programmatic access

  3. Attach policy AmazonS3FullAccess (testing)

  4. Save Access Key & Secret Key

On Ubuntu, run:

aws configure

Fill in:

  • Access Key

  • Secret Key

  • Region ()

πŸ“¦ Step 4 – Create S3 Bucket

πŸ“‚ Step 5 – Prepare Local Folder

ls
cd aws
mkdir mybackup

Add your files (HTML, CSS, JS, etc.) here.

πŸ–₯ Step 6 – Create Backup & Restore Script

vim backup-to-s3.sh
#!/bin/bash

FOLDER_PATH="/home/ubuntu/mybackup"
BUCKET_NAME="s3-bucket-apurv-data"
LOG_FILE="/home/ubuntu/backup-log.txt"
DATE=$(date +"%Y-%m-%d %H:%M:%S")

if [ "$1" == "backup" ]; then
    echo "[$DATE] Backup started..." | tee -a "$LOG_FILE"
    aws s3 sync "$FOLDER_PATH" "s3://$BUCKET_NAME/" --exact-timestamps --sse AES256 >> "$LOG_FILE" 2>&1
    echo "[$DATE] Backup completed." | tee -a "$LOG_FILE"

elif [ "$1" == "restore" ]; then
    echo "[$DATE] Restore started..." | tee -a "$LOG_FILE"
    aws s3 sync "s3://$BUCKET_NAME/" "$FOLDER_PATH" --exact-timestamps >> "$LOG_FILE" 2>&1
    echo "[$DATE] Restore completed." | tee -a "$LOG_FILE"

else
    echo "Usage: $0 backup|restore"
fi

Step 7 – Manual Backup & Restore

# Make the script executable
chmod +x backup-to-s3.sh    # make it executable

# Take a backup
./backup-to-s3.sh backup    # to take backup

# Restore from backup
./backup-to-s3.sh restore   # to restore

⏱ Step 8 – Automate with Cron

crontab -e

Add:

*/2 * * * * /home/ubuntu/aws/backup-to-s3.sh backup

This cron job will automatically run the backup script every 2 minutes.

πŸ§ͺ Step 9 – Case Studies

πŸ“Œ Case 1 – Existing Data in Bucket

When the bucket was created, it already contained:

index.html
style.css
script.js

Backup script compared timestamps:

  • Same files β†’ Skipped

  • Updated files β†’ Uploaded again


πŸ“Œ Case 2 – Adding a New File

We added:

echo "This is a test file" > /home/ubuntu/mybackup/test.txt

βœ…Check Total files and folders :-

ls

Backup run:

/home/ubuntu/aws/backup-to-s3.sh backup

Verification:

aws s3 ls s3://s3-bucket-apurv-data/ --recursive --human-readable --summarize

Now test.txt appears in the bucket.

πŸ’‘
Notice :-The DIVP PROJECT/ folder contained code files (HTML, CSS, Java).

🏁 Conclusion

We successfully built an automated backup & restore system with:

  • AWS S3

  • AWS CLI

  • Cron automation

  • Logging & encryption

With this setup, your data is secure, versioned, and restorable anytime.

πŸ‘¨β€πŸ’» About the Author

This series isn't just about using AWS; it's about mastering the core services that power modern cloud infrastructure.


πŸ“¬ Let's Stay Connected

1
Subscribe to my newsletter

Read articles from Gujjar Apurv directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gujjar Apurv
Gujjar Apurv

Gujjar Apurv is a passionate DevOps Engineer in the making, dedicated to automating infrastructure, streamlining software delivery, and building scalable cloud-native systems. With hands-on experience in tools like AWS, Docker, Kubernetes, Jenkins, Git, and Linux, he thrives at the intersection of development and operations. Driven by curiosity and continuous learning, Apurv shares insights, tutorials, and real-world solutions from his journeyβ€”making complex tech simple and accessible. Whether it's writing YAML, scripting in Python, or deploying on the cloud, he believes in doing it the right way. "Infrastructure is code, but reliability is art."