如何使用 SQL Server Express 安排每日备份?
我正在运行一个小型 Web 应用程序,以 SQL Server Express (2005) 作为后端。 我可以使用 SQL 脚本创建备份,但是我想每天安排此操作。 作为额外的选项(应该有),我想只保留最后的 X 个备份(显然是出于节省空间的原因)有什么指示吗?
[编辑] SQL Server 代理在 SQL Server Express 中不可用...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Eduardo Molteni 给出了一个很好的答案:
Eduardo Molteni had a great answer:
您无法在 SQL Server Express 中使用 SQL Server 代理。
我之前完成的方法是创建一个 SQL 脚本,然后每天将其作为计划任务运行,您可以有多个计划任务来适应您的备份计划/保留。 我在计划任务中使用的命令是:
"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE" -i"c:\path\to\sqlbackupScript.sql"
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"
只需使用此脚本即可动态备份服务器上的所有数据库。 然后根据文章创建批处理文件。 创建两个批处理文件很有用,一个用于完整备份,一个用于差异备份。 然后在任务计划程序中创建两个任务,一项用于完整任务,一项用于差异任务。
批处理文件可以如下所示:
这种方法的优点
是,如果添加新数据库或删除数据库,您不需要更改任何内容,甚至不需要在脚本中列出数据库。 JohnB 的回答对于具有一个数据库的服务器来说更好/更简单,这种方法更适合多数据库服务器。
Just use this script to dynamically backup all databases on the server. Then create batch file according to the article. It is usefull to create two batch files, one for full backup a and one for diff backup. Then Create two tasks in Task Scheduler, one for full and one for diff.
And batch file can look like this:
and
The advantage of this method is that you don't need to change anything if you add new database or delete a database, you don't even need to list the databases in the script. Answer from JohnB is better/simpler for server with one database, this approach is more suitable for multi database servers.
MSSQLTips 的人员有一些非常有用的文章,与此最相关的一篇是“自动化 SQL Server 2005 Express 备份和删除旧备份文件”
基本方法是使用 Windows 任务计划程序设置两个任务。 其中一项任务运行一个 TSQL 脚本,为所有 MSSQL 数据库(TEMPDB 除外)生成单独的备份文件,并将数据库名称和文件名中的日期/时间戳保存到指定目录中。 第二个任务运行一个 VBScript 脚本,该脚本遍历该目录并删除超过 3 天的所有扩展名为 .BAK 的文件。
这两个脚本都需要针对您的环境进行少量编辑(路径、保留这些数据库转储的时间),但非常接近即插即用。
请注意,如果您不小心使用这些文件或目录权限,则可能会产生安全隐患,因为它们是纯文本文件,需要以某种级别的权限运行。 不要马虎。
The folks at MSSQLTips have some very helpful articles, the one most relevant for this is "Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files"
The basic approach is to set up two tasks using the Windows Task Scheduler. One task runs a TSQL script that generates separate backup files for all MSSQL databases (except TEMPDB) with the database name and a date/time stamp in the file name into a specified directory. The second task runs a VBScript script that goes through that directory and deletes all files with a .BAK extension that are more than 3 days old.
Both scripts require minor editing for your environment (paths, how long to keep those database dumps) but are very close to drop-in-and-run.
Note that there are possible security implications if you're sloppy with these or with directory permissions, since they are plain text files that will need to run with some level of privilege. Don't be sloppy.
我们使用了以下组合:
Cobian Backup 进行调度/维护
< a href="http://www.codeplex.com/ExpressMaint" rel="nofollow noreferrer">ExpressMaint 用于备份
这两个都是免费的。 该过程是编写 ExpressMaint 脚本以将备份作为 Cobian“备份之前”事件。 我通常让它覆盖以前的备份文件。 然后,Cobian 从中取出 zip/7zip 并将其存档到备份文件夹中。 在 Cobian 中,您可以指定要保留的完整副本的数量、进行多个备份周期等。
ExpressMaint 命令语法示例:
We have used the combination of:
Cobian Backup for scheduling/maintenance
ExpressMaint for backup
Both of these are free. The process is to script ExpressMaint to take a backup as a Cobian "before Backup" event. I usually let this overwrite the previous backup file. Cobian then takes a zip/7zip out of this and archives these to the backup folder. In Cobian you can specify the number of full copies to keep, make multiple backup cycles etc.
ExpressMaint command syntax example:
您可以在服务器对象中创建备份设备,比方说
,然后创建一个包含以下命令的批处理文件,
让我们说出名称
然后你可以调用
根据您的方便在任务计划程序中
You can create a backup device in server object, let us say
and then create a batch file contain following command
let us say with name
then you can call
in task scheduler according to your convenience