The Wake-Up Call
A friend of mine lost three months of work last year. No backups. His laptop died and that was that — photos, documents, a side project he'd been working on, all gone.
After that conversation I wrote a backup script that night. It took 20 minutes and has been running quietly every Sunday morning ever since. I haven't thought about it in months. That's the point.
Here's the script. Copy it, tweak two lines, and you're protected.
What This Script Does
- Copies everything from a folder you choose
- Puts a timestamp on the copy (so you can have multiple versions)
- Saves it to a backup location (external drive, another folder, wherever)
- Keeps only the last 5 backups so it doesn't eat your disk space
The Full Script
Create a file called backup.py:
import os
import shutil
import zipfile
from datetime import datetime
from pathlib import Path
# CHANGE THESE TWO LINES
SOURCE = "/home/yourname/Documents" # what to back up
DEST = "/mnt/backup-drive/backups" # where to save
KEEP = 5 # how many old backups to keep
def backup():
# Create a timestamped folder name
today = datetime.now().strftime("%Y-%m-%d_%H%M")
backup_name = f"backup-{today}"
backup_path = Path(DEST) / backup_name
print(f"Backing up: {SOURCE}")
print(f"To: {backup_path}")
# Copy everything
try:
shutil.copytree(SOURCE, backup_path)
print(f"Done! {backup_name}")
except FileExistsError:
print("Today's backup already exists, skipping.")
return
# Remove old backups, keep only the latest KEEP
all_backups = sorted(Path(DEST).glob("backup-*"))
if len(all_backups) > KEEP:
for old in all_backups[:-KEEP]:
shutil.rmtree(old)
print(f"Removed old backup: {old.name}")
if __name__ == "__main__":
backup()
How to Use It
Save the script somewhere — I keep mine in ~/scripts/backup.py.
Test it manually first:
$ python3 backup.py
Backing up: /home/yourname/Documents
To: /mnt/backup-drive/backups/backup-2026-08-05_0900
Done! backup-2026-08-05_0900
Check your backup location — you should see a folder with today's date.
Schedule It to Run Automatically
On Linux / Mac (cron)
Open your crontab:
crontab -e
Add this line to run every Sunday at 8 AM:
0 8 * * 0 /usr/bin/python3 /home/yourname/scripts/backup.py
On Windows (Task Scheduler)
- Open Task Scheduler (search for it in the Start menu)
- Create Basic Task → name it "Weekly Backup"
- Trigger: Weekly, Sunday, 8 AM
- Action: Start a program →
python.exe, argument:C:\Users\yourname\scripts\backup.py
Variations
ZIP Instead of Copy
If you'd rather have a single zip file:
def backup_zip():
today = datetime.now().strftime("%Y-%m-%d")
zip_path = Path(DEST) / f"backup-{today}.zip"
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
for root, dirs, files in os.walk(SOURCE):
for file in files:
full = Path(root) / file
rel = full.relative_to(SOURCE)
zf.write(full, rel)
print(f"Zipped: {zip_path}")
Email Yourself a Report
Add this at the end of the script to get a quick confirmation:
import smtplib
from email.mime.text import MIMEText
def send_report(backup_name):
msg = MIMEText(f"Backup completed: {backup_name}")
msg['Subject'] = 'Backup Complete'
msg['From'] = 'you@gmail.com'
msg['To'] = 'you@gmail.com'
# This only works if you set up an app password in Gmail
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login('you@gmail.com', 'your-app-password')
server.send_message(msg)
Back Up to the Cloud
If you have rclone set up (for Google Drive, Dropbox, etc.), add this after the copy:
import subprocess
subprocess.run(["rclone", "copy", str(backup_path), "gdrive:backups"])
Two Rules of Backups
Rule 1: A backup that only exists on the same computer doesn't count. Copy to an external drive or cloud storage.
Rule 2: Test your backup at least once. Try restoring a file and make sure it actually works.
Next Steps
Once this is running, you've covered the most important automation of all — not losing your data. Next, try automating something else you do every week: renaming downloaded files, organizing photos, or generating reports.
It doesn't have to be fancy. If it saves you five minutes every week, it's worth the 20 minutes you spent writing the script.