-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.bat
212 lines (187 loc) · 6.01 KB
/
backup.bat
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
@echo off
setlocal enabledelayedexpansion
:: Created by reflecct (discord: reflecct)
:: Configuration
set "backupDir=C:\BackupsReflect"
set "tokenFile=%backupDir%\backupTokens.txt"
set "logFile=%backupDir%\backup_log.txt"
:: Ensure running as administrator
net session >nul 2>&1 || (
echo [ERROR] This script must be run as an administrator.
echo [INFO] Exiting script...
pause
exit /b 1
)
:: Create necessary directories and files
if not exist "%backupDir%" mkdir "%backupDir%" 2>nul
if not exist "%tokenFile%" type nul > "%tokenFile%" 2>nul
:MainMenu
cls
call :PrintHeader "Windows Backup and Restore Script"
echo 1. Backup Windows (Save)
echo 2. Restore Windows
echo 3. Troubleshoot Common Issues
echo 4. Manual Backup of Important Files
echo 5. List Existing Backups
echo 6. Delete a Backup
echo 7. Exit
call :PrintFooter
set /p "choice=Choose an option (1-7): "
if "%choice%"=="1" goto BackupOperation
if "%choice%"=="2" goto RestoreOperation
if "%choice%"=="3" goto TroubleshootMenu
if "%choice%"=="4" goto ManualBackup
if "%choice%"=="5" goto ListBackups
if "%choice%"=="6" goto DeleteBackup
if "%choice%"=="7" exit /b 0
echo [ERROR] Invalid choice. Please choose 1-7.
pause
goto MainMenu
:BackupOperation
cls
call :PrintHeader "Backup Operation"
call :EnterToken
if defined token (
echo [INFO] Performing Windows Backup...
call :RunCommand "wbadmin start backup -backupTarget:%backupDir% -include:C: -allCritical -quiet"
if !errorlevel! equ 0 (
ren "%backupDir%\WindowsImageBackup" "!token!"
echo [INFO] Backup completed successfully with token: !token!
echo [INFO] Backup saved to: %backupDir%\!token!
) else (
call :HandleBackupError
)
)
pause
goto MainMenu
:RestoreOperation
cls
call :PrintHeader "Restore Operation"
call :EnterExistingToken
if defined token (
echo [INFO] Performing Windows Restore...
call :RunCommand "wbadmin start recovery -version:!token! -itemType:AllCritical -quiet"
if !errorlevel! equ 0 (
echo [INFO] Restore completed successfully. The system will reboot in 10 seconds.
shutdown /r /t 10
) else (
call :HandleRestoreError
)
)
pause
goto MainMenu
:ManualBackup
cls
call :PrintHeader "Manual Backup of Important Files"
set "importantDirs=%systemdrive%\Windows\System32 %systemdrive%\Users\%USERNAME% %systemdrive%\Windows\SystemApps %systemdrive%\Windows\SystemResources"
set "timestamp=%date:~-4%%date:~3,2%%date:~0,2%-%time:~0,2%%time:~3,2%%time:~6,2%"
set "timestamp=!timestamp: =0!"
set "backupFolder=%backupDir%\ManualBackup_!timestamp!"
mkdir "%backupFolder%" 2>nul
for %%D in (%importantDirs%) do (
echo [INFO] Backing up %%D...
start /b robocopy "%%D" "%backupFolder%\%%~nxD" /E /COPY:DAT /DCOPY:T /R:0 /W:0 /XA:SH /XJ /XF *.lock /MT:16 /NFL /NDL /NP /LOG+:"%logFile%"
)
echo [INFO] Manual Backup completed. Files saved to %backupFolder%
echo [INFO] Check the log file at %logFile% for details.
pause
goto MainMenu
:TroubleshootMenu
cls
call :PrintHeader "Troubleshooting Common Issues"
echo 1. Check/Start Windows Backup Service
echo 2. Check/Start Volume Shadow Copy Service
echo 3. Exit Troubleshooting
call :PrintFooter
set /p "troubleChoice=Choose an option (1-4): "
if "%troubleChoice%"=="1" call :ManageService "wbengine" "Windows Backup" & goto TroubleshootMenu
if "%troubleChoice%"=="2" call :ManageService "VSS" "Volume Shadow Copy" & goto TroubleshootMenu
if "%troubleChoice%"=="3" goto MainMenu
echo [ERROR] Invalid choice. Please choose 1-4.
pause
goto TroubleshootMenu
:ListBackups
call :PrintHeader "Existing Backups"
for /f "tokens=*" %%a in ('dir /b /ad "%backupDir%"') do (
echo %%a
)
pause
goto MainMenu
:DeleteBackup
call :PrintHeader "Delete a Backup"
call :EnterExistingToken
if defined token (
set /p "confirm=Are you sure you want to delete the backup !token!? (Y/N): "
if /i "!confirm!"=="Y" (
rmdir /s /q "%backupDir%\!token!"
findstr /v /c:"!token!" "%tokenFile%" > "%tokenFile%.tmp"
move /y "%tokenFile%.tmp" "%tokenFile%" >nul
echo [INFO] Backup !token! has been deleted.
) else (
echo [INFO] Deletion cancelled.
)
)
pause
goto MainMenu
:: Helper Functions
:PrintHeader
echo ==================================================
echo %~1
echo ==================================================
exit /b
:PrintFooter
echo ==================================================
exit /b
:EnterToken
set /p "token=Enter a unique token for this backup (e.g., Backup123): "
if "!token!"=="" (
echo [ERROR] Token cannot be empty. Please enter a valid token.
exit /b 1
)
findstr /x "!token!" "%tokenFile%" >nul && (
echo [ERROR] This token already exists. Please choose a different one.
exit /b 1
)
echo !token!>> "%tokenFile%"
exit /b 0
:EnterExistingToken
set /p "token=Enter the token of the backup: "
findstr /x "!token!" "%tokenFile%" >nul || (
echo [ERROR] Invalid token. No backup found for the given token: !token!
set "token="
exit /b 1
)
if not exist "%backupDir%\!token!" (
echo [ERROR] Backup folder not found for token: !token!
set "token="
exit /b 1
)
exit /b 0
:RunCommand
%~1
exit /b %errorlevel%
:HandleBackupError
echo [ERROR] Backup failed. Common causes:
echo - Insufficient disk space on the backup target.
echo - The backup service is not running.
exit /b
:HandleRestoreError
echo [ERROR] Restore failed. Common causes:
echo - The backup version is corrupt or incomplete.
echo - Insufficient permissions to restore the system.
exit /b
:ManageService
sc query "%~1" | findstr /I "RUNNING" >nul
if errorlevel 1 (
echo [INFO] Starting %~2 Service...
sc start "%~1" >nul
if errorlevel 1 (
echo [ERROR] Failed to start %~2 Service.
) else (
echo [INFO] %~2 Service started successfully.
)
) else (
echo [INFO] %~2 Service is already running.
)
pause
exit /b