http://michael.carbenay.info

Scripts Powershell de sauvegarde SQLServer

31 oct. 2006  –  PowerShell | Sql Server  –  0 Commentaires

Bon, pendant que je m'amusais à casser mon serveur, Powershell RC2 est sorti. A l'occasion de cette release, je vous propose deux petites fonctions Powershell :

  • backup-database dbname rootfolder qui lance le backup d'une base (sur le serveur local) SQL Server 2005 dans un dossier
  • backup-alldatabase rootfolder qui fait le tour des bases non-systèmes et lance un backup-database dessus.

function backup-database($dbName, $rootFolder)
{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") 
| out-null [System.IO.Directory]::CreateDirectory($rootFolder) | out-null $_srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "(local)" $_bck=new-object "Microsoft.SqlServer.Management.Smo.Backup" $_bck.Action = 'Database'
$_fil=new-object "Microsoft.SqlServer.Management.Smo.BackupDeviceItem" $_fil.DeviceType='File' $_fil.Name=[System.IO.Path]::Combine($rootFolder, $dbName + "-"
+ [DateTime]::Today.ToString("yyyy-MM-dd")+".bak") $_bck.Devices.Add($_fil) $_bck.Database=$dbName $_bck.SqlBackup($_srv) write-host "Sauvegarde de " $dbName " terminee" } function backup-alldatabase($rootFolder) { [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
| out-null $_srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "(local)" $_srv.Databases |
?{$_.IsSystemObject -eq 0} |
%{backup-database $_.Name $rootFolder} }
Technorati tags: , ,

del.icio.us tags: , ,

Ajouter un commentaire


(Affichera votre icône Gravatar)

biuquote
  • Commentaire
  • Aperçu immédiat
Loading