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

π 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
Go to AWS Console β IAM
Create a user
backup-userwith Programmatic accessAttach policy
AmazonS3FullAccess(testing)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.

π 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
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar






