[UE5][BackUp]Cppプロジェクトでいらないフォルダを含めずにバックアップzipを作成するバッチを作った件

gitだと大容量なサーバーとか用意するの大変だから・・・

実行内容はこんなかんじ

D:\Sandbox\BookAnimation_Codex\UE574Book\UE5CppFolder_MakeBackUpZip.bat
にフォルダをドラッグしたら
中身の
.vs
Binaries
Build
DerivedDataCache
Intermediate
Saved
(pluginsのプラグイン名フォルダ内BinariesとIntermidiateも消してます)
を除いた内容のzipを
D:\Sandbox\BookAnimation_Codex\UE574Book\ドラッグしたフォルダ名.zipに作るバッチを作った
zip実行中になんでもいいからプログレスバーとパーセントを表示するようにした

※バックアップからの復旧方法

VisualStudioなどでビルドします。

UE5CppFolder_MakeBackUpZip.bat

@echo off
setlocal EnableExtensions

if "%~1"=="" (
    echo Usage:
    echo   Drag a folder onto this bat file.
    echo.
    pause
    exit /b 1
)

set "BACKUP_SOURCE=%~f1"
if not exist "%BACKUP_SOURCE%\" (
    echo Error: folder not found.
    echo   %BACKUP_SOURCE%
    echo.
    pause
    exit /b 1
)

set "BACKUP_ZIP=%~dp1%~nx1.zip"
set "BACKUP_SCRIPT=%~dp0UE5CppFolder_MakeBackUpZip.ps1"

echo Source:
echo   %BACKUP_SOURCE%
echo.
echo Output:
echo   %BACKUP_ZIP%
echo.
echo Excluding folders:
echo   .vs Binaries Build DerivedDataCache Intermediate Intermidiate Saved
echo.

if not exist "%BACKUP_SCRIPT%" (
    echo Error: helper script not found.
    echo   %BACKUP_SCRIPT%
    echo.
    pause
    exit /b 1
)

powershell -NoProfile -ExecutionPolicy Bypass -File "%BACKUP_SCRIPT%" -Source "%BACKUP_SOURCE%" -ZipPath "%BACKUP_ZIP%"
set "BACKUP_ERROR=%ERRORLEVEL%"

if not "%BACKUP_ERROR%"=="0" (
    echo.
    echo Failed.
    pause
    exit /b %BACKUP_ERROR%
)

echo.
echo Done.
pause

UE5CppFolder_MakeBackUpZip.ps1

param(
    [Parameter(Mandatory = $true)]
    [string]$Source,

    [Parameter(Mandatory = $true)]
    [string]$ZipPath
)

$ErrorActionPreference = 'Stop'

$sourceFullPath = [System.IO.Path]::GetFullPath($Source).TrimEnd('\', '/')
$zipFullPath = [System.IO.Path]::GetFullPath($ZipPath)

$exclude = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
'.vs', 'Binaries', 'Build', 'DerivedDataCache', 'Intermediate', 'Intermidiate', 'Saved' | ForEach-Object {
    [void]$exclude.Add($_)
}

function Test-IsExcludedBackupFile {
    param([Parameter(Mandatory = $true)][string]$FileFullPath)

    if ([System.StringComparer]::OrdinalIgnoreCase.Equals($FileFullPath, $zipFullPath)) {
        return $true
    }

    $relative = $FileFullPath.Substring($sourceFullPath.Length).TrimStart('\', '/')
    foreach ($part in ($relative -split '[\\/]')) {
        if ($exclude.Contains($part)) {
            return $true
        }
    }

    return $false
}

Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem

if (Test-Path -LiteralPath $zipFullPath) {
    Remove-Item -LiteralPath $zipFullPath -Force
}

Write-Host 'Scanning files...'
$files = @(
    Get-ChildItem -LiteralPath $sourceFullPath -Force -Recurse -File | Where-Object {
        $fileFullPath = [System.IO.Path]::GetFullPath($_.FullName)
        -not (Test-IsExcludedBackupFile -FileFullPath $fileFullPath)
    }
)

$total = $files.Count
Write-Host ("Files to zip: {0}" -f $total)

$zip = [System.IO.Compression.ZipFile]::Open($zipFullPath, [System.IO.Compression.ZipArchiveMode]::Create)
$count = 0

try {
    if ($total -eq 0) {
        Write-Progress -Activity 'Creating backup zip' -Status 'No files to zip' -PercentComplete 100
    }
    else {
        foreach ($file in $files) {
            $count++
            $fileFullPath = [System.IO.Path]::GetFullPath($file.FullName)
            $relative = $fileFullPath.Substring($sourceFullPath.Length).TrimStart('\', '/')
            $entryName = $relative -replace '\\', '/'
            $percent = [int][Math]::Floor(($count * 100.0) / $total)

            Write-Progress `
                -Activity 'Creating backup zip' `
                -Status ("{0}%  {1}/{2}  {3}" -f $percent, $count, $total, $entryName) `
                -PercentComplete $percent

            if (($count % 25) -eq 0 -or $count -eq 1 -or $count -eq $total) {
                Write-Host ("{0,3}%  {1}/{2}  {3}" -f $percent, $count, $total, $entryName)
            }

            [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
                $zip,
                $fileFullPath,
                $entryName,
                [System.IO.Compression.CompressionLevel]::Optimal) | Out-Null
        }
    }
}
finally {
    $zip.Dispose()
    Write-Progress -Activity 'Creating backup zip' -Completed
}

Write-Host ("Created: {0}" -f $zipFullPath)
Write-Host ("Files: {0}" -f $count)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です