# 🚀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**

```powershell
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**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754816114937/cacc0aa0-4b59-46e0-8641-0361ed37f1b3.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754816131822/e75657b6-e6dc-4387-a061-b3d7f6cf03db.png align="center")

On Ubuntu, run:

```powershell
aws configure
```

Fill in:

* Access Key
    
* Secret Key
    
* Region ()
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754818192609/73d1931e-557b-4dfd-9f4a-1d9f656df36a.png align="center")
    

## **📦 Step 4 – Create S3 Bucket**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754816305243/b9c0874d-7a2d-4cb5-b553-da2b53aa7646.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754816347267/b302e6ff-3a61-4adc-8bcc-8e0370008841.png align="center")

## **📂 Step 5 – Prepare Local Folder**

```powershell
ls
cd aws
mkdir mybackup
```

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754816371784/a6caa8fc-4116-43f0-b857-9e95c6a566cc.png align="center")

## **🖥 Step 6 – Create Backup & Restore Script**

```powershell
vim backup-to-s3.sh
```

```powershell
#!/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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754816553593/1c842408-0216-4fd8-9cc9-fb73632b7c0f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754816603761/513984db-3092-4132-8d97-654313fc26c6.png align="center")

### **Step 7 – Manual Backup & Restore**

```powershell
# 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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754816711057/a6c2911a-5e79-4162-9a94-2e224daacc43.png align="center")

## **⏱ Step 8 – Automate with Cron**

```powershell
crontab -e
```

Add:

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754817159956/40054f71-54ec-43fd-8762-b3adb5473e80.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754817298204/a3870d4c-f4f1-498d-a2ba-a6d5204261f9.png align="center")

## **🧪 Step 9 – Case Studies**

### **📌 Case 1 – Existing Data in Bucket**

When the bucket was created, it already contained:

```powershell
index.html
style.css
script.js
```

Backup script compared timestamps:

* Same files → Skipped
    
* Updated files → Uploaded again
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754817434728/c6e80dce-dc06-4124-940b-e139cfc56864.png align="center")

---

### **📌 Case 2 – Adding a New File**

We added:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754817530900/5f10d49e-63d8-4848-9735-d95f62185f59.png align="center")

✅Check Total files and folders :-

```powershell
ls
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754818405058/7ffd5d9b-08e3-46e2-b540-e43429d39be3.png align="center")

Backup run:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754817574261/4fd33b2b-759c-482c-8df5-b925be989987.png align="center")

Verification:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754817614642/fdfa5099-1be2-40a9-8186-ab8803888e89.png align="center")

Now `test.txt` appears in the bucket.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754817642424/7393ea9b-3a4f-4770-8d45-26644808cf7c.png align="center")

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong>Notice :-</strong>The <strong>DIVP PROJECT/</strong> folder contained code files (HTML, CSS, Java).</div>
</div>

## **🏁 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**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751797710818/123a7231-3dca-4273-ad68-7bd026f69b95.png?auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp align="left")

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**](mailto:gujjarapurv181@gmail.com)
    
* 🐙 **GitHub**: [**github.com/ApurvGujjar07**](http://github.com/ApurvGujjar07)
    
* 💼 **LinkedIn**: [**linkedin.com/in/apurv-gujja**](http://linkedin.com/in/apurv-gujjar)**r**
