无法执行完整 SQL 备份
Can not execute a Full SQL backup
我是 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
当我尝试执行它时,出现以下错误:
服务器主体“”无法在当前安全上下文下访问数据库“”。
不知何故我应该添加权限,但我不能这样做!
请尝试授予脚本包含文件夹的权限,即 d:\backup
在 Windows 资源管理器中,右键单击要使用的文件或文件夹。
从弹出菜单中选择 select 属性,然后在“属性”对话框中单击“安全”选项卡。
在名称列表框中,select您要查看其权限的用户、联系人、计算机或组。
如果权限变暗,则表示权限是从父对象继承的。
您可以使用 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'"
)
sqlcmd -U "username" -P "password" -i D:\Programs\FastGlacier\backupAllDB.sql
我是 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
当我尝试执行它时,出现以下错误: 服务器主体“”无法在当前安全上下文下访问数据库“”。
不知何故我应该添加权限,但我不能这样做!
请尝试授予脚本包含文件夹的权限,即 d:\backup
在 Windows 资源管理器中,右键单击要使用的文件或文件夹。
从弹出菜单中选择 select 属性,然后在“属性”对话框中单击“安全”选项卡。
在名称列表框中,select您要查看其权限的用户、联系人、计算机或组。
如果权限变暗,则表示权限是从父对象继承的。
您可以使用 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'"
)
sqlcmd -U "username" -P "password" -i D:\Programs\FastGlacier\backupAllDB.sql