Answers:
注意:首先在
D:
驱动器上创建一个文件夹。(例如D:\User_DataBackup\
)
步骤1:创建下面给出的过程。
Create PROCEDURE [dbo].[UserDataBaseBackUp]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = 'D:\User_DataBackup\' -- as same as your created folder'
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),104)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb','ReportServer','ReportServerTempDB')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
END
步骤2:执行上述步骤。
EXEC [UserDataBaseBackUp]
您也可以安排此过程。对该程序进行了测试,希望会对您有所帮助。
制定维护计划
SQL Server具有此惊人的功能,它将为您创建脚本和作业
第1步:
右键点击Maintenance Plan
下Management
步骤2:为
您的计划命名
步骤3:
选择数据库备份任务
步骤4:
配置任务,选择数据库,文件夹位置,备份类型(完整,差异,事务日志)连接等。
步骤5:
配置作业计划
我建议为系统数据库和您的用户数据库制定一个单独的计划,以避免出现任何类型的问题
帮助链接:
我知道,使用SSMS维护许多数据库可能会有些混乱,但这很简单。
我可以推荐一种更有效的方法,即使用Ola Hallengren的维护脚本。它非常酷,非常有效。您不仅可以备份所有数据库,还可以执行所有类型的维护过程。
例如,您可以使用以下命令来备份所有数据库,对其进行压缩并使用您选择的证书对它们进行加密(它们都是可选的,并且加密和压缩在SQL Server 2005上将不起作用,但我认为它将可以显示Ola脚本的灵活性和强度):
EXECUTE dbo.DatabaseBackup @Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@Compress = 'Y',
@Encrypt = 'Y',
@EncryptionAlgorithm = 'AES_256',
@ServerCertificate = 'MyCertificate'
根据JP的答案,我添加了一个参数来传递目标目录(并且很可能会添加更多选项):
Create PROCEDURE [dbo].[UserDataBaseBackUp] (
@OutputDir varchar(255)
) AS
BEGIN
SET NOCOUNT ON;
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = @OutputDir
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),104)
PRINT 'Starting Backups'
DECLARE db_cursor CURSOR FOR
SELECT name FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb','ReportServer','ReportServerTempDB')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0 BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
PRINT 'Starting Backup For ' + @name
BACKUP DATABASE @name TO DISK = @fileName WITH FORMAT
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
PRINT 'Backups Finished'
END
GO
因此:
EXEC UserDataBaseBackUp @OutputDir = 'F:\Backups\SQL Databases\'
您可以像这样使用SELECT语句或CURSOR:
DECLARE @PathForBackUp VARCHAR(255)
SET @PathForBackUp = 'F:\Backup\User DB\'
SELECT 'BACKUP DATABASE [' + name + '] TO DISK = N''' + @PathForBackUp + '' + name + '.bak''
WITH NOFORMAT, NOINIT, NAME = N''' + name + '_FullBackUp'', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 5'
FROM sys.databases
WHERE database_id > 4
要么
DECLARE @DBName VARCHAR(255)
DECLARE @PathForBackUp VARCHAR(255)
DECLARE @FileName VARCHAR(255)
DECLARE @DateFile VARCHAR(255)
DECLARE @SQL NVARCHAR(2048)
SET @PathForBackUp = 'F:\Backup\User DB\'
SET @DateFile = REPLACE(REPLACE(CONVERT(VARCHAR(20),GETDATE(),120) ,' ','T'), ':','')
DECLARE BACKUPING CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases WHERE dbid > 4
OPEN BACKUPING
FETCH NEXT FROM BACKUPING INTO @DBName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @FileName = @PathForBackUp + @DBName + '_' + @DateFile + '.BAK'
SET @SQL = 'BACKUP DATABASE '+@DBName+ ' TO DISK = '''+@FileName+''' WITH COMPRESSION '
PRINT @SQL
EXECUTE sp_executesql @sql
FETCH NEXT FROM BACKUPING INTO @DBName
END
CLOSE BACKUPING
DEALLOCATE BACKUPING
您还可以按Buck Woody的“ 使用PowerShell备份所有用户数据库”中所示使用PowerShell:
# Performs a Full backup followed by a transaction log backup on all user databases
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'BWOODY1\SQL2K8'
$bkdir = $s.Settings.BackupDirectory
$dbs = $s.Databases
$dbs | foreach-object {
$db = $_
if ($db.IsSystemObject -eq $False -and $db.IsMirroringEnabled -eq $False) {
$dbname = $db.Name
$dt = get-date -format yyyyMMddHHmmss
$dbbk = new-object ('Microsoft.SqlServer.Management.Smo.Backup')
$dbbk.Action = 'Database'
$dbbk.BackupSetDescription = "Full backup of " + $dbname
$dbbk.BackupSetName = $dbname + " Backup"
$dbbk.Database = $dbname
$dbbk.MediaDescription = "Disk"
$dbbk.Devices.AddDevice($bkdir + "\" + $dbname + "_db_" + $dt + ".bak", 'File')
$dbbk.SqlBackup($s)
if ($db.DatabaseOptions.RecoveryModel -ne 'Simple') {
$dt = get-date -format yyyyMMddHHmmss
$dbtrn = new-object ('Microsoft.SqlServer.Management.Smo.Backup')
$dbtrn.Action = 'Log'
$dbtrn.BackupSetDescription = "Trans Log backup of " + $dbname
$dbtrn.BackupSetName = $dbname + " Backup"
$dbtrn.Database = $dbname
$dbtrn.MediaDescription = "Disk"
$dbtrn.Devices.AddDevice($bkdir + "\" + $dbname + "_tlog_" + $dt + ".trn", 'File')
$dbtrn.SqlBackup($s)
}
}
}