Skip to content

Instantly share code, notes, and snippets.

@taomaree
Last active December 26, 2015 20:59
Show Gist options
  • Save taomaree/7213105 to your computer and use it in GitHub Desktop.
Save taomaree/7213105 to your computer and use it in GitHub Desktop.
backup SQL Server DBs
# run first by administrator :
# set-executionpolicy remotesigned
$timestr=get-date -uformat "%Y-%m-%d-%H-%M-%S"
$tsql=""
$prefix="D:\sqlbackup\"
$filepath=$prefix + $timestr + "\"
if (! ( Test-Path $filepath)) {mkdir $filepath }
#if (! (Test-Path D:\sqlbackuptsql) ) {mkdir D:\sqlbackuptsql }
function backup_mssql($dbname) {
$tsql += "BACKUP DATABASE " + $dbname +" TO DISK = N'" + $filepath + $dbname + "_FULL_" + $timestr+".bak' WITH CHECKSUM, COMPRESSION, BUFFERCOUNT = 50, MAXTRANSFERSIZE = 4194304;"
#$tsql >> ("D:\sqlbackuptsql\" + $timestr + ".sql")
sqlcmd -Q $tsql
return $tsql
}
$array="[DB1]","[DB2]","[DB3]","[DB4]","[DB5]","[DB6]","[DB7]"
foreach ($n in $array) {
backup_mssql($n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment