首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用7zip

如何使用7zip
EN

Stack Overflow用户
提问于 2014-07-14 21:31:50
回答 1查看 487关注 0票数 0

我有一个大的脚本,做所有的工作,恢复,备份和删除现有的。然而,在这样做之前,我从主脚本调用我的备份脚本,该脚本在删除现有站点并将其恢复到以前的状态之前备份现有站点。但是,当我的备份脚本(我从主脚本调用)在文件被删除的同时运行时,我希望等待一段时间,以便备份在当前文件被删除之前首先完成,然后恢复以前的备份并将文件粘贴到各自的文件夹中。

这是我的主脚本,它是

$content =获取子项'C:\Backups\my_site‘

代码语言:javascript
复制
$sortedContent = $content |  Sort-Object LastWriteTime -Descending  

write-host“这是my_site的所有备份的列表:”

$count =1 foreach ($sortedContent中的$item){

代码语言:javascript
复制
Write-Host ("{0}: {1}" -f $count, $item.Name)
$count++ 
}



# 2.Take input from user
$itemNumber = Read-Host "Please select which backup you want to restore"
$confirmation = Read-Host "Are you Sure You Want To Proceed:"
# 2.Take input from user

if ($confirmation -eq 'y') {

# 3. BACKUP script

./ BACKUP _mysite.ps1 # 3.备份

代码语言:javascript
复制
# 4. DELETE CONTENTS OF my_site


get-childitem "C:\inetpub\wwwroot\my_site\" -recurse | % {

remove-item $_.FullName -recurse -force

}

}


# 4. DELETE CONTENTS OF APP

# 5. COPY CONTENTS OF ZIP INTO APP DIRECTORY
$itemNumber = $itemNumber -1
if($sortedContent[$itemNumber].PSIsContainer -eq $true)
{ 
$src = $sortedContent[$itemNumber].FullName + "\"


$WebzipPath = $src + "my_site.zip"


$Date = Get-Date
$folder_date = $Date.ToString("yyyy-MM-dd_HHmm")
$tempPath = 'C:\_Temp\Restore\my_site_restore_' + $folder_date
if (!(Test-Path -path $tempPath)) 
{
    New-Item $tempPath -type directory
}   


./Library/unzip.ps1 $WebzipPath $tempPath



$tempPathWeb = $tempPath + "/my_site/*" 

Copy-Item  -Path $tempPat -Destination 'C:\inetpub\wwwroot\my_site\' -Recurse -
force

我使用的zip脚本是这样的

代码语言:javascript
复制
$path = $args[0]
$files = $input

if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

if (-not (test-path $path)) { 
set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
} 

$ZipFile = (new-object -com shell.application).NameSpace($path) 
$files | foreach {$zipfile.CopyHere($_.fullname)} 

有人建议我不能使用这个zip脚本,因为它是异步的,这个人建议我使用7zip来做所有的压缩,但我不知道怎么做,这是我的备份脚本,然后调用上面的zip脚本来完成所有的压缩。

代码语言:javascript
复制
$Service_folder_name = 'C:\Services\'
$Pics_folder_name = 'C:\Pics\'


$Date = Get-Date

$folder_date = $Date.ToString("yyyy-MM-dd_HHmm")

$backup_folder_name = 'c:\_Backups\my_site\' + $folder_date

if (!(Test-Path -path $backup_folder_name)) {

New-Item $backup_folder_name -type directory
}

if ((Test-Path -path $Pics_folder_name)) {
gi $pics_folder_name | .\Library\out-zip.ps1 $backup_folder_name\pics.zip $_
}

if ((Test-Path -path $Service_folder_name)) {
gi $Service_folder_name | .\Library\out-zip.ps1 $backup_folder_name\Services.zip $_
}
EN

回答 1

Stack Overflow用户

发布于 2014-07-14 23:03:03

我之前写了一个使用7zip的函数,这个函数使用的是独立的7za.exe。它应该会让你对如何同步使用它有一个很好的想法。

代码语言:javascript
复制
Function out-7zip
{
<#
.SYNOPSIS
Will add files to a zip package

.DESCRIPTION
Uses the 7za.exe standalone component to add files to an archive, if the archive specified in the path doesn't exist it will create a new archive automatically.

.EXAMPLE
ls c:\logs -recurse | Where {(!($_.PSiscontainer))} | out-7zip -path c:\logs.zip

-----------------

Adds all files from the c:\logs directory to the c:\logs.zip archive

.EXAMPLE
get-item c:\logs | out-7zip -path c:\newzipfile.zip

----------------

Takes the c:\logs directory and adds it, and it's contents to a new archive called newzipfile.zip

.NOTES
Writen by Jason Morgan
Last Modified 8/5/2013


#>
[cmdletbinding()]
Param
(
    # Accepts a file system object that will get added to the zip file
    [Parameter( 
    ValueFromPipeline=$True, 
    ValueFromPipelineByPropertyName=$True,
    HelpMessage='This Parameter requires a filesystem object as input')]
    [Parameter(ParameterSetName='Default')]
    [PSObject]$inputobject,
    # Set the path to the new or existing zip archive
    [Parameter(
    Mandatory=$true,
    HelpMessage='Path to the zip file, can be a new zip file or an existing archive')]
    [ValidateScript({$_.endswith('.zip')})]
    [Parameter(ParameterSetName='Default')]
    [string]$path,
    #Path to 7za.exe file
    [Parameter(
    Mandatory=$false)]
    [ValidateScript({$_.endswith('7za.exe')})]
    [Parameter(ParameterSetName='Default')]
    [String]$ZipApp = 'C:\ISDps\Configs\7za.exe'
)
Process
{
    $inputobject | foreach {
        Try
            {
                [Array]$arguments = "a", "-tzip", $path, $_.fullname
                Start-Process -FilePath $ZipApp -ArgumentList $arguments -Wait | Out-Null
            }
        Catch
            {
                Write-Error "Unable to add $($_.fullname) to $path"
            }
    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24737694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档