Skip to content

Commit 1f460cf

Browse files
committed
Initial commit
1 parent fe64b20 commit 1f460cf

File tree

4 files changed

+872
-0
lines changed

4 files changed

+872
-0
lines changed

README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
# `github` 저장소 백업 스크립트
3+
4+
- 윈도우즈 `cmd`와 파워쉘 `ps1` 파일로 구성.
5+
6+
## ✅ CMD 사용 예제 (`backup_github_full.cmd`)
7+
8+
### **기본 사용 (OutDir 미지정 → 현재 폴더에 백업 생성)**
9+
10+
```cmd
11+
backup_github_full.cmd https://github.com/j2doll/discussion.git
12+
```
13+
14+
동작:
15+
16+
* `GITHUB_TOKEN` 없음 → **LITE 모드**
17+
* 실행 전에 아래와 같이 표시:
18+
19+
```
20+
Detected mode: LITE
21+
This will back up: Code+Wiki only (no metadata).
22+
Proceed with this mode? [y/N]:
23+
```
24+
25+
---
26+
27+
### **출력 경로 지정**
28+
29+
```cmd
30+
backup_github_full.cmd https://github.com/j2doll/discussion.git D:\GH_Backups
31+
```
32+
33+
→ 백업 위치:
34+
35+
```
36+
D:\GH_Backups\discussion_backup\
37+
```
38+
39+
---
40+
41+
### **FULL 모드 실행 (환경 변수 GITHUB_TOKEN 사용)**
42+
43+
```cmd
44+
setx GITHUB_TOKEN "ghp_XXXXX"
45+
backup_github_full.cmd https://github.com/j2doll/discussion.git
46+
```
47+
48+
동작:
49+
50+
* 토큰 감지 → FULL 모드
51+
* 실행 전 메시지:
52+
53+
```
54+
Detected mode: FULL
55+
This will back up: Code+Wiki+Metadata (issues, PRs, reviews, labels, milestones, releases+assets).
56+
Proceed with this mode? [y/N]:
57+
```
58+
59+
---
60+
61+
### **CMD 내에서 PowerShell 없이 강제 LITE만 실행하고 싶을 때**
62+
63+
(의도적으로 메타데이터 백업 생략)
64+
65+
```cmd
66+
set GITHUB_TOKEN=
67+
backup_github_full.cmd https://github.com/j2doll/discussion.git
68+
```
69+
70+
---
71+
72+
## ✅ PowerShell 사용 예제 (`backup_github_full.ps1`)
73+
74+
### **기본 사용 (OutDir 미지정 → 현재 폴더)**
75+
76+
```powershell
77+
pwsh .\backup_github_full.ps1 -RepoUrl "https://github.com/j2doll/discussion.git"
78+
```
79+
80+
### **출력 경로 지정**
81+
82+
```powershell
83+
pwsh .\backup_github_full.ps1 -RepoUrl "https://github.com/j2doll/discussion.git" -OutDir "D:\GH_Backups"
84+
```
85+
86+
### **FULL 모드 (토큰 직접 지정)**
87+
88+
```powershell
89+
pwsh .\backup_github_full.ps1 -RepoUrl "https://github.com/j2doll/discussion.git" -Token "ghp_XXXXX"
90+
```
91+
92+
### **FULL 모드 (환경 변수 이용)**
93+
94+
```powershell
95+
$env:GITHUB_TOKEN="ghp_XXXXX"
96+
pwsh .\backup_github_full.ps1 -RepoUrl "https://github.com/j2doll/discussion.git"
97+
```
98+
99+
### **Discussions까지 포함 백업 (토큰 필요)**
100+
101+
```powershell
102+
pwsh .\backup_github_full.ps1 -RepoUrl "https://github.com/j2doll/discussion.git" -IncludeDiscussions
103+
```
104+
105+
### **ZIP 압축까지 생성**
106+
107+
```powershell
108+
pwsh .\backup_github_full.ps1 -RepoUrl "https://github.com/j2doll/discussion.git" -Zip
109+
```
110+
111+
### **CMD에서 코드+위키 백업 후, PS에서 메타데이터만 실행 (MetaOnly)**
112+
113+
(일반적으로 CMD가 내부적으로 자동 호출할 때 사용)
114+
115+
```powershell
116+
pwsh .\backup_github_full.ps1 -RepoUrl "https://github.com/j2doll/discussion.git" -OutDir "D:\GH_Backups" -MetaOnly
117+
```
118+
119+
---
120+
121+
## 🔍 요약
122+
123+
| 상황 | CMD 예제 | PS 예제 |
124+
| --------------- | ------------------------------------ | ---------------------------------------------- |
125+
| 기본 백업 | `backup_github_full.cmd URL` | `pwsh .\backup_github_full.ps1 -RepoUrl "URL"` |
126+
| 출력 경로 지정 | `backup_github_full.cmd URL D:\path` | `-OutDir "D:\path"` |
127+
| FULL 모드 (TOKEN) | `setx GITHUB_TOKEN "xxx"` 후 실행 | `-Token "xxx"` 또는 `$env:GITHUB_TOKEN="xxx"` |
128+
| Discussions까지 | (CMD에서 가능) | `-IncludeDiscussions` |
129+
| ZIP 생성 | (CMD에서 수동 압축) | `-Zip` |
130+
131+
---
132+
133+

backup_github.cmd

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)