I struggled with this for a while, because it was not obvious how to work with a mode that created files with different names, so that one launch would not overwrite another. In the end, he created the following Windows batch file
:: Daily Backup of SQLSERVER databases :: AKC 30 Apr 2011 :: :: Set environment variables SET SQLCMDPASSWORD=xxxxxx SET BACKUPDIR=C:\backups\db\ SET SCRIPTDIR=D:\Public\DB\batch_scripts\ :: Issue backup commands from a sql script SQLCMD -U a_backup -S SERVER\SQLEXPRESS -i %SCRIPTDIR%daily_backup.sql :: Tidy Up Old Backup Files (keep for 5 days) FORFILES /P %BACKUPDIR% /S /M "*.bak" /D -5 /C "cmd /c del @path"
where a_backup is my sqlserver login with backup privileges. Corresponding sql
DECLARE @thistime nvarchar(25); DECLARE @filename nvarchar(255); SET @thistime = CONVERT(nvarchar,GETDATE(),126); SET @filename = "$(BACKUPDIR)" + N'PASL' + SUBSTRING(@thistime,1,10) + N'_DB.bak'; BACKUP DATABASE DB_live TO DISK = @FILENAME WITH INIT; GO
Opening the FORFILES command to clean old files was key to me.
Transaction Log Equivalents
:: Transaction Log Backups of SQLSERVER databases :: AKC 30 Apr 2011 :: Run at reasonably spread out times of the day :: Set environment variables SET SQLCMDPASSWORD=xxxxxx SET BACKUPDIR=C:\backups\db\ SET SCRIPTDIR=D:\Public\DB\batch_scripts\ :: Issue backup commands from a sql script SQLCMD -U a_backup -S SERVER\SQLEXPRESS -i %SCRIPTDIR%tlog_backup.sql
with sql file
DECLARE @thistime nvarchar(25); DECLARE @filename nvarchar(255); SET @thistime = CONVERT(nvarchar,GETDATE(),126); SET @filename = "$(BACKUPDIR)" + N'PASL' + SUBSTRING(@thistime,1,10) + SUBSTRING(@thistime,11,3) + N'_LOG.bak'; BACKUP LOG DB_live TO DISK = @FILENAME WITH INIT; GO
I should note that the database files are on my D: drive, so I took backups to C :.
A daily backup is entered as a task in the Windows Task Scheduler to run daily at 4 a.m. The transaction log backup is started daily at 8:00 a.m. and repeated every 4 hours after 13:00 (making it work at 8:00 p.m., from 4:00 p.m. to 8:00 p.m. every day).
akc42 May 2 '11 at 7:40 a.m. 2011-02-02 07:40
source share