Automating Backups for Diablo 1 Save Files with PowerShell
December 2, 2024โข650 words
Diablo 1, a timeless classic, continues to captivate fans of dungeon-crawling RPGs. For many, save files are not just game data but a record of countless hours of adventure. However, accidental overwrites or corrupted save files can quickly turn triumph into despair.
To ensure your progress remains safe, this PowerShell script automates backups of your Diablo 1 save files. It monitors changes, creates timestamped backups of your save folder, and maintains a tidy backup directory by deleting older backups beyond a set limit.
This script is designed for use with the default installation of DevilutionX, a popular open-source engine recreation for Diablo. If your save files are stored in a custom location, simply adjust the file paths in the script accordingly.
The Script
# Configuration Section
# Dynamically determine the AppData folder
$sourceFolder = Join-Path $env:APPDATA "diasurgical\devilution" # Save game folder
$backupFolder = "C:\data" # Folder to store backups
$fileToWatch = Join-Path $sourceFolder "multi_0.sv" # File to monitor
$debounceDelay = 5 # Time (seconds) to avoid redundant backups
$maxBackups = 10 # Maximum backups to retain
# Initialize the last backup time
$lastBackupTime = Get-Date -Year 1970 -Month 1 -Day 1
# Function to generate unique backup file names
function Get-UniqueBackupName {
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
return "Backup_$timestamp.zip"
}
# Function to create backups
function Backup-Folder {
$currentTime = Get-Date
if (($currentTime - $global:lastBackupTime).TotalSeconds -ge $debounceDelay) {
$zipName = Get-UniqueBackupName
$zipPath = Join-Path -Path $backupFolder -ChildPath $zipName
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourceFolder, $zipPath)
Write-Host "Backup created at: $zipPath"
$global:lastBackupTime = $currentTime
Cleanup-OldBackups
} else {
Write-Host "Change detected but backup skipped due to debounce."
}
}
# Function to clean up old backups
function Cleanup-OldBackups {
$backups = Get-ChildItem -Path $backupFolder -Filter "*.zip" | Sort-Object LastWriteTime -Descending
if ($backups.Count -gt $maxBackups) {
$backupsToRemove = $backups | Select-Object -Skip $maxBackups
foreach ($backup in $backupsToRemove) {
Remove-Item $backup.FullName -Force
Write-Host "Deleted old backup: $($backup.Name)"
}
}
}
# Set up a file watcher to monitor save file changes
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = Split-Path $fileToWatch -Parent
$watcher.Filter = (Split-Path $fileToWatch -Leaf)
# Trigger a backup when the file changes
$watcher.Changed += { Backup-Folder }
# Enable monitoring
$watcher.EnableRaisingEvents = $true
Write-Host "Monitoring file: $fileToWatch. Press Ctrl+C to stop."
# Keep the script running
while ($true) { Start-Sleep -Seconds 1 }
Key Features
- Default Paths for DevilutionX: The script uses the standard file paths for DevilutionX save files. If your setup is customized, update
$sourceFolder
,$backupFolder
, and$fileToWatch
accordingly. - Automated Backups: When changes are detected in the specified save file, the script creates a timestamped ZIP backup of the entire save folder.
- Debounce Mechanism: The
debounceDelay
prevents multiple redundant backups from being created if several quick changes are detected. - Backup Retention: Only the most recent backups are retained, up to the limit defined by
$maxBackups
. Older backups are deleted automatically to save space. - Continuous Monitoring: The script runs indefinitely, continuously monitoring the save file for changes. Use
Ctrl+C
to stop it.
How to Use
- Default Setup: For DevilutionX installations, simply run the script. It will monitor the save file at its default location.
- Custom Save Locations: If your save files are stored elsewhere, update these paths:
$sourceFolder
: Folder containing your save files.$backupFolder
: Destination folder for your backups.$fileToWatch
: Specific save file to monitor.
- Run the Script: Copy the script into a
.ps1
file and execute it in PowerShell. Ensure you have the necessary permissions to access the save files and the backup folder. - Enjoy Peace of Mind: The script will automatically protect your progress, leaving you free to focus on your adventures.
This simple yet powerful script ensures that your Diablo 1 adventures are always safe from unexpected data loss. By combining modern automation with classic gameplay, it bridges the gap between nostalgic gaming and modern convenience. Happy gaming!