-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-project-backup.bat
More file actions
156 lines (130 loc) · 4.93 KB
/
Copy pathcreate-project-backup.bat
File metadata and controls
156 lines (130 loc) · 4.93 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
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SCRIPT_DIR=%~dp0"
cd /d "%SCRIPT_DIR%" || goto :project_root_error
for %%I in ("%SCRIPT_DIR:~0,-1%") do set "PROJECT_NAME=%%~nxI"
set "SELF_TEST=0"
if /i "%~1"=="--self-test" set "SELF_TEST=1"
call :find_7zip
if errorlevel 1 goto :missing_7zip
for /f "usebackq delims=" %%I in (`powershell.exe -NoProfile -Command "Get-Date -Format 'yyyy-MM-dd_HH-mm-ss'"`) do set "TIMESTAMP=%%I"
if not defined TIMESTAMP goto :timestamp_error
if "%SELF_TEST%"=="1" (
set "ARCHIVE_NAME=%PROJECT_NAME%_backup_self-test.7z"
) else (
set "ARCHIVE_NAME=%PROJECT_NAME%_backup_%TIMESTAMP%.7z"
)
set "ARCHIVE_PATH=%SCRIPT_DIR%%ARCHIVE_NAME%"
if exist "%ARCHIVE_PATH%" del /q "%ARCHIVE_PATH%" >nul 2>&1
if "%SELF_TEST%"=="0" (
echo.
echo Project backup
echo ==============
echo Project: %PROJECT_NAME%
echo Output: %ARCHIVE_PATH%
echo.
echo WARNING: This archive is not encrypted.
echo It contains local signing files and may contain keystores.
echo Store the archive in a protected location.
echo.
) else (
echo Running project backup self-test...
)
"%SEVEN_ZIP%" a -t7z "%ARCHIVE_PATH%" "." ^
-mx=7 -m0=lzma2 ^
-xr!build -xr!.gradle -xr!.idea ^
-xr!benchmark-reports -xr!artifacts -xr!logs ^
-xr!*.log -xr!*.perfetto-trace -xr!*benchmarkData.json ^
-xr!emulator-screen.png -xr!ui*.xml ^
-xr!%PROJECT_NAME%_backup_*.7z
if errorlevel 1 goto :archive_failed
echo.
echo Verifying archive integrity...
"%SEVEN_ZIP%" t "%ARCHIVE_PATH%" -bsp0
if errorlevel 1 goto :verification_failed
if "%SELF_TEST%"=="1" goto :validate_self_test
for %%I in ("%ARCHIVE_PATH%") do set "ARCHIVE_SIZE=%%~zI"
echo.
echo Backup created and verified successfully.
echo File: %ARCHIVE_PATH%
echo Bytes: %ARCHIVE_SIZE%
echo.
pause
exit /b 0
:validate_self_test
set "LIST_FILE=%TEMP%\%PROJECT_NAME%-backup-list-%RANDOM%%RANDOM%.txt"
"%SEVEN_ZIP%" l -slt "%ARCHIVE_PATH%" >"%LIST_FILE%"
if errorlevel 1 goto :self_test_failed
findstr.exe /i /l /c:"Path = .git\HEAD" "%LIST_FILE%" >nul
if errorlevel 1 goto :missing_required_entry
findstr.exe /i /l /c:"Path = app\src\main" "%LIST_FILE%" >nul
if errorlevel 1 goto :missing_required_entry
findstr.exe /i /l /c:"Path = settings.gradle.kts" "%LIST_FILE%" >nul
if errorlevel 1 goto :missing_required_entry
findstr.exe /i /l /c:"Path = gradlew.bat" "%LIST_FILE%" >nul
if errorlevel 1 goto :missing_required_entry
if exist "apks\" (
findstr.exe /i /l /c:"Path = apks" "%LIST_FILE%" >nul
if errorlevel 1 goto :missing_required_entry
)
if exist "signing.local.properties" (
findstr.exe /i /l /c:"Path = signing.local.properties" "%LIST_FILE%" >nul
if errorlevel 1 goto :missing_required_entry
)
if exist "app\pocastcloni-release.jks" (
findstr.exe /i /l /c:"Path = app\pocastcloni-release.jks" "%LIST_FILE%" >nul
if errorlevel 1 goto :missing_required_entry
)
powershell.exe -NoProfile -Command "$bad = Get-Content -LiteralPath $env:LIST_FILE | Where-Object { $_ -match '^Path = (?:(?:.*\\)?build(?:\\|$)|\.gradle(?:\\|$)|\.idea(?:\\|$)|benchmark-reports(?:\\|$)|artifacts(?:\\|$)|logs(?:\\|$))' }; if ($bad) { $bad | Select-Object -First 20; exit 1 }"
if errorlevel 1 goto :found_excluded_entry
del /q "%LIST_FILE%" >nul 2>&1
del /q "%ARCHIVE_PATH%" >nul 2>&1
echo.
echo Self-test passed: integrity, required content, and exclusions are correct.
exit /b 0
:find_7zip
set "SEVEN_ZIP="
for %%E in (7z.exe 7zz.exe) do (
if not defined SEVEN_ZIP for /f "delims=" %%P in ('where.exe %%E 2^>nul') do set "SEVEN_ZIP=%%P"
)
if defined SEVEN_ZIP exit /b 0
if exist "%ProgramFiles%\7-Zip\7z.exe" set "SEVEN_ZIP=%ProgramFiles%\7-Zip\7z.exe"
if defined SEVEN_ZIP exit /b 0
if exist "%ProgramFiles(x86)%\7-Zip\7z.exe" set "SEVEN_ZIP=%ProgramFiles(x86)%\7-Zip\7z.exe"
if defined SEVEN_ZIP exit /b 0
exit /b 1
:archive_failed
echo.
echo [ERROR] 7-Zip could not create the archive. Partial output is removed.
if exist "%ARCHIVE_PATH%" del /q "%ARCHIVE_PATH%" >nul 2>&1
goto :failed
:verification_failed
echo.
echo [ERROR] Archive verification failed. Unverified output is removed.
if exist "%ARCHIVE_PATH%" del /q "%ARCHIVE_PATH%" >nul 2>&1
goto :failed
:self_test_failed
echo.
echo [ERROR] Project backup self-test failed.
if defined LIST_FILE if exist "%LIST_FILE%" del /q "%LIST_FILE%" >nul 2>&1
if exist "%ARCHIVE_PATH%" del /q "%ARCHIVE_PATH%" >nul 2>&1
goto :failed
:missing_required_entry
echo [ERROR] A required project entry is missing from the backup.
goto :self_test_failed
:found_excluded_entry
echo [ERROR] An excluded generated entry is present in the backup.
goto :self_test_failed
:missing_7zip
echo [ERROR] 7-Zip was not found in PATH or a standard installation folder.
echo Install 7-Zip and run this script again.
goto :failed
:timestamp_error
echo [ERROR] Could not create a timestamp with PowerShell.
goto :failed
:project_root_error
echo [ERROR] Could not open the project root: %SCRIPT_DIR%
goto :failed
:failed
if "%SELF_TEST%"=="0" pause
exit /b 1