Last active
December 26, 2015 20:59
-
-
Save taomaree/7213105 to your computer and use it in GitHub Desktop.
backup SQL Server DBs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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