7

現在、ファイルをバックアップするための 1 行のバッチ ファイルがあります。ファイルをバックアップする必要があるときは、手動で実行します。追加したいのは現在の日付だけです。ここに私が持っているものがあります:

xcopy /W /Y ACTIVE.DB ACTIVE.DB.BACKUP

コピー先ファイルは単に ACTIVE.DB.BACKUP.YYYYMMDD にする必要があります。Windows エクスプローラーからスクリプトをダブルクリックして xcopy を実行できるようにするスクリプトを作成するにはどうすればよいですか?

4

3 に答える 3

13

Copy-Itemを使用してこれを実行できることを指摘するだけです。例:

Set-Location $path
Copy-Item ACTIVE.DB "ACTIVE.DB.$(get-date -f yyyyMMdd)" -Force -Confirm

堅牢にする場合は、を使用しますrobocopy.exe

于 2010-09-08T22:10:55.430 に答える
5

[datetime]::now次のように、PowerShell のファイル名にフォーマットされた を埋め込むことで、ファイル名をカスタマイズできます。

xcopy /W /Y ACTIVE.DB "ACTIVE.DB.BACKUP.$([datetime]::now.ToString('yyyy-MM-dd'))"

行が忙しくて保守できない場合は、複数の行にリファクタリングできます。

$now = [datetime]::now.ToString('yyyy-MM-dd')
xcopy /W /Y ACTIVE.DB "ACTIVE.DB.BACKUP.$now"

ダブルクリックで実行するために、私は通常、次のように PowerShell コマンドを実行するバッチ ファイルを作成します。

自動実行用に PowerShell スクリプトを設定する

于 2010-09-08T20:41:27.937 に答える
0

今月、Powershell で日次/週次/月次/四半期/年次のバックアップ スクリプトを作成しました。

この DWMQY バックアップ シナリオでは、ソース フォルダーを日付付きの zip ファイルに圧縮し、次の zip ファイルを保持します。

  • 過去 7 日間
  • 4週間(毎週金曜日)
  • 6ヶ月(毎月最終金曜日)
  • 4 四半期 (四半期の最後の月)
  • 2 年 (年の最後の四半期)。

スケジュールされたタスクとして毎日実行され、Microsoft OneDrive のローカル フォルダーでもあるターゲット フォルダーに zip が配置されるため、zip もリモートで OneDrive サーバーに同期されます。これらの古い (金曜日以外の毎日または最後の DWMQY 以外の) zip は、リモート同期されていないフォルダーに移動されます。

今日は 2016 年 3 月 5 日で、次の zip がターゲット フォルダーにあるはずです。

  • 7 日: 160304-160229-160227
  • 4 週間: 160304、160226、160219、160212
  • 6 か月: 160226、160129、161225、151127、151025、150925
  • 4 四半期: 151225、150925、150626、150327
  • 2 年: 151225、141226

したがって、23 個の zip が存在します (実際には、DWMQY 間の重複があるため、これよりも少なくなります)。ファイルは 250 個のテキスト ドキュメントであり、圧縮後は 0.4 GB であるため、合計で 23*0.4 = 9.2 GB になり、OneDrive の無料の 15 GB クォータよりも少なくなります。

大規模なソース データの場合は、最大 16 ミル TB の zip サイズを提供する 7-zip を使用できます。zip の代わりにフォルダーを直接バックアップする場合は、試していません。今のジップウェイから乗り換え可能な手順だと思います。

# Note: there are following paths:
# 1. source path: path to be backed up. 
# 2. target path: current zips stored at, which is also a remote-sync pair's local path.
# 3. moved-to path: outdated zips to be moved in this non-sync'able location.
# 4. temp path: to copy the source file in to avoid zip.exe failing of compressing them if they are occupied by some other process.
# Function declaration
. C:\Source\zipSaveDated\Functions.ps1 
# <1> Zip data
$sourcePath = '\\remoteMachine1\c$\SourceDocs\*'
$TempStorage = 'C:\Source\TempStorage'
$enddate = (Get-Date).tostring("yyyyMMdd")
$zipFilename = '\\remoteMachine2\d$\DailyBackupRemote\OneDrive\DailyBackupRemote_OneDrive\' + $enddate + '_CompanyDoc.zip'
Remove-Item ($TempStorage + '\*') -recurse -Force
Copy-Item $sourcePath $TempStorage -recurse -Force

Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory($TempStorage, $zipFilename) 

# <2> Move old files
$SourceDir = "\\remoteMachine2\d$\DailyBackupRemote\OneDrive\DailyBackupRemote_OneDrive"
$DestinationDir = "\\remoteMachine2\d$\DailyBackupRemote\bak" # to store files moved out of the working folder (OneDrive)
$KeepDays = 7
$KeepWeeks = 4
$KeepMonths = 6
$KeepQuarters = 4
$KeepYears = 2
# <2.1>: Loop files
$Directory = $DestinationDir
if (!(Test-Path $Directory))
{
    New-Item $directory -type directory -Force
}
$files = get-childitem $SourceDir *.*
foreach ($file in $files) 
{ # L1
    # daily removal will not remove weekly copy, 7 
    If($file.LastWriteTime -lt (Get-Date).adddays(-$KeepDays).date  `
        -and $file.LastWriteTime.DayOfWeek -NotMatch "Friday"  `
        )
        {
        Move-Item $file.fullname $Directory -force
        }
} # L1 >>
$files = get-childitem $SourceDir *.*
foreach ($file in $files) 
{ # L1
    # weekly removal will not remove monthly copy, 4
    If($file.LastWriteTime -lt (Get-Date).adddays(-$KeepWeeks * 7).date  `
        -and (Get-LastFridayOfMonth ($file.LastWriteTime)).Date.ToString("yyyyMMdd") -NotMatch $file.LastWriteTime.Date.ToString("yyyyMMdd")
        )
        {
        Move-Item $file.fullname $Directory -force
        }
} # L1 >>
$files = get-childitem $SourceDir *.*
foreach ($file in $files) 
{ # L1
    # monthly removal will not remove quarterly copy, 6
    If($file.LastWriteTime.Month -lt ((Get-Date).Year - $file.LastWriteTime.Year) * 12 + (Get-Date).Month - $KeepMonths `
        -and $file.LastWriteTime.Month -NotIn 3, 6, 9, 12
        )
        {
        Move-Item $file.fullname $Directory -force
        }
} # L1 >>
$files = get-childitem $SourceDir *.*
foreach ($file in $files) 
{ # L1
    # quarterly removal will not remove yearly copy, 4
    If($file.LastWriteTime.Month -lt ( (Get-Date).Year - $file.LastWriteTime.Year) * 12 + (Get-Date).Month - $KeepQuarters * 3 `
        -and $file.LastWriteTime.Month -NotIn 12
        )
        {
        Move-Item $file.fullname $Directory -force
        }
} # L1 >>
$files = get-childitem $SourceDir *.*
foreach ($file in $files) 
{ # L1
    # yearly removal will just go straight ahead. 2
    If($file.LastWriteTime.Year -lt (Get-Date).Year - $KeepYears )
        {
        Move-Item $file.fullname $Directory -force
        }
} # L1 >>


<Functions.ps1>
function Get-TimesResult3 
    {
    Param ([int]$a,[int]$b)
    $c = $a * $b
    Write-Output $c
    }

function Get-Weekday {
    param(
        $Month = $(Get-Date -format 'MM'),
        $Year = $(Get-Date -format 'yyyy'),
        $Days = 1..5
    )
$MaxDays = [System.DateTime]::DaysInMonth($Year, $Month)
1..$MaxDays | ForEach-Object {
        Get-Date -day $_ -Month $Month -Year $Year |
          Where-Object { $Days -contains $_.DayOfWeek }  
    }
}

function Get-LastFridayOfMonth([DateTime] $d) {    
    $lastDay = new-object DateTime($d.Year, $d.Month, [DateTime]::DaysInMonth($d.Year, $d.Month))
    $diff = ([int] [DayOfWeek]::Friday) - ([int] $lastDay.DayOfWeek)

    if ($diff -ge 0) {
        return $lastDay.AddDays(- (7-$diff))
    }
    else {
        return $lastDay.AddDays($diff)
    }    
}
于 2016-03-24T20:27:44.157 に答える