Created
August 29, 2012 15:27
-
-
Save webRat/3514353 to your computer and use it in GitHub Desktop.
Windows Batch File for backing up a db
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
@echo off | |
set HOSTNAME= | |
set USERNAME= | |
SET SQLPASSWORD= | |
SET FILENAME= | |
SET BACKUPPATH=D:\Dev\SQL Server 2005 Express\datafiles\backup | |
SET BACKUPSQLSCRIPT=%BACKUPPATH%\dbbackup.sql | |
:: execute backup SQL script | |
sqlcmd -S %HOSTNAME% -U %USERNAME% -P %SQLPASSWORD% -i "%BACKUPSQLSCRIPT%" | |
::7zip the bastard | |
7z a -t7z "%BACKUPPATH%\%FILENAME%_backup-%DATE:~-4%.%DATE:~4,2%.%DATE:~7,2%.7z" "%BACKUPPATH%\*.bak" | |
:: Remove sql bak file | |
del "%BACKUPPATH%\*.bak" | |
:: You can write additional commands here (moving files, etc) |
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
DECLARE @pathName NVARCHAR(512) | |
SET @pathName = 'D:\Dev\SQL Server 2005 Express\datafiles\backup\db_backup_' + Convert(varchar(8), GETDATE(), 112) + '.bak' | |
BACKUP DATABASE [database_name_here] TO DISK = @pathName WITH NOFORMAT, NOINIT, NAME = N'db_backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment