Python Script to take backup of folder on amazon s3 – Windows
Introduction
Data backup is a critical part of any IT infrastructure. Whether you are managing personal files, application data, or business documents, maintaining regular backups helps protect against accidental deletion, hardware failures, ransomware attacks, and other unexpected incidents.
Amazon S3 (Simple Storage Service) is a highly durable, secure, and scalable cloud storage service provided by Amazon Web Services (AWS). By storing backups in Amazon S3, organizations can ensure their data remains accessible and protected from local system failures.
In this tutorial, we will create a Python script that automatically uploads files from a local Windows folder to an Amazon S3 bucket. The script organizes backups into date-based folders, making it easy to manage historical backups and restore files when required. We will also discuss how to schedule the script to run automatically using Windows Task Scheduler.
Prerequisites
Before proceeding, ensure the following requirements are met:
1. Windows Machine
The script is designed to run on a Windows system.
2. Python Installation
Install Python 3 on your Windows machine and verify the installation:
python --version
3. AWS Account
You need an active AWS account with access to Amazon S3.
4. S3 Bucket
Create an S3 bucket where backups will be stored.
Example bucket:
my-backup-bucket
5. IAM User Credentials
Create an IAM user with the necessary S3 permissions and obtain:
- AWS Access Key ID
- AWS Secret Access Key
A sample IAM policy is shown below:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::my-backup-bucket",
"arn:aws:s3:::my-backup-bucket/*"
]
}
]
}
Install Required Python Package
Install the AWS SDK for Python (boto3):
pip install boto3
Verify installation:
pip show boto3
Create the Backup Script
Create a file named:
s3backup.py
Add the following code:
import boto3
import os
from datetime import datetime
# AWS Configuration
AWS_ACCESS_KEY_ID = "YOUR_ACCESS_KEY"
AWS_SECRET_ACCESS_KEY = "YOUR_SECRET_KEY"
AWS_REGION = "us-east-1"
# S3 Configuration
BUCKET_NAME = "my-backup-bucket"
# Local Folder to Backup
SOURCE_DIR = r"C:\backup"
# Create Date-Based Folder
date_folder = datetime.utcnow().strftime("%Y%m%d")
# Create S3 Client
s3 = boto3.client(
"s3",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_REGION
)
# Upload Files
for root, dirs, files in os.walk(SOURCE_DIR):
for file in files:
local_file = os.path.join(root, file)
# Preserve Folder Structure
relative_path = os.path.relpath(local_file, SOURCE_DIR)
s3_key = f"{date_folder}/{relative_path.replace(os.sep, '/')}"
print(f"Uploading {local_file}")
try:
s3.upload_file(local_file, BUCKET_NAME, s3_key)
print(f"Uploaded to s3://{BUCKET_NAME}/{s3_key}")
except Exception as e:
print(f"Failed: {e}")
print("Backup completed successfully.")
How the Script Works
Step 1: Connect to AWS
The script creates a connection to Amazon S3 using your IAM credentials.
s3 = boto3.client(...)
Step 2: Generate a Date-Based Folder
The current date is generated in the format:
20260818
This helps organize backups by date.
date_folder = datetime.utcnow().strftime("%Y%m%d")
Step 3: Scan the Local Directory
The script recursively scans all files under:
C:\backup
using:
os.walk()
Step 4: Upload Files to S3
Each file is uploaded to the S3 bucket while maintaining the original folder structure.
Example:
C:\backup\documents\report.pdf
becomes:
s3://my-backup-bucket/20260818/documents/report.pdf
Running the Script
Execute the script from PowerShell:
python s3backup.py
Example output:
Uploading C:\backup\documents\report.pdf
Uploaded to s3://my-backup-bucket/20260818/documents/report.pdf
Uploading C:\backup\images\logo.png
Uploaded to s3://my-backup-bucket/20260818/images/logo.png
Backup completed successfully.
Verify Backup in Amazon S3
Log in to the AWS Management Console and navigate to:
Amazon S3 → Your Bucket
You should see a date-based folder:
20260818/
Inside the folder, all files and subdirectories from the source folder will be available.
Automating Daily Backups
Windows Task Scheduler can be used to automate the backup process.
Step 1: Open Task Scheduler
Press:
Windows + R
and run:
taskschd.msc
Step 2: Create a New Task
Select:
Create Basic Task
Step 3: Configure Trigger
Choose:
Daily
and specify the desired execution time.
Step 4: Configure Action
Program:
C:\Python312\python.exe
Arguments:
C:\scripts\s3backup.py
Step 5: Save the Task
The backup process will now execute automatically every day.
Best Practices
Use IAM Roles and Policies
Grant only the required permissions to the backup user.
Avoid Hardcoding Credentials
Instead of storing credentials in scripts, use:
AWS CLI Credentials File
or
IAM Roles
where possible.
Enable S3 Versioning
Versioning provides additional protection against accidental deletions and overwrites.
Use Lifecycle Policies
Configure lifecycle policies to move older backups to:
- S3 Standard-IA
- S3 Glacier
- S3 Glacier Deep Archive
to reduce storage costs.
Enable Encryption
Use:
- SSE-S3
- SSE-KMS
to protect backup data.
Alternative Approach: AWS CLI
For simple backup requirements, AWS CLI can be used instead of Python.
Install AWS CLI:
aws configure
Sync a folder:
aws s3 sync C:\backup s3://my-backup-bucket/backup/
Create date-based backups:
$today = Get-Date -Format "yyyyMMdd"
aws s3 sync C:\backup s3://my-backup-bucket/$today/
AWS CLI is often the preferred choice for straightforward backup tasks because it is easy to configure and maintain.
Conclusion
Amazon S3 provides a secure, scalable, and cost-effective solution for storing backups in the cloud. By combining Python and the AWS SDK, you can automate the process of uploading files from a Windows machine to an S3 bucket while maintaining an organized backup structure based on dates.
This approach not only protects your data from local system failures but also simplifies backup management and recovery. For enterprise environments, additional features such as versioning, lifecycle policies, encryption, and automated scheduling can further enhance reliability and security. Whether you are backing up personal files or critical business data, Amazon S3 offers a dependable platform for long-term data protection and disaster recovery.
