数据库每日自动备份

Posted by Tesla9527 on July 12, 2016

数据库的备份使用Sql Server Job来执行

备份数据库

1
2
3
DECLARE @SQLStatement VARCHAR(2000)
SET @SQLStatement = 'S:\DbTemp\Dfyf.Bpm.bak'
BACKUP DATABASE [Dfyf.Bpm] TO  DISK = @SQLStatement;

压缩数据库到指定目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory("S:\DbTemp\", "S:\DbBackUp\Dfyf.Bpm.bak.zip");
$fileName = 'S:\DbBackUp\Dfyf.Bpm.bak.zip'
$fileObj = get-item $fileName
# Get the date
$DateStamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"
$extOnly = $fileObj.extension

if ($extOnly.length -eq 0) {
   $nameOnly = $fileObj.Name
   rename-item "$fileObj" "$nameOnly-$DateStamp"
   }
else {
   $nameOnly = $fileObj.Name.Replace( $fileObj.Extension,'')
   rename-item "$fileName" "$nameOnly-$DateStamp$extOnly"
   }
Remove-Item S:\DbTemp\Dfyf.Bpm.bak

删除指定目录下创建日期大于某个天数的文件

1
2
3
4
5
6
7
function Remove-FilesCreatedBeforeDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $DateTime, [switch] $DeletePathIfEmpty, [switch] $OutputDeletedPaths, [switch] $WhatIf)
{
    Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.CreationTime -lt $DateTime } |
        ForEach-Object { if ($OutputDeletedPaths) { Write-Output $_.FullName } Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf }
    Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty:$DeletePathIfEmpty -OnlyDeleteDirectoriesCreatedBeforeDate $DateTime -OutputDeletedPaths:$OutputDeletedPaths -WhatIf:$WhatIf
}
Remove-FilesCreatedBeforeDate -Path "C:\Some\Directory" -DateTime ((Get-Date).AddDays(-30)) -DeletePathIfEmpty