|
| 1 | +@echo off |
| 2 | +:: Switch to UTF-8 to avoid garbled output |
| 3 | +chcp 65001 >nul |
| 4 | +setlocal EnableExtensions EnableDelayedExpansion |
| 5 | + |
| 6 | +:: ───────────────────────────────────────────────────────────── |
| 7 | +:: GitHub Full/Lite Backup (CMD wrapper with confirmation) |
| 8 | +:: - Auto-detects mode by presence of GITHUB_TOKEN |
| 9 | +:: * FULL: Code+Wiki+Metadata (issues, PRs, releases, ...) |
| 10 | +:: * LITE: Code+Wiki only |
| 11 | +:: - Asks before overwrite (if backup folder exists) |
| 12 | +:: - Usage: backup_github_full.cmd <Git-Repo-URL> [OutDir] |
| 13 | +:: ───────────────────────────────────────────────────────────── |
| 14 | + |
| 15 | +if "%~1"=="" ( |
| 16 | + echo Usage: %~nx0 Git_Repository_URL [OutDir] |
| 17 | + echo Example: %~nx0 https://github.com/user/repo.git D:\backups |
| 18 | + goto :eof |
| 19 | +) |
| 20 | + |
| 21 | +set "REPO_URL=%~1" |
| 22 | +set "OUT_DIR=%~2" |
| 23 | +if "%OUT_DIR%"=="" set "OUT_DIR=." |
| 24 | + |
| 25 | +:: Check git |
| 26 | +where git >nul 2>&1 |
| 27 | +if errorlevel 1 ( |
| 28 | + echo [ERROR] Git is not installed or not found in PATH. Install from https://git-scm.com |
| 29 | + goto :eof |
| 30 | +) |
| 31 | + |
| 32 | +:: Extract repo name from URL |
| 33 | +set "TMP=%REPO_URL%" |
| 34 | +if /i "%TMP:~-4%"==".git" set "TMP=%TMP:~0,-4%" |
| 35 | +set "P=%TMP:/=\%" |
| 36 | +for %%I in ("%P%") do set "NAME=%%~nI" |
| 37 | +if "%NAME%"=="" ( |
| 38 | + echo [ERROR] Failed to extract repository name from URL. Check the URL. |
| 39 | + goto :eof |
| 40 | +) |
| 41 | + |
| 42 | +for %%I in ("%OUT_DIR%") do set "OUT_DIR_FQ=%%~fI" |
| 43 | +set "BACKUP_DIR=%OUT_DIR_FQ%\%NAME%_backup" |
| 44 | + |
| 45 | +:: Decide mode by token |
| 46 | +set "MODE=LITE" |
| 47 | +if defined GITHUB_TOKEN set "MODE=FULL" |
| 48 | + |
| 49 | +echo -------------------------------------------------- |
| 50 | +echo Detected mode: %MODE% |
| 51 | +if /i "%MODE%"=="FULL" ( |
| 52 | + echo This will back up: Code+Wiki+Metadata (issues, PRs, reviews, labels, milestones, releases+assets). |
| 53 | +) else ( |
| 54 | + echo This will back up: Code+Wiki only (no metadata). You can set GITHUB_TOKEN to enable FULL mode. |
| 55 | +) |
| 56 | +set /p CONFIRM=Proceed with this mode? [y/N] : |
| 57 | +if /i not "%CONFIRM%"=="y" ( |
| 58 | + echo Aborted. Nothing was changed. |
| 59 | + goto :eof |
| 60 | +) |
| 61 | + |
| 62 | +echo -------------------------------------------------- |
| 63 | +echo [1/6] Preparing backup folder: %BACKUP_DIR% |
| 64 | +echo -------------------------------------------------- |
| 65 | +if exist "%BACKUP_DIR%" ( |
| 66 | + echo The backup folder already exists: "%BACKUP_DIR%" |
| 67 | + set /p OVERWRITE=Do you want to OVERWRITE it? [y/N] : |
| 68 | + if /i not "%OVERWRITE%"=="y" ( |
| 69 | + echo Aborted. Nothing was changed. |
| 70 | + goto :eof |
| 71 | + ) |
| 72 | + echo Removing existing folder... |
| 73 | + rmdir /s /q "%BACKUP_DIR%" |
| 74 | + if exist "%BACKUP_DIR%" ( |
| 75 | + echo [ERROR] Failed to remove the existing folder. Check permissions. |
| 76 | + goto :eof |
| 77 | + ) |
| 78 | +) |
| 79 | +mkdir "%BACKUP_DIR%" 2>nul |
| 80 | +if errorlevel 1 ( |
| 81 | + echo [ERROR] Failed to create backup folder. Check path/permissions. |
| 82 | + goto :eof |
| 83 | +) |
| 84 | + |
| 85 | +pushd "%BACKUP_DIR%" >nul |
| 86 | + |
| 87 | +echo -------------------------------------------------- |
| 88 | +echo [2/6] Backing up main repository (mirror: all branches/tags) |
| 89 | +echo -------------------------------------------------- |
| 90 | +git clone --mirror "%REPO_URL%" "%NAME%.git" |
| 91 | +if errorlevel 1 ( |
| 92 | + echo [ERROR] Failed to back up the main repository. |
| 93 | + popd >nul |
| 94 | + goto :eof |
| 95 | +) |
| 96 | + |
| 97 | +echo -------------------------------------------------- |
| 98 | +echo [3/6] Checking for Wiki repository... |
| 99 | +echo -------------------------------------------------- |
| 100 | +set "WIKI_URL=%REPO_URL%" |
| 101 | +if /i "%WIKI_URL:~-4%"==".git" ( |
| 102 | + set "WIKI_URL=%WIKI_URL:~0,-4%.wiki.git" |
| 103 | +) else ( |
| 104 | + set "WIKI_URL=%WIKI_URL%.wiki.git" |
| 105 | +) |
| 106 | +echo Wiki URL: %WIKI_URL% |
| 107 | + |
| 108 | +git ls-remote "%WIKI_URL%" >nul 2>&1 |
| 109 | +if %errorlevel%==0 ( |
| 110 | + echo Wiki repository found. Backing up Wiki... |
| 111 | + git clone --mirror "%WIKI_URL%" "%NAME%.wiki.git" |
| 112 | + if errorlevel 1 ( |
| 113 | + echo [WARNING] Failed to back up Wiki repository. Main repository backup succeeded. |
| 114 | + ) |
| 115 | +) else ( |
| 116 | + echo Wiki repository not found or inaccessible. Skipping... |
| 117 | +) |
| 118 | + |
| 119 | +popd >nul |
| 120 | + |
| 121 | +if /i "%MODE%"=="FULL" ( |
| 122 | + echo -------------------------------------------------- |
| 123 | + echo [4/6] Exporting GitHub metadata via PowerShell... |
| 124 | + echo (issues, issue comments, PRs, PR comments/reviews, labels, milestones, releases+assets) |
| 125 | + echo -------------------------------------------------- |
| 126 | + where pwsh >nul 2>&1 |
| 127 | + if errorlevel 1 ( |
| 128 | + where powershell >nul 2>&1 |
| 129 | + if errorlevel 1 ( |
| 130 | + echo [ERROR] PowerShell not found. Install PowerShell 7+ or run backup_github_full.ps1 directly. |
| 131 | + goto after_meta |
| 132 | + ) else ( |
| 133 | + set "PWSH=powershell" |
| 134 | + ) |
| 135 | + ) else ( |
| 136 | + set "PWSH=pwsh" |
| 137 | + ) |
| 138 | + %PWSH% -NoLogo -NoProfile -ExecutionPolicy Bypass -File "%~dp0backup_github_full.ps1" -RepoUrl "%REPO_URL%" -OutDir "%OUT_DIR%" -MetaOnly -Token "%GITHUB_TOKEN%" |
| 139 | + if errorlevel 1 ( |
| 140 | + echo [WARNING] Metadata export returned a non-zero exit code. Code/Wiki backup is available. |
| 141 | + ) else ( |
| 142 | + echo Metadata export completed. |
| 143 | + ) |
| 144 | +) else ( |
| 145 | + echo -------------------------------------------------- |
| 146 | + echo [4/6] Skipping metadata (Lite mode, no token). |
| 147 | +) |
| 148 | + |
| 149 | +:after_meta |
| 150 | +echo -------------------------------------------------- |
| 151 | +echo [5/6] Verifying results |
| 152 | +echo Code: %BACKUP_DIR%\%NAME%.git |
| 153 | +if exist "%BACKUP_DIR%\%NAME%.wiki.git" ( |
| 154 | + echo Wiki: %BACKUP_DIR%\%NAME%.wiki.git |
| 155 | +) else ( |
| 156 | + echo Wiki: (none) |
| 157 | +) |
| 158 | +if /i "%MODE%"=="FULL" ( |
| 159 | + echo Meta: %BACKUP_DIR%\meta |
| 160 | +) else ( |
| 161 | + echo Meta: (skipped) |
| 162 | +) |
| 163 | +echo -------------------------------------------------- |
| 164 | +echo [6/6] Backup finished (%MODE% mode). |
| 165 | +echo -------------------------------------------------- |
| 166 | + |
| 167 | +endlocal |
0 commit comments