SQL Server: backup all databases

后端 未结 2 947
天命终不由人
天命终不由人 2021-01-14 06:11

I was wondering if there was any way of making a backup of an entire SQL Server (we are using SQL Server 2008) at regular intervals to a specific location. I know we are abl

2条回答
  •  情深已故
    2021-01-14 06:36

    Consider backing up hidden resource database as well

    Use master
    GO
    
    --enable
    sp_configure 'show advanced options'
    GO
    /* 0 = Disabled , 1 = Enabled */
    sp_configure 'xp_cmdshell', 1
    GO
    RECONFIGURE WITH OVERRIDE
    GO
    
    
    --===Resource file details
    SELECT 'ResourceDB' AS 'Database Name'
        , NAME AS [Database File]
        , FILENAME AS [Database File Location] 
    FROM sys.sysaltfiles 
        WHERE DBID = 32767
    ORDER BY [Database File]
    GO
    
    --===Copy resource files
    DECLARE @ResDataFile VARCHAR(1000), @ResLogFile VARCHAR(1000)
    SELECT 
        @ResDataFile = CASE NAME WHEN  'Data' THEN FILENAME ELSE @ResDataFile END,
        @ResLogFile = CASE NAME WHEN 'Log' THEN FILENAME ELSE @ResLogFile END
    FROM sys.sysaltfiles 
    WHERE DBID = 32767
    
    SELECT @ResDataFile, @ResLogFile
    
    DECLARE @cmd VARCHAR(1000)
    
    --===Copy data file
    SELECT @cmd = 'COPY /Y "' + @ResDataFile + '" "G:\SQLBackups"'
    --PRINT @cmd
    EXEC xp_cmdshell @cmd
    
    --===Copy log file
    SELECT @cmd = 'COPY /Y "' + @ResLogFile + '" "G:\SQLBackups"'
    --PRINT @cmd
    EXEC xp_cmdshell @cmd
    
    GO
    --Disable
    sp_configure 'show advanced options'
    GO
    /* 0 = Disabled , 1 = Enabled */
    sp_configure 'xp_cmdshell', 0
    GO
    RECONFIGURE WITH OVERRIDE
    GO
    

提交回复
热议问题