Skip to content

Instantly share code, notes, and snippets.

@webRat
Created August 29, 2012 15:27
Show Gist options
  • Save webRat/3514353 to your computer and use it in GitHub Desktop.
Save webRat/3514353 to your computer and use it in GitHub Desktop.
Windows Batch File for backing up a db
@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)
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