Stop dragging and dropping folders to back up your files. A simple Python script can copy your important files to a dated backup folder automatically—and it takes only 10 lines of code. This tutorial uses the built-in shutil and os libraries, so no third-party packages are required.
What You Need
- Python installed on your computer (any version 3.6+)
- A folder with files you want to back up (e.g.,
C:\Users\YourName\Documents\Project) - A destination folder for backups (e.g.,
C:\Backups)
Step 1: Set Up the Script
Create a new file called backup.py and open it in any text editor. Add the following imports at the top:
python
import shutil import os import datetime
shutilhandles copying folders and files.oscreates directories and checks paths.datetimegenerates the timestamp for each backup.
Step 2: Define Source and Destination
Add two variables for the folder you want to back up and where backups should go:
python
source = r"C:\Users\YourName\Documents\Project" destination = r"C:\Backups"
The r before each string tells Python to treat backslashes literally, which is important on Windows. Replace the paths with your actual folders.
Step 3: Create a Timestamped Backup Folder
Use datetime to create a unique folder name for each backup:
python
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
backup_folder = os.path.join(destination, f"backup_{timestamp}")
This produces names like backup_2025-03-21_14-30-00. The timestamp prevents new backups from overwriting old ones.
Step 4: Copy the Files
Use shutil.copytree() to copy the entire source folder into the new backup folder:
python
shutil.copytree(source, backup_folder)
print(f"Backup created: {backup_folder}")
copytree copies all files and subfolders recursively. The print statement confirms the backup path.
Step 5: Add a Safety Check (Optional but Recommended)
If the destination doesn't exist, copytree will fail. Add a check before copying:
python
if not os.path.exists(destination):
os.makedirs(destination)
Place this line before the copytree call. It creates the destination folder if needed.
Full Script (10 Lines)
Here is the complete script with the safety check included:
python
import shutil
import os
import datetime
source = r"C:\Users\YourName\Documents\Project"
destination = r"C:\Backups"
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
backup_folder = os.path.join(destination, f"backup_{timestamp}")
if not os.path.exists(destination):
os.makedirs(destination)
shutil.copytree(source, backup_folder)
print(f"Backup created: {backup_folder}")
That is 10 lines of actual code (excluding blank lines).
Step 6: Run the Script
- Open Command Prompt or Terminal.
- Navigate to the folder containing
backup.py. - Run:
cmd
python backup.py
You should see a message with the path to your new backup folder.
Step 7: Automate It (Optional)
To run the backup automatically, use Task Scheduler on Windows or cron on macOS/Linux. Point the task to python backup.py and set your preferred schedule—daily, weekly, or hourly.
Why This Works
shutil.copytree() handles the entire folder copy in one call, while datetime and os.path.join() generate a unique destination. The script is short, readable, and safe: it creates the destination if missing and never overwrites previous backups because each one has its own timestamped folder.
Quick Recap
- Use
shutil.copytree()to copy folders. - Use
datetimefor timestamped backup names. - Use
os.makedirs()to create the destination if needed. - Run with
python backup.pyor schedule it.
This approach works for documents, photos, code projects, or any folder you want to protect. Adjust the paths and schedule, and your backups run themselves.