-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_script.bat
More file actions
28 lines (19 loc) · 1.45 KB
/
backup_script.bat
File metadata and controls
28 lines (19 loc) · 1.45 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
@echo off
REM source= the location of your minecraft data
set "source=A:\mcdata"
REM destination= the location you want to backup minecraft to
set "destination=A:\_backups"
for /f "tokens=2 delims==." %%a in ('wmic os get LocalDateTime /value') do set "timestamp=%%a"
set "timestamp=%timestamp:~0,4%-%timestamp:~4,2%-%timestamp:~6,2%_%timestamp:~8,2%-%timestamp:~10,2%-%timestamp:~12,6%"
REM Append the timestamp to the folder name
set "folderName=mc_data_%timestamp%"
REM Copy the source directory including all files
xcopy "%source%" "%destination%\%folderName%\" /E /C /I
REM Compress the copied directory into a ZIP file, retry if the debug.log file is locked
:Compress
powershell -nologo -noprofile -command "try { Add-Type -A 'System.IO.Compression.FileSystem'; [System.IO.Compression.ZipFile]::CreateFromDirectory('%destination%\%folderName%', '%destination%\%folderName%.zip'); exit 0 } catch { exit 1 }"
if errorlevel 1 goto :Compress
REM Delete the new folder
rmdir /s /q "%destination%\%folderName%"
REM Delete old backups (retain the last 7)
powershell -nologo -noprofile -Command " $backupFiles = Get-ChildItem -Path \"%destination%\mc_data_*\" -File | Sort-Object CreationTime; $filesToRetain = $backupFiles.Count - 7; if ($filesToRetain -gt 0) { $backupFiles | Select-Object -First $filesToRetain | ForEach-Object { Remove-Item -Path $_.FullName -Force -Recurse; $zipFile = \"$($_.FullName).zip\"; if (Test-Path $zipFile) { Remove-Item -Path $zipFile -Force } } }"