nakaji
5/24/2016 - 9:52 AM

SQLServerの全データベースバックアップ.ps1


Invoke-Sqlcmd -Query "exec sp_databases" | ?{ -not (@("master","tempdb","msdb","model") -contains $_.Item(0)) } | %{
    $datebaseName = $_.Item(0)
    
    Write-Host ("■ {0}のフルバックアップを実行します" -f $datebaseName)
    
    $backupSql=@"
BACKUP DATABASE {0}
   TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\{0}.Bak'
   WITH FORMAT,
   MEDIANAME = 'SQLServerBackups',
   NAME = 'Full Backup of {0}'
go
exit
"@ -f $datebaseName

    sqlcmd -q $backupSql
    
    Write-Host ""
}