Skip to content

Instantly share code, notes, and snippets.

@frogonwheels
Created January 6, 2011 02:40
Show Gist options
  • Save frogonwheels/767418 to your computer and use it in GitHub Desktop.
Save frogonwheels/767418 to your computer and use it in GitHub Desktop.
Backup git repos
@echo off
setlocal ENABLEDELAYEDEXPANSION
rem Search for the backup drive!
set volname=Backup Volumne
set bkd=
for %%i in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
for /f "usebackq tokens=*" %%j in (`vol %%i: 2^>nul`) do (
set msg=%%j#
if "!msg!" NEQ "!msg:%volname%#=!" (
set bkd=%%i:
goto found
)
)
)
echo !volname! not Found
endlocal
exit /b 1
:found
echo Found %volname% on %bkd%
set src=c:\projects
set dest=%bkd%\Git
set git=call git
set txtfile=%src%\backup.txt
copy %~f0 %dest% > nul
copy %txtfile% %dest% > nul
if not exist %dest% md %dest%
pushd "%src%"
rem Go through all sub-directories
for /f "usebackq tokens=*" %%I in (`type %txtfile%`) do (
call :backup "%%I"
set dir=%%~I
pushd !dir:/=\!
for /f "usebackq tokens=*" %%J in (`git submodule foreach -q echo $path`) do (
call :backup "%%J"
)
popd
)
popd
rem Extract Git documentation on howto recover
if exist %dest%\GitDoc.git (
pushd %dest%\GitDoc.git
git archive --format zip -o ../GitDoc.zip HEAD
)
endlocal
goto :eof
:backup
setlocal ENABLEDELAYEDEXPANSION
set rep=%~1
set dstrep=%~n1.git
if exist "!dest!\!dstrep!" (
rem Backup Repo exists - so just fetch
pushd "!dest!\!dstrep!"
%git% fetch origin
%git% gc --auto
popd
) else (
rem Backup Repo doesn't exist - clone it
rem Must use / here, or it's not recognised as a pathspec.
%git% clone --mirror "!rep!" "%dest%/!dstrep!"
)
endlocal
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment