Skip to main content

Command Palette

Search for a command to run...

How to Automatically Schedule Your Python Folder Backup Script

Published
3 min read
How to Automatically Schedule Your Python Folder Backup Script

Introduction

Creating a folder backup script in Python is a great way to protect your data. But to truly automate it, you need the script to run on a schedule — without needing to launch it manually every time.

This article will guide you through setting up automatic scheduling on both Windows (Task Scheduler) and Linux/macOS (cron jobs) so your backup script runs like clockwork.

Why Schedule Your Backup Script?

Manual backups are easy to forget. Automating them gives you:

  • Peace of mind

  • Regular, timestamped backups

  • Protection from accidental data loss or corruption

  • A foundation for more advanced automation

Prerequisites

Before scheduling, make sure you:

  1. Have a working backup script in Python.

  2. Know the full file path to your Python script (e.g., C:\Scripts\backup.py or /home/user/backup.py).

  3. Know where your Python interpreter is installed (especially on Linux/macOS).

For Windows Users: Use Task Scheduler

Windows includes a built-in tool called Task Scheduler that lets you run programs on a schedule — perfect for Python scripts.

Step 1: Create a .bat File (Batch File)

This .bat file will run your Python script with one double-click — or scheduled run.

  1. Open Notepad

  2. Add the following lines:

@echo off
python "C:\Path\To\Your\backup_script.py"

Replace the path with your actual Python script path.

  1. Save the file as run_backup.bat (choose All Files in the Save As dialog).

Tip: If you're using a virtual environment, activate it first:

@echo off
call "C:\Path\To\venv\Scripts\activate.bat"
python "C:\Path\To\Your\backup_script.py"

Step 2: Open Task Scheduler

  1. Press Windows + S and type Task Scheduler

  2. Click Create Basic Task

  3. Name it: e.g., Daily Folder Backup

Step 3: Choose a Trigger (When to Run)

Choose:

  • Daily or Weekly

  • Set the start time (e.g., 2:00 AM)

Step 4: Set the Action

  1. Choose "Start a program"

  2. Browse for your .bat file

  3. Click Next and Finish

Step 5: Test It!

Right-click your new task → Run to test it. Check your backup folder to confirm it worked.

For Linux/macOS Users: Use Cron Jobs

Linux and macOS come with cron, a time-based job scheduler that’s perfect for this.

Step 1: Open Your Crontab

crontab -e

This opens your cron jobs for editing.

Step 2: Add a New Cron Entry

To run the backup script every day at 2:00 AM:

0 2 * * * /usr/bin/python3 /home/yourname/scripts/backup_script.py

Breakdown:

  • 0 2 * * * → Minute 0, Hour 2 (2:00 AM)

  • /usr/bin/python3 → Path to your Python interpreter

  • /home/yourname/scripts/backup_script.py → Your script's location

Find Python path with: which python3

Step 3: Optional – Add Logging

Want to keep a log of when backups run? Use:

0 2 * * * /usr/bin/python3 /home/yourname/scripts/backup_script.py >> /home/yourname/backup_log.txt 2>&1

This appends output and errors to backup_log.txt.

Use CaseFrequency
Daily project backupsDaily (2 AM)
Work foldersWeekly
Monthly snapshotsMonthly

Bonus: Clean Up Old Backups Automatically

To keep only the latest 5 backups, you can add this to your script:

def cleanup_old_backups(folder, max_backups=5):
    backups = sorted([f for f in os.listdir(folder) if f.startswith("backup_")])
    while len(backups) > max_backups:
        old = os.path.join(folder, backups.pop(0))
        shutil.rmtree(old)
        print(f"Deleted old backup: {old}")

Call this function after each backup to avoid disk clutter.

Final Thoughts

Scheduling your Python folder backup script is a small step that adds huge value:

  • Reduces manual work

  • Protects against data loss

  • Integrates easily with task scheduling tools on all platforms

Happy Scripting ! !

More from this blog

PyScript Academy

29 posts

PyScript Academy is a blog sharing practical Python scripts, tips, and mini projects—helping you learn Python by doing, one useful script at a time.