我正在使用SQL server express(2005)作为后端运行一个小型Web应用程序。我可以使用SQL脚本创建备份,但是,我想每天安排这个。作为额外的选项(应该)我只想保留最后的X备份(显然是为了节省空间的原因)任何指针?
[edit] SQL Server代理在SQL server express中不可用...
答案 0 :(得分:52)
您无法在SQL Server Express中使用SQL Server代理。 我之前完成它的方法是创建一个SQL脚本,然后每天将其作为计划任务运行,您可以有多个计划任务以适应您的备份计划/保留。我在计划任务中使用的命令是:
“C:\ Program Files \ Microsoft SQL Server \ 90 \ Tools \ Binn \ SQLCMD.EXE”-i“c:\ path \ to \ sqlbackupScript.sql”
答案 1 :(得分:49)
使用Windows计划任务:
在批处理文件中
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -S
(local)\SQLExpress -i D:\dbbackups\SQLExpressBackups.sql
在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
答案 2 :(得分:7)
只需使用this脚本动态备份服务器上的所有数据库。然后根据文章创建批处理文件。创建两个批处理文件很有用,一个用于完全备份,一个用于差异备份。然后在任务计划程序中创建两个任务,一个用于完整,一个用于差异。
-- // Copyright © Microsoft Corporation. All Rights Reserved.
-- // This code released under the terms of the
-- // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_BackupDatabases] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Microsoft
-- Create date: 2010-02-06
-- Description: Backup Databases for SQLExpress
-- Parameter1: databaseName
-- Parameter2: backupType F=full, D=differential, L=log
-- Parameter3: backup file location
-- =============================================
CREATE PROCEDURE [dbo].[sp_BackupDatabases]
@databaseName sysname = null,
@backupType CHAR(1),
@backupLocation nvarchar(200)
AS
SET NOCOUNT ON;
DECLARE @DBs TABLE
(
ID int IDENTITY PRIMARY KEY,
DBNAME nvarchar(500)
)
-- Pick out only databases which are online in case ALL databases are chosen to be backed up
-- If specific database is chosen to be backed up only pick that out from @DBs
INSERT INTO @DBs (DBNAME)
SELECT Name FROM master.sys.databases
where state=0
AND name=@DatabaseName
OR @DatabaseName IS NULL
ORDER BY Name
-- Filter out databases which do not need to backed up
IF @backupType='F'
BEGIN
DELETE @DBs where DBNAME IN ('tempdb','Northwind','pubs','AdventureWorks')
END
ELSE IF @backupType='D'
BEGIN
DELETE @DBs where DBNAME IN ('tempdb','Northwind','pubs','master','AdventureWorks')
END
ELSE IF @backupType='L'
BEGIN
DELETE @DBs where DBNAME IN ('tempdb','Northwind','pubs','master','AdventureWorks')
END
ELSE
BEGIN
RETURN
END
-- Declare variables
DECLARE @BackupName varchar(100)
DECLARE @BackupFile varchar(100)
DECLARE @DBNAME varchar(300)
DECLARE @sqlCommand NVARCHAR(1000)
DECLARE @dateTime NVARCHAR(20)
DECLARE @Loop int
-- Loop through the databases one by one
SELECT @Loop = min(ID) FROM @DBs
WHILE @Loop IS NOT NULL
BEGIN
-- Database Names have to be in [dbname] format since some have - or _ in their name
SET @DBNAME = '['+(SELECT DBNAME FROM @DBs WHERE ID = @Loop)+']'
-- Set the current date and time n yyyyhhmmss format
SET @dateTime = REPLACE(CONVERT(VARCHAR, GETDATE(),101),'/','') + '_' + REPLACE(CONVERT(VARCHAR, GETDATE(),108),':','')
-- Create backup filename in path\filename.extension format for full,diff and log backups
IF @backupType = 'F'
SET @BackupFile = @backupLocation+REPLACE(REPLACE(@DBNAME, '[',''),']','')+ '_FULL_'+ @dateTime+ '.BAK'
ELSE IF @backupType = 'D'
SET @BackupFile = @backupLocation+REPLACE(REPLACE(@DBNAME, '[',''),']','')+ '_DIFF_'+ @dateTime+ '.BAK'
ELSE IF @backupType = 'L'
SET @BackupFile = @backupLocation+REPLACE(REPLACE(@DBNAME, '[',''),']','')+ '_LOG_'+ @dateTime+ '.TRN'
-- Provide the backup a name for storing in the media
IF @backupType = 'F'
SET @BackupName = REPLACE(REPLACE(@DBNAME,'[',''),']','') +' full backup for '+ @dateTime
IF @backupType = 'D'
SET @BackupName = REPLACE(REPLACE(@DBNAME,'[',''),']','') +' differential backup for '+ @dateTime
IF @backupType = 'L'
SET @BackupName = REPLACE(REPLACE(@DBNAME,'[',''),']','') +' log backup for '+ @dateTime
-- Generate the dynamic SQL command to be executed
IF @backupType = 'F'
BEGIN
SET @sqlCommand = 'BACKUP DATABASE ' +@DBNAME+ ' TO DISK = '''+@BackupFile+ ''' WITH INIT, NAME= ''' +@BackupName+''', NOSKIP, NOFORMAT'
END
IF @backupType = 'D'
BEGIN
SET @sqlCommand = 'BACKUP DATABASE ' +@DBNAME+ ' TO DISK = '''+@BackupFile+ ''' WITH DIFFERENTIAL, INIT, NAME= ''' +@BackupName+''', NOSKIP, NOFORMAT'
END
IF @backupType = 'L'
BEGIN
SET @sqlCommand = 'BACKUP LOG ' +@DBNAME+ ' TO DISK = '''+@BackupFile+ ''' WITH INIT, NAME= ''' +@BackupName+''', NOSKIP, NOFORMAT'
END
-- Execute the generated SQL command
EXEC(@sqlCommand)
-- Goto the next database
SELECT @Loop = min(ID) FROM @DBs where ID>@Loop
END
批处理文件可能如下所示:
sqlcmd -S localhost\myDB -Q "EXEC sp_BackupDatabases @backupLocation='c:\Dropbox\backup\DB\', @backupType='F'" >> c:\Dropbox\backup\DB\full.log 2>&1
和
sqlcmd -S localhost\myDB -Q "EXEC sp_BackupDatabases @backupLocation='c:\Dropbox\backup\DB\', @backupType='D'" >> c:\Dropbox\backup\DB\diff.log 2>&1
这种方法的优点是,如果添加新数据库或删除数据库,您不需要更改任何内容,甚至不需要在脚本中列出数据库。对于具有一个数据库的服务器,JohnB的答案更好/更简单,这种方法更适合多数据库服务器。
答案 3 :(得分:4)
MSSQLTips的人有一些非常有用的文章,与此最相关的是“Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files”
基本方法是使用Windows任务计划程序设置两个任务。一个任务运行TSQL脚本,该脚本为所有MSSQL数据库(TEMPDB除外)生成单独的备份文件,其中数据库名称和文件名中的日期/时间戳记到指定目录中。第二个任务运行一个VBScript脚本,该脚本通过该目录并删除所有超过3天的.BAK扩展名的文件。
两个脚本需要对您的环境进行微小编辑(路径,保留这些数据库转储的时间),但非常接近于运行。
请注意,如果您对这些或目录权限不熟悉,可能存在安全隐患,因为它们是纯文本文件,需要以某种级别的权限运行。不要马虎。
答案 4 :(得分:3)
我们使用了以下组合:
Cobian Backup用于安排/维护
这两个都是免费的。该过程是将ExpressMaint脚本作为Cobian“备份前”事件进行备份。我通常会让它覆盖以前的备份文件。 Cobian然后从中取出一个zip / 7zip并将这些存档到备份文件夹。在Cobian中,您可以指定要保留的完整副本数量,进行多个备份周期等。
ExpressMaint命令语法示例:
expressmaint -S HOST\SQLEXPRESS -D ALL_USER -T DB -R logpath -RU WEEKS -RV 1 -B backuppath -BU HOURS -BV 3
答案 5 :(得分:0)
您可以在服务器对象中创建备份设备,我们说
BDTEST
然后创建一个包含以下命令的批处理文件
sqlcmd -S 192.168.1.25 -E -Q "BACKUP DATABASE dbtest TO BDTEST"
让我们说出名字
BACKUP.BAT
然后你可以打电话
BACKUP.BAT
在任务计划程序中根据您的方便