Automate Your Security: Build a Python Backup Script

3D visualization of a robotic arm cloning data into a secure safe, representing an automated Python backup script.

We all know we should back up our files, but we often forget. Let’s write a Python Backup Script to do it for us. We will use the shutil module, which can easily create ZIP archives.

The Goal

Take a folder (e.g., “MyImportantProject”), zip it into MyImportantProject_backup.zip, and move it to an external drive or cloud sync folder.

The Script

import shutil
import os
import datetime

# 1. Configuration
SOURCE_FOLDER = "C:/Users/Name/Documents/MyProject"
BACKUP_DESTINATION = "D:/Backups/" # External drive

# 2. Generate a unique name with today's date
# (e.g., "MyProject_2025-11-10")
today = datetime.date.today()
backup_name = f"MyProject_{today}"
full_backup_path = os.path.join(BACKUP_DESTINATION, backup_name)

# 3. Create the ZIP file
print(f"Zipping {SOURCE_FOLDER}...")
# 'make_archive' automatically adds .zip to the end of the name
shutil.make_archive(full_backup_path, 'zip', SOURCE_FOLDER)

print(f"Backup successful! Saved to: {full_backup_path}.zip")

Automation

Combine this with Windows Task Scheduler (or cron on Linux) to run every Friday at 5 PM, and you’ll never lose more than a week’s worth of work again!


Similar Posts

Leave a Reply