Skip to content

Instantly share code, notes, and snippets.

@dsebastien
Created January 24, 2025 13:01
Show Gist options
  • Save dsebastien/dbff841a773be4ecdb61e5233e104feb to your computer and use it in GitHub Desktop.
Save dsebastien/dbff841a773be4ecdb61e5233e104feb to your computer and use it in GitHub Desktop.
# Configuration
$ignoredPaths = @(
".git",
".stversions",
".obsidian\plugins\obsidian-icon-folder\icons"
)
# Get current timestamp for zip name
$timestamp = Get-Date -Format "yyyy_MM_dd-HH_mm_ss"
$zipName = "Seb-Backup-$timestamp.zip"
$destination = "D:\BACKUPS\Obsidian\$zipName"
Write-Host "`nStarting backup process at $timestamp" -ForegroundColor Green
# Get the root directory of the git repo (current directory)
$repoRoot = git rev-parse --show-toplevel
Write-Host "Backing up repository at: $repoRoot"
# List ignored paths
Write-Host "`nIgnored paths:" -ForegroundColor Yellow
$ignoredPaths | ForEach-Object {
Write-Host "- $_\" -ForegroundColor Gray
}
# Create a temporary staging directory (clean it first if it exists)
$tempDir = Join-Path $env:TEMP "backup_staging"
if (Test-Path $tempDir) {
Write-Host "`nCleaning up existing temp directory..."
Remove-Item -Path $tempDir -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
Write-Host "Created temporary staging directory at: $tempDir"
# Copy all files except ignored paths
Write-Host "`nCopying files to staging directory..." -ForegroundColor Yellow
$fileCount = 0
$totalSize = 0
Get-ChildItem -Path "./" -Recurse -Force | Where-Object {
$relativePath = $_.FullName.Substring($repoRoot.Length + 1)
$isIgnored = $false
foreach ($ignoredPath in $ignoredPaths) {
if ($relativePath.StartsWith($ignoredPath) -or $relativePath.Contains($ignoredPath)) {
$isIgnored = $true
break
}
}
!$isIgnored
} | ForEach-Object {
$relativePath = $_.FullName.Substring($repoRoot.Length + 1)
$targetPath = Join-Path $tempDir $relativePath
if ($_.PSIsContainer) {
# Create directory
New-Item -ItemType Directory -Force -Path $targetPath | Out-Null
} else {
# Create target directory if it doesn't exist
$targetDir = Split-Path $targetPath -Parent
if (!(Test-Path $targetDir)) {
New-Item -ItemType Directory -Force -Path $targetDir | Out-Null
}
# Copy file
Copy-Item -LiteralPath $_.FullName -Destination $targetPath -Force
$fileCount++
$totalSize += $_.Length
}
}
Write-Host "Copied $fileCount files (Total size: $([math]::Round($totalSize / 1MB, 2)) MB)"
# Create archive using 7-Zip with Zstandard
Write-Host "`nCreating compressed archive..." -ForegroundColor Yellow
$7zip = "C:\Program Files\7-Zip-Zstandard\7z.exe"
& $7zip a -tzip -mx=9 -mmt=on "$destination" "$tempDir\*"
# Cleanup
Write-Host "`nCleaning up temporary files..." -ForegroundColor Yellow
Remove-Item -Path $tempDir -Recurse -Force
$endTime = Get-Date
$duration = $endTime - [DateTime]::ParseExact($timestamp, "yyyy_MM_dd-HH_mm_ss", $null)
Write-Host "`nBackup completed successfully!" -ForegroundColor Green
Write-Host "Location: $destination"
Write-Host "Duration: $([math]::Round($duration.TotalSeconds, 2)) seconds"
Write-Host "Files backed up: $fileCount"
Write-Host "Total size: $([math]::Round($totalSize / 1MB, 2)) MB`n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment