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