30

What are the recommendations of software products for creating automated backups of SQL Server 2008 databases?

The backup should happen without taking the database offline/detatching.

blu
  • 12,905
  • 20
  • 70
  • 106

3 Answers3

73

If you are using SQL Server Express, you won't find a UI to run periodic backups.
In this case you have to run a batch using Windows Scheduled Tasks or something similar.

Don't forget to use a user with enough privileges to access SQL Server.

In the batch file

"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -S 
(local)\SQLExpress -i D:\dbbackups\SQLExpressBackups.sql

In SQLExpressBackups.sql

BACKUP DATABASE MyDataBase1 TO  DISK = N'D:\DBbackups\MyDataBase1.bak' 
WITH NOFORMAT, INIT,  NAME = N'MyDataBase1 Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

BACKUP DATABASE MyDataBase2 TO  DISK = N'D:\DBbackups\MyDataBase2.bak' 
WITH NOFORMAT, INIT,  NAME = N'MyDataBase2 Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

GO
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
  • Remember to set backup location within the SQL installation folders. I was getting: "SQL Server Operating system error 5: 5(Access is denied.)” because I set my backup to go outside the "official" SQL server installation folder (SQL Server database engine service account must have permissions to read/write in the new folder). Alternatively, look through [this solution](http://stackoverflow.com/questions/18286765/sql-server-operating-system-error-5-5access-is-denied) – full_prog_full Feb 24 '16 at 15:15
  • Is there a way to append a timestamp to the name of the backup file so it doesn't get overwrite everytime? – rodsarria Jul 11 '16 at 19:11
  • Have NOREWIND and NOUNLOAD options any effect for a backup to DISK? I don't think so since they are "Tape options". – jacktric Jul 18 '17 at 09:52
17

I would recommend just creating a maintenance plan in SQL Server to handle the backups, it can be configured to backup to a specified location at specified times, without taking the databases offline, and will handle your incremental backup cleanup.

http://msdn.microsoft.com/en-us/library/ms189715.aspx

cmsjr
  • 56,771
  • 11
  • 70
  • 62
11

I struggled with this for a while because it wasn't obvious how to work a regime that produced files with different names so that one run didn't over write the other. In the end it 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. The corresponding sql is

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

The discovery of "FORFILES" command to purge older files was the key finding for me.

The transaction logs equivalents are

:: 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, which is why I took the backups onto the C: drive.

The Daily backup is entered as a job in the Windows Task Scheduler to run daily at 4:00am. The transaction log backup is set to run daily at 8:00am with a repeat every 4 hours finishing after 13 hours (causing it to run at 8am Midday, 4pm and 8pm every day)

akc42
  • 4,893
  • 5
  • 41
  • 60