-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
137 lines (105 loc) · 3.71 KB
/
app.py
File metadata and controls
137 lines (105 loc) · 3.71 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
import requests
import os
import subprocess
import logging
from logging.handlers import RotatingFileHandler
from typing import List, Dict, Optional
# ================== CONFIG ==================
GITLAB_INSTANCE = "https://gitlab.com"
BASE_PATH = os.path.join(os.getcwd(), "repos")
ACCESS_TOKEN = os.getenv("GITLAB_TOKEN", "glpat-XXXX") # Prefer env var
REQUEST_TIMEOUT = 15
PER_PAGE = 100
# ================== LOGGING ==================
LOG_DIR = "logs"
os.makedirs(LOG_DIR, exist_ok=True)
logger = logging.getLogger("gitlab_backup")
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
"%(asctime)s | %(levelname)s | %(message)s"
)
# Console
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
# File (rotating)
file_handler = RotatingFileHandler(
os.path.join(LOG_DIR, "backup.log"),
maxBytes=2_000_000,
backupCount=3
)
file_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# ================== HTTP ==================
HEADERS = {
"PRIVATE-TOKEN": ACCESS_TOKEN
}
session = requests.Session()
session.headers.update(HEADERS)
# ================== FUNCTIONS ==================
def gitlab_get(url: str, params: Optional[Dict] = None) -> List[Dict]:
"""Paginated GET request to GitLab API"""
results = []
page = 1
while True:
query = params.copy() if params else {}
query.update({"page": page, "per_page": PER_PAGE})
try:
response = session.get(url, params=query, timeout=REQUEST_TIMEOUT)
response.raise_for_status()
except requests.RequestException as e:
logger.error(f"GitLab API error: {e}")
break
data = response.json()
if not data:
break
results.extend(data)
page += 1
return results
def clone_or_pull(repo_url: str, path: str) -> None:
"""Clone repo if missing, otherwise pull"""
try:
if not os.path.exists(path):
logger.info(f"Cloning: {path}")
subprocess.run(
["git", "clone", repo_url, path],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
)
else:
logger.info(f"Pulling: {path}")
subprocess.run(
["git", "-C", path, "pull"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
)
except subprocess.CalledProcessError as e:
logger.error(f"Git error on {path}: {e.stderr.decode().strip()}")
# ================== MAIN ==================
def main():
os.makedirs(BASE_PATH, exist_ok=True)
logger.info("Starting GitLab backup")
groups = gitlab_get(
f"{GITLAB_INSTANCE}/api/v4/groups",
{"min_access_level": 10, "top_level_only": True}
)
logger.info(f"Found {len(groups)} root groups")
for group in groups:
logger.info(f"Processing group: {group['full_path']}")
projects = gitlab_get(
f"{GITLAB_INSTANCE}/api/v4/groups/{group['id']}/projects",
{"include_subgroups": True}
)
logger.info(f" Projects found: {len(projects)}")
for project in projects:
project_dir = os.path.join(
BASE_PATH,
project["path_with_namespace"]
)
os.makedirs(os.path.dirname(project_dir), exist_ok=True)
clone_or_pull(project["http_url_to_repo"], project_dir)
logger.info("GitLab backup completed successfully")
if __name__ == "__main__":
main()