How to Automate a Git Repository Backup in Batch Script
While Git handles version control beautifully, what happens if your local machine dies, or if a catastrophic commit destroys the repository history before it can be pushed? A common safety measure is automating a full local backup of the repository directory (including the .git folder) to a secondary drive, network share, or cloud-synced folder.
In this guide, we will demonstrate how to automate a robust Git repository backup using a Batch script, generating timestamped .zip archives.
The Strategy: Timestamped Zipping
- Identify the source Git repository directory.
- Identify the backup destination directory.
- Generate a unique timestamp to name the backup file.
- Use PowerShell's
Compress-Archive(or a third-party tool like 7-Zip) to zip the entire folder. - Optionally, implement a retention policy to delete backups older than X days.
Implementation Script (Native Windows Tools)
This script uses PowerShell's built-in compression cmdlet, so no external tools like 7-Zip are required.
@echo off
setlocal enabledelayedexpansion
:: 1. Configuration
set "sourceRepo=C:\Development\MyProject"
set "backupDestination=D:\GitBackups"
:: 2. Verify source repository exists
if not exist "%sourceRepo%\.git" (
echo [ERROR] "%sourceRepo%" is not a Git repository or does not exist.
pause
exit /b 1
)
:: 3. Ensure destination exists
if not exist "%backupDestination%" (
echo Creating backup directory...
mkdir "%backupDestination%"
)
:: 4. Generate Timestamp (YYYYMMDD_HHMM)
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set "dt=%%I"
set "timestamp=!dt:~0,8!_!dt:~8,4!"
:: 5. Define the backup archive name
set "backupFile=%backupDestination%\MyProject_Backup_!timestamp!.zip"
echo Starting backup of Git repository...
echo Source: "%sourceRepo%"
echo Target: "!backupFile!"
echo.
:: 6. Compress using PowerShell
:: We compress the entire directory, including the hidden .git folder
powershell.exe -NoProfile -Command ^
"Compress-Archive -Path '%sourceRepo%' -DestinationPath '!backupFile!' -Force"
:: Capture the exit code immediately
set "psResult=!errorlevel!"
if !psResult! neq 0 (
echo [ERROR] Backup failed during compression with exit code !psResult!.
pause
exit /b 1
)
:: 7. Verify the archive was created
if not exist "!backupFile!" (
echo [ERROR] Archive file was not created.
pause
exit /b 1
)
echo.
echo ==========================================
echo BACKUP SUCCESSFUL
echo Archive Saved: "!backupFile!"
echo ==========================================
endlocal
pause
exit /b 0
Adding a Retention Policy (Cleanup)
To prevent your backup drive from filling up over time, you can add a cleanup step at the end of the script to delete archives older than an arbitrary number of days (e.g., 30 days).
@echo off
setlocal enabledelayedexpansion
set "backupDestination=D:\GitBackups"
set "retentionDays=30"
:: Verify backup directory exists
if not exist "%backupDestination%" (
echo [ERROR] Backup directory "%backupDestination%" does not exist.
pause
exit /b 1
)
echo Cleaning up backups older than %retentionDays% days...
:: Use ForFiles to find and delete old .zip files
:: forfiles returns 1 if no matching files are found
forfiles /p "%backupDestination%" /m *.zip /d -%retentionDays% /c "cmd /c del @path" 2>nul
:: Capture the exit code immediately
set "cleanResult=!errorlevel!"
if !cleanResult! equ 0 (
echo [SUCCESS] Old backups removed successfully.
) else (
echo [INFO] No backups older than %retentionDays% days found.
)
endlocal
pause
exit /b 0
Why Automate Git Repository Backups?
- Air-Gapped Security: Creating isolated, read-only copies of the repository in case ransomware encrypts the active development drive.
- Pre-Migration Snapshots: Before attempting a risky
git rebaseor repository migration, a double-click.batscript provides an instant point-in-time recovery option. - Local Redundancy: For developers who go days without pushing to a remote server (like GitHub or GitLab), local backups prevent days of lost work in the event of a hard drive failure.
Important Considerations
- Repository Size: If your
.gitfolder contains gigabytes of binary assets, PowerShell'sCompress-Archivemight be exceptionally slow. In that scenario, downloading and using7z.exe(7-Zip) from the command line is vastly faster and supports stronger compression. .gitignoreBypass: Compressing the raw directory ignores.gitignorerules, meaningnode_modules,obj/, andbin/folders are backed up as well. To strictly backup tracked Git files and branches, agit bundle createcommand is a valid alternative to zipping the directory.- Open Handles: Ensure your IDE (like Visual Studio or VS Code) is not actively locking files in the repository during the backup, which could cause the compression process to throw "File in use" errors.
Conclusion
Automating a Git repository backup ensures that your version history escapes localized corruption, branching disasters, and hardware failures. By combining simple string manipulation for timestamps with native PowerShell compression, your Batch script becomes a reliable disaster-recovery mechanism, securing your codebase effortlessly behind the scenes.