无法执行完整SQL备份

时间:2015-01-19 14:12:13

标签: sql batch-file backup database-backups

我是SQL的新手。在我工作的地方,我得到了一种关于自动化完整SQL备份的新工作。几乎得到它,它是从SQL Server Management Studio作为查询。 (顺便说一句,我必须在服务器上这样做)所以我试图像这样执行这个查询“ - sqlcmd -E -S EUTAX-WS \ REMEK -i D:\ Programs \ FastGlacier \ backupAllDB.sql”和它在我的电脑上工作。但是在服务器中没有。

有我的批处理文件

sqlcmd -E -S EUTAX-WS\REMEK -i D:\Programs\FastGlacier\backupAllDB.sql

还有我的SQL查询

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 

-- please change the set @path = 'change to your backup location'. for example,  
-- SET @path = 'C:\backup\' 
-- or SET @path = 'O:\sqlbackup\' if you using remote drives
-- note that remotedrive setup is extra step you have to perform in sql server in order to backup your dbs to remote drive 
-- you have to chnage you sql server accont to a network account and add that user to have full access to the network drive you are backing up to

SET @path = 'd:\Backup\Remek\'  

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) 

DECLARE db_cursor CURSOR FOR  
SELECT name 
FROM master.dbo.sysdatabases 
WHERE name NOT IN ('master','model','msdb','tempdb')  

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 

当我尝试执行它时,我收到以下错误: 服务器主体“”无法在当前安全上下文中访问数据库。

不知怎的,我应该添加权限,但我不能怎么做!

3 个答案:

答案 0 :(得分:0)

请尝试授予包含文件夹的脚本,即d:\ backup

在Windows资源管理器中,右键单击要使用的文件或文件夹。

从弹出菜单中选择“属性”,然后在“属性”对话框中单击“安全”选项卡。

在“名称”列表框中,选择要查看其权限的用户,联系人,计算机或组。

如果权限为灰色,则表示权限是从父对象继承的。

答案 1 :(得分:0)

您可以使用SqlCmd命令行工具使用简单的批处理脚本执行此操作:

@ECHO OFF

REM Location to store the BAK files.
SET BackupDir=C:\Location\To\Save
REM SQL Connection information.
SET Switches=-E -S %ComputerName%

REM Pull online user databases.
FOR /F "usebackq tokens=*" %%A IN (`SqlCmd %Switches% -W -h-1 -Q "SET NoCount ON; SELECT [Name] FROM sys.databases WHERE [State]=0 AND NOT [Name] IN ('master','model','msdb','tempdb')"`) DO (
    REM Backup each.
    ECHO Database: %%A
    SqlCmd %Switches% -Q "BACKUP DATABASE [%%A] TO DISK='%BackupDir%\%%A.bak'"
)

答案 2 :(得分:0)

sqlcmd -U "username" -P "password" -i D:\Programs\FastGlacier\backupAllDB.sql