Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I schedule a daily backup with SQL Server Express? [duplicate]

I'm running a small web application with SQL server express (2005) as backend. I can create a backup with a SQL script, however, I'd like to schedule this on a daily basis. As extra option (should-have) I'd like to keep only the last X backups (for space-saving reasons obviously) Any pointers?

[edit] SQL server agent is unavailable in SQL server express...

like image 508
edosoft Avatar asked Jan 28 '09 13:01

edosoft


People also ask

How do I automate backup in SQL Express?

Create the Backup Job Run SQL Server Management Studio Express. In the tree view, expand Server Objects => New Backup Device. For Device Name, type in a name for your new backup job. For Destination, select the path to store the backups.

How do I automate backup and restore in SQL Server?

To get started, copy the following PowerShell into the editor of your choice and configuring the variables for your purposes. To obtain the logical and physical file names for the source and target databases: From Object Explorer in SQL Server Management Studio: Right click on source database.


2 Answers

You cannot use the SQL Server agent in SQL Server Express. The way I have done it before is to create a SQL Script, and then run it as a scheduled task each day, you could have multiple scheduled tasks to fit in with your backup schedule/retention. The command I use in the scheduled task is:

"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE" -i"c:\path\to\sqlbackupScript.sql"

like image 130
Sam Cogan Avatar answered Oct 18 '22 08:10

Sam Cogan


Eduardo Molteni had a great answer:

  • SQL Server Automated Backups

Using Windows Scheduled Tasks:

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 
like image 28
JohnB Avatar answered Oct 18 '22 09:10

JohnB