Figer's Technology Consulting | Save Money! Schedule backups on free SQL Server Express

Save Money! Schedule backups on free SQL Server Express



When we're bootstrapping our startups we try to save money and we lean towards SQL Server Express (lacks SQL agent to schedule backups) because it's free until the idea is proven / funded and it justifies the enterprise edition. We of course still need to make sure our data is securely backed up be it onsite with a client or as an Amazon EC2 instance spun up in the cloud.


Here is how we solved this problem.

-------------------------------------------------


Add the exact line below and save it as SQLBackup.bat


"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.exe" -S localhost -i "D:\SQLBackup\SQLBackup.sql" >> log.txt


Add all the SQL below and save it as SQLBackup.sql (update server folder location D:\SQLBackup\ and database name SEA2014 as needed, this will create a backup for each day of the week by name and overwrite if the file exists so you'll get the last 7 days of backups). Make sure the folder location has security permission to be accessed, test running the .bat from the command prompt to test.


DECLARE @dest nvarchar(255)

SET @dest = N'D:\SQLBackup\SEA2014_' + CAST(DATEPART(weekday, GETDATE()) AS nvarchar(1)) + '.bak'

BACKUP DATABASE [SEA2014] TO DISK = @dest WITH NOFORMAT, NOINIT, NAME = N'SEA2014-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10

GO


DECLARE @dest nvarchar(255)

SET @dest = N'D:\SQLBackup\SEA2014_' + CAST(DATEPART(weekday, GETDATE()) AS nvarchar(1)) + '.bak'

declare @backupSetId as int

select @backupSetId = position from msdb..backupset where database_name=N'SEA2014' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'SEA2014' )

if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''SEA2014'' not found.', 16, 1) end

RESTORE VERIFYONLY FROM DISK = @dest WITH FILE = @backupSetId, NOUNLOAD, NOREWIND

GO


Finally in Windows Task Scheduler call the .bat file on a weekly basis.

Comments are closed