40 – Real-World Python Projects – Automatic Backup Script

A Python tool that automatically backs up your important files to another folder, external drive, or cloud folder (Google Drive/OneDrive).


๐ŸŽฏ What This Backup Script Can Do

โœ” Automatically copies all files from source โ†’ backup folder
โœ” Creates a new backup folder with date (optional)
โœ” Logs which files were copied
โœ” Can run daily/weekly using scheduler
โœ” Supports multiple source folders
โœ” Avoids copying duplicates
โœ” Shows backup summary
โœ” Works on Windows, Mac, Linux


๐Ÿงฐ Python Modules Used

  • shutil โ€“ copying files
  • os โ€“ file paths
  • datetime โ€“ timestamp folders
  • hashlib โ€“ prevents duplicate backup
  • schedule โ€“ auto backups

All are built-in (no installation needed), except schedule (optional):

pip install schedule

๐Ÿ“ Folder Structure

AutoBackup/
โ”‚โ”€โ”€ backup.py
โ”‚โ”€โ”€ config.json
โ”‚โ”€โ”€ logs.txt
โ”‚โ”€โ”€ backups/

๐Ÿ“Œ config.json Example

{
    "source_paths": [
        "C:/Users/Sameer/Documents",
        "C:/Users/Sameer/Desktop/Projects"
    ],
    "backup_path": "D:/Backups",
    "create_daily_folder": true
}

๐Ÿงฉ FULL WORKING BACKUP SCRIPT (backup.py)

import os
import shutil
import json
from datetime import datetime
import hashlib

# Load config
config = json.load(open("config.json"))
SOURCE_PATHS = config["source_paths"]
BACKUP_PATH = config["backup_path"]
DAILY_FOLDER = config["create_daily_folder"]

# Create daily backup folder
if DAILY_FOLDER:
    today = datetime.now().strftime("%Y-%m-%d")
    BACKUP_PATH = os.path.join(BACKUP_PATH, today)

os.makedirs(BACKUP_PATH, exist_ok=True)

def file_hash(path):
    """Return MD5 hash of a file to prevent duplicate backup."""
    hasher = hashlib.md5()
    with open(path, 'rb') as f:
        buf = f.read()
        hasher.update(buf)
    return hasher.hexdigest()

def backup_files():
    logs = []
    existing_hashes = set()

    # Load existing backup hashes
    for root, dirs, files in os.walk(BACKUP_PATH):
        for file in files:
            full_path = os.path.join(root, file)
            existing_hashes.add(file_hash(full_path))

    for src in SOURCE_PATHS:
        for root, dirs, files in os.walk(src):
            for file in files:
                src_file = os.path.join(root, file)

                # Skip duplicates
                if file_hash(src_file) in existing_hashes:
                    continue

                rel_path = os.path.relpath(root, src)
                dest_dir = os.path.join(BACKUP_PATH, rel_path)
                os.makedirs(dest_dir, exist_ok=True)

                dst_file = os.path.join(dest_dir, file)
                shutil.copy2(src_file, dst_file)
                logs.append(f"Copied: {src_file} โ†’ {dst_file}")

    # Save logs
    with open("logs.txt", "a") as f:
        f.write("\n".join(logs) + "\n")

    print("Backup completed.")
    print(f"Total files copied: {len(logs)}")


if __name__ == "__main__":
    backup_files()

โ–ถ Run Manually

python backup.py

โฒ๏ธ Auto Backup Every Day

Create: schedule_backup.py

import schedule
import time
import os

def run_backup():
    os.system("python backup.py")

schedule.every().day.at("09:00").do(run_backup)

while True:
    schedule.run_pending()
    time.sleep(1)

Runs backup every day at 9 AM.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *