【linux】精简版代码备份

条件: 不要二进制,或者文件夹下大于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

相关推荐

  1. linux精简代码备份

    2024-07-11 15:44:06       24 阅读
  2. 【lesson8】云备份服务端完整代码

    2024-07-11 15:44:06       22 阅读
  3. linux中数据库备份

    2024-07-11 15:44:06       52 阅读
  4. Linux / Ubuntu 备份数据

    2024-07-11 15:44:06       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 15:44:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 15:44:06       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 15:44:06       58 阅读
  4. Python语言-面向对象

    2024-07-11 15:44:06       69 阅读

热门阅读

  1. 学习小记-使用Redis的令牌桶算法实现分布式限流

    2024-07-11 15:44:06       22 阅读
  2. 梯度下降算法的原理

    2024-07-11 15:44:06       22 阅读
  3. pytorch的axis的理解

    2024-07-11 15:44:06       22 阅读
  4. 搭建基于 ChatGPT 的问答系统

    2024-07-11 15:44:06       22 阅读