Бэкап календаря
Копирует файлы ICS и CSV из папки календаря в отдельный каталог с manifest.
Что делает скрипт
Скрипт проверяет источник, копирует только календарные форматы и сохраняет структуру подпапок. Каждый файл получает запись с SHA-256 в CSV. Флаг -Zip создаёт дополнительный архив, исходные файлы не меняются.
- Файлы календаря могут содержать личные встречи.
Быстрый старт
powershell -File Backup-Calendar.ps1 -Source C:\Users\user\Calendar -Destination E:\Backup\Calendar
Скачайте файл, откройте терминал в папке со скриптом и выполните команду. Запускайте в PowerShell 5.1 или PowerShell 7.
Другие варианты
powershell -File Backup-Calendar.ps1 -Source C:\Users\user\Calendar -Destination E:\Backup\Calendarpowershell -File Backup-Calendar.ps1 -Source C:\Users\user\Calendar -Destination E:\Backup\Calendar -Zip
Что понадобится
Встроен в Windows 10/11 (PowerShell 5.1)
Встроено в Windows 10/11
Backup-Calendar.ps1
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[string]$Source,
[Parameter(Mandatory = $true)]
[string]$Destination,
[switch]$Zip
)
$ErrorActionPreference = 'Stop'
$sourceFull = [System.IO.Path]::GetFullPath($Source)
$destFull = [System.IO.Path]::GetFullPath($Destination)
$trimChars = [char[]]@('\', '/')
if (-not (Test-Path -LiteralPath $sourceFull -PathType Container)) {
throw "Calendar folder not found: $sourceFull"
}
if ($destFull.TrimEnd($trimChars) -eq $sourceFull.TrimEnd($trimChars)) {
throw 'Source and Destination must differ.'
}
if ($destFull.StartsWith($sourceFull.TrimEnd($trimChars) + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) {
throw 'Destination must not be inside Source.'
}
if ($sourceFull.StartsWith($destFull.TrimEnd($trimChars) + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) {
throw 'Source must not be inside Destination.'
}
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$targetRoot = Join-Path $destFull ('calendar-' + $stamp)
if (Test-Path -LiteralPath $targetRoot) {
throw "Destination already exists: $targetRoot"
}
if (-not $PSCmdlet.ShouldProcess($targetRoot, 'Backup calendar files')) {
return
}
New-Item -ItemType Directory -Path $targetRoot -Force | Out-Null
& robocopy $sourceFull $targetRoot *.ics *.csv /S /XJ /COPY:DAT /R:1 /W:1 /NP /NFL /NDL | Out-Null
$code = $LASTEXITCODE
if ($code -ge 8) {
throw "Robocopy failed with code $code"
}
$files = @(Get-ChildItem -LiteralPath $targetRoot -Recurse -File -Force -ErrorAction SilentlyContinue)
$manifest = New-Object System.Collections.Generic.List[object]
foreach ($file in $files) {
$manifest.Add([pscustomobject]@{
File = $file.FullName
Length = $file.Length
Modified = $file.LastWriteTime
SHA256 = (Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256).Hash
})
}
$manifestPath = Join-Path $targetRoot 'manifest.csv'
if ($manifest.Count -gt 0) {
$manifest | Export-Csv -LiteralPath $manifestPath -NoTypeInformation -Encoding UTF8
} else {
Set-Content -LiteralPath $manifestPath -Value 'File,Length,Modified,SHA256' -Encoding UTF8
}
if ($Zip) {
if ($files.Count -eq 0) {
Write-Warning 'Calendar folder has no ICS or CSV files, zip skipped.'
} else {
$zipPath = $targetRoot + '.zip'
if (Test-Path -LiteralPath $zipPath) {
throw "Archive already exists: $zipPath"
}
Compress-Archive -Path (Join-Path $targetRoot '*') -DestinationPath $zipPath -CompressionLevel Optimal
Write-Host "Archive: $zipPath"
}
}
Write-Host "Files: $($files.Count)"
Write-Host "Folder: $targetRoot"
Скрипты работают с файлами и настройками системы. Перед запуском прочитайте код и проверьте пути. Особенно если скрипт удаляет, перезаписывает или перемещает данные.