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

Search for a command to run...

thanks very nice explanation and useful
Learn AWS from basics to advanced with daily hands-on tasks and real-world projects. Master core services like EC2, S3, IAM, Lambda, and more all explained in a practical, job-ready DevOps format.
π° Introduction In this guide, Iβll walk you through how I deployed a basic NGINX web server setup using AWS services like EC2, ALB, Subnets, and VPC. Each server displays a simple custom message and the traffic is distributed using an Application Lo...
Master GitLab CI/CD Variables, Secrets Management, Artifacts & Best Practices π Introduction In the previous article, we learned how GitLab Runners execute CI/CD pipelines and how to configure Self-H

Learn GitLab Runners, Hosted vs Self-Hosted Runners, Runner Registration, Tags, Pipeline Editor & Parallel Jobs π Introduction In the previous article, we created our first GitLab CI/CD pipeline and

Learn Continuous Integration, Continuous Delivery, Pipelines, Stages, Jobs & .gitlab-ci.yml π Introduction In the previous articles, we explored GitLab fundamentals, repository management, and collab

Learn Docker Scout, Multi-Stage Builds, Docker Hardened Images (DHI), SBOM, Docker Model Runner, Ask Gordon AI, and production-ready container security through practical, real-world examples. Introduc

Import Repositories, Mirroring & Repository Management Best Practices π Introduction In the previous articles, we learned the fundamentals of GitLab, created projects, and configured secure authentic

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
π 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
You need:
AWS Account
Ubuntu server / EC2 instance
AWS CLI installed
IAM User with S3 access
Basic Linux command knowledge
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
Go to AWS Console β IAM
Create a user backup-user with Programmatic access
Attach policy AmazonS3FullAccess (testing)
Save Access Key & Secret Key


On Ubuntu, run:
aws configure
Fill in:
Access Key
Secret Key
Region ()



ls
cd aws
mkdir mybackup
Add your files (HTML, CSS, JS, etc.) here.

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


# 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

crontab -e
Add:
*/2 * * * * /home/ubuntu/aws/backup-to-s3.sh backup
This cron job will automatically run the backup script every 2 minutes.


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

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.

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.

This series isn't just about using AWS; it's about mastering the core services that power modern cloud infrastructure.
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar