Robocopy (Robust File Copy), a powerful command-line utility built into Windows, is often used to process data backups. However, manually running Robocopy, checking the log files for errors, and ensuring that the backup process has completed can be time-consuming. This is where Python comes in. By automating the execution and verification of Robocopy with a Python script, you can ensure that your backups are running smoothly and on schedule, all without manual intervention.
This guide will walk you through setting up such automation, turning a critical task into a reliable, hands-free process.
A real-life example of a program that would benefit from the Python script described is a backup utility that creates a log file after completing a backup. This could be something like running the robocopy
command on Windows, which is often used for robust file copying and backup purposes.
Scenario: Automating Backup Verification with Robocopy
Robocopy (Robust File Copy) is a command-line utility built into Windows. It’s often used to copy large volumes of data, synchronize directories, or create backups. After running, Robocopy typically generates a log file that details the operation’s success, any errors, and what files were copied.
The task here is to automate the running of Robocopy at regular intervals, check that the log file is created after each run, and ensure that the backup process completed before starting the next iteration.
Python Script: monitor_backup.py
import os
import time
import subprocess
from datetime import datetime
def run_backup():
# Command to run Robocopy. Replace the source and destination with your directories.
robocopy_command = [
'robocopy',
'C:\\SourceDirectory', # Replace with your source directory
'D:\\BackupDirectory', # Replace with your backup directory
'/MIR', # Mirror the directories
'/LOG:backup_log.txt', # Log file
'/R:3', # Retry 3 times on failure
'/W:10' # Wait 10 seconds before retry
]
process = subprocess.Popen(robocopy_command)
return process
def check_log_file(filename):
# Wait until the log file is created
while not os.path.exists(filename):
print("Waiting for the backup log file to be generated...")
time.sleep(5) # Check every 5 seconds
print(f"{filename} has been generated.")
return True
def monitor():
while True:
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Starting backup at {current_time}")
# Run the backup using Robocopy
process = run_backup()
# Check if the log file is generated
log_filename = 'backup_log.txt' # Robocopy log file
if check_log_file(log_filename):
print(f"Backup log {log_filename} detected.")
# Wait for the Robocopy process to finish
process.wait()
print(f"Backup completed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
# Wait for 1 minute before running the backup again
time.sleep(60)
if __name__ == "__main__":
monitor()
How the Script Works with Robocopy
- Running Robocopy: The
run_backup()
function executes the Robocopy command to mirror the contents of a source directory to a backup directory. The command logs its activity in abackup_log.txt
file. - Log File Verification: The
check_log_file()
function checks every 5 seconds to see if the log file has been created, confirming that the backup has been logged properly. - Ensuring Completion: The script waits for Robocopy to finish running (
process.wait()
), ensuring that the backup completes before starting the next run. - Repetition: The script then waits 1 minute (
time.sleep(60)
) before starting the backup process again.
Running the Script
To run this script:
- Create and Save the Script: Save the above Python code as
monitor_backup.py
. - Execute the Script: Open a terminal or command prompt, navigate to the script’s directory, and run:
python monitor_backup.py
- This will start the process of running Robocopy every minute, checking for the completion log, and ensuring that each backup finishes before the next begins.
Converting to an Executable (.exe)
If you want to run this script on systems that do not have Python installed:
- Install PyInstaller:
pip install pyinstaller
2. Convert to Executable:
pyinstaller --onefile monitor_backup.py
This will generate a monitor_backup.exe
file in the dist
directory, which can be run on any Windows machine.
Why Use This Approach?
This setup is useful for businesses or individuals who need to ensure their files are regularly backed up with minimal oversight. Running Robocopy through this Python script automates the entire backup process, ensuring that it runs on schedule and that logs are checked to verify the operation. This method reduces the risk of data loss by ensuring that backups happen consistently and are correctly logged.
Thank you for following along with this tutorial. We hope you found it helpful and informative. If you have any questions, or if you would like to suggest new Python code examples or topics for future tutorials/articles, please feel free to join and comment. Your feedback and suggestions are always welcome!
You can find the same tutorial on Medium.com.