Skip to content

Instantly share code, notes, and snippets.

@lyhcode
Created December 20, 2017 12:47
Show Gist options
  • Save lyhcode/9897687ff7221e12f30ef8dbb653dc27 to your computer and use it in GitHub Desktop.
Save lyhcode/9897687ff7221e12f30ef8dbb653dc27 to your computer and use it in GitHub Desktop.
PowerShell script for sql server database backup with timestamp file name.
$dt = Get-Date -Format yyyyMMddHHmmss
$dbname = 'dfi'
$username = 'DFINewWebsiteSI'
#read-host -assecurestring | convertfrom-securestring | out-file sqlsecure.txt
$password = Get-Content sqlsecure.txt
$secpasswd = $password | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username,$secpasswd
$bakfile = "C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup\dfi_$($dt).bak"
Write-Output "backup database to disk file = $bakfile"
Backup-SqlDatabase -ServerInstance DFINW -Database $dbname -Credential $credential -BackupFile $bakfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment