2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

大きいものをまるっと圧縮してバックアップする(Windows)

Last updated at Posted at 2019-08-29
  • このディレクトリ以下をすべて圧縮して取っておきたい!
  • でも合計サイズが大きすぎてzipできない!
  • 時間がかかってもいいからシンプルにやりたい!

こんなときは7zipが便利。
さらにちょくちょくやるから実行ファイルにしておきたい。
そこで簡単なPowerShellスクリプトを作りました。

7zipをコマンドラインで使う

dllを必要としないスタンドアローンな7zipコマンドラインツール7za.exeは、通常のパッケージとは別にダウンロードすることになります。
https://www.7-zip.org/download.html
上記の7-Zip Extra: standalone console version, 7z DLL, Plugin for Far Managerと書かれているものに含まれている。

この7za.exeをコマンドラインで呼び出す形で行きます。
詳しい使い方は公式ガイドを参照のこと

今回下で使ったのは圧縮コマンドのみ

"7za.exe" a -r c:\path\to\Dist.7z
  • -r はrecoursiveオプション
  • カレントにある全てのファイルを再帰的に c:\path\to\Dist.7z に圧縮する
  • tarコマンドで言うところの-Cオプションに相等するものがないので、この上でSet-Location(cd)している

スクリプト

  • このスクリプトと7za.exeは同じディレクトリにある必要がある
  • 実行するとスクリプトと同じディレクトリにYYYY_MM_DD_bak.7zという名前の7zipバックアップができる
  • 3以上バックアップがあると古いものから消していく
  • できたバックアップを解凍すると、$SourceDir(C:\path\to\Source)の中身がまるっと手に入る
7zBackup.ps1
# [7za.exe](https://sevenzip.osdn.jp/)を使ってディレクトリをbackup

# Set the properties
$7za = $PSScriptRoot+"\7za.exe"
$SourceDir = "C:\checkout\koba"
$Dist = $PSScriptRoot + (Get-Date -UFormat "\%Y_%m_%d_bak.7z")
$BackupMaxNum = 3

# Stop the apps using SourcePath

# backup
Set-Location $SourceDir
Invoke-Expression "$7za a -r $Dist"
Set-Location $PSScriptRoot

# delete old backup
$counter = $BackupMaxNum
Get-ChildItem $PSScriptRoot -Filter "*_bak.7z" | `
Sort-Object -Property LastWriteTime -Descending | `
ForEach-Object -Process {
	Write-Output "in"
	if($counter -gt 0){
        $counter--
    }
    else{
        Remove-Item $_.FullName
    }
}

# Restart apps
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?