powershell 容器磁盘空间相关
# 批量清理
$logDir = 'C:\Users\ContainerAdministrator\AppData\Roaming\Tencent\WXWork\Log'
$tmpDir = 'C:\Users\ContainerAdministrator\AppData\Local\Temp\Tencent'
docker ps --filter label=gnproxy -q   | ForEach-Object { docker exec $_ powershell -Command "echo $_; Get-ChildItem -Path $($logDir) -Include *.log,*.txt,*.dmp -Recurse | Remove-Item -Force -ErrorAction SilentlyContinue; Get-ChildItem -Path $($logDir) -Filter *.log -Recurse | ForEach-Object { Clear-Content -Path $_.FullName -ErrorAction SilentlyContinue };Get-ChildItem -Path $($tmpDir) -Include *.log,*.txt,*.dmp -Recurse  -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue; Get-PSDrive 'c'" }

# 批量查看
docker ps --filter label=gnproxy -q   | ForEach-Object { docker exec $_ powershell -Command "echo $_; Get-PSDrive 'c'; " }

# 删除 wxwork 生成的 dump 文件 
docker exec f56e197d09  powershell -Command "Get-PSDrive; Remove-Item -Path 'C:\Users\ContainerAdministrator\AppData\Local\Temp\Tencent' -Recurse -Force -ErrorAction SilentlyContinue; Get-PSDrive"
# 获取指定目录及子目录(最多4级)的空间占用

function ConvertTo-ReadableSize {
    param (
        [long]$size
    )
    $sizes = "Bytes", "KB", "MB", "GB", "TB"
    $index = 0
    while ($size -gt 1kb -and $index -lt $sizes.Length - 1) {
        $size /= 1kb
        $index++
    }
    return "{0:N2} {1}" -f $size, $sizes[$index]
}

function CountDirSize {
    param (
        $directoryPath,
        $name,
        $deep        
    )

    $files = Get-ChildItem -Path $directoryPath -Recurse -File
    $totalSize = ($files | Measure-Object -Property Length -Sum).Sum

    # 太小的就不展示了
    if($totalSize -lt  10mb){
        return
    }

    $spear = "  " * $deep
    # Write-Output " $spear - $($name): $($totalSize / 1kb) KB。"
    # Write-Output " $spear - $($name): $($totalSize / 1mb) MB。"
    # Write-Output " $spear - $($name): $($totalSize / 1gb) GB。"

    $readableSize = ConvertTo-ReadableSize -size $totalSize
    $spear = "  " * $deep
    Write-Output " $spear - $($name): $readableSize。"    
}

function CountSubDirSize {
    param (
        $directoryPath,
        $name,
        $deep
    )

    if ($deep -gt 4) {
        return
    }

    CountDirSize -directoryPath $directoryPath -name $name -deep $deep
    $subDirs = Get-ChildItem -Path $directoryPath -Directory  # -Recurse
    foreach ($subDir in $subDirs) {
        CountSubDirSize -directoryPath $subDir.FullName -name $subDir.Name -deep ($deep + 1)
    }
}

CountSubDirSize -name "根目录" -directoryPath "C:\users\"
1. 运行 GnCleaner
2. 运行 Find-OrphanDockerLayers
3. 删除 D:\gnlab\gnapi\files 目录下三个子目录中旧日期的文件 
4. 删除最近的日志文件   
docker system prune -a 包括未使用的镜像 
或 
docker system prune 包括未引用的镜像 

Write-Output '删除已关闭容器的企业微信文件夹'

$root = "D:\gnlab\gnproxy"
$url = "http://127.0.0.1/gnapi/Test/GetOnlineWxs"

$response = Invoke-WebRequest -Uri $url
$json =  $response.Content | ConvertFrom-Json

$guidHash = @{}
$json.data.data | ForEach-Object Guid | % {$guidHash.Add($_, $true)}

Get-ChildItem -Path $root -Directory | ForEach-Object {
    $guid = $_.Name
    if ($guidHash.ContainsKey($guid)) {        
        # Write-Output "保留容器:$guid"
    } else {

        # Write-Output "删除文件夹:$guid"
        $fullPath = Join-Path $root -ChildPath $guid
        Write-Output "删除文件夹:$fullPath"

        Remove-Item -Path $fullPath -Recurse

        # Remove-Item -Recurse -Force $_.FullName

    }
}
容器标签
(docker inspect c324ae698681  | ConvertFrom-Json )[0].config.Labels
# 获取磁盘空间相关信息
Get-PSDrive

# 获取当前目录占用空间
Get-ChildItem  -Recurse  -ErrorAction SilentlyContinue | Measure-Object -Sum Length
# 以 GB 为单位显示
Get-ChildItem  -Recurse  -ErrorAction SilentlyContinue | Measure-Object -Sum Length | Select-Object @{Name='TotalSize (GB)'; Expression={$_.Sum / 1GB -as [int]}}

# 获取运行中容器使用卷占用空间
[array]$ContainerDetails = docker ps -a -q | ForEach { docker inspect $_ | ConvertFrom-Json}
ForEach ($Container in $ContainerDetails) {

    $id = $Container.Id 
    $size = 0
    echo $id
    ForEach($ContainerLayer in $Container.GraphDriver.Data.dir){
        # echo "  -> $ContainerLayer"
        $size = (Get-ChildItem "$ContainerLayer\sandbox.vhdx").Length / 1Gb
        echo $size
        # if($size > 10){
        #   echo $size 
        # }

    }   
}

# 看附件 replaceReporter.7z

# 列出文件的真实路径 
# docker exec f46423f8d283 powershell -Command 'Get-ChildItem TxBugReport* -Path c:\gnlab\wxwork -Recurse | Select-Object FullName | Format-Table -HideTableHeaders -Wrap' 

# 列出最近的 n 条数据
$dockerIds = docker ps -n 6 -q

$dockerPaths = @()
$dockerPaths += 'C:\gnlab\WXWork\4.1.22.6014\TxBugReport.exe'
$dockerPaths += 'C:\gnlab\WXWork\4.1.22.6014\FlutterPlugins\TxBugReport.exe'
$dockerPaths += 'C:\gnlab\WXWork\4.1.22.6014\wemail\TxBugReport.exe'
$dockerPaths += 'C:\gnlab\WXWork\4.1.22.6014\WeMeet\3.10.5.636\TxBugReport.exe'
$dockerPaths += 'C:\gnlab\WXWork\4.1.22.6014\compatible_web\WXWorkWeb.exe'

$desktop = [system.Environment]::GetFolderPath('Desktop')
$sleepPath = Join-Path $desktop -ChildPath 'Sleep10.exe'

if (-not [System.IO.File]::Exists($sleepPath))
{
    throw [System.IO.FileNotFoundException]::new('请在桌面上准备 sleep10.exe 文件 ')
}

foreach ($dockerPath in $dockerPaths) {

    $length = $dockerIds.Length
    $dockerIds | % { echo $_; $length -= 1; echo $length; docker cp $sleepPath "$($_):$($dockerPath)" }   
}

处理 windows 容器中使用 windowsfilter 目录
https://gist.github.com/olljanat/340b4033eb24d8d33ec75f2c3c3b6b3d
https://dotpeekser.de/posts/how-to-remove-orphan-layers-in-docker-for-windows
https://geek-docs.com/docker/docker-tutorials/t_how-to-find-the-layers-and-layer-sizes-for-each-docker-image.html

清空外部文件

GnCleaner

可创建定时器,清空容器内日志文件

auto_clear_docker

替换文件

replaceReporter

清理文件样本

原文件_Find-OrphanDockerLayers

上一篇
下一篇