条件: 不要二进制,或者文件夹下大于100个项目
Windows 版本(带进度条)
# Define the source directory and backup zip file
$sourceDir = "C:\Path\To\Your\Directory"
$zipFile = "C:\Path\To\Backup\backup.zip"
# Define the temporary directory explicitly
$tempDir = "C:\Path\To\Temp\Backup"
# Function to determine if a file is binary
function Is-BinaryFile {
param (
[string]$filePath
)
$binary = $false
$fileStream = [System.IO.File]::OpenRead($filePath)
try {
$buffer = New-Object byte[] 512
$fileStream.Read($buffer, 0, $buffer.Length) | Out-Null
$binary = $buffer -contains 0
} catch {
$binary = $true
} finally {
$fileStream.Dispose()
}
return $binary
}
# Create the temporary directory to hold the files to be zipped
if (Test-Path $tempDir) {
Remove-Item $tempDir -Recurse -Force
}
New-Item -ItemType Directory -Path $tempDir
# Function to process directories recursively
function Process-Directory {
param (
[string]$dirPath,
[string]$destPath,
[ref]$processedCount,
[int]$totalCount
)
# Check if the directory contains more than 100 items
$items = Get-ChildItem -Path $dirPath
if ($items.Count -gt 100) {
Write-Output "Skipping directory: $dirPath (contains more than 100 items)"
return
}
foreach ($item in $items) {
$destination = Join-Path $destPath $item.Name
if ($item.PSIsContainer) {
# Create the directory in the temporary location
New-Item -ItemType Directory -Path $destination -Force
# Recurse into subdirectories
Process-Directory -dirPath $item.FullName -destPath $destination -processedCount $processedCount -totalCount $totalCount
} else {
# Check if the file is binary
if (-not (Is-BinaryFile $item.FullName)) {
# Copy the non-binary file
Copy-Item -Path $item.FullName -Destination $destination
}
$processedCount.Value++
$percentComplete = [math]::round(($processedCount.Value / $totalCount) * 100)
Write-Progress -Activity "Backing Up Files" -Status "$percentComplete% Complete" -PercentComplete $percentComplete
}
}
}
# Count total files to be processed
function Count-Files {
param (
[string]$dirPath
)
$totalFiles = 0
$items = Get-ChildItem -Path $dirPath -Recurse
foreach ($item in $items) {
if (-not $item.PSIsContainer) {
$totalFiles++
}
}
return $totalFiles
}
# Initialize total count and processed count
$totalCount = Count-Files -dirPath $sourceDir
$processedCount = 0
# Start processing from the source directory
Process-Directory -dirPath $sourceDir -destPath $tempDir -processedCount ([ref]$processedCount) -totalCount $totalCount
# Create the zip file
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory($tempDir, $zipFile)
# Clean up the temporary directory
Remove-Item -Path $tempDir -Recurse -Force
Write-Output "Backup complete: $zipFile"
运行
.\BackupScript.ps1
linux 版本
#!/bin/bash
# Define the source directory and backup zip file
sourceDir="/path/to/your/directory"
zipFile="/path/to/backup/backup.zip"
tempDir="/path/to/temp/backup"
# Create the temporary directory to hold the files to be zipped
mkdir -p "$tempDir"
# Function to determine if a file is binary
is_binary_file() {
local file="$1"
if file "$file" | grep -qE 'binary|executable'; then
return 0 # True
else
return 1 # False
fi
}
# Function to process directories recursively
process_directory() {
local dirPath="$1"
local destPath="$2"
# Check if the directory contains more than 100 items
if [ $(find "$dirPath" -mindepth 1 -maxdepth 1 | wc -l) -gt 100 ]; then
echo "Skipping directory: $dirPath (contains more than 100 items)"
return
fi
# Create the destination directory
mkdir -p "$destPath"
# Iterate over files and subdirectories in the current directory
find "$dirPath" -mindepth 1 -maxdepth 1 | while read -r item; do
local destination="$destPath/$(basename "$item")"
if [ -d "$item" ]; then
# Recurse into subdirectories
process_directory "$item" "$destination"
else
# Check if the file is binary
if ! is_binary_file "$item"; then
# Copy the non-binary file
cp "$item" "$destination"
fi
fi
done
}
# Start processing from the source directory
process_directory "$sourceDir" "$tempDir"
# Create the zip file
(cd "$tempDir" && zip -r "$zipFile" .)
# Clean up the temporary directory
rm -rf "$tempDir"
echo "Backup complete: $zipFile"
运行方式
./backup_script.sh